C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

C++

#include <iostream> using namespace std; int main() { int i; for(i=1;i<=5;++i) { cout<<"\n"; for(int j=1;j<=i;++j) cout<<i; } return 0; } Pls can i know the working proccess to get the output Also can someone explain the program for me please

22nd Dec 2018, 8:28 AM
Jothika
Jothika - avatar
13 Answers
+ 10
#include <iostream> using namespace std; int main() { int i; for (i = 1; i <= 5; ++i) // set the total "number of digits in each row" (e.g. row1 one digit, row2 two digits, row3 three digits and so on) { for (int j = 1; j <= i; ++j) { // handles the "number of prints (column)" based upon the current value of i cout << i; // prints out the value of i } cout << "\n"; // break the line upon completion of each row } return 0; } 1 22 333 4444 55555
22nd Dec 2018, 8:48 AM
Babak
Babak - avatar
+ 6
Increase j by one. Basically, j gets incremented when the program flow reaches to the right curly brace as for (int j = 1; j <= i; ++j) { } // here, j gets incremented after each loop cycle
22nd Dec 2018, 11:32 AM
Babak
Babak - avatar
+ 6
n = 15 # size for x in range(n//2,n + 1,+2): for y in range(1,n - x,+2): print(" ",end="") for y in range(1,x + 1): print("*",end="") for y in range(1,n - x + 1): print(" ",end="") for y in range(1,x + 1): print("*",end="") print() for x in range(n, 0,-1): for y in range(x, n): print(" ",end="") for y in range(1,(x * 2)): print("*",end="") print() print("heart")
11th Dec 2020, 1:41 AM
Melekte Petros
Melekte Petros - avatar
+ 5
I think the output will be like this : 1 22 333 4444 55555 Explanation : In First iteration a new line and 1 from j (checks j(1)<=i(1)) will be executed In Second iteration a new line and 22 from j (checks j(1)<=i(2) ie., checks 2 times inside second loop until the condition (j<=i) becomes false) will be executed inside the second for loop In Third iteration a new line and 333 from j (checks j(1)<=i(3) ie., checks 3 times inside second loop until the condition (j<=i) becomes false) will be executed inside the second for loop In Fourth iteration a new line and 4444 from j (checks j(1)<=i(4) ie., checks 4 times inside second loop until the condition (j<=i) becomes false) will be executed inside the second for loop In Fifth iteration a new line and 55555 from j (checks j(1)<=i(5) ie., checks 5 times inside second loop until the condition (j<=i) becomes false) will be executed inside the second for loop After both the loops becomes false the execution of the program terminates
22nd Dec 2018, 9:06 AM
deeyae
deeyae - avatar
+ 5
22nd Dec 2018, 9:07 AM
Jothika
Jothika - avatar
+ 5
"j is the value of digits" No, the number of digits for printing out. `j` in the inner loop is responsible for iterating till the current value of `i` and provides the correct number of cycles over the `cout << i;`. For example, if the i = 3 in the outer loop, it says to the inner loop "iterate from 1 to 3 over cout << 3", so, in this particular iteration, the output would be as 333
22nd Dec 2018, 9:19 AM
Babak
Babak - avatar
+ 5
What is the meaning of ++j
22nd Dec 2018, 11:21 AM
Jothika
Jothika - avatar
22nd Dec 2018, 9:21 AM
Jothika
Jothika - avatar
+ 3
C++ Soldier (Babak) thankyou i have a doubt now is: i the total number of digits and j is the value of digits
22nd Dec 2018, 9:01 AM
Jothika
Jothika - avatar
+ 1
19th May 2020, 10:50 PM
Naveed
Naveed - avatar
+ 1
When i=1 (1) When i=2 (22) When i=3 (333) . . . . .
8th Aug 2020, 6:56 PM
Erfan
Erfan - avatar
0
Narayana minutes balance
23rd Dec 2018, 2:41 AM
Dhurga rao Dhurga
Dhurga rao Dhurga - avatar
- 1
Output is 1 22 333 4444 55555
8th Oct 2020, 8:09 PM
Chethine Liyanarachchi
Chethine Liyanarachchi - avatar