Knowee
Questions
Features
Study Tools

#include <stdio.h>int main(){ int num; int sum=0, rev=0; printf("Enter a number"); scanf("%d", &num); while(num>0) { sum=sum + num%10; rev=rev*10 + num%10; num=num/10; } printf("sum=%d\n", sum); printf("reverse=%d\n", rev); return 0;}

Question

#include <stdio.h>int main(){ int num; int sum=0, rev=0; printf("Enter a number"); scanf("%d", &num); while(num>0) { sum=sum + num%10; rev=rev*10 + num%10; num=num/10; } printf("sum=%d\n", sum); printf("reverse=%d\n", rev); return 0;}

🧐 Not the exact question you are looking for?Go ask a question

Solution

This is a simple C program that takes an integer input from the user, calculates the sum of its digits, reverses the number, and then prints both the sum and the reversed number.

Here's a step-by-step explanation:

  1. The program starts with the inclusion of the standard input-output library using #include <stdio.h>.

  2. The main() function is declared, which is the entry point of the program.

  3. Inside the main() function, three integer variables num, sum, and rev are declared. num is for storing the user input, sum is for storing the sum of the digits, and rev is for storing the reversed number.

  4. The program then prompts the user to enter a number using printf("Enter a number").

  5. The entered number is read and stored in the num variable using scanf("%d", &num).

  6. A while loop is used to iterate through each digit of the number. The condition for the loop to run is num>0, which means the loop will continue until num becomes 0.

  7. Inside the loop, the last digit of num is added to sum and rev is multiplied by 10 and the last digit of num is added to it. This effectively reverses the number.

  8. num is then divided by 10 to remove the last digit. This process continues until num becomes 0.

  9. After the loop ends, the sum of the digits and the reversed number are printed using printf().

  10. The program then returns 0 and ends.

This problem has been solved

Similar Questions

0/0

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.