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:
a) Complete the int Q7a(intQ7_input) function takes 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.
b) Modify your program so it returns backwards any positive integer, not necessarily a seven-digit one.
#include <stdio.h> int Q7a(int Q7_input) { int result = 0; while(Q7_input>0){ result *= 10; result += (Q7_input%10); Q7_input /= 10; } return result; } int main(){ int n; printf("Enter value for n: "); scanf("%d",&n); printf("Reverse: %d\n",Q7a(n)); return 0; }
int Q7a(int Q7_input) { int result = 0; while(Q7_input>0){ result *= 10; result += (Q7_input%10); Q7_input /= 10; } return result; }
Get Answers For Free
Most questions answered within 1 hours.