473,222 Members | 1,802 Online

# Write a nested loop to create following pattern

I have to write a nested loop using for statement to produce the following pattern if the user input is 5

Please enter an integer > 5 (this will be using printf and scanf to get the integer)

1
121
12321
1234321
123454321

and also another program (different coding) to produce the following pattern if user input is 5:

1
2 2
3 3
4 4
55555555
Oct 19 '06 #1
8 10634
I have to write a nested loop using for statement to produce the following pattern if the user input is 5

Please enter an integer > 5 (this will be using printf and scanf to get the integer)

1
121
12321
1234321
123454321

and also another program (different coding) to produce the following pattern if user input is 5:

1
2 2
3 3
4 4
55555555
Do you need help with printing or the loops?

The first loop is a simple for loop (i=1;i<=input;i++)
{j=input-1;
print i;
if i=input
{ while(i>0)
if(j>0)
{print j;
--j;}
insert end of line;
}

You get the idea. Next one is just a simple variation.

Mitch
Oct 19 '06 #2
Hie, for the first program you can use this code:
int i,j;
for(i=1;i<=5;i++)
{
{
for(j=1;j<=i;j++)
printf("%d",j);
for(j=(i-1);j>0;j--)
printf("%d",j);
}
printf("\n");
}
Oct 19 '06 #3
I'm so sorry, actually i mean the looping, not print the numbers, and also the message earlier doesnt allow me to type space in between....i want the following output using while loop statement....please help...thank you

######1######
#####2#2#####
####3###3####
###4#####4###
##555555555##

in which # symbol is actually space....and 5 is the input

and also this output (for another different program), input also 5

######1######
#####121#####
####12321####
###1234321###
##123454321##
Oct 19 '06 #4
D_C
293 100+
I'm not sure what is supposed to happen for numbers greater than nine. This does the simpler case (with spaces in between).
Expand|Select|Wrap|Line Numbers
1. int i = 1;
2. int count = 0;
3. while(i <= input)
4. {
5.   while(count < ((2*input)+3))
6.   {
7.     if( ... )
8.       print a space
9.     else if( ... )
10.            print count
11.          else if ( ... )
12.            print either input or count
13.               else
14.                 print a space
15.
16.     count++;
17.   }
18.   i++;
19. }
Oct 19 '06 #5
i'm so sorry but i'm just starting to learn c in which i dun really understand the code that you gave, can someone make it simpler?
Oct 21 '06 #6
/* ###1### in this case input is 4 when i=0 u have 3 spaces
** ##2#2## when i=1 u have 2 spaces
** #3###3# when i=2 u have 1 space
** 4#####4 when i=3 u have 0 space
*/

#include<stdio.h>
main()
{
int i, j, numspace, input;
printf(" Type a number ");
scanf("%d", &input);
i = 0;

while(i < input)
{
numspace = input - ( i + 1); // in the first line when input is 4 and i is 0
// u have 4 - (1 + 0 ) spaces
// now a loop for printing spaces
for( j = 0; j< numspace; j++)
printf(" "); // this prints spaces
// now to print the number... after printing the spaces u simply print the number
// whis is ( i + 1 ) since on the first line.. the number is 1 and i is 0
// on the 2nd line where i is one.. the number is 2 .. that is ( i + 1 ) and so on
// so we have the following
printf("%d", (i + 1) );
// now.. to print the other number on the line...
// note that on the second line u have this sequence 2#2 one space btween them where i = 1
// and on the 3rd line............................. 3###3 3spaces between them where i = 2
// soo.. we have 2*i -1 spaces beween the number
// for i = 1 ... 2*1 - 1 = 1 for i = 2.... 2*2 - 1 = 3
// so.. we loop again for the other spaces
// but first we define the number of spaces
if(i>0) // since on the first line u only have one number
{
numspace = 2*i -1;
for(j=0; j<numspace; j++)
printf(" ");
printf("%d", i+1);
}

// almost done.. now we have to print a newline and increment i
printf("\n");
i++;

}
}
Oct 21 '06 #7
yeah that works but what about 5 ?the last line must print all 555555....

what about coding this program with only for statements? is it possible?
Oct 21 '06 #8