The reverse procedure

I am trying to write a program that reverses integers. And the number must be positive. I almost have to program done however, when the input is 537 the output is 734 and I don't know why.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* PROGRAM COMMENT */
/* The program computes and displays the reversed number of an
integer entered by the user */
#include <stdio.h> /* LIBRARY INCLUSIONS */
#include "genlib.h"
#include "simpio.h"
#include "math.h"
/* FUNCTIONS PROTOTYPE */
void displayPrompt ( void ); /* displayPrompt */
void displayRevNum ( int number ) ; /* displayRevNum */
int rev(int number); /* reverse */
int count_digits(int num); /* countDigits */
main() /* MAIN PROGRAM */
{
int number;
displayPrompt();
number=GetInteger();
rev(number);
displayRevNum(number);
}
/*_____________THE BODY OF THE FUNCTIONS ____________________*/
int rev(int number) /* The reverse function */
{
int n_digits, rev_num, units;
rev_num = 0;
while (number > 0 )
{
units = number % 10;
n_digits = count_digits (number);
rev_num = rev_num + units * pow (10,n_digits - 1);
number = number /10;
}
return (rev_num);
}
int count_digits(int num) /* The count_digits function */
{
int n_digits;
n_digits = 0;
while (num > 0)
{
n_digits = n_digits + 1;
num = num/10;
}
return (n_digits);
}
void displayPrompt ( void ) /* The displayPrompt function */
{
printf (" This program computes the reversed number of a given integer.\n");
printf ("Please enter a positive integer ");
}
void displayRevNum ( int number ) /* The displayRevNum function */
{
printf ("The reversed number of %d is %d\n", number, rev (number));
getchar();
}
Whenever the input number is more then 2 digits long then the last digit in the output will be subtracted by 1. I have no idea why it's doing this.
please help.
i have checked the programme, its working fine.
i removed genlib and simpio header files and instead of GetInteger() i used scanf("%d",&number);
Dont have idea. Whats the definition of the function GetInteger();
the function revNum (int num) is perfectly. DEbug what GEInteger() is doing!
Im using Dev-C++ and GetInteger() is basically the same thing as cin in mv C++. But GetInteger(); gets integers.
Last edited on
the program works fine . but it will get the number wrong when the input is 3 digits long.
Last edited on
declare rev_num as double.. i believe your compiler is giving you a warning something like
warning: converting 'int' from 'double'

this code below demonstrates your mistake..
1
2
3
4
5
6
7
8
9
10
#include <math.h>
#include <iostream>
using namespace std;

int main() {
    int t = 3;
    int x = 7*pow(10, t-1);
    cout << x << endl;
    return 0;
}


hope it helps
I never thought that because a number was int instead of being double that it would cause my program to work wrong. Thank you so much blackcoder and you too somshekhar. The program works now.
i'm glad it helped you..
Topic archived. No new replies allowed.