Write a program in C language to Check a given number is Prime and Armstrong Number.

0
18

In this program, two user-defined functions checkPrimeNumber() and checkArmstrongNumber() are created.

#include <stdio.h>
    #include <math.h>
    int checkPrimeNumber(int n)
    {
        int i, flag = 1;
        for(i=2; i<=n/2; ++i)
        {
        
            if(n%i == 0) // condition for non-prime number
            {
                flag = 0;
                break;
            }
        }
        return flag;
    }
    int checkArmstrongNumber(int n)
    {
        int originalNumber, remainder, result = 0, n = 0, flag;
        originalNumber = number;
        while (originalNumber != 0)
        {
            originalNumber /= 10;
            ++n;
        }
        originalNumber = number;
        while (originalNumber != 0)
        {
            remainder = originalNumber%10;
            result += pow(remainder, n);
            originalNumber /= 10;
        }
        
        if(result == number)  // condition for Armstrong number
            flag = 1;
        else
            flag = 0;
        return flag;
    }
    int main()
    {
        int n, flag;
        printf("Enter a positive integer: ");
        scanf("%d", &n);
        
        flag = checkPrimeNumber(n); // Check prime number
        if (flag == 1)
            printf("%d is a prime number.\n", n);
        else
            printf("%d is not a prime number.\n", n);
        
        flag = checkArmstrongNumber(n);  // Check Armstrong number
        if (flag == 1)
            printf("%d is an Armstrong number.", n);
        else
            printf("%d is not an Armstrong number.",n);
        return 0;
    }
Output:

Enter a positive integer: 407
407 is not a prime number.
407 is an Armstrong number.

LEAVE A REPLY

Please enter your comment!
Please enter your name here