473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Help C++ Beginer

If someone could please help me...I'm learning how to use C++ but no matter how long I attempt this problem I cannot seem to get anywhere...if you could help with some input, I would be much appreciated...its the following...

* a perfect number has a sum of divisors equal to the number itself. The first
perfect number is 6, because the sum of its divisors (1,2,3) is equal to 6 itself.
* a deficient number has a sum of divisors less than the number itself. Therefore, 5 is a deficient number because the sum of its divisors (1) is less than 5.
* an abundant number has a sum of divisors more than the number itself. Therefore, 12 is an abundant number because the sum of its divisors (1,2,3,4,6) is more than 12.

Program Specifications
Your program will:

1. Your program takes a single input, the highest number to examine. Thus, if you enter 1000, you examine all the integers from 1 to 1000 (inclusive).
2. Every time a perfect number is found, that number is reported.
3. At the end of execution, a count of the three categories is provided for the numbers between 1 and the entered number. We will test the program on the interval 1 to 1000.

Assignment Notes:

The first problem is to find the divisors of a number. Think about two things. First, given any target number, how should I check if another number is a divisor of the target? You will find the % operator to be useful in this task.

Second, if I can find divisors, how can I determine if a number is perfect, deficient or abundant? How do I design a piece of program to make that decision? Look at the Overview above for the conditions and think about how to make that decision.

Third, how can use the above solutions and apply it to every number between 1 and the entered number?
Jan 29 '07 #1
7 3136
horace1
1,510 Expert 1GB
initially read in a number and then loop from 1 to that number looking for divisors and list them. Once that is working you can sum the divisors to determine if it is a perfect number, etc.
Jan 29 '07 #2
Ganon11
3,652 Expert 2GB
Step 1.0: Get user input to determine high range of checks (1000 in your example)
(Stored in variable high)
Step 2.0: Variable Declarations (You'll need a total for abundant, deficient, and perfect numbers)
(Variable names abunTotal, defTotal, and perfTotal)
Step 2.1: Variable initialization (Set abunTotal, defTotal, and perfTotal to 0.

Step 3.0: For loop, from 1 to 1000, inclusive (Index called i)
Step 3.1: Declare temporary variable called sum (set to 0)
Step 3.2: Second for loop, from 1 to i\2, inclusive (index called j)
Step 3.2.a: IF the number i is a multiple of the number j
Add j to sum
Step 3.3: IF sum == i, the number is perfect
Step 3.4: IF sum < i, the number is deficient
Step 3.5: IF sum > i, the number is abundant

Step 4: Report the information back using cout statements.
Jan 29 '07 #3
http://rafb.net/p/jcBCk693.html

the above link is my code wriiten on lines...

Am I on the right track...???

Thanks for the help... I really appreciate it
Jan 29 '07 #4
Ganon11
3,652 Expert 2GB
You're very close.

1) Add the second for loop to the inside of the first for loop, something like:

Expand|Select|Wrap|Line Numbers
  1. for ( int i=0; i <= 1000; i++ )
  2. {
  3.     // whatever...
  4.     for ( int j=0, j <= i/2; j++)
  5.     {
  6.         // whatever...
  7.     }
  8.  
  9.     // Checks for addition for abundant, deficient, perfect (your if statements)
  10. }
2) Your first for loop uses 0 and <= to 1000 as its limits: You should be using 1 as your lower limit, and the user-input number as your upper limit (highNum).

3) Define sum within your first for loop, so that it will automatically reset every time you start calculations on a new number.
Jan 30 '07 #5
I got it to return the correct information...how does that look...I really do appreciate your help, its not that im lazy, it's i really dont understand this all that well yet...
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. /*-------------
  6. -------------*/
  7.  
  8.  
  9. int main ()
  10. {
  11.     int highNum, abunTotal, defTotal, perfTotal;
  12.  
  13.     abunTotal = 0;  // sum of divisors more than the number itself
  14.     defTotal = 0;   // sum of divisors less than the number itself
  15.     perfTotal = 0;  // sum of divisors equal to the number itself
  16.     int sum = 0;
  17.  
  18.     cout<< "Please enter a positive integer:";
  19.     cin>> highNum;
  20.  
  21.     for(int i = 1; i <= highNum; i++ )
  22.     {
  23.         sum = 0;
  24.         for(int j = 1; j < i; j++ )
  25.         {
  26.             if((i % j) == 0)
  27.             {
  28.                 sum += j;
  29.             }
  30.         }
  31.  
  32.          if ( sum == i) //the number is perfect
  33.         {    
  34.             perfTotal++;
  35.             cout << "PERFECT NUMBER: " << i << endl;
  36.         }
  37.  
  38.         if ( sum < i)  //the number is deficient
  39.         {
  40.             defTotal++;
  41.         }    
  42.  
  43.         if ( sum > i) //the number is abundant
  44.         {
  45.             abunTotal++;
  46.         }
  47.     }
  48.  
  49.     cout << "The perfTotal is:" << perfTotal << endl;
  50.     cout << "The defTotal is:" << defTotal << endl;
  51.     cout << "The abunTotal is:"<< abunTotal << endl;
  52.     cout << endl;
  53. }
  54.  
Jan 30 '07 #6
horace1
1,510 Expert 1GB
the results look OK, when I ran with 10000 I got
Please enter a positive integer: 10000
PERFECT NUMBER: 6
PERFECT NUMBER: 28
PERFECT NUMBER: 496
PERFECT NUMBER: 8128
The perfTotal is:4
The defTotal is:7508
The abunTotal is:2488
Jan 30 '07 #7
Ganon11
3,652 Expert 2GB
Yeah, that code looks wonderful. Glad you were able to figure it out.
Jan 30 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: scott289 | last post by:
I have successfully downloaded the Enterprise Library and the data access block (C#). I am able to compile and run the quickstart application. So I would like to try the DAB in a new project....
1
by: serge calderara | last post by:
Dear all, I am beginer in asp.net and I try to understand how the context.handler works I have first web page on which I have 2 text box prefilled with default text Then from the second...
3
by: dencdr | last post by:
Hi, I have an application VB.NET (using Form Win32) with no icon in TaskBar. When a FORM of this application is visible I see this application in the list of "Alt+Tab" choice. How do for...
1
by: itsjyotika | last post by:
Hello Everyone, I need to read data from a CVS file(i created it from micosoft excel) and then need to match it with the one of the date from the command line.If the date is there then it should say...
5
by: hn.ft.pris | last post by:
Hi: I'm a beginer of STL, and I'm wondering why none of below works: ######################################################################## .......... string str("string"); if ( str == "s" )...
3
by: Nicholas | last post by:
Hello, I'm a Javascript beginer. The resources on the Internet are enormus. Does anyone know is there somethinh like a help file or something similar that incluedes only a list of commands,...
1
by: =?Utf-8?B?YWJh?= | last post by:
okay so i'm creating a game using a tutorial from code4fun called beginer programming. It is telling me to create a render loop but does not represent how to do this clearly enough for me. could...
1
by: hamed steph | last post by:
i'm a beginer in programing and i need very much your help .i need explanation about while statement (loop initialization and structure of while ) also qualifier (long,short,unsigned ,signed,) type...
3
trueadvantage
by: trueadvantage | last post by:
Hi, I am having a slight but not critical problem. I am beginer with the MS Access and still learning. Problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.