Check Prime or Armstrong Number Using User-defined Function in C

Exercise:

Write a C Program to check prime or Armstrong number using user-defined function.

Click Here to View the Solution!
#include <math.h>
#include <stdio.h>
int checkPrimeNumber(int num);
int checkArmstrongNumber(int num);

int main() {
    int num, flag;
    printf("Enter a positive integer: ");
    scanf("%d", &num);

    // check for prime number
    flag = checkPrimeNumber(num);
    if (flag == 1)
        printf("%d is a prime number.\n", num);
    else
        printf("%d is not a prime number.\n", num);

    // check for Armstrong number
    flag = checkArmstrongNumber(num);
    if (flag == 1)
        printf("%d is an Armstrong number.", num);
    else
        printf("%d is not an Armstrong number.", num);
    return 0;
}

// function to check prime number
int checkPrimeNumber(int num) {
    int i, flag = 1, squareRoot;
    // computing the square root
    squareRoot = sqrt(num);
    for (i = 2; i <= squareRoot; ++i) {
        // condition for non-prime number
        if (num % i == 0) {
            flag = 0;
            break;
        }
    }
    return flag;
}

// function to check Armstrong number
int checkArmstrongNumber(int num) {
   int originalNum, remainder, n = 0, flag;
   double result = 0.0;

   // store the number of digits of num in n
   for (originalNum = num; originalNum != 0; ++n) {
       originalNum /= 10;
   }
   for (originalNum = num; originalNum != 0; originalNum /= 10) {
       remainder = originalNum % 10;
       result += pow(remainder, n);
   }

   // Condition for Armstrong number
   if (round(result) == num)
       flag = 1;
   else
       flag = 0;
   return flag;
}
Click Here to View the Output!
Enter a positive integer: 6                                   
6 is not a prime number.                                      
6 is an Armstrong number.
Click Here to View the Explanation!
  • This program is used to determine whether a number is a prime number or an Armstrong number using a user-defined function.
  • Two integer functions checkPrimeNumber and checkArmstrongNumber are declared, each taking an integer num as a parameter.
  • In main(), the program requests the user to enter a positive integer and stores it in the variable num using the scanf() function.
  • num will now be checked whether it is a prime number by assigning the checkPrimeNumber function to the variable flag. If flag will be equal to 1, the number will be prime else not.
  • Th program will then check whether the number is an Armstrong number by assigning the function checkArmstrongNumber to the variable flag. If flag will be equal to 1, the number will be Armstrong else not.
  • The functions checkPrimeNumber and checkArmstrongNumber will be initialized.
  • In checkPrimeNumber, the square root of num will be used which will be used for setting the number of iterations the for loop will execute. In each iteration, it will be checked whether n is a non-prime number, if yes, flag = 1.
  • In checkArmstrongNumber, the number of digits of num will be stored in num  and the sum of the nth power of individuals digits is stored in the variable result and finally, both the result and num are compared to check whether number is an Armstrong. If true, flag = 1.

%d bloggers like this: