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

In C programming, user-defined functions provide a powerful tool for organizing and reusing code. This article will guide you through creating C Program to Check Prime or Armstrong Number Using User-defined Function. We will explore multiple variations of solutions and provide detailed explanations and outputs for each example.

Prerequisites

Before diving into the examples, make sure you have:

  • Basic understanding of C programming.
  • Knowledge of functions and loops in C.
  • A C compiler installed (e.g., GCC).

Understanding Prime and Armstrong Numbers

Prime Numbers

Prime numbers are natural numbers greater than 1 that have no positive divisors other than 1 and themselves. For example, 2, 3, 5, and 7 are prime numbers.

Armstrong Numbers

An Armstrong number (or narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because

1^3 + 5^3 + 3^3 = 153

Example 1: Check Prime Number Using User-defined Function

1.1 Explanation

This program checks if a number is prime using a user-defined function. The function iterates through possible divisors to determine primality.

1.2 Program: Check Prime Number

C
#include <stdio.h>

int isPrime(int num);

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

    if(isPrime(num))
        printf("%d is a prime number.\n", num);
    else
        printf("%d is not a prime number.\n", num);

    return 0;
}

int isPrime(int num) {
    if (num <= 1)
        return 0;
    for (int i = 2; i <= num / 2; i++) {
        if (num % i == 0)
            return 0;
    }
    return 1;
}

1.3 Output

C
Enter a number: 29
29 is a prime number.

Example 2: Check Armstrong Number Using User-defined Function

2.1 Explanation

This program checks if a number is an Armstrong number using a user-defined function. The function calculates the sum of digits raised to the power of the number of digits.

2.2 Program: Check Armstrong Number

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

int isArmstrong(int num);

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

    if(isArmstrong(num))
        printf("%d is an Armstrong number.\n", num);
    else
        printf("%d is not an Armstrong number.\n", num);

    return 0;
}

int isArmstrong(int num) {
    int originalNum, remainder, result = 0, n = 0;
    originalNum = num;

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

    originalNum = num;

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

    return (result == num);
}

2.3 Output

C
Enter a number: 153
153 is an Armstrong number.

Example 3: Combined Program to Check Both Prime and Armstrong Numbers

3.1 Explanation

This program combines the functionality to check both prime and Armstrong numbers using user-defined functions.

3.2 Program: Check Both Prime and Armstrong Numbers

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

int isPrime(int num);
int isArmstrong(int num);

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

    if(isPrime(num))
        printf("%d is a prime number.\n", num);
    else
        printf("%d is not a prime number.\n", num);

    if(isArmstrong(num))
        printf("%d is an Armstrong number.\n", num);
    else
        printf("%d is not an Armstrong number.\n", num);

    return 0;
}

int isPrime(int num) {
    if (num <= 1)
        return 0;
    for (int i = 2; i <= num / 2; i++) {
        if (num % i == 0)
            return 0;
    }
    return 1;
}

int isArmstrong(int num) {
    int originalNum, remainder, result = 0, n = 0;
    originalNum = num;

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

    originalNum = num;

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

    return (result == num);
}

3.3 Output

C
Enter a number: 153
153 is not a prime number.
153 is an Armstrong number.

Conclusion

In this article, we explored how to check if a number is a prime or an Armstrong number using user-defined functions in C. We covered three different variations:

  1. A simple prime number checker.
  2. An Armstrong number checker.
  3. A combined program to check both prime and Armstrong numbers.

Each example provided a unique approach to solving the problem, showcasing the power and flexibility of user-defined functions in C programming. By understanding and implementing these examples, you will be well-equipped to handle similar tasks in your future C programming projects.