C Programming Problem : Write a C program with a function that takes an integer
ID: 3735428 • Letter: C
Question
C Programming Problem : Write a C program with a function that takes an integer value (1 integer value 9999) and returns the number with its digits reversed. For example, given the number 6798, the function should return 8976. Those two - original number and reversed number -numbers will pass to another function as parameters and calculate their sum. The program should use the function reverseDigits to reverse the digits and SumOriginalReverse to calculate their sum. Your output should appear in the following format Output Main rev cligts 1234 Enter a number between 1 and 9999: 6798 The number with its digits reversed is: 8976 6798 + 8976 15774 4321 Enter a number between 1 and 9999: 6655 The number with its digits reversed is: 5566 6655+5566 12221 Enter a number between 1 and 9999: 8 The number with its digits reversed is: 8 8+8 16 Enter a number between 1 and 9999: 123 The number with its digits reversed is: 321 123+321 444 Enter a number between 1 and 9999: 67 The number with its digits reversed is: 76 67+76-143 Enter a number between 1 and 9999: 12000 Out of Range Enter a number between 1 and 9999:-10 Out of RangeExplanation / Answer
#include <stdio.h>
//declare the required functions
int reverseDigits(int number);
int SumOriginalReverse(int actual, int reverse);
//define the function reverseDigits
int reverseDigits(int number){
int remainder, rev=0; // declare variable rev to store the reversed number and remainder for internal calculation
//reversing the number
while(number != 0)
{
remainder = number%10;
rev = rev*10 + remainder;
number /= 10;
}
return rev;
}
//define the function SumOriginalReverse
int SumOriginalReverse(int actual, int reverse){
int sum; // declare variable sum to store the result of addition
sum=actual+reverse; // adding the original and reversed numbers
return sum;
}
void main()
{
int num, reversed; //declare input variable num to store the value of input and reversed to store the reversed number
printf("Enter a number between 1 and 9999: ");
scanf("%d", &num);
reversed=reverseDigits(num); //calling function reverseDigits
printf(" The number with its digits reversed is: %d", reversed);//printing reversed number
printf(" %d + %d = %d", num,reversed,SumOriginalReverse(num, reversed)); //calling function SumOriginalReverse
//and printing the sum of original & reversed numbers
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.