473,222 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,222 software developers and data experts.

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
erm...can someone answer me please...
Oct 23 '06 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: ahokdac-sql | last post by:
Hi, I'm adapting access queries to sql server and I have difficulties with the following pattern : query1 : SELECT * FROM Query2 WHERE A=@param1 query 2: SELECT * FROM Table2 WHERE B=@param2 ...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
10
by: Pavan | last post by:
Hi i have two nested loops as shown below: 1. for(i=0;i<=1000;i++) { for(i=0;i<=100;i++) { .....; .....; }
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
18
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an...
3
by: bennie72 | last post by:
need to script a "*" pattern in nested loop? ********************** ********************** ********* ********* ********************* ********************* ********* *********
7
by: nemir | last post by:
I am new in Java and am really struggling with a this problem: Use nested loops that print the following patterns as shown: Pattern 1 Pattern 2 Pattern 3 Pattern 4...
3
by: Dieter Maurer | last post by:
I met the following surprising behaviour .... for i in range(3): .... def gen1(): .... yield i .... yield i, gen1() .... .... 0 0 1 1
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.