In this tutorial you will learn about the C program to Check Number is a Prime, Armstrong, or Perfect Number and its application with practical example.
C Program to Check Number is a Prime, Armstrong, or Perfect Number
In this tutorial, we will learn to create a C program that will check Number is a Prime, Armstrong, or Perfect Number in C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- While loop in C programming.
What is a prime number?
The prime numbers are those numbers that are only divisible by themselves. These numbers can’t be complete, divide by the 2 & 3.
What is an Armstrong number?
Armstrong number is a number that is equal to the sum of cubes of its digits.
1 2 |
For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. |
What is a Perfect number?
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.
1 2 3 4 5 6 7 |
For Example:- 6 has divisors 1, 2 and 3 (excluding itself). And 1 + 2 + 3 = 6. So 6 is a perfect number. |
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declare the variables for the program. 2. Taking the input number from the user to check. 3. Checking if the number is the Prime, Armstrong, or Perfect Number. 4. Print the Result. 5. End the program. |
Program For checking the Number is a Prime, Armstrong, or Perfect Number:-
With the help of this program, we can check THE Number.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
/* C program to check prime, armstrong and perfect numbers using functions */ #include <stdio.h> #include <math.h> /* Function declarations */ int isPrime(int num); int isArmstrong(int num); int isPerfect(int num); int main() { //declaring the required variables for the function. int num; //taking input from the user printf("Enter any number: "); scanf("%d", &num); // Call isPrime() functions if(isPrime(num)) { printf("%d is Prime number.\n", num); } else { printf("%d is not Prime number.\n", num); } // Call isArmstrong() function if(isArmstrong(num)) { printf("%d is Armstrong number.\n", num); } else { printf("%d is not Armstrong number.\n", num); } // Call isPerfect() function if(isPerfect(num)) { printf("%d is Perfect number.\n", num); } else { printf("%d is not Perfect number.\n", num); } return 0; } /* Check whether a number is prime or not. */ int isPrime(int num) { int i; for(i=2; i<=num/2; i++) { if(num%i == 0) { return 0; } } return 1; } /* Check whether a number is Armstrong number or not. */ int isArmstrong(int num) { int lastDigit, sum, originalNum, digits; sum = 0; originalNum = num; /* Find total digits in num */ digits = (int) log10(num) + 1; while(num > 0) { // Extract the last digit lastDigit = num % 10; // Compute sum of power of last digit sum = sum + round(pow(lastDigit, digits)); // Remove the last digit num = num / 10; } return (originalNum == sum); } /* Check whether the number is perfect number or not. */ int isPerfect(int num) { int i, sum, n; sum = 0; n = num; for(i=1; i<n; i++) { /* If i is a divisor of num */ if(n%i == 0) { sum += i; } } return (num == sum); } |
Output:-
In the above program, we have first initialized the required variable.
- num = it will hold the integer value.
- i = it will hold the integer value.
Taking the input integer number from the user.