Hi,i cant figure out what is wrong this program.Pls help.
Code:#include<stdio.h> int main() { int n,i,sum=0; printf("enter upper limit"); scanf("%d",&n); for(i=0;i<n;i++) { sum=sum+i; printf("%d",sum); } return; }
Hi,i cant figure out what is wrong this program.Pls help.
Code:#include<stdio.h> int main() { int n,i,sum=0; printf("enter upper limit"); scanf("%d",&n); for(i=0;i<n;i++) { sum=sum+i; printf("%d",sum); } return; }
Line 6... the user enters an upper limit... say 5
line 7 ... a loop that counts from 0 to the upper limit -1 (0, 1, 2, 3, 4)
line 9 ... the value of the loop counter is added to the variable sum. (sum + 0, sum + 1, sum + 2, sum + 3, sum + 4)
line 10... prints the result accumulated in sum... (sum + 0 + 1 + 2 + 3 + 4 = 10)
line 12... actually this line contains an error. it should be return 0; since an integer is promised at int main()
Does that help?
No,i still get garbage value:013610
Damn never realised that.Thx man
Ok now i put newline character.Thx for your help.
Code:#include<stdio.h> int main() { int n,i,sum=0; printf("enter upper limit"); scanf("%d",&n); for(i=0;i<n;i++) { sum=sum+i; printf("%d\n",sum); }
Last edited by Monochrome; 11-01-2011 at 09:17 AM.