C Program to Check Prime or Armstrong Number Using User-defined Function

In this post, we will learn how to check Prime or Armstrong Number Using User-defined Function in C Programming language.

C Program to Check Prime or Armstrong Number Using User-defined Function

As we know, a Prime Number is a whole number which is greater than 1 and has only two factors – 1 and itself. For example: 2, 3, 5, 7, . . . and so on.

Similarly, an Armstrong Number is a number that is equal to the sum of cubes of its digits. For example: 153, 370, 371, . . . and so on.

We will be checking whether the entered number is prime or armstrong in this program.

So, without further ado, let’s begin this tutorial.

C Program to Check Prime or Armstrong Number Using User-defined Function

// C Program to Check Prime or Armstrong Number Using User-defined Function
#include <stdio.h>
#include <math.h>

int checkPrime(int num);
int checkArmstrong(int num);

int main(){
    int num;
    
    // Asking for Input
    printf("Enter an integer: ");
    scanf("%d", &num);
    
    // Check Prime Number
    if (checkPrime(num) == 0){
        printf("%d is a Prime Number.\n", num);
    }
    else{
        printf("%d is not a Prime Number.\n", num);
    }
    
    // Check Armstrong Number
    if (checkArmstrong(num) == 0){
        printf("%d is an Armstrong Number.\n", num);
    }
    else{
        printf("%d is not an Armstrong Number.\n", num);
    }
    return 0;
}

// Function To Check Prime Number
int checkPrime(int num){
   int i, count = 0;
   for (i = 2; i <= num/2; i++){
       if (num % i == 0){
           count = 1;
           break;
       }
   }
   if (num == 1)
        count = 1;
    return count;    
}

// Function To Check Armstrong Number
int checkArmstrong(int num){
    int lastdigit = 0, power = 0, sum = 0;
    
    int n = num;
    while (n != 0){
        lastdigit = n % 10;
        power = lastdigit * lastdigit * lastdigit;
        sum = sum + power;
        n = n / 10;
    }
    if (sum == num)
        return 0;
    else
        return 1;
}

Output 1

Enter an integer: 101
101 is a Prime Number.
101 is not an Armstrong Number.

Output 2

Enter an integer: 371
371 is not a Prime Number.
371 is an Armstrong Number.

How Does This Program Work ?

    int num;

In this program, we have declared an int data type variable named as num.

    // Asking for Input
    printf("Enter an integer: ");
    scanf("%d", &num);

Then, the user is asked to enter an integer. The value of this integer will get stored in the num named variable.

    // Check Prime Number
    if (checkPrime(num) == 0){
        printf("%d is a Prime Number.\n", num);
    }
    else{
        printf("%d is not a Prime Number.\n", num);
    }

Then, we check whether the number is Prime or not.

// Function To Check Prime Number
int checkPrime(int num){
   int i, count = 0;
   for (i = 2; i <= num/2; i++){
       if (num % i == 0){
           count = 1;
           break;
       }
   }
   if (num == 1)
        count = 1;
    return count;    
}

For this, we have declared a custom user-defined function named checkPrime which will return 0 if the number is prime.

    // Check Armstrong Number
    if (checkArmstrong(num) == 0){
        printf("%d is an Armstrong Number.\n", num);
    }
    else{
        printf("%d is not an Armstrong Number.\n", num);
    }

Similarly, we check whether the entered number is Armstrong or not.

// Function To Check Armstrong Number
int checkArmstrong(int num){
    int lastdigit = 0, power = 0, sum = 0;
    
    int n = num;
    while (n != 0){
        lastdigit = n % 10;
        power = lastdigit * lastdigit * lastdigit;
        sum = sum + power;
        n = n / 10;
    }
    if (sum == num)
        return 0;
    else
        return 1;
}

For this also, we have declared a custom function named checkArmstrong which will return 0, if the number is an Armstrong Number.

Conclusion

I hope after going through this post, you understand how to check Prime or Armstrong Number Using User-defined function in C Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *