-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGet1.c
108 lines (91 loc) · 2.33 KB
/
Get1.c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**********************************************************
Statement - Get The 1
Programmer - Vineet Choudhary
Written For - https://developerinsider.co
**********************************************************/
/*
----------------------------------------------------
Problem:
----------------------------------------------------
Katoch, a notorious boy, is always keen about getting number 1. Given a number, he squares
every digit of the number and adds to the result. He repeats the process until he gets a 1
digit number. Suppose he is given a number 82, he squares 8 & 2 individually and then add the
result to make it 68. Then he continues with 68 and repeats the process until he gets a single
digit number. If he gets 1, he jumps otherwise he doesn't. Given a number, decide whether he
jumps or not.
Example for 82:
---------------
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
Katoch jumps
INPUT:
------
T (number of test cases) followed by T cases. 0 < T (test cases) < 1000 N (Number) < 10^9 Time limit: 5 sec
OUTPUT:
-------
Case #'case_no':YES //if he jumps
Case #'case_no':NO //if he does not jump
----------------------------------------------------------
Sample Test case:
----------------------------------------------------------
Input:
------
3
82
1
67
Output:
-------
Case #1:YES
Case #2:YES
Case #3:NO
*/
/*-------------------------------------------------------
Solution - Try yourself first :P
---------------------------------------------------------*/
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <conio.h>
static int counter=0;
static void calcSum(int num, int tc)
{
if (num == 1)
{
printf("Case #%d:YES\n", tc);
}
else
{
counter++;
if (counter >= 20)
printf("Case #%d:NO\n", tc);
else
{
int tempSum=0;
while (num > 0)
{
tempSum += pow((num % 10),2);
num = num / 10;
}
calcSum(tempSum, tc);
}
}
}
void main()
{
int tc;
clrscr();
scanf("%d",&tc);
if (tc >= 1 && tc <= 1000)
{
for (int i = 0; i < tc; i++)
{
counter = 0;
int num;
scanf("%d",&num);
calcSum(num, i+1);
}
}
getch();
}