Sampoo C programming
#include <stdio.h>
int main() {
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
reversedNumber = reversedNumber * 10 + remainder;
n /= 10;
}
printf("Reversed number: %d\n", reversedNumber);
return 0;
}
Explanation:
#include <stdio.h>
: Includes the standard input/output library to use functions likeprintf
andscanf
.int main() { ... }
: The main function where the program execution starts.int n, reversedNumber = 0, remainder;
: Declares three integer variablesn
,reversedNumber
, andremainder
.printf("Enter an integer: ");
: Prints a message prompting the user to enter an integer.scanf("%d", &n);
: Reads an integer input from the user and stores it in the variablen
.while (n != 0) { ... }
: Initiates a while loop that continues untiln
becomes 0.remainder = n % 10;
: Calculates the remainder whenn
is divided by 10 and stores it in the variableremainder
.reversedNumber = reversedNumber * 10 + remainder;
: Builds the reversed number by multiplying the existing reversed number by 10 and adding theremainder
.n /= 10;
: Updates the value ofn
by dividing it by 10 (removing the last digit).printf("Reversed number: %d\n", reversedNumber);
: Prints the reversed number obtained from the input integer.return 0;
: Indicates the successful termination of the program.