Back to JavaScript
2026-04-146 min read

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

Learn C Program to Check Prime or Armstrong Number Using User-defined Function step by step with clear examples and exercises.

Why This Matters

In this lesson, you will learn about creating a C program that checks if a number is prime or Armstrong using user-defined functions. Understanding and mastering these skills are essential for coding interviews and building a strong foundation in C programming. Let's dive deeper into the world of C programming!

Prerequisites

To follow along with this lesson, you should be familiar with:

  1. Basic C syntax: variables, functions, loops, control structures, and data types such as integers, floats, and booleans.
  2. Understanding of user-defined functions in C.
  3. Familiarity with mathematical operations and functions like square roots and powers.
  4. A basic understanding of number theory (prime numbers and Armstrong numbers).

If you're new to these topics, we recommend checking out our C tutorials for a full guide.

Core Concept

Prime Numbers

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, and 7 are all prime numbers. A prime number checker function helps determine whether a given number is prime or not.

Armstrong Numbers

An Armstrong number is a number equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 = (1^3) + (5^3) + (3^3), so 153 is an Armstrong number. An Armstrong number checker function helps determine whether a given number is an Armstrong number or not.

In this lesson, we'll create two user-defined functions: one to check if a number is prime and another to check if it's Armstrong.

Prime Number Checker Function

int isPrime(int num) {
// Base case: 2 is the smallest prime number
if (num <= 1) return 0;

// Check divisibility from 2 to square root of the number
for (int i = 2; i <= sqrt(num); ++i) {
if (num % i == 0) return 0;
}

// If no factors found, the number is prime
return 1;
}

Armstrong Number Checker Function

int isArmstrong(int num) {
int originalNum = num;
int sum = 0;
int tempNum;

// Convert the number to a string for easier digit manipulation
char strNum[10];
sprintf(strNum, "%d", num);

// Calculate the number of digits in the number
int len = strlen(strNum);

// Reconstruct the number from its digits and check if it's Armstrong
for (int i = 0; i < len; ++i) {
tempNum = 0;
tempNum += pow(strNum[i] - '0', len);
strNum[i] = 0;

// Reconstruct the number without the current digit
sprintf(&strNum[i], "%s", &strNum[i+1]);
num = atoi(strNum);

if (num != tempNum) break;

// Move to the next digit if the current one is correct
if (i == len - 1) {
sum += tempNum;
strNum[0] = tempNum + '0';
num = originalNum;
}
}

// If the reconstructed number equals the original number, it's an Armstrong number
return (num == originalNum) ? 1 : 0;
}

Worked Example

Let's test our functions with some examples:

#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);

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

// Check if the number is Armstrong
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) {
// ... (same as before)
}

int isArmstrong(int num) {
// ... (same as before)
}

Common Mistakes

  1. Not initializing variables: Make sure to initialize all variables used in your functions, especially when dealing with user input.
  2. Incorrectly handling negative numbers: Both prime and Armstrong number checkers should only accept positive integers.
  3. Ignoring the base case for prime numbers: The smallest prime number is 2.
  4. Miscalculating the square root of a number: Use sqrt() instead of calculating it manually to avoid potential errors.
  5. Not reconstructing the original number after checking each digit in the Armstrong checker: This ensures that we don't skip any digits during the comparison process.
  6. Not properly handling edge cases: Consider testing your functions with small and large numbers, as well as with numbers having only one or two digits.
  7. Not considering zero as a non-prime number: Zero is neither prime nor Armstrong.
  8. Using inefficient algorithms: Optimize your code to make it faster by reducing the number of iterations required for checking primality and Armstrongness.
  9. Not properly handling overflow or underflow errors: Ensure that your calculations do not exceed the maximum or minimum value that can be represented by an integer data type.
  10. Not validating user input: Always validate user input to ensure it is within the expected range, such as only accepting positive integers for prime and Armstrong number checkers.

Practice Questions

  1. Modify the prime number checker function to handle negative numbers and return an error message instead of checking primality.
  2. Write a user-defined function that checks if a number is a palindrome (reads the same forwards and backwards).
  3. Extend the Armstrong number checker function to also check if a number is a super Armstrong number, which is a number equal to the sum of its own digits raised to the power of their positions.
  4. Write a user-defined function that checks if a given number is a perfect number (a positive integer that is equal to the sum of its proper divisors excluding itself).
  5. Optimize the prime number checker function to reduce the number of iterations required for checking primality.
  6. Implement a recursive version of the Armstrong number checker function.
  7. Write a user-defined function that checks if a given number is a Mersenne prime (a prime number of the form 2^n - 1, where n is an integer greater than 0).
  8. Write a user-defined function that checks if a given number is a Fibonacci number (a number in the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, ...).
  9. Implement a user-defined function that finds all prime numbers up to a given limit.
  10. Write a user-defined function that checks if a given number is a perfect square (a number that can be expressed as the square of an integer).

FAQ

  1. Why do we use user-defined functions for prime and Armstrong number checking?
  • Using user-defined functions makes our code more modular, easier to test, and simpler to reuse in other programs.
  1. What is the time complexity of the prime number checker function?
  • The time complexity of the prime number checker function is O(sqrt(n)), where n is the input number. This is because we only need to check divisors up to the square root of the number.
  1. Why do we convert the number to a string in the Armstrong number checker function?
  • Converting the number to a string allows us to easily access its individual digits for the Armstrong test.
  1. What is the time complexity of the Armstrong number checker function?
  • The time complexity of the Armstrong number checker function is O(n), where n is the number of digits in the input number. This is because we need to iterate through each digit once. However, this can be optimized by using a recursive approach with a time complexity of O(log n).
  1. Can we create a single function that checks both prime and Armstrong numbers?
  • It's possible to create a single function that checks both prime and Armstrong numbers, but it would require additional logic and potential complications in the code. Separating these functions makes the code more readable and maintainable.
C Program to Check Prime or Armstrong Number Using User-defined Function | JavaScript | XQA Learn