+ 2
How can l write this number in c++
1 12 123 1234 12345
14 odpowiedzi
+ 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
+ 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);
}
+ 4
Thank you 👌
I will try again
+ 4
Anas Welcome
+ 4
very good and easy 👌 ~ swim ~
+ 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;
}
}
+ 3
eduardo g Yes this is true and very good for training .... I love this kind of algorithms and I try to train them
+ 3
nice!
+ 3
Mohamed Ghazouly because
I missed 'l' in 'endl'
Thanks for Reporting
+ 2
Bad_bit
Hey man
Its not working
+ 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.
+ 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;
}
}
+ 1
#include <iostream>
using namespace std;
int main (){
cout<<'1'<<endl<<"12"<<endl<<"123"<<endl<<"1234"<<endl<<"12345";
return 0;
}
No need for thanks😀😁
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;
}
}