In: Computer Science
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf())
Complete the following functions using C programming language:
Complete the int Q7a(intQ7_input) function takes only a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use arrays either. For the case when the integer ends with 0, the number printed cannot have leading 0’s (Eg: input 3412400; output 42143).
Note: Use the division and remainder operators to separate the number into its individual digits.
#include <stdio.h>
// function to calculate reverse of number
int Q7a(int Q7_input){
// initializing reverse number with 0
int reverse = 0;
// repeat until input number is greater than 0
while(Q7_input > 0){
// update reverse number each time
// multiply the current value of reverse with 10
// and then add it to the last digit of input
reverse = reverse * 10 + Q7_input % 10;
// shift the digits of the input one digit right
Q7_input /= 10;
}
// returning the reverse number
return reverse;
}
int main(){
int Q7_input;
scanf("%d", &Q7_input);
int reverse = Q7a(Q7_input);
printf("%d", reverse);
return 0;
}
OUTPUT:-
if you have any doubt, feel free to ask in the comments.