Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the output has to be (for an example if we choose the number 4)
1
2 3
4 5 6 
7 8 9 10

and the program that I wrote is
C
#include<stdio.h>
void main()
{
int i,j,num;
printf("please enter a number\n");
scanf("\n");
for (j=1;j<=num;j++)
{
for(i=1;i<=j;i++)
printf("...") //this is where I get stuck.
printf("\n");
}
}</stdio.h>
Posted
Updated 18-Nov-13 5:58am
v2
Comments
CHill60 18-Nov-13 12:00pm    
What are the "rules" for defining the output ... in other words how was that requirement stated?
Member 10409403 18-Nov-13 12:04pm    
write a program which's output is the following (the example was the output 4)
1
2 3
4 5 6
7 8 9 10

Member 10409403 18-Nov-13 12:04pm    
I'm sorry the example for the input 4*
Zoltán Zörgő 18-Nov-13 12:09pm    
Looks better :)
Member 10409403 18-Nov-13 12:11pm    
so can you help?

I can:
C#
#include<stdio.h>
int main()
{
int i,j,num=1, n = 4;
for(i=1;i<=n;i++)
 {
  for(j=1;j<=i;j++) printf("%d ",num++);
  printf("\n");
 }
return 0;
}

You did actually right. You get stuck with the number you want to print. I simply introduced a new variable, that is printed and incremented.
 
Share this answer
 
Comments
Member 10409403 18-Nov-13 12:22pm    
thank you sooooooooooo much :)
CHill60 18-Nov-13 12:23pm    
My 5.
I was going to restate the requirements for the OP like this
a) the input is the number of rows required.
b) the number of items on each row is equal to the row "number"
c) Each item printed is the previous item + 1
Where point (c) is the important bit ... your new variable "num"
The series you expected as output is called Floyd's triangle.

You can found its program here.
 
Share this answer
 
Hi User 10409403, try this code buddy
C++
int count=1; //declare and initialize a variable 
//read maxSize with the value you want like 4 or 5 and test for different numbers
for(int i=1;i<=maxSize;i++)
{
	for(j=1;j<=i;j++)
	{
	    printf("%d\t",count++);
	}
printf("\n");
}

As I donot have c compiler with me, I could not able to test but according to my logic that would work perfectly. Please try and let us know if that works or not.

Thanks
Ganesh
 
Share this answer
 
v4
Comments
CHill60 18-Nov-13 12:34pm    
That is going to work ... except for the names of variables and the fact you've added a tab between the items this is identical to Solution 1
Ganesh KP 18-Nov-13 13:05pm    
thanks Chill60, actually I have not seen solution1 when I posted my solution, seems both the solutions same. My internet connection is too slow to come up with the whole page loading.
Member 10409403 18-Nov-13 12:43pm    
It did work:)!!
Thanks!

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