424,289 Members | 1,911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,289 IT Pros & Developers. It's quick & easy.

Make program to square the numbers from 1 to 20

P: 5
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

Expand|Select|Wrap|Line Numbers
  1. void main(void)
  2. {int a;
  3.   int  b;
  4. b=a*a;
  5. for (a=1; a<21; a++)
  6. {printf("%d",b);
  7. printf("the square is%d=b");
  8. }
  9. getche();}
Oct 21 '12 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 9,197
Your calculation of the square is outside your loop.
Oct 21 '12 #2

P: 8
Expand|Select|Wrap|Line Numbers
  1. printf("the square is%d=b");
//statement wrong.It should be

Expand|Select|Wrap|Line Numbers
  1. printf("the square is %d",b);
//if not garbage value only returns.
Oct 25 '12 #3

Post your reply

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