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

Created with Sketch.

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

Introduction

In this blog post, we’ll explore a C program that checks whether a given number is prime or an Armstrong number using user-defined functions. The program will take an integer as input, determine if it is prime or Armstrong, and display the result. We’ll discuss the algorithm, provide a step-by-step explanation, present the C code, and include examples with outputs.

Understanding the Algorithm

The algorithm for checking whether a number is prime or Armstrong involves the following steps:

  1. Input Number: Take an integer as input.

  2. Prime Check Function: Create a function to check if the number is prime.

  3. Armstrong Check Function: Create a function to check if the number is Armstrong.

  4. Output Result: Display whether the number is prime, Armstrong, or neither.

C Program to Check Prime or Armstrong Number

Let’s implement the algorithm in a C program:

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

// Function to check if a number is prime
int isPrime(int num) {
    if (num <= 1) {
        return 0;  // Not a prime number
    }
    for (int i = 2; i <= sqrt(num); ++i) {
        if (num % i == 0) {
            return 0;  // Not a prime number
        }
    }
    return 1;  // Prime number
}

// Function to check if a number is Armstrong
int isArmstrong(int num) {
    int originalNum = num;
    int sum = 0;
    int count = 0;

    // Count the number of digits
    while (originalNum != 0) {
        originalNum /= 10;
        ++count;
    }

    originalNum = num;

    // Calculate the sum of nth power of each digit
    while (originalNum != 0) {
        int remainder = originalNum % 10;
        sum += pow(remainder, count);
        originalNum /= 10;
    }

    return (sum == num);  // Armstrong if sum is equal to the original number
}

int main() {
    int inputNumber;

    // Example Usage
    printf("Enter an integer: ");
    scanf("%d", &inputNumber);

    // Check if the number is prime or Armstrong
    if (isPrime(inputNumber)) {
        printf("%d is a Prime Number.\n", inputNumber);
    } else {
        printf("%d is not a Prime Number.\n", inputNumber);
    }

    if (isArmstrong(inputNumber)) {
        printf("%d is an Armstrong Number.\n", inputNumber);
    } else {
        printf("%d is not an Armstrong Number.\n", inputNumber);
    }

    return 0;
}

Output Example

Example: Check Prime or Armstrong for the Number 153

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

Explanation

The program defines two functions, isPrime and isArmstrong, to check if a number is prime or Armstrong, respectively. The main function takes an integer as input, uses these functions to check for prime and Armstrong conditions, and prints the results.

Conclusion

This C program demonstrates the use of user-defined functions to check whether a given number is prime or Armstrong. It provides a versatile template that you can modify for different input numbers. Feel free to experiment with other numbers to see the program’s output.

Understanding prime and Armstrong numbers and writing programs to check for these properties is a valuable exercise in programming and number theory. If you have questions or want to explore more C programming topics, feel free to ask!

Leave a Reply

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