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
forandwhile - 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.
#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.
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:
Enter a number: 153
153 is an Armstrong number.

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.
- Read an integer
nfrom the user. - Check if
nis less than 2 for prime validation, return false if so. - For potential divisors
ifrom 2 tosqrt(n), check divisibility; if divisible, it's not prime. - Calculate the Armstrong condition by summing the cubes of
n's digits. - Use separate functions for checking prime and Armstrong criteria.
- 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 --> HHow 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
isPrimefunction checks if a numbernis a prime by testing divisibility. - The
isArmstrongfunction computes the sum of the cubes ofn's digits and checks againstn. main()function: The program starts executing from here, reading input and calling the necessary functions.printfandscanfare used for displaying prompts and receiving user input.
Quick Quiz — test your understanding
Tap an option to check your answerWhat does the Armstrong number check rely on?
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:
O(sqrt(n)), which makes checking numbers more efficient than going all the way to n.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Display Armstrong Number Between Two Intervals
- C Program to Display Factors of a Number
- C Program to Check Whether a Number is Prime or Not
- C Program to Display Prime Numbers Between Two Intervals
- C Program to Check if a Number is Sum of Two Primes
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.