Hello friends,
I am making a simple to calculate the digits of a number. The basic code is fine and it is working.
Now I want that if the user is interested he can input another no and found the sum of digit for it.
For this I am using a while loop with condition that a char variable x has the value 'y'. At the end of the loop I ask the user to press 'y' if he is interested in running the program again.
But the while loop does not run again. Here is the code
The problem is that the control moves out of the outer while loop even if the user types 'y'.Code:main() { int num, n, rem, sum = 0; char x = 'y'; while(x == 'y') { printf("enter a number less than equal to 32767\n"); scanf("%d",&num); n = num; while(n>0) { rem = n%10; n = n/10; sum = sum + rem; } printf("the sum of digits is %d", sum); printf("\n do you want to continue,press 'y' if yes, else press any other char"); scanf("%c",&x); } getch(); }
Thanks a lot friends



LinkBack URL
About LinkBacks


