this is the problem given:
Take any integer and find the sum of the squares of its digits. For example, if we take 6287, then the sum is:
62 + 22 + 82 + 72 = 153
Take this sum and repeat the operation, ie.
12 + 52 + 32 = 35
Continue until you either reach 1 or 4 as an answer. If the answer is 1, the number is HAPPY (the operation on this number no longer changes it), otherwise if the answer is 4, the number is SAD (since continued operations on the number will eventually reach 4 again).
Your task is to produce a table in the format below for all integers from 100 to 300 inclusive.
Starting Number Happy/Sad No. of Operations
100 Happy 1
101 Sad 2
. . .
. . .
. . .
300 Sad 11
The number of operations in the third column is the number of times that you square and add until you reach 1 or 4.
Print only 55 results per page and appropriate headings on each page.
When all results are printed, you should provide a summary printout on a fresh page of:
(a) the proportion of happy numbers.
(

the starting number or numbers leading to the largest number of operations.
© the average number of operations.
This summary printout should be suitably labelled.
CODE
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
void main()
{
int ctr = 1, sum = 0,num1=0,mult=10,o;
int number[200];
bool happy = true;
cout<<"Starting number\tHappy/Sad\tNo. of Operations\n\n\n";
for(int i = 100;i<300;i++)
{
number[num1] = i;
num1++;
}
num1=0;
sum=number[num1];
do
{
for( o = number[num1];(sum!=1)||(sum!=4);number[num1]=sum)
{
o=sum;
do
{
o= o%10;
o= pow(o,2);
sum+=o;
o=number[num1];
o/=mult;
mult*=10;
ctr+=1;
}
while(o!=0);
}
num1++;
cout<<" "<<ctr<<" "<<sum;
}while(num1<54);
}
This post has been edited by angelf: 12 Mar, 2008 - 09:14 PM