Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C
#include <stdio.h>
void main()
{
   int i,j,rows;

   printf("Input number of rows : ");
   scanf("%d",&rows);
   for(i=1;i<=rows;i++)
   {
	for(j=1;j<=i;j++)
	   printf("%d",i);
	printf("\n");
   }
}


What I have tried:

Input number of rows : 10                                                                                     
1                                                                                                             
22                                                                                                            
333                                                                                                           
4444                                                                                                          
55555                                                                                                         
666666                                                                                                        
7777777                                                                                                       
88888888                                                                                                      
999999999                                                                                                     
10101010101010101010

I want to know why this code give answer like below.
Posted
Updated 16-Mar-22 5:00am
v2
Comments
Richard MacCutchan 16-Mar-22 9:41am    
Because that is what you told it to do: print a line of 1s, then a line of 2s, ... and finally a line of 10s.

The output contains n rows, each of which displays a number (1<=m<=n) m times. The first for statement generates the n rows, and the second for statement repeats a number m times within its row.
 
Share this answer
 
v2
Comments
Maciej Los 16-Mar-22 8:41am    
Short and to the point, a 5!
Greg Utas 16-Mar-22 8:47am    
Which probably means that there will be a follow-up question. :)
CPallini 16-Mar-22 9:05am    
5.
Patrice T 16-Mar-22 19:08pm    
+5
Quote:
I want to know why this code give answer like below.

Because it is exactly what it is supposed to do.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Richard MacCutchan 16-Mar-22 11:19am    
I don't think you need the debugger for something that simple.
Greg Utas 16-Mar-22 11:27am    
I do! You'd be amazed at some of the stupid mistakes I have to debug!
Richard MacCutchan 16-Mar-22 12:02pm    
I bet they are not as stupid as mine. :(
Patrice T 16-Mar-22 11:46am    
Debugger is not for debugging source code only.
Debugger in this case is a meant to slow down the execution and allow OP to see with its own eyes what is going on, step by step. It is an incredible way to learn.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900