468,496 Members | 1,801 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

Square of asterisk in c

Geekibz
12
Hi.I want to print out a 5 by 5 square of asterisks in c using nested for loops.I tried but my code(see below) resulted in an infinite loop.Pls help.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. main()
  3. {
  4. int x,y,c=5;
  5. for(x=c;x<=c;x--)
  6. {
  7.     for(y=c;y<=c;y--)
  8.         printf("*");
  9.  
  10. }
  11. printf("\n");
  12. }
Feb 2 '08 #1
3 10873
hi geek biz
in u r code u r decrementing the x value which is always less than c and hence it falls in to infinite loop.

try this
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. main()
  3. {
  4.     int x, y, c=5;
  5.     for (x=c; x>0; x--)
  6.     {
  7.         for (y=c; y>1; y--)  //changed this line
  8.             printf("*");    // changed this line
  9.         printf("*\n");      // changed this line
  10.     }
  11.     printf("\n");
  12. }
Feb 2 '08 #2
Geekibz
12
Thanx.Works fine.how about modifying that to printout a pyramid of asterisk?I intend to use if -else statements.Is there an easier way out?
Feb 4 '08 #3
sicarie
4,677 Expert Mod 4TB
Yes. Yes there is.
Feb 4 '08 #4

Post your reply

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

Similar topics

3 posts views Thread by sarmin | last post: by
1 post views Thread by sarmin kho | last post: by
2 posts views Thread by Marco | last post: by
2 posts views Thread by Evan | last post: by
reply views Thread by selvarani | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.