Make program to square the numbers from 1 to 20

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bisyar khub
    New Member
    • Sep 2012
    • 5

    Make program to square the numbers from 1 to 20

    hello,...i want to make program to square the numbers from 1 to 20. i have written this program, it's running but not giving the required answer.i think it is giving a garbage value...please tell me where is the issue....I'm very basic programmer studying C++...hope you'll response soon

    Code:
    void main(void)
    {int a;
      int  b;
    b=a*a;
    for (a=1; a<21; a++)
    {printf("%d",b);
    printf("the square is%d=b");
    }
    getche();}
    Last edited by zmbd; Oct 21 '12, 03:31 PM. Reason: Removed the garbage. - Please Do NOT do a direct screen paste. Also format submitted code using the <CODE/> button on the toolbar.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your calculation of the square is outside your loop.

    Comment

    • sgayathri2412
      New Member
      • Oct 2012
      • 8

      #3
      Code:
      printf("the square is%d=b");
      //statement wrong.It should be

      Code:
      printf("the square is %d",b);
      //if not garbage value only returns.
      Last edited by Meetee; Oct 25 '12, 07:22 AM. Reason: code tags added

      Comment

      Working...