Write a C program to check whether a number is prime, armstrong, perfect number or not using the concept of functions. How to check whether a number is prime number or armstrong number or perfect number in C programming using functions.
Example:
Input any number: 11
Output: 11 is prime number
11 is not a armstrong number
11 is not a perfect number
As I have already explained in my earlier posts about what is prime, armstrong and perfect numbers and how to check them in C programming. If you haven't gone through it is highly recommended to go through all three posts before continuing to this as this program is fully based on my previous posts. C program to check prime number, C program to check armstrong number, C program to check perfect number.
Now when you are done with above posts what we need to do is embed the logic of these programs one by one in functions.
Happy coding ;)
Example:
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, Prime numbers, Armstrong numbers, Perfect numbersAs I have already explained in my earlier posts about what is prime, armstrong and perfect numbers and how to check them in C programming. If you haven't gone through it is highly recommended to go through all three posts before continuing to this as this program is fully based on my previous posts. C program to check prime number, C program to check armstrong number, C program to check perfect number.
Now when you are done with above posts what we need to do is embed the logic of these programs one by one in functions.
Program to check prime, armstrong and perfect numbers
/**
* C program to check prime, armstrong and perfect numbers using functions
*/
#include <stdio.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);
//Calls the isPrime() functions
if(isPrime(num))
{
printf("%d is Prime number.\n", num);
}
else
{
printf("%d is not Prime number.\n", num);
}
//Calls the isArmstrong() function
if(isArmstrong(num))
{
printf("%d is Armstrong number.\n", num);
}
else
{
printf("%d is not Armstrong number.\n", num);
}
//Calls the isPerfect() function
if(isPerfect(num))
{
printf("%d is Perfect number.\n", num);
}
else
{
printf("%d is not Perfect number.\n", num);
}
return 0;
}
/**
* Checks whether a number is prime or not. Returns 1 if the number is prime
* otherwise returns 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;
}
/**
* Checks whether a number is Armstrong number or not. Returns 1 if the number
* is Armstrong number otherwise returns 0.
*/
int isArmstrong(int num)
{
int lastDigit, sum, n;
sum = 0;
n = num;
while(n!=0)
{
/* Finds last digit of number */
lastDigit = n % 10;
/* Finds cube of last digit and adds to sum */
sum += lastDigit * lastDigit * lastDigit;
n = n/10;
}
return (num == sum);
}
/**
* Checks whether the number is perfect number or not. Returns 1 if the number
* is perfect otherwise returns 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.
11 is Prime number.
11 is not Armstrong number.
11 is not Perfect number.
Happy coding ;)