write a C program to find the sum of a given number and its reverse.
For Example : number is 123 .Reveres of number is 321 and sum of 123 and 321 is 444.
Answers
Here's a C program that calculates the sum of a given number and its reverse:
#include <stdio.h>
int reverseNumber(int num) {
int reverse = 0;
while (num != 0) {
reverse = reverse * 10 + num % 10;
num = num / 10;
}
return reverse;
}
int main() {
int number, reverse, sum;
printf("Enter a number: ");
scanf("%d", &number);
reverse = reverseNumber(number);
sum = number + reverse;
printf("Reverse of %d is %d\n", number, reverse);
printf("Sum of %d and %d is %d\n", number, reverse, sum);
return 0;
}
In this program, we define a function reverseNumber() that takes an integer as input and returns its reverse. We use a while loop to extract the digits from the number and build the reverse by multiplying it by 10 and adding the remainder.
In the main() function, we prompt the user to enter a number. We then call the reverseNumber() function to find the reverse of the given number. Finally, we calculate the sum of the number and its reverse and display the results.
For example, if the user enters 123, the program will output:
Reverse of 123 is 321
Sum of 123 and 321 is 444
This program ensures that the logic is implemented correctly and provides the desired output.
For more such questions on C program:
https://brainly.in/question/5790832
#SPJ1