Square of asterisk in c

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Geekibz
    New Member
    • Jan 2008
    • 12

    Square of asterisk in c

    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.
    [code=c]
    #include<stdio. h>
    main()
    {
    int x,y,c=5;
    for(x=c;x<=c;x--)
    {
    for(y=c;y<=c;y--)
    printf("*");

    }
    printf("\n");
    }[/code]
    Last edited by sicarie; Feb 2 '08, 02:45 PM. Reason: Code tags
  • babu198649
    New Member
    • Sep 2007
    • 22

    #2
    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
    [code=c]
    #include<stdio. h>
    main()
    {
    int x, y, c=5;
    for (x=c; x>0; x--)
    {
    for (y=c; y>1; y--) //changed this line
    printf("*"); // changed this line
    printf("*\n"); // changed this line
    }
    printf("\n");
    }
    [/code]
    Last edited by sicarie; Feb 2 '08, 02:46 PM. Reason: Code tags

    Comment

    • Geekibz
      New Member
      • Jan 2008
      • 12

      #3
      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?

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Yes. Yes there is.

        Comment

        Working...