-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraded_Program1.cpp
More file actions
71 lines (62 loc) · 2.27 KB
/
Copy pathGraded_Program1.cpp
File metadata and controls
71 lines (62 loc) · 2.27 KB
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
#include <iostream>
using namespace std;
// Please write your code, within the "BEGIN-END" blocks given below.
// A "BEGIN-END" block is identified as follows :
//
// "//// BEGIN: Some string DONT_ERASE_xx_yy"
//
//
// "//// END: Some other string DONT_ERASE_xx_yy"
//
// where "xx" is the block number and "yy" is the
// marks allocated for the block
//
// The FIRST block (BLOCK 1 i.e. DONT_ERASE_01_0) carries 0 marks and
// is a placeholder for your personal information, to be written as a comment.
//
// WARNING :
// (1) Do NOT write any cout or cin statements
// (2) Do NOT delete or modify the existing code i.e. main function, comments, blocks, etc.
// (3) Write your code in between BEGIN and END of the respective blocks only
// (4) Do NOT rename the .cpp file
//// ---------------------------------------------------------------------------
//// BEGIN: Fill your details as comments below DONT_ERASE_01_0
//// Name: Neeraj Patidar
////
//// END: Fill your details as comments above DONT_ERASE_01_0
//// ---------------------------------------------------------------------------
int main() {
int number, max_cycles=10;
int finalNumber, cycle_no=0;
cout << "Enter a number " << endl;
cin >> number;
//// ---------------------------------------------------------------------------
//// BEGIN: Write code below to check whether the number entered is a happy number or not (for max 10 cycles). DONT_ERASE_02_01
finalNumber=0;
int a;
while(cycle_no<max_cycles)
{
cycle_no++;
while(number!=0)
{
a=number%10;
finalNumber=finalNumber+a*a;
number=number/10;
}
if(finalNumber==1)
break;
else
{
number=finalNumber;
if(cycle_no!=10)
finalNumber=0;
}
}
//// END: End of your code DONT_ERASE_02_01
//// ---------------------------------------------------------------------------
if ( finalNumber == 1 )
cout << "The number is a happy number. Detected after " << cycle_no << " cycles " <<endl;
else
cout << "Could not establish the fact that the number entered by the user is a happy number or not. The number obtained in " << cycle_no << " cycles is " << finalNumber << endl;
return 0;
}