Pleaseee help,Problems with formatting

i was writing a prefect number programme,

#include <iostream>
using namespace std;

int N1, N2, sum=0;
cin >> N1 >> N2;
for (; N1< N2; N1++){
for (int j=1; j<=i/2; j++){
if (i%j==0) sum+=j;}
if(sum==i) cout << sum;}

It outputs a lot of error, how should i solve it? thank youuu

main.cpp:13:1: error: 'cin' does not name a type
cin >> N1 >> N2;
^
main.cpp:14:1: error: expected unqualified-id before 'for'
for (; N1< N2; N1++){
^
main.cpp:14:8: error: 'N1' does not name a type
for (; N1< N2; N1++){
^
main.cpp:14:16: error: 'N1' does not name a type
for (; N1< N2; N1++){
^
Last edited on
#include <iostream>
using namespace std;
int main(){
int N1, N2, sum=0;
cin >> N1 >> N2;
for (int i=N1; N1< N2; i++){
for (int j=1; j<=i/2; j++){
if (i%j==0) sum+=j;}
if(sum==i) cout << i;}

return 0;

}

why when i input 1 100, it outputs nothing.....
Because it goes on for ever.


for (int i=N1; N1< N2; i++){
should be
for (int i=N1; i <=N2; i++){


You need to re-set sum to 0 before the start of EVERY j loop (not just the start of the program).


Unless you want all output jammed together
if(sum==i) cout << i;}
should be
if(sum==i) cout << i << '\n';}


PLEASE USE CODE TAGS
Topic archived. No new replies allowed.