Write a c program to reverse given number

Ram Pothuraju

 #include <stdio.h>


int main()

{

    int n, reversedN = 0, remainder;


    printf("Enter a positive integer: ");

    scanf("%d", &n);


    // Reversing the number

    while (n != 0)

    {

        remainder = n % 10;

        reversedN = reversedN * 10 + remainder;

        n /= 10;

    }


    printf("Reversed number = %d", reversedN);


    return 0;

}



In this program, we first take a positive integer as input from the user. Then, we loop through the digits of the number and reverse the number by repeatedly multiplying by 10 and adding the remainder of n divided by 10. Finally, we print the computed value of the reversed number.
Tags

Post a Comment

0Comments

Post a Comment (0)