GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Jan-2008, 15:14
fomi101 fomi101 is offline
New Member
 
Join Date: Sep 2007
Posts: 20
fomi101 is on a distinguished road

Determine if a number is perfect, abundant or deficient


I have a question but I don't know how to form it, please help!

Write a program that will determine if a number is perfect, abundant or deficient..

Perfect Number – 6
Factors are 1, 2, 3, and 6
If I do not include the number itself (6), 1 + 2 + 3 = 6

6 = 6 so the number is perfect.

Deficient Number 7
Factors are 1, 7
Ignoring the seven, 1 < 7, so the it is deficient

Abundant Number 12
Factors are 1, 2, 3, 4, 6, 12
Ignoring 12, 1+2+3+4+6 = 16
16> 12 there abundant
  #2  
Old 09-Jan-2008, 20:03
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,464
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Help on question


Think about it in pseudo-code fashion, and then apply it to code:

Any necessary variables, initialization, and error checking are assumed, but the basic logic could look like this:
Code:
1. retrieve an input number 2. find factors: LOOP 1 to number - 1 if ( (number % loopIteration#) == 0 ) { factorSum += loopIteration# } 3. check result: if ( factorSum > number ) { print Abundant (might also display the 'factorSum') } else if ( factorSum < number ) { print Deficient } else { print Perfect! }
One improvement that could be added for efficiency, would be to provide an exit from the loop once the 'factorSum' becomes abundant. (avoids wasting loop cycles)

There are other ways, but this is just my $0.02.
HTH

EDIT:
After applying the above psuedo-code to source code, the interaction appears like this: (I supply the number to the program as an argument, but console input will give the same result)
Code:
C:\Documents and Settings\User\Desktop\Debug>PerfectNumber 6 6 is PERFECT as the factors totaled: 6 C:\Documents and Settings\User\Desktop\Debug>PerfectNumber 7 7 is Deficient as the factors totaled: 1 C:\Documents and Settings\User\Desktop\Debug>PerfectNumber 12 12 is Abundant as the factors totaled: 16 C:\Documents and Settings\User\Desktop\Debug>PerfectNumber 13 13 is Deficient as the factors totaled: 1 C:\Documents and Settings\User\Desktop\Debug>PerfectNumber 14 14 is Deficient as the factors totaled: 10 C:\Documents and Settings\User\Desktop\Debug>PerfectNumber 16 16 is Deficient as the factors totaled: 15 C:\Documents and Settings\User\Desktop\Debug>PerfectNumber 18 18 is Abundant as the factors totaled: 21 C:\Documents and Settings\User\Desktop\Debug>PerfectNumber 22 22 is Deficient as the factors totaled: 14 C:\Documents and Settings\User\Desktop\Debug>PerfectNumber 24 24 is Abundant as the factors totaled: 36 C:\Documents and Settings\User\Desktop\Debug>PerfectNumber 28 28 is PERFECT as the factors totaled: 28
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 09-Jan-2008 at 20:58.
  #3  
Old 10-Jan-2008, 19:58
fomi101 fomi101 is offline
New Member
 
Join Date: Sep 2007
Posts: 20
fomi101 is on a distinguished road

Re: Determine if a number is perfect, abundant or deficient


how would you find the factorSum, or the Formula for that ??
  #4  
Old 10-Jan-2008, 20:06
fomi101 fomi101 is offline
New Member
 
Join Date: Sep 2007
Posts: 20
fomi101 is on a distinguished road

Re: Determine if a number is perfect, abundant or deficient


Is the code like this ???


CPP / C++ / C Code:
#include<iostream.h>
void main()
{
	int number, factorSum;

	cout<<"Enter a number: ";
	cin>>number;

	  

       if ( (number % 2) == 0 )
       {
            factorSum += 2;
       }
	   


    if ( factorSum > number )
    {
        cout<<"Abundant";
    }
    else if ( factorSum < number )
    {
        cout<<"Deficient";
    }
    else
    {
        cout<<"Perfect!";
    }
	
}

Plzz CHANGE IF NEEDED!! THANX!
  #5  
Old 11-Jan-2008, 09:30
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,464
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Determine if a number is perfect, abundant or deficient


Quote:
Originally Posted by fomi101
Is the code like this ???
That's close...
CPP / C++ / C Code:
#include<iostream.h>
void main()
{
	int number, factorSum;

	cout<<"Enter a number: ";
	cin>>number;

/// Where's the loop? Take a look at the pseduo-code again.	  

       if ( (number % 2) == 0 ) /// need the loop value, not 2.
       {
            factorSum += 2;     /// (again) loop value, not 2.
       }
	   
///  

    if ( factorSum > number )
    {
        cout<<"Abundant";
    }
    else if ( factorSum < number )
    {
        cout<<"Deficient";
    }
    else
    {
        cout<<"Perfect!";
    }
	
}

Quote:
Originally Posted by fomi101
Plzz CHANGE IF NEEDED!! THANX!
I won't do it for you, but I added some comments for what's missing.

Also, main() is improperly defined, see here.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #6  
Old 11-Jan-2008, 16:54
fomi101 fomi101 is offline
New Member
 
Join Date: Sep 2007
Posts: 20
fomi101 is on a distinguished road

Re: Determine if a number is perfect, abundant or deficient


Which Loop, and what kind of loop ?

For loop or While loop ?
  #7  
Old 11-Jan-2008, 21:15
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,464
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Determine if a number is perfect, abundant or deficient


Quote:
Originally Posted by fomi101
Which Loop...?
It was provided in general pseudo-code form: item #2, in post #2.
Your understanding of at least one of the three loop types is assumed known.
Quote:
Originally Posted by fomi101
For loop or While loop ?
What do you prefer? It can be either. Whatever you feel more comfortable coding to accomplish the task.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
question on sorting items is not clear...plz help me gvsivannarayana C Programming Language 0 28-Mar-2006 00:26
non-member function question crq C++ Forum 1 03-Feb-2005 21:59
Simple question on arrays--please help! brookeville C++ Forum 16 17-Nov-2004 23:23
Repetition structure problem and question brookeville C++ Forum 17 29-Oct-2004 17:48
question of practice magiccreative C++ Forum 1 06-Feb-2004 07:17

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:48.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.