Program to Number Reverse in C (With Easy Example)

In today's program, we will know how to create a reverse number program in programming.

So let's know how to create a program of number reverse in C.


    What is Reverse Number Program?


    Number-Reverse-in-C


    A program in which numbers are inputted and reverting the input number to show as output.

    If the user enters 1255  as the input, then its reverse output will be 5521.

    Like something else


    42 = 24

    125  = 521

    132564 =  465231

    235689 =  986532


    We will create a number reversing program and explanation in two ways.

    1. Number Reverse In Using Loop.
    2. Number Reverse in Using Recursion.


    Program to reverse number using (For Loop)


    #include <stdio.h>

    #include <conio.h>

    void main ()

    {

     int a, rev = 0, b, c;

    printf ("Enter Any Number for Reverse:");

    scanf ("% d", & a);

    c = a;

    for (; a> 0;)

        {

    b = a% 10;

    rev = rev * 10 + b;

    a = a / 10;

    }

    printf ("Reverse of Number% d is:% d", c, rev);

    getch ();

    }

     

    Input


    Enter Any Number for Reverse: 123


    Output


    Reverse of Number 123 is: 321


    Explanation


    • In the program, we take four variables a, b, c, and rev where the value of rev is 0.
    • Then the user inputs the number to be reversed.
    • Now let's add value of c.
    • With the help of the for loop, the number of the number is found and the number of the reverse is printed.

    Program to reverse number using (While Loop)


    #include <stdio.h>

     int main ()

    {

    int a, rev = 0, b;

    printf ("Enter Any Number for Reverse");

    scanf ("% d", & a);

    while (a! = 0)

      {

         b = a% 10;

         rev = rev * 10 + b;

         a / = 10;

      }

      printf ("Reverse of Number:% d", rev);

    return 0;

    }


    Input


    Enter Any Number for Reverse: 123


    Output


    Reverse of Number: 321


    Program to reverse a number using recursion


    #include <stdio.h>

    long rev (long);

     int main ()

    {

       long  a, b;

       printf ("Enter Any Number for Reverse");

       scanf ("% ld", & a);

       b = rev (a);

       printf ("% ld \ n", b);

       return 0;

    }

    long rev (long a)

    {

       static long b = 0;

       if (a == 0)

       return 0;

       b = b * 10;

       b = b + a% 10;

       rev (a / 10);

       return b;

    }


    Recommend:

    Post a Comment

    0 Comments