How can l write this number in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How can l write this number in c++

1 12 123 1234 12345

10th Jan 2019, 6:35 PM
Anas H Hmiady
Anas H Hmiady - avatar
14 Answers
+ 8
You can do this with the help of nested loops It's easy you should try yourself first then look at the solution Source Code: for(int i=1 ; i<6 ; i++) { for(int j=1 ; j<i+1 ; j++) { cout<<j; } cout<<endl; } I hope this will help Thank you
10th Jan 2019, 6:43 PM
good_bits
good_bits - avatar
+ 6
Hi everyone, you can do it with just one for loop, something like this: int x = 0; for (int i = 1; i <= 5; i++) { x = x * 10 + i; printf("%d\n", x); }
12th Jan 2019, 8:14 PM
notqueued
notqueued - avatar
+ 4
Thank you 👌 I will try again
10th Jan 2019, 11:12 PM
Anas H Hmiady
Anas H Hmiady - avatar
+ 4
Anas Welcome
11th Jan 2019, 1:55 AM
good_bits
good_bits - avatar
+ 4
very good and easy 👌 ~ swim ~
12th Jan 2019, 4:23 PM
Anas H Hmiady
Anas H Hmiady - avatar
+ 3
#include<iostream> using namespace std; int main() { for(int i=1;i<6;i++){ for(int j=1;j<i+1;j++) {cout<<j; } cout<<endl; } }
11th Jan 2019, 9:03 PM
Anas H Hmiady
Anas H Hmiady - avatar
+ 3
eduardo g Yes this is true and very good for training .... I love this kind of algorithms and I try to train them
12th Jan 2019, 12:39 PM
Anas H Hmiady
Anas H Hmiady - avatar
+ 3
nice!
12th Jan 2019, 4:39 PM
Mohammad Elahi
Mohammad Elahi - avatar
+ 3
Mohamed Ghazouly because I missed 'l' in 'endl' Thanks for Reporting
13th Jan 2019, 4:07 AM
good_bits
good_bits - avatar
+ 2
Bad_bit Hey man Its not working
11th Jan 2019, 8:56 PM
Mohamed Ghazouly
Mohamed Ghazouly - avatar
+ 2
Anas exactly what i was going to post! if done like the first answer the sequence will be linear, you need a line break for every loop and i tried a couple of ways including putting the //cout<<j<<endl;// but it made the output vertical. These type of exercises helps understand this widely used algorithm.
12th Jan 2019, 6:35 AM
eduardo g
eduardo g - avatar
+ 1
#include <iostream> using namespace std; int main (){ for(int i=1;i <=5;i++){ for (int j=1;j <=5;j++){ if (i>=j) cout <<j; else cout <<" "; } cout <<endl; } }
13th Jan 2019, 4:50 PM
Alone
Alone - avatar
+ 1
#include <iostream> using namespace std; int main (){ cout<<'1'<<endl<<"12"<<endl<<"123"<<endl<<"1234"<<endl<<"12345"; return 0; } No need for thanks😀😁
5th Feb 2019, 2:24 PM
Eyad Al-Ahmar
Eyad Al-Ahmar - avatar
0
#include <iostream> using namespace std; int main (){ for (int i=1;i <=5;i++){ for (int j=1;j <=5;j++){ if (i>=j) cout <<j; } cout <<endl; } }
4th Feb 2019, 4:29 AM
Alone
Alone - avatar