c
  1. c-program-to-check-prime-or-armstrong-number-using-user-defined-function

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

Example

#include <stdio.h>
#include <math.h>

/* Function declarations */
int checkPrimeNumber(int n);
int checkArmstrongNumber(int n);

int main()
{
    int n, flag_prime, flag_armstrong;
    
    printf("Enter a positive integer: ");
    scanf("%d", &n);

    /* Check prime number */
    flag_prime = checkPrimeNumber(n);
    if (flag_prime)
        printf("%d is a prime number.\n", n);
    else
        printf("%d is not a prime number.\n", n);

    /* Check Armstrong number */
    flag_armstrong = checkArmstrongNumber(n);
    if (flag_armstrong)
        printf("%d is an Armstrong number.\n", n);
    else
        printf("%d is not an Armstrong number.\n", n);

    return 0;
}

/* Function to check prime number */
int checkPrimeNumber(int n)
{
    int i, flag = 1;

    for (i = 2; i <= n/2; ++i)
    {
        if (n%i == 0)
        {
            flag = 0;
            break;
        }
    }

    return flag;
}

/* Function to check Armstrong number */
int checkArmstrongNumber(int n)
{
    int originalNumber, remainder, result = 0, nDigits = 0;

    originalNumber = n;
    
    while (originalNumber != 0)
    {
        originalNumber /= 10;
        ++nDigits;
    }

    originalNumber = n;

    while (originalNumber != 0)
    {
        remainder = originalNumber%10;
        result += pow(remainder, nDigits);
        originalNumber /= 10;
    }

    if (result == n)
        return 1;
    else
        return 0;
}

Output

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

Explanation

This program takes a positive integer from the user and checks whether it is a prime number and/or an Armstrong number using user-defined functions.

First, the program takes the input integer and passes it to the checkPrimeNumber() function to check if it is a prime number. The checkPrimeNumber() function checks for divisibility from 2 to n/2 and sets a flag variable to 1 if it is a prime number and 0 if it is not.

Next, the program passes the input integer to the checkArmstrongNumber() function to check if it is an Armstrong number. The checkArmstrongNumber() function calculates the number of digits in the input integer and then calculates the sum of each digit raised to the power of the number of digits. If this sum is equal to the input integer, the function sets a flag variable to 1, indicating that the number is an Armstrong number, and 0 otherwise.

Finally, the program prints the output to the console to indicate whether the input integer is a prime number, an Armstrong number, or neither.

Use

This program can be used to check if a given positive integer is a prime number and/or an Armstrong number. It can be modified to include additional number properties as well.

Summary

This program demonstrates how to check if a given positive integer is a prime number and/or an Armstrong number using user-defined functions. It takes an input from the user, passes it to the two functions, and prints the output to the console. The program can be used as a template for checking other number properties as well.

Published on: