pc-ip-02.c (560B)
1 /* 2 * Question 2: 3 * Write a program to reverse a non-negative integer using a function. 4 */ 5 6 #include <stdio.h> 7 8 int reverse(int); 9 10 int main() 11 { 12 int num; 13 printf("Enter the number: "); 14 scanf("%d", &num); 15 if (num < 0) 16 { 17 printf("\nOnly poitive integers are allowed."); 18 return 1; 19 } 20 printf("\nReverse of input %d is : %d", num, reverse(num)); 21 return 0; 22 } 23 24 int reverse(int num) 25 { 26 int result = 0; 27 while (num > 0) 28 { 29 result = (result * 10) + (num % 10); 30 num /= 10; 31 } 32 return result; 33 }