Write a C++ Program to check whether a number is a perfect number or not
NOTE: E&OE, means errors and
omissions are expected and accepted, in other words,
if there is any error in the following article, the same has to be corrected by
the user or the beneficiary accordingly.
This article contains works derived from other reputed works of huge publication houses and notes
that were made by me in my school life.These articles are just to help people
who need these articles desperately and urgently. Any queries, complaints, and
corrections will be enthusiastically entertained.
Write a C++ Program to check whether a number is a perfect number or not
Checking whether a number is a perfect number or not in C++ Programming Language
Code Sample 1 (for Dev C++ & CodeBlocks)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include<iostream> using namespace std; int main() { int n, i=1, s=0; cout<<"Enter a number you want to check:-\t"; cin>> n; while (i<n) { if (n%i ==0) s=s+i; i++; } if (s==n) cout<<"\n\nIt is a prefect number!!"; else cout<<"\n\nIt is not a perfect number!!"; return(0); } |
Code Sample 2 (for Turbo C++)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include<iostream.h> #include<conio.h> void main() { clrscr(); int n, i=1, s=0; cout<<"Enter a number you want to check:-\t"; cin>> n; while (i<n) { if (n%i ==0) s=s+i; i++; } if (s==n) cout<<"\n\nIt is a prefect number!!"; else cout<<"\n\nIt is not a perfect number!!"; getch(); } |
Sample
Output:
Programs on control structures and string
manipulations under C++ Programming
language are covered here.
___________________________________________________________________
Know more about stuff related to this practical
experiment (Important for Boards):-
- Difference between Dev C++ and Turbo C++.
- C++ Program to find the Product of the Matrices | C++ Program | Class XI
- C++ Program to find the Sum of the Series, 1– x/2! + x^2/3! – x^3/4! + x^4/5! .... | C++ Program | Class XI
- C++ Program to find sum of each row and each column of a Matrix | C++ Program | Class XI
- Qualitative analysis for the identification of the functional group in the organic sample Acetic Acid (Example 1)
- How to find out the mass of salt required to achieve the required concentration of a solution
Reference:-
- Arora, Sumita. Computer Science with C++ for Class - 12
- Arora, Sumita. Computer Science with C++ for Class - 11
- C programming Interview questions and answers [http://www.cquestions.com/]
- The Crazy Programmer [https://www.thecrazyprogrammer.com/]
For latest syllabus, both theory and practical papers
for class XII, check out the CBSE’s official site http://cbseacademic.in/ for more info.
For more awesome coding and programs related to this language log onto http://www.thecrazyprogrammer.com or https://www.w3schools.in/.
___________________________________________________________________
Related Questions-
Loop Based program code
Loops for while
Array manipulations
Header files
Dev c++
Turbo C++
CodeBlocks
CBSE class 12 computer science practicals
CBSE class XII computer science practicals
Benzoic Acid