c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ganeshforall
    New Member
    • Jan 2010
    • 3

    c++

    Write a function perfect that determines whether a number is a perfect number or not. This function should receive a number and return true if the number is perfect and false otherwise. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the divisors of each perfect number to confirm that the number is indeed perfect as shown in the sample output below.

    An integer is said to be a perfect number if the sum of its divisors, including 1 (but not the number itself), is equal to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3.
    I try to code but not run...check it once my code

    #include <iostream.h>
    #include <conio.h>

    int perfectNumber(i nt);
    int main()
    {
    clrscr();
    cout<<"this progame will print all the perfect numbers\n\n";
    for(short v=2;v<=1000;v++ )
    if(perfectNumbe r(v))
    {
    cout<<v<<" factors are:";
    for(short c=1;c<v;c++)
    if(v%c==0)
    cout<<c<<" ";
    cout<<endl;
    }
    getch();
    return 0;
    }

    int perfectNumber(i nt num)
    {
    int sum=0, r;
    for(short v=1;v<num;v++)
    {
    r=num%v;
    if(r==0)
    sum=sum+v;
    }
    if(sum==num)
    return 1;
    return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It runs for me.

    Why do you say it doesn't run for you?

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      Doesn't run for me because of clrscr().

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Did it compile and link?

        Comment

        Working...