C program to Reverse a Number

C program to reverse a number; Through this tutorial, we will learn how to reverse a number in c program using for loop, while loop, function, and recursion.

Programs to Reverse a Number in C

  • C program to Reverse a Number using For Loop
  • C program to Reverse a Number using While Loop
  • C program to Reverse a Number using Function
  • C program to Reverse a Number using Recursion

C program to Reverse a Number using For Loop

#include<stdio.h>

void main(){

    int Num,rev_Num=0,remainder,a;

    printf("Enter the number to reverse: ");

    scanf("%d",&Num);

    a=Num;

    for(;Num>0;){

        remainder=Num%10;

        rev_Num=rev_Num*10+remainder;

        Num=Num/10;

    }

    printf("Reverse of %d is %d",a,rev_Num);

}

The output of the above c program; as follows:

Enter the number to reverse :- 4545
The reversed number is: 5454

C program to Reverse a Number using While Loop

#include <stdio.h>

int main(){

    int Num, rev_Num = 0, remainder;

    printf("Enter the number to reverse: ");

    scanf("%d", &Num);

    while (Num != 0){

        remainder = Num % 10;

        rev_Num = rev_Num * 10 + remainder;

        Num = Num/10;

    }

    printf("The reversed number is: %d", rev_Num);

    return 0;

}

The output of the above c program; as follows:

Enter the number to reverse :- 4545
The reversed number is: 5454

C program to Reverse a Number using Function

#include <stdio.h>

int rev_Int(int);

int main(){

    int Num, Rev = 0;

    printf("\nEnter the number to reverse: ");

    scanf("%d", &Num);

    Rev = rev_Int (Num);

    printf("Reverse of %d is = %d\n", Num, Rev);

    return 0;

}

int rev_Int (int Num){

    int remainder, Rev = 0;

    while (Num > 0){

        remainder = Num %10;

        Rev = Rev *10+ remainder;

        Num = Num /10;

    }

    return Rev;

}

The output of the above c program; as follows:

Enter the number to reverse :- 4545
The reversed number is: 5454

C program to Reverse a Number using Recursion

#include<stdio.h>

int main(){

    int Num,rev_Num;

    printf("Enter the number to reverse: ");

    scanf("%d",&Num);

    //Calling our function that will repeat itself

    rev_Num=rev_Func(Num);

    printf("\nThe reversed number is :%d",rev_Num);

    return 0;

}

int sum=0,remainder;

rev_Func(int Num){

    if(Num){

        remainder=Num%10;

        sum=sum*10+remainder;

        rev_Func(Num/10);

    }

    else

        return sum;

    return sum;

}

The output of the above c program; as follows:

Enter the number to reverse :- 4545
The reversed number is: 5454

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *