C Program to Check Number is Perfect Square, Armstrong or Prime using For Loop
Write a Program to Check Number is Perfect Square, Armstrong or Prime using For Loop
// C Program to Check Number is Perfect Square, Armstrong or Prime using For Loop #include <stdio.h> #include <math.h> int isPerfectSquare(int n) { if (pow((int)sqrt(n), 2) == n) return 1; return 0; } int isArmstrongNumber(int n) { int digits = ceil(log10(n)); int temp = 0, temp2 = n, rem; for(; n > 0; ) { rem = n % 10; temp += pow(rem, digits); n /= 10; } if (temp2 == temp) return 1; return 0; } int isPrime(int n) { int i; for ( i = 2; i < n; i++) { if (n % i == 0) { return 0; } } return 1; } int main(int argc, char *argv[]) { int n, flag = 0; printf("Enter the number: "); scanf("%d", &n); if (isPerfectSquare(n)) { flag=1; printf("It is a Perfect Square\n"); } if (isArmstrongNumber(n)) { flag=1; printf("it is an Armstrong number\n"); } if (isPrime(n)) { flag=1; printf("it is a Prime number\n"); } if(flag == 0) { printf("It's not prime , Armstrong or Perfect Square"); } return 0; }
Output:
Run 1:
Enter the number: 36
It is a perfect square
Run 2:
Enter the number: 89
it is a prime number