C++ programming. i need help in this.plz help me!

Write a program that repeatedly reads a positive integer, determines if the integer is *deficient*, *perfect*, or *abundant*, and outputs the number along with its classification. A positive integer, n, is said to be *perfect* if the sum of its proper divisors equals the number itself. (Proper divisors include 1 but not the number itself. For example 6 is a perfect integer because its divisors are 1, 2 and 3 and the sum of them is 6). If this sum is less that n (the number), the number is *deficient*, and if the sum is greater than n, the number is *abundant*.

this is the question and i dont know how shuld i begin.........i know that i hv to use if/else .....but how to get the divisors condition?

Re: C++ programming. i need help in this.plz help me!

For a given number n, you can run a loop for i from 1 to n - 1: if i divides n, then add i to a special variable-accumulator (before the loop, this variable has to be initialized to zero). This way, after the loop ends, the accumulator will contain the sum of the divisors of n.

Re: C++ programming. i need help in this.plz help me!

what do you mean by special variable-accumulator ?

Re: C++ programming. i need help in this.plz help me!

Hi Riana! :)

To check if a number i is a divisor of n, you can use:

Code:

`if (n / i * i == n)`

{

// i is a divisor of n

}

This works because (n / i) will do a division rounding down when both i and n are integers.

Re: C++ programming. i need help in this.plz help me!

#include <iostream>

using namespace std;

int main()

{ int n;

int sum=0;

cout<<"enter your number"<<endl;

cin>>n;

for(int i=1;i<n;i++)

if (n / i * i == n){

sum+=i;

}

if (sum<n){

cout<<"def"<<endl;}

if (sum>n){

cout<<"abun"<<endl;}

else{

cout<<"perf"<<endl;}

return 0;

}

this is my code but its not working properly..............help me plz.

Re: C++ programming. i need help in this.plz help me!

You may want to insert an "else" here:

Code:

`if (sum<n){`

cout<<"def"<<endl;

}

*else* if (sum>n){

cout<<"abun"<<endl;

}

else{

cout<<"perf"<<endl;

}

Re: C++ programming. i need help in this.plz help me!

thanx it works now....:)

can you help me in other problems too?

Re: C++ programming. i need help in this.plz help me!

Sure!

But please start a new thread.

Re: C++ programming. i need help in this.plz help me!

Re: C++ programming. i need help in this.plz help me!

Quote:

Originally Posted by

**Riana** what do you mean by special variable-accumulator ?

I mean a variable that acts as an accumulator, i.e., it accumulates the divisors found so far. In your code, it's "int sum".

Quote:

Originally Posted by

**ILikeSerena** To check if a number i is a divisor of n, you can use:

Code:

`if (n / i * i == n)`

{

// i is a divisor of n

}

A better way is to check if (n % i == 0). Here n % i returns the remainder when n is divided by i.

Quote:

Originally Posted by

**Riana** this is my code but its not working properly.

In the future, always describe exactly why you think that the code is not working properly. The message that it is just not working carries almost no information. In this case, you could write that when n = 7, the program prints that it is both deficient and perfect.

Riana, you may also need to consider that the problem statement says to print the number and not just its classification, and it asks to read numbers repeatedly.