C program to check prime, armstrong, perfect number using functions

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

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

Must know -

Declare function to find Prime number

Function declarations for checking prime, armstrong and perfect numbers are same. Hence, I will only explain how to declare a function to check prime number.

  1. First let us give a meaningful name to our prime checking function. Say isPrime() function will check a number for prime.
  2. Next, since our function checks a number for prime condition. Hence, it must accept a number from user. So pass an integer to the function, say isPrime(int num);.
  3. Finally, the function should be able to 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 or false depending on the prime check result. Therefore return an integer from function either 1 or 0.

The function declaration to check prime number must be something like - int isPrime(int num);. Similarly you can declare functions to check armstrong and perfect numbers.

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);
    
    // 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, 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);
}



/**
 * 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 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>