Write a C program to check whether a number is prime, armstrong, perfect number or not using functions. How to check prime or armstrong or perfect number in C programming using functions.
Example
Input
Input any number: 11
Output
11 is prime number 11 is not a armstrong number 11 is not a perfect number
Required knowledge
Basic C programming, Functions, Returning value from function
Declare function to find Prime number
Function declarations to check prime, armstrong and perfect numbers are same. Hence, I will only explain how to declare a function to check prime number.
- First give a meaningful name to our prime checking function say
isPrime()
function will check a number for prime. - Next, since our function checks a number for prime condition. Hence, it must accept a number, say
isPrime(int num);
. - Finally, the function should return a value to the caller, so that the caller can know whether the integer passed to the function is prime or not. For this we must return boolean
true
orfalse
depending on the prime check result. Therefore return an integer from function either 1 or 0.
The function declaration to check prime number is int isPrime(int num);
. Similarly you can declare functions to check armstrong and perfect numbers.
Learn –
Program to check prime, armstrong and perfect numbers using function
/**
* 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()
{
int num;
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.
* Returns 1 if the number is prime otherwise 0.
*/
int isPrime(int num)
{
int i;
for(i=2; i<=num/2; i++)
{
/*
* If the number is divisible by any number
* other than 1 and self then it is not prime
*/
if(num%i == 0)
{
return 0;
}
}
return 1;
}
/**
* Check whether a number is Armstrong number or not.
* Returns 1 if the number is Armstrong number otherwise 0.
*/
int isArmstrong(int num)
{
int lastDigit, sum, originalNum, digits;
sum = 0;
originalNum = num;
/* Find total digits in num */
digits = (int) log10(num) + 1;
/*
* Calculate sum of power of digits
*/
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.
* Returns 1 if the number is perfect otherwise 0.
*/
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
Enter any number: 11 11 is Prime number. 11 is not Armstrong number. 11 is not Perfect number.
Happy coding 😉
Recommended posts
- Function and recursion programming exercise index.
- C program to find prime numbers in given range using function.
- C program to find strong numbers in given range using function.
- C program to find armstrong numbers in given range using function.
- C program to find perfect numbers in given range using function.
- C program to check even number using function.