-
Notifications
You must be signed in to change notification settings - Fork 3
/
74thProgram_Functions_3.cpp
59 lines (57 loc) · 1.16 KB
/
74thProgram_Functions_3.cpp
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
56
57
58
59
/*********
* Palindrome Number using Functions
*
* ****************************************************************/
#include <iostream>
using namespace std;
int countDigits(int); // function declaration
int reverse(int, int); // function declaration
int Palindrome(int, int); // function declaration
int main()
{
int range;
cout << "Enter a range:"
<< "\n";
cin >> range;
for (int i = 1; i <= range; i++)
{
int temp = i;
int k = Palindrome(temp, reverse(temp, countDigits(temp)));
if (k > 0)
{
cout << k << "\n";
}
}
return 0;
}
int countDigits(int i)
{
int s = 0;
while (i != 0)
{
// a = a%10(is un-necessary just for showing mod op)
i = i / 10;
s = s + 1;
}
return s;
}
int reverse(int num, int s)
{
int digit;
int rev = 0;
for (int i = 1; i <= s; i++)
{
digit = num % 10;
rev = rev * 10 + digit;
num = num / 10;
}
return rev;
}
int Palindrome(int num, int rev)
{
if (num == rev)
{
return rev;
}
return 0;
}