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:

  1. #include <stdio.h>: Includes the standard input/output library to use functions like printf and scanf.

  2. int main() { ... }: The main function where the program execution starts.

  3. int n, reversedNumber = 0, remainder;: Declares three integer variables n, reversedNumber, and remainder.

  4. printf("Enter an integer: ");: Prints a message prompting the user to enter an integer.

  5. scanf("%d", &n);: Reads an integer input from the user and stores it in the variable n.

  6. while (n != 0) { ... }: Initiates a while loop that continues until n becomes 0.

  7. remainder = n % 10;: Calculates the remainder when n is divided by 10 and stores it in the variable remainder.

  8. reversedNumber = reversedNumber * 10 + remainder;: Builds the reversed number by multiplying the existing reversed number by 10 and adding the remainder.

  9. n /= 10;: Updates the value of n by dividing it by 10 (removing the last digit).

  10. printf("Reversed number: %d\n", reversedNumber);: Prints the reversed number obtained from the input integer.

  11. return 0;: Indicates the successful termination of the program.