473,386 Members | 1,705 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,386 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 10649
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
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.