✓ Link copied to clipboard
C 📅 July 7, 2026 ⏱ 5 min read

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

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

In this program, we’re gonna figure out if a number is prime or an Armstrong number using custom functions in C. For example, 153? That's an Armstrong number because the sum of its digits raised to the power of three equals the number itself: 1³ + 5³ + 3³ = 153. Let's see how we implement this logic.

Prerequisites for the C Program

Before writing this program, ensure you're familiar with these concepts:

  • Understanding of loops like for and while
  • Functions in C, including return types and arguments
  • Operators: modulus (%) and division (/)
  • <stdio.h> library for input/output
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

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

Here is the complete code for checking if a number is prime or an Armstrong number using user-defined functions.

check_prime_armstrong.cC▶ Run
#include <stdio.h>

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

int isArmstrong(int n) {
    int sum = 0, temp = n;
    while (temp != 0) {
        int digit = temp % 10;
        sum += digit * digit * digit;
        temp /= 10;
    }
    return sum == n;
}

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

    if (isPrime(n))
        printf("%d is a Prime number.\n", n);
    else if (isArmstrong(n))
        printf("%d is an Armstrong number.\n", n);
    else
        printf("%d is neither a Prime nor an Armstrong number.\n", n);

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the C Program to Check Prime or Armstrong Number

When you execute this program and enter a number, it verifies whether it's prime or Armstrong. Here's a sample run:

output.txtOutput
Enter a number: 153
153 is an Armstrong number.
Output of C Program to Check Prime or Armstrong Number Using User-defined Function
Output of C Program to Check Prime or Armstrong Number Using User-defined Function

Algorithm to Check Prime or Armstrong Number in C

Checking if a number is prime or Armstrong involves specific steps. A prime number has no divisors other than 1 and itself, while an Armstrong number means the sum of its own digits raised to the power of the number of digits equals the number. Here is the exact logic the program uses, step by step.

  1. Read an integer n from the user.
  2. Check if n is less than 2 for prime validation, return false if so.
  3. For potential divisors i from 2 to sqrt(n), check divisibility; if divisible, it's not prime.
  4. Calculate the Armstrong condition by summing the cubes of n's digits.
  5. Use separate functions for checking prime and Armstrong criteria.
  6. Output the result based on the checks.
flowchart TD
  A([Start]) --> B[/Read a number n/]
  B --> C{Is Prime(n)?}
  C -->|Yes| D[Print "Prime"]
  C -->|No| E{Is Armstrong(n)?}
  E -->|Yes| F[Print "Armstrong"]
  E -->|No| G[Print "Neither"]
  F --> H([End])
  D --> H
  G --> H
Flowchart: the algorithm step by step

How the C Program to Check Prime or Armstrong Number Works

Let's break down how each part of the code achieves the task.

  • #include <stdio.h>: This library is used for input and output operations.
  • The isPrime function checks if a number n is a prime by testing divisibility.
  • The isArmstrong function computes the sum of the cubes of n's digits and checks against n.
  • main() function: The program starts executing from here, reading input and calling the necessary functions.
  • printf and scanf are used for displaying prompts and receiving user input.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What does the Armstrong number check rely on?

🎉Correct! Each digit's cube adds up to the original number.
🤔Not quite. Think about how each digit contributes back to the total.

How Fast Is the C Program to Check Prime or Armstrong?

Understanding the program's efficiency is crucial in optimization.

Time Complexity: Both checks operate with a complexity of O(n). In the prime check, n represents the number itself. The loop for (i = 2; i * i <= n; i++) iterates potentially less than n times. For Armstrong, n is the number of digits, about log10(n) comparisons.

Space Complexity: This program uses constant space O(1). It only keeps integer variables like n, i and sum — three ints — regardless of input size.

⚠️

Common mistake:Not resetting variables or indices when reusing functions can mess up your logic.

Key takeaway:User-defined functions make checks for specific conditions simpler, leading to more modular and manageable code.

FAQs About Checking Prime or Armstrong Numbers in C

Here are some common questions students ask about this program:

An Armstrong number's where the sum of its digits raised to the power of the number of digits equals the number itself.
User-defined functions make your code modular, easier to read, and let you reuse specific logic.
Yeah, but those numbers are rare. Most Armstrong numbers aren’t prime.
It’s O(sqrt(n)), which makes checking numbers more efficient than going all the way to n.
They mostly interest math folks and are great for coding exercises to practice logic and algorithms.
Just change the power in the Armstrong function to match the number of digits of the input.

If you found this program helpful, check out these related programs:

Conclusion

Knowing how to check prime and Armstrong numbers with functions helps you write clean, reusable code. Modularity really helps; it makes debugging and enhancing easier. Try using a similar approach for other number properties and see how it helps you get better at C programming.

Keep up your learning journey with our C programming quiz and test this code in the EduSeekho online compiler.