forked from TheRenegadeCoder/sample-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Palindromic Number in C++ (TheRenegadeCoder#2522)
- Loading branch information
1 parent
170929a
commit bdd422c
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
accept an integer, reverse it, compare it with original | ||
print true, if original and reversed number are same | ||
print false, if original and reversed number are same | ||
*/ | ||
#include <iostream> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
using namespace std; | ||
|
||
void palindromic_number(int number){ | ||
int temp = number, no_of_digits = 0,reversed_number = 0; | ||
|
||
while (temp > 0){ | ||
reversed_number = (reversed_number * 10) + (temp % 10); | ||
temp = (int)(temp / 10); | ||
no_of_digits ++; | ||
} /*end of building reverse number*/ | ||
|
||
if (no_of_digits < 2){ | ||
cout <<"Usage: please input a number with at least two digits"; | ||
exit(1); | ||
} | ||
else{ | ||
if (reversed_number == number){ | ||
cout <<"true"; | ||
} | ||
else{ | ||
cout <<"false"; | ||
} | ||
} /*end of more than 2 digit number check*/ | ||
/*end of palindromic_number function*/ | ||
} | ||
|
||
/*Return 1 if number, else return 0*/ | ||
int is_int(char *number_string){ | ||
if(number_string[0] > '9' || number_string[0] < '0') | ||
return(0); | ||
for (int counter = 0; number_string[counter]; counter++){ | ||
if(number_string[0] > '9' || number_string[0] < '0') | ||
return(0); | ||
} | ||
return(1); | ||
|
||
} | ||
|
||
/* accept only integers with minimum 2 digits */ | ||
int main(int argc, char **argv){ | ||
/*Read command line arg*/ | ||
if (argc != 2 || is_int(argv[1]) != 1){ | ||
cout <<"Usage: please input a number with at least two digits"; | ||
return(1); | ||
} | ||
palindromic_number(atoi(argv[1])); | ||
return(0); | ||
} |