Reversed Integer Using Arrays, My friend told me its to complicated

The assignment everyone has done before..lol...reversing digits. How do i make my code more simple (as my friend stated)?

Trying to write a program that reverses the digits in an integer. Assuming that the max value of the integer is 99999. And using arrays to solve the problem.

For example if integer:
0 was entered, output is 0
10 was entered, output is 1
12 was entered, output is 21
123 was entered, output is 321
7600 was entered, output is 67
8015 was entered, output is 5108
90000 was entered, output is 9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <stdio.h>
#include <math.h>

int main()
{
      int intnumber, condition, remainder;
      int counter = 0, i = 0, j = 0, counter1 = 0;
      int reverseint[99999];       

	  printf("Enter an integer number: ");
      scanf_s("%d", &intnumber);
      condition = intnumber;  

	  while (condition > 0)
      {
         condition  = condition /10;
         counter = counter + 1;
         counter1 = counter1 + 1;
      } 

	  counter = counter - 1;
      printf("The number in reverse: ");
      while (counter >= 0)
     {
         remainder = intnumber % (int) pow(10, counter);
         intnumber = intnumber/(int) pow(10,counter);     
		 reverseint[i] = intnumber;
         i++;
         intnumber = remainder;
         counter = counter - 1;
      }

      for(j=counter1-1;j >= 0;j--)
	  {
         printf("%d ", reverseint[j]);
		 printf("\n");
	  }  
}
Last edited on

I found out that, I can't use printf but instead use couts.
I'm in a beginner level's C++ class and my teacher also wants it more simpler..aha

said i can't use scanf_s, pow, and strings!?!?!
Your teacher wants you to use cin and cout instead of printf and scanf.

And they're right, you're making this way more complicated than it needs to be.

Think of it this way... to move a digit to the left, you multiply by 10, right?
and to move a digit to the right... you divide by 10.

There's also the mod operator, which gives you the remainder of division. So basically x % 10; will give you the rightmost digit of x.

You should only need 1 loop, 1 division, 1 mod, 1 multiply, and no other math.
As Disch says ........but also......
1. Start with a 5 element array with all elements initializes to 0 (zero)
2. Using a for loop with condition set to i<5; enter up to 5 integers. (Pad with zeros to get to 5.)
3. Read them out backwards using i--;but if array[4]==0 don't print it. print ""
4. Test at each iteration as in 3. above.
5. However if array [i] >0 print array[i-1] even if == 0.
If you need to make several manipulations put the whole lot in a while loop.
I'm sure you will be able to improve on this... especially if I've missed something!
You don't need an array. Using a 5 element array limits you to 5 characters. It also makes you convert to a string (or a series of characters) which is wholly unnecessary.

You can do this with 2 integers and a loop. We're talking 3 or 4 lines of code.
@Ditch
Yes the OP has stated MAX value to be handled is 99999 so 5 integers are involved.
It also makes you convert to a string (or a series of characters) which is wholly unnecessary.

No incorrect.
I'd be interested to read your pseudo code as I can't quite visualize the process.
It's Disch with an 's'. Pronounced like "Dish". The extra C is for "cool". ;D

I missed that about the max. And also I might have gotten this post mixed up with another one regarding unnecessary conversion to strings. My mistake!

I can't really give pseudocode without giving it away completely. So I'll just give it away completely:

1
2
3
4
5
6
7
8
9
10
11
int ReverseANumber( int num )
{
  int rev = 0;
  while( num > 0 )
  {
    rev *= 10;  // move reversed digits to the left
    rev += (num % 10);  // add the rightmost digit of original number
    num /= 10;  // move original number digits to the right
  }  // repeat until number is zero
  return rev;
}
Sorry about my typo Disch
Yes I can see why you include the 'c'
The code is very cool
******************************
Enter a number using 5 digits max.
90720
rev:= 0 num:= 9072
rev:= 2 num:= 907
rev:= 27 num:= 90
rev:= 270 num:= 9
rev:= 2709 num:= 0
The reverse of this number is 2709
******************************
I suppose its very obvious to you blokes but i really had to go through it with a pencil before i could get it. Many thanks








































Topic archived. No new replies allowed.