In this program, two user-defined functions checkPrimeNumber() and checkArmstrongNumber() are created.
The checkPrimeNumber() returns 1 if the number entered by the user is a prime number. Similarly, checkArmstrongNumber() returns 1 if the number entered by the user is an Armstrong number.
#include < stdio.h >
#include < math.h >
int checkPrimeNumber(int n);
int checkArmstrongNumber(int n);
int main()
{
int m, flag;
printf("Enter a positive integer: ");
scanf("%d", &m);
// Check prime number
flag = checkPrimeNumber(m);
if (flag == 1)
printf("%d is a prime number.\n", m);
else
printf("%d is not a prime number.\n", m);
// Check Armstrong number
flag = checkArmstrongNumber(m);
if (flag == 1)
printf("%d is an Armstrong number.", m);
else
printf("%d is not an Armstrong number.",m);
return 0;
}
int checkPrimeNumber(int m)
{
int i, flag = 1;
for(i=2; i < =m/2; ++i)
{
// condition for non-prime number
if(m%i == 0)
{
flag = 0;
break;
}
}
return flag;
}
int checkArmstrongNumber(int number)
{
int originalNumber, remainder, result = 0, m = 0, flag;
originalNumber = number;
while (originalNumber != 0)
{
originalNumber /= 10;
++m;
}
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += pow(remainder, m);
originalNumber /= 10;
}
// condition for Armstrong number
if(result == number)
flag = 1;
else
flag = 0;
return flag;
}
Output
Enter a positive integer: 407
407 is not a prime number.
407 is an Armstrong number.
The checkPrimeNumber() returns 1 if the number entered by the user is a prime number. Similarly, checkArmstrongNumber() returns 1 if the number entered by the user is an Armstrong number.
#include < stdio.h >
#include < math.h >
int checkPrimeNumber(int n);
int checkArmstrongNumber(int n);
int main()
{
int m, flag;
printf("Enter a positive integer: ");
scanf("%d", &m);
// Check prime number
flag = checkPrimeNumber(m);
if (flag == 1)
printf("%d is a prime number.\n", m);
else
printf("%d is not a prime number.\n", m);
// Check Armstrong number
flag = checkArmstrongNumber(m);
if (flag == 1)
printf("%d is an Armstrong number.", m);
else
printf("%d is not an Armstrong number.",m);
return 0;
}
int checkPrimeNumber(int m)
{
int i, flag = 1;
for(i=2; i < =m/2; ++i)
{
// condition for non-prime number
if(m%i == 0)
{
flag = 0;
break;
}
}
return flag;
}
int checkArmstrongNumber(int number)
{
int originalNumber, remainder, result = 0, m = 0, flag;
originalNumber = number;
while (originalNumber != 0)
{
originalNumber /= 10;
++m;
}
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += pow(remainder, m);
originalNumber /= 10;
}
// condition for Armstrong number
if(result == number)
flag = 1;
else
flag = 0;
return flag;
}
Output
Enter a positive integer: 407
407 is not a prime number.
407 is an Armstrong number.
No comments