Page Contents
Interview Content
- C Programming Coding Questions
- C Pattern Programming Questions
- C Programming Interview Questions
- Java Programming Coding Questions
- Java Pattern Programming Questions
- Java Programming Interview Questions
- Python Programming Coding Questions
- Python Pattern Programming Questions
- Python Programming Interview Questions
- SQL Interview Questions
Program to check given number is Perfect number in C
Ans:
In this tutorial you will learn how to write a program in C programming language to check a given number is perfect or not.
This program is little bit tricky and logical.
But Before directly moving on writing and learning the program first you should know
What is Perfect Number?
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.
Lets understand it with example
6 is a positive number and its divisor is 1,2,3 and 6 itself.
But we should not include 6 as by the definition of perfect number.
Lets add its divisor excluding itself
1+2+3 = 6 which is equal to number itself.
It means 6 is a Perfect Number.
How our program will behave?
C program for perfect number is written below. This program will take a positive number as an input.
Suppose you want to check that a number is perfect or not then you have to give that number as an input to this below program at time of execution.
After the calculation it will give you appropriate output.
If number is perfect then output will be number is perfect. And if number is not perfect then output will be number is not perfect.
Below is a program to check a given number is perfect or not in C
#include<stdio.h>
#include<conio.h>
void main() {
int reminder, sum=0, i, originalNum;
printf("please enter number: ");
scanf("%d",&originalNum);
for (i = 1; i <= originalNum/2; i++)
{
reminder = originalNum % i;
if (reminder == 0)
{
sum = sum + i;
}
}
if (sum == originalNum)
printf("given no. is perfect number");
else
printf("given no. is not a perfect number");
getch();
}
Output:
Explanation of the above program
- In the above program there are 4 variables. originalNum, sum, i, reminder, variable is to store the value given as an input.
- Main login is written in the body of for loop.
- Loop will execute half times of the given input number. As a number can only divide itself by half of its number and itself.
- Now inside for loop there is a if statement. And body of if statement is adding the number which is divisor of a given input.
- originalNum % i is a logic to check the divisor of a number. if originalNum % i return 0 it means that i can divide the given number so we can consider it as it is a divisor of given number.
- And we will add all divisor to get sum of divisor.
- At last we will check using if statement that if sum of divisor is equal to its original number or not. If equal it means that the given number is Perfect Number otherwise it is not a perfect number
I hope above tutorial has helped you to understand program of perfect number using C.
Also Prepare C interview programs given below
C Questions on number
- Write a program to reverse an integer in C.
- Write a program in C to check whether an integer is Armstrong number or not.
- Write a program in C to print the fibonacci series using recursive method.
- Write a program in C to check whether a number is palindrome or not using recursive method.
- Write a program in C to add two integer without using arithmetic + operator.
C Questions on String
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program