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?
7 3136
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.
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.
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
You're very close.
1) Add the second for loop to the inside of the first for loop, something like: - 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.
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... -
#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;
-
}
-
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
Yeah, that code looks wonderful. Glad you were able to figure it out.
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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...
|
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...
|
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" )...
|
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,...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |