So far I have written a code to find the reverse of a number as well as the sum of the digits for an integer. The program is meant to keep asking the user for a new integer so that it will find the sum and reverse of that new integer again. However when the program asks the user for a new integer it just adds it to the original one how can I separate the operation. For example this is what came out:
Enter an integer:
123
The sum of the digits is: 6
The reverse of the number is : 321
1 <------------ (at this part it didn't even go back to the original printf statement)
The sum of the digits is: 7
The reverse of the number is : 3211
Code:#include <stdio.h> int main() { int n, sum=0, r, reverse=0; printf("Enter an integer:\n"); scanf("%d", &n); while(n!=-1) { while(n!=0) { r=n%10; sum=sum+r; reverse=reverse*10; reverse=reverse+n%10; n=n/10; } printf("The sum of the digits is: %d\n", sum); printf("The reverse of the number is : %d\n", reverse); scanf("%d", &n); } return 0; }
By the way I am new with this so please bear with me, thank you in advance.



LinkBack URL
About LinkBacks



