Results 1 to 10 of 10
Like Tree2Thanks
  • 1 Post By ILikeSerena
  • 1 Post By ILikeSerena

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

  1. #1
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

    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?
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor
    Joined
    Oct 2009
    Posts
    5,513
    Thanks
    769

    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.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

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

    what do you mean by special variable-accumulator ?
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Super Member ILikeSerena's Avatar
    Joined
    Dec 2011
    Posts
    733
    Thanks
    121

    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.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

    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.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Super Member ILikeSerena's Avatar
    Joined
    Dec 2011
    Posts
    733
    Thanks
    121

    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;
    }
    Thanks from Riana
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

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

    thanx it works now....
    can you help me in other problems too?
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Super Member ILikeSerena's Avatar
    Joined
    Dec 2011
    Posts
    733
    Thanks
    121

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

    Sure!
    But please start a new thread.
    Thanks from Riana
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

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

    i did it.
    Follow Math Help Forum on Facebook and Google+

  10. #10
    MHF Contributor
    Joined
    Oct 2009
    Posts
    5,513
    Thanks
    769

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

    Quote Originally Posted by Riana View Post
    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 View Post
    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 View Post
    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.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. C++ programming. i need help !
    Posted in the New Users Forum
    Replies: 3
    Last Post: February 22nd 2013, 06:32 PM
  2. Replies: 8
    Last Post: February 20th 2013, 07:35 PM
  3. Programming For HP 48G+
    Posted in the Calculators Forum
    Replies: 0
    Last Post: February 3rd 2009, 03:09 AM
  4. Replies: 1
    Last Post: November 17th 2008, 03:18 AM
  5. Please help on R programming
    Posted in the Math Software Forum
    Replies: 0
    Last Post: August 16th 2008, 02:51 AM

Search Tags


/mathhelpforum @mathhelpforum