Perfect Number using loop
Write a C++ Program to find Perfect Number using loop. Here’s simple C++ Program to find Perfect Number using loop in C++ Programming Language.
Numbers in C++
Normally, when we work with Numbers, we use primitive data types such as int, short, long, float and double, etc. The number data types, their possible values and number ranges have been explained while discussing C++ Data Types.
Perfect number : :
A perfect numbers is a positive number that equals the sum of its divisors, excluding itself. This is also known as its aliquot sum. At this time, it is unknown how many perfect numbers truly exist in our number system.
While we have discovered 48 perfect numbers, the fact that there are an infinite number of prime numbers leads us to believe that there could be an infinite number of perfect numbers.
Here is source code of the C++ Program to find Perfect Number using loop. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.
SOURCE CODE : :
/* C++ Program to find Perfect Number using loop */ #include<iostream> using namespace std; int main() //Start of main { int i=1, u=1, sum=0; while(i<=500) { // start of first loop. while(u<=500) { //start of second loop. if(u<i) { if(i%u==0 ) sum=sum+u; } //End of if statement u++; } //End of second loop if(sum==i) { cout<<i<<" is a perfect number."<<"\n"; } i++; u=1; sum=0; } //End of First loop return 0; } //End of main
OUTPUT : :
**************** OUTPUT ***************** 6 is a perfect number. 28 is a perfect number. 496 is a perfect number.
Above is the source code for C++ Program to find Perfect Number using loop which is successfully compiled and run on Windows System.The Output of the program is shown above .
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post….
i want to grade any number of students offering any number of subjects , which variables do i use
#include <iostream>
using namespace std;
int main(){
int num, cont;
for(num=1; num<500; num++){
cont=0;
for(int i=1; i<num; i++){
if(num%i==0){
cont+=i;
}
}
if(num==cont){
cout << “\nThe number [“ << num << “] is a Perfect number \n“;
}
} cout << endl;
}