-
Notifications
You must be signed in to change notification settings - Fork 2
/
PointersExamples_4.cpp
51 lines (44 loc) · 1.08 KB
/
PointersExamples_4.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
#include<iostream>
using namespace std;
int lengthOfDigits(int *);
int reverseOfDigits(int* , int *);
int main()
{
int range;
cout<<"Enter a range: ";
cin>>range;
int len ;
int rev ;
for(int i =1; i<=range; i++)
{
int temp = i;
int temp2 = i;
int temp3 = i;
int *ptr = &temp; // ptr is a pointer variable that holds the address of temp.
len = lengthOfDigits(ptr); // len is a variable that holds the length of the number.
rev = reverseOfDigits(&temp2,&len); // rev is a variable that holds the reverse of the number.
if(temp3 == rev){
cout<< i << "\n";
}
}
}
int lengthOfDigits(int *number)
{
int count=0;
while(*number>0)
{
*number=*number/10;
count++;
}
return count;
}
int reverseOfDigits(int *num, int *s){
int digit, sum = 0;
for(int i=1; i<=*s; i++)
{
digit = *num%10;
*&sum = *&sum * 10 + digit;
*num=*num/10;
}
return *∑
}