Q:

C Program to Check Prime or Armstrong Number Using Function

0

Prime Number is a whole number which is greater than 1 and has only two factors – 1 and itself.

For example: 2, 3, 5, 7, . . . and so on.

Similarly, an Armstrong Number is a number that is equal to the sum of cubes of its digits.

For example: 153, 370, 371, . . . and so on.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>

int prime(int n);
int armstrong(int n);

int main()
{
    char c;
    int n,temp=0;
    printf("Eneter a positive integer: ");
    scanf("%d",&n);
    printf("Enter P to check prime and  A to check Armstrong number: ");
    c=getche();
    if (c=='p' || c=='P')
    {
        temp=prime(n);
        if(temp==1)
           printf("\n%d is a prime number.", n);
        else
           printf("\n%d is not a prime number.", n);
    }
    if (c=='a' || c=='A')
    {
        temp=armstrong(n);
        if(temp==1)
           printf("\n%d is an Armstrong number.", n);
        else
           printf("\n%d is not an Armstrong number.",n);
    }
    return 0;
}

int prime(int n)
{
    int i, flag=1;
    for(i=2; i<=n/2; ++i)
    {
       if(n%i==0)
       {
          flag=0;
          break;
       }
    }
    return flag;
}

int armstrong(int n)
{
    int num=0, temp, flag=0;
    temp=n;
    while(n!=0)
    {
        num+=(n%10)*(n%10)*(n%10);
        n/=10;
    }
    if (num==temp)
       flag=1;
    return flag;
}

 

Output:

Enter a Positive integer :371

Enter P to check prime and A to check Armstrong number:a

371 is an Armstrong  number

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now