English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of C Programming Examples
In this example, you will learn to check if an integer is a prime number or an Armstrong (Armstrongnumber), or both.
To understand this example, you should understand the followingC programming languageTopic:
In this program, create two user-defined functions checkPrimeNumber() and checkArmstrongNumber().
If the number entered by the user is a prime number, the checkPrimeNumber() function returns1. Similarly, if the number entered by the user is an Armstrong number, the checkArmstrongNumber() function will also return1.
Visit these pages to find out if there are numbers
#include <math.h> #include <stdio.h> int checkPrimeNumber(int n); int checkArmstrongNumber(int n); int main() { int n, flag; printf("Enter a positive integer: "); scanf("%d", &n); //Check prime number flag = checkPrimeNumber(n); if (flag == 1) printf("%d is a prime number.\n", n); else printf("%d is not a prime number.\n", n); //Check Armstrong number flag = checkArmstrongNumber(n); if (flag == 1) printf("%d is an Armstrong number.", n); else printf("%d is not an Armstrong number.", n); return 0; } // Function to check prime number int checkPrimeNumber(int n) { int i, flag = 1, squareRoot; //Calculate the square root squareRoot = sqrt(n); for (i = 2; i <= squareRoot; ++i) { //Condition for non-prime number if (n % i == 0) { flag = 0; break; } } return flag; } // Function to check Armstrong number int checkArmstrongNumber(int num) { int originalNum, remainder, n = 0, flag; double result = 0.0; // Store the number of digits of num in n for (originalNum = num; originalNum != 0; ++n) { originalNum /= 10; } for (originalNum = num; originalNum != 0; originalNum /= 10) { remainder = originalNum % 10; // The sum of the powers of each number stored in the result result += pow(remainder, n); } //Armstrong Number Condition if (round(result) == num) flag = 1; else flag = 0; return flag; }
Output Result
Enter a positive integer: 407 407 It is not a prime number. 407 It is an Armstrong number.