I got this assignment and I'm stuck. I have to write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367. Can someone help?
:confused:

I got this assignment and I'm stuck. I have to write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367. Can someone help?
:confused:

This question gets asked about once a week on a dozen forums. Do a search. Or post code. Read the announcement.

Hi! I also received this assignment, but was wondering if someone could help me fix my program (hopefully this constitutes "showing effort"). I don't understand why it will work for relatively small numbers but not bigger ones. I just started programming so I'm sorry if this has a very simple answer.

/*
*File: reverse.c
*This program will take a number from the user and reverse its digits.
*/

#include<stdio.h>
#include"genlib.h"
#include"simpio.h"

main()
{
	int num, rem;
	printf("Enter your number: ");
	num=GetInteger();
	printf("Your number reversed is: ");
	while(num>0)
	{
		rem=num%10;
		printf("%d", rem);
		num=num/10;
	}
	printf("\n");
}

Thanks!

Hi! I also received this assignment, but was wondering if someone could help me fix my program (hopefully this constitutes "showing effort"). !

Depends -- is this your work or did you plagarize it from some other source ?

>> don't understand why it will work for relatively small numbers but not bigger ones
Integers can only hold a finite number of digits (see limits.h). So if you try to enter a number bigger than that the program can't work the way you think it should.

Thanks very much! I did not know that.

>>Depends -- is this your work or did you plagarize it from some other source ?
I am happy to hear that it looks like I may have taken it from another source :) (I didn't think it was very good!) I just started this month after all and am very interested in learning more.

What does this do?

num=GetInteger();

you would need a scanf here...

No, never use scanf. Use fgets or even fread or read, and sscanf maybe. But then, the numbers conversion by sscanf is not specified in standard anyhow. And using %d, the sscanf does some strange things. %d must read int, but when we read into int and then print that number, it prints correctly even numbers which are greater than 65536. This should mean that it would write into memory larger than int allows, which is overflow. The easiest solution is to always use long int, and %ld, when reading with sscanf, but this would not much help when the aim is to enter very big numbers.

And using %d, the sscanf does some strange things. %d must read int, but when we read into int and then print that number, it prints correctly even numbers which are greater than 65536. This should mean that it would write into memory larger than int allows, which is overflow. The easiest solution is to always use long int, and %ld, when reading with sscanf, but this would not much help when the aim is to enter very big numbers.

This is only true if you are using
1) 486 or earlier computer
2) an older 16bit compiler

If neither, int s have been 32 bits ever since the 32 bit machines/compliers have been around.

Indeed, in my limits.h, short int is two bytes, int is four bytes and long int is eight bytes, thanks. And i have always thought that int is two bytes!!! There is information about limits.h, in standard, and there INT_MAX is defined as 32767, and INT_MIN as -32767, and i kind of believed it.

Yes indeed, i run the test again, and it works correctly, if this can be called to be correct, i'm still not quite sure that it doesn't write over the allowed memory. What it does is that when i enter a number greater than the limit, then it prints exactly the int upper limit as defined in my limits.h.

But still, it's better to consider that int is two bytes, to write portable programs, because in some platforms or compilers the int can still be two bytes. Or then, maybe it's better to use short int instead of int, because that at least seems to have remained the same, it's two bytes both in standard and in my limits.h, though of course even that is not guaranteed. It's really very bad that the int has been made bigger, this may cause completely unnecessary incompatability, and very bad potential bugs.

Hi Guys,

I wrote a small code for this (given below). I think this will work. You can implement it in a function which can directly take types of integer and return reverse of it. I had implemented it for type int.

int main() {

 int num;
 int quotient = 0, reminder = 0, reverse = 0;

printf("Enter the number : " );
scanf("%d", &num);
 quotient =  num;
 do{

    reminder =  reverse +(quotient%10);
    quotient /= 10;
    if(quotient != 0){
    reverse = reminder * 10;
    }else{
        reverse= reminder;
    }


 }while(quotient != 0);

 printf("\n Reverse of the entered number = %d", reverse);

 return 0;

}

Regards,
Amar

You still want to use scanf directly, instead of fgets and sscanf, atoi, or even better, strtol. This would cause you trouble at least because of all the input and newlines which scanf would discard, but which remain in the input stream. Much better and easier is to never use scanf. I would also trust more functions like atoi and strtol instead of sscanf, because at least it's described how they work, and what i can expect.

This works exactly as for int in 32 bit machine, but reverses longer numbers in 64 bit machine:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main ()
{
        long int r = 0, n;
        char buf [FILENAME_MAX];

        printf ("Enter a number: ");
        fgets (buf, FILENAME_MAX, stdin);
        n = strtol (buf, NULL, 10);
        if (errno == ERANGE) n = 0;
        for (; n; n /= 10)
                r = 10 * r + n % 10;
        printf ("Reversed: %ld\n", r);
        return 0;
}
#include<stdio.h>
#include<conio.h>
main()
{
int num, rem=0;
printf("\n Enter the number ");
scanf("%d", &num);
while(num>0)
{
rem = (rem * 10) + (num % 10);
num = num / 10;
}
printf("\n Reverse of the number is %d", rem);
getch();
}

Hye hi!
You have to first separate all the digits of the number passed to the function you may do it by the following way
a=n%10;
n=n/10;
b=n%10;
n=n/10;
until n>0
and then combine all these digits in the reverse order like if a is 1st, b is 2nd in the original number then you have to add them as: 10*b + a
so, that your number get reversed and b becomes 1st digit and a becomes 2nd one.

please give me the solution of reversing number 523 into 325 by using for loop

commented: Don't bump old threads -1

please give me the solution of reversing number 523 into 325 by using for loop

What did you think the posts above were trying to do, exactly?

you can use an array to enter numbers and print that array in reversible order

Another method using string

#include <iostream>
#include <string>
using namespace std;
void main()
{
int index = 0;
string number;

cout<<"Please enter an integer."<<endl;
cin>>number;


for (index; ;index++) // Counting the integer
{
string a (number ,index,1);

   if(a =="")
   {
   index--;
   break;
   }
}


cout<<"Number: ";


for (index ; index >-1 ;index--)//Outputting the numbers
{
string a (number ,index,1);
cout <<a<<" ";
}

cout<<endl;
}

@andro: this is C forum, not C++. And you used void main() instead of int main().

sorry i haven't noticed :$ :$

problem solving technique to reverse the digits of an integer

Surprisingly, After 14 years, I got the same assignment too!

Write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367.

whatisthisworld
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.