Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Why my program dont cout perfect number? (http://www.programmingforums.org/showthread.php?t=15444)

adenus Mar 19th, 2008 3:29 PM

Why my program dont cout perfect number?
 
:

#include <iostream>
using namespace std;

//aprekjina funkcija

int Perfectnumber (int rob) {
    int result;
    int sum = 0;
        for (int i = 1; i < rob; i++) {
            if (rob % i == 0) {
              sum = sum + i;
            }
        }
        if (sum == rob) {
          result=sum;
        }
return (result);
}
//galvena funkcija
int main() {
int rez;
int lim;
    cout<<"ievadi robesu lidz kurai meklet : ";
          cin>>lim ;     
    for (int i = 1; i < lim; i++) {
        rez=Perfectnumber(lim);
    }
        cout<<"perfect numbers from 1 to"<<lim<<" : "<<rez<<endl;
    system ("pause");
return 0;
}


why dont it cout perfect numbers, ? and in one forum for me advice to inicialise result, but i dont know which value add to it?

adenus Mar 19th, 2008 3:59 PM

Re: Why my program dont cout perfect number?
 
pls help!

peaceofpi Mar 19th, 2008 4:06 PM

Re: Why my program dont cout perfect number?
 
Quote:

:

    for (int i = 1; i < lim; i++) {
        rez=Perfectnumber(lim);
    }
        cout<<"perfect numbers from 1 to"<<lim<<" : "<<rez<<endl;


Rethink the logic here.

grumpy Mar 19th, 2008 4:11 PM

Re: Why my program dont cout perfect number?
 
Don't bump threads. If people have an answer to offer, they will. If you don't get a reply, it means people can't help or are disinclined to.

In any event, the code;
:

for (int i = 1; i < lim; i++) {
        rez=Perfectnumber(lim);
    }
cout<<"perfect numbers from 1 to"<<lim<<" : "<<rez<<endl;

only prints out the value of lim, and the value of rez produced in the last iteration through the for loop. If you want to print out every value that rez takes, put the output statement inside the loop.

adenus Mar 19th, 2008 4:24 PM

Re: Why my program dont cout perfect number?
 
:

#include <iostream>
using namespace std;
//aprekjina funkcija

int Perfectnumber (int rob) {
    int result;
    int sum = 0;
        for (int i = 1; i < rob; i++) {
            if (rob % i == 0) {
              sum += i;
            }
            if (sum == rob) {
              return true;
            } else {
              return false;     
            }
        }
return true;
}
//galvena funkcija
int main() {
int rez;
int lim;
    cout<<"ievadi robesu lidz kurai meklet : ";
          cin>>lim ;     
    for (int i = 1; i < lim; i++) {
        rez=Perfectnumber(lim);
    }
        cout<<"perfektie skaitļi no 1 lidz "<<lim<<" : "<<endl<<rez<<endl;
    system ("pause");
return 0;
}
//ievadot jau zinamu sk, to ari izvada, bet citus nee

if i have like that is it right, or what i have to correct, because this advice to move cout into loop, dont help.?

Seif Mar 19th, 2008 4:50 PM

Re: Why my program dont cout perfect number?
 
:

int Perfectnumber (int rob) {
    int result;
    int sum = 0;
        for (int i = 1; i < rob; i++) {
            if (rob % i == 0) {
              sum += i;
            }
            if (sum == rob) {
              return true;
            } else {
              return false;     
            }
        }
return true;
}


is wrong for a start

the if sum == rob statement should be outside of the for loop, otherwise your exiting after the first iteration, you need that loop to finish calculating sum before you do this if check.

:

for (int i = 1; i < lim; i++) {
        rez=Perfectnumber(lim);
    }


I don't understand why you need to check if n is a perfect number n amount of times. Perhaps you should look at your code and understand whats happening.

grumpy Mar 19th, 2008 6:41 PM

Re: Why my program dont cout perfect number?
 
Quote:

Originally Posted by adenus (Post 142684)
if i have like that is it right, or what i have to correct, because this advice to move cout into loop, dont help.?

The advice helps as much as it is possible, given the way you asked your question.

The basic problem in your code is that you have code outside loops that need to be inside the loops, and other things inside loops that need to be outside. Hence the code runs incorrectly. That is enough information for you to fix your problem, if you just think about it a bit.


All times are GMT -5. The time now is 3:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC