![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2008
Posts: 3
Rep Power: 0
![]() |
#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? |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Mar 2008
Posts: 3
Rep Power: 0
![]() |
Re: Why my program dont cout perfect number?
pls help!
|
|
|
|
|
|
#3 | |
|
hi: for(;;) goto hi;
|
Re: Why my program dont cout perfect number?
Quote:
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5
![]() |
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; |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2008
Posts: 3
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 213
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5
![]() |
Re: Why my program dont cout perfect number?
Quote:
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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Guess a Number | 3n! | C++ | 7 | Dec 2nd, 2007 3:38 AM |
| Language display in program | Prm753 | C++ | 3 | May 30th, 2006 5:45 PM |
| Creating a program to test a program | sixstringartist | C | 8 | Jan 21st, 2006 1:15 PM |
| Support With Perfect Number. | TecBrain | C++ | 1 | Mar 7th, 2005 10:17 AM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |