0

Program to find a perfect number in c++. (please tell where im wrong)

What did i do wrong??? My code for t test cases- #include <bits/stdc++.h> using namespace std; int main() { int t, count, n; cin>>t; count=0; while(t--){ cin>>n; int i; for(i=1; i=n/2; i++) { if(n%i==0) count= count + i; if(n%i!=0) count= count + 0; } if(count ==n) cout<<"true"<<endl; else cout<<"false"<<endl; } return 0; }

11th Aug 2020, 12:18 PM
Navneet Chauhan
Navneet Chauhan - avatar
1 Odpowiedź
+ 1
for loop condition is wrong.. Missing <= Corrected code but if you have confusion in giving input do it with single input. and Better, not to use while loop... #include <bits/stdc++.h> using namespace std; int main() { int t, count =0, n,i; cin>>t; while(t--){ cin>>n; for(i=1; i <= n/2; i++) { if(n%i==0) count= count + i; //your 2nd if has no use, I removed it. } if(count ==n) cout<<"true"<<endl; else cout<<"false"<<endl; } return 0; }
11th Aug 2020, 3:32 PM
Jayakrishna 🇮🇳