perfect number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int s=0;
for (int i=1; i<=1000; i++)
{
for (int j=1; j<=i; j++)
{
if(i%j==0)
s=s+j;
if(s==i)
{
cout <<"perfect number"<<i;
}}}
getche();
}

i am unable to find the error in this program. program doesnot run
This is the cleaned-up version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main( int argc, char* argv[] ) {
    int s = 0;

    for ( int i = 1; i <= 1000; i++ ) {
        for ( int j = 1; j <= i; j++ ) {
            if ( i % j == 0 ) {
                s = s + j;
            }
            if ( s == i ) {
                std::cout << "Perfect number: " << i << std::endl;
            }
        }
    }

    std::cin.get();

    return 0;
}

- use iostream, not iostream.h
- do not use conio.h and its functions ( clrscr() and getche() ), because it's non-standard in C++. In one compiler it might work, in another it doesn't
- use int main with return 0;
- indent your code. That saves every reader of it an headache :)
Last edited on
@Fransje he may be using Turbo C++

By the way what is the error, compile time, run time or what.
Last edited on
Fransje he may be using Turbo C++

Yeah, but still it is good practice to indent code and use int main(); return 0;.
Topic archived. No new replies allowed.