Help C++ Beginer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • laceemup
    New Member
    • Jan 2007
    • 3

    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...i ts 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?
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    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.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      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.

      Comment

      • laceemup
        New Member
        • Jan 2007
        • 3

        #4
        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

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          You're very close.

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

          Code:
          for ( int i=0; i <= 1000; i++ )
          {
              // whatever...
              for ( int j=0, j <= i/2; j++)
              {
                  // whatever...
              }
          
              // Checks for addition for abundant, deficient, perfect (your if statements)
          }
          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.

          Comment

          • laceemup
            New Member
            • Jan 2007
            • 3

            #6
            I got it to return the correct information...h ow 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...
            Code:
            #include <iostream>
             
            using namespace std;
             
            /*-------------
            -------------*/
             
             
            int main ()
            {
            	int highNum, abunTotal, defTotal, perfTotal;
             
            	abunTotal = 0;  // sum of divisors more than the number itself
            	defTotal = 0;   // sum of divisors less than the number itself
            	perfTotal = 0;  // sum of divisors equal to the number itself
            	int sum = 0;
             
            	cout<< "Please enter a positive integer:";
            	cin>> highNum;
             
            	for(int i = 1; i <= highNum; i++ )
            	{
                    sum = 0;
            		for(int j = 1; j < i; j++ )
                    {
                        if((i % j) == 0)
                        {
                            sum += j;
                        }
            		}
             
             		if ( sum == i) //the number is perfect
            		{	
            			perfTotal++;
                        cout << "PERFECT NUMBER: " << i << endl;
            		}
             
            		if ( sum < i)  //the number is deficient
            		{
            			defTotal++;
            		}	
             
            		if ( sum > i) //the number is abundant
            		{
            			abunTotal++;
            		}
            	}
             
            	cout << "The perfTotal is:" << perfTotal << endl;
            	cout << "The defTotal is:" << defTotal << endl;
            	cout << "The abunTotal is:"<< abunTotal << endl;
            	cout << endl;
            }
            Last edited by horace1; Jan 30 '07, 09:37 AM. Reason: added code tags

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              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

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Yeah, that code looks wonderful. Glad you were able to figure it out.

                Comment

                Working...