[My site ]
Main » 2013 » December » 13 » 2011-2012 question 5
0:12 AM
2011-2012 question 5
what is output ,, val = 316 :

#include"stdio.h"
main()
{
long val, sum=0, n;
for(printf("enter a value = "), scanf("%ld", &val);val!=0;val/=10)
{
n = val%10;
sum+=n;
}
printf("Result = %ld", sum);
getch();
return 0;
}

answer :

val =316 != 0
n=316%10  =  6
sum = 6
val = 316 /10  = 31
val = 31 != 0
n= 31%10 = 1
sum = 6+1 = 7
val = 31/10 = 3
val = 3 != 0
n = 3%10 = 3
sum = 7+3 = 10
val = 3/10 = 0
val is equal 0 ,, program will out from for loop
result = final sum = 10
Category: C Programming - 1 | Views: 339 | Added by: ahansaary | Rating: 0.0/0
Total comments: 0
Only registered users can add comments.
[ Sign Up | Login ]