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.
/* 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();
}
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!
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.