Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,227 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,059 people online right now. Registration is fast and FREE... Join Now!




Happy NUmbers

 
Reply to this topicStart new topic

Happy NUmbers

angelf
post 12 Mar, 2008 - 09:13 PM
Post #1


New D.I.C Head

*
Joined: 11 Mar, 2008
Posts: 6


My Contributions


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.
(cool.gif 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
User is offlineProfile CardPM

Go to the top of the page

Bench
post 13 Mar, 2008 - 01:30 AM
Post #2


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 601



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


QUOTE(angelf @ 13 Mar, 2008 - 05:13 AM) *

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

You've lost me already there - How did you get 153 from that? 62+22+82+72 = 238 according to my math

For that matter, where did you get the component numbers? I'd have assumed "squares of its digits" for 6287 would mean 6*6 + 2*2 + 8*8 + 7*7 (which does equal 153)

This post has been edited by Bench: 13 Mar, 2008 - 01:31 AM
User is offlineProfile CardPM

Go to the top of the page

angelf
post 13 Mar, 2008 - 01:39 AM
Post #3


New D.I.C Head

*
Joined: 11 Mar, 2008
Posts: 6


My Contributions


QUOTE(Bench @ 13 Mar, 2008 - 02:30 AM) *

QUOTE(angelf @ 13 Mar, 2008 - 05:13 AM) *

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

You've lost me already there - How did you get 153 from that? 62+22+82+72 = 238 according to my math

For that matter, where did you get the component numbers? I'd have assumed "squares of its digits" for 6287 would mean 6*6 + 2*2 + 8*8 + 7*7 (which does equal 153)

User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 13 Mar, 2008 - 04:38 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,170



Thanked 33 times

Dream Kudos: 25
My Contributions


I think the OP may have been using the 2 component of each 2 digit integer to indicate a square.
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 09:10PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month