-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path73rdProgram_Functions_2.cpp
More file actions
41 lines (39 loc) · 955 Bytes
/
Copy path73rdProgram_Functions_2.cpp
File metadata and controls
41 lines (39 loc) · 955 Bytes
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
/****************************************************************
* Reverse of a number using Functions :-
*
* ****************************************************************/
#include <iostream>
using namespace std;
int countDigits(int); // function declaration
int reverse(int, int); // function declaration
int main(){
int num;
cout << "Enter a number:" << "\n";
cin >> num;
int rev = reverse(num, countDigits(num));
cout << "Reverse of the number is:" << rev << "\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;
}