ADVERTISEMENT
ADVERTISEMENT

C program to Reverse an Integer Number/ Program for Reverse Number in C

This program will read an integer positive number and reverse that number. For Example input number is 1234 after reversing number will be 4321.

The logic behind to implement this program - Input Number, and run a loop until number is greater than 0, using remainder (Modulus) operator extract last digits and then dividing by 10, last digits will be removed. This process will be executed until number is not zero.

Reverse Number program in C

/* program to reverse an integer number.*/

#include <stdio.h>

int main()
{
    int n;
    int dig, revNumber;

    printf("Enter an integer number : ");
    scanf("%d", &n);

    /*Reversing Number*/
    revNumber = 0;

    while (n > 0) {
        dig = n % 10; /*get digit*/
        revNumber = (revNumber * 10) + dig;
        n = n / 10;
    }

    printf("Reverse Number is : %d\n", revNumber);
    return 0;
}

Output:

Enter an integer number : 1234
Reverse Number is : 4321

Using User Define Function

/* program to reverse an integer number.*/

#include <stdio.h>

/*	function:	reverseNum
	to reverse an integer number.
*/

int reverseNum(int num)
{
    int sum = 0, rem;
    while (num > 0) {
        rem = num % 10;
        sum = (sum * 10) + rem;
        num = num / 10;
    }

    return sum;
}

int main()
{
    int n;
    printf("Enter an integer number : ");
    scanf("%d", &n);

    printf("Reverse Number is : %d\n", reverseNum(n));
    return 0;
}

Output:

Enter an integer number : 1234
Reverse Number is : 4321

C Number Manipulation Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.