Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Thursday, 26 September 2013

Reverse the digits of a given number in C language

Reverse Number:  If you enter 789 as input then gives output as 987. Reverses the digits of a given number. Following is a source code of reverse the number in c language.



1) Reverse Number Program Using while Loop

#include<stdio.h>

int main()
{
    int no, remainder, reverse=0;

    printf("Enter No : ");
    scanf("%d",&no);

    while(no)
    {
        remainder = no % 10;
        reverse = reverse * 10 + remainder;
        no = no / 10;
    }

    printf("Reverse No : %d",reverse);

    return 0;
}


2) Reverse Number Program Using Recursion

#include<stdio.h>

int reverse_digit(int);

int main()
{
    int no,reverse;

    printf("Enter No : ");
    scanf("%d",&no);

    reverse = reverse_digit(no);

    printf("Reversed No : %d",reverse);
    return 0;
}
int reverse_digit(int no)
{
    static int remainder, sum;

    if( no )
    {
        remainder = no % 10;
        sum = sum * 10 + remainder;
        reverse_digit( no / 10 );
    }
    else
        return 0;

    return sum;
}


0 comments :

Post a Comment