C Program to Check for Prime Armstrong or Perfect Number

In this tutorial, i am going to show you how to check whether a number is prime, Armstrong, or perfect number in c program with the help of functions and without function.

All C Programs to Check for Prime Armstrong or Perfect Number

  • C Program to Check for Prime Armstrong or Perfect Number
  • C Program to Check for Prime Armstrong or Perfect Number using function

C Program to Check for Prime Armstrong or Perfect Number

/* C Program to Check for Prime Armstrong or Perfect Number */
 
#include <stdio.h>
#include <math.h>
 
int Check_Armstrong (int Number);
int Perfect_Number(int Number); 
int Prime_Number(int Number); 
 
int main()
{
	int Number;
 
  	printf("\Please Enter Number :- ");
  	scanf("%d", &Number);
  	
 	// Calling Check_Armstrong Function to Check whether it is an Armstrong Number or Not
 	if (Check_Armstrong(Number))
    	printf("\n %d is an Armstrong Number.", Number);
  	else
    	printf("\n %d is not an Armstrong Number.", Number);
    	
     // Calling Prime_Number Function to Check whether it is a Prime Number or Not
	if(Prime_Number(Number) )
   		printf("\n %d is a Prime Number", Number);
   	else
   		printf("\n %d is Not a Prime Number", Number);
   		
	// Calling Perfect_Number Function to Check whether it is a Perfect Number or Not
	if (Perfect_Number(Number) ) 
    	printf("\n %d is a Perfect Number", Number) ;   
 	else   
    	printf("\n %d is not a Perfect Number", Number) ;   
 
  return 0;
}
/* C Program for Armstrong Number */
int Check_Armstrong (int Number)
{
	int Temp, Reminder, Times = 0, Sum = 0;
	Temp = Number;
	
	while (Temp != 0) 
	{
		Times = Times + 1;
     	Temp = Temp / 10;
 	}
  
  	for(Temp = Number; Temp > 0; Temp =  Temp /10 )
   	{
    	Reminder = Temp % 10;
    	Sum = Sum + pow(Reminder, Times);
   	}
  	if ( Number == Sum )
  		return 1;
  	else
	 	return 0; 	
}
/* C Program for Perfect Number */
int Perfect_Number(int Number)   
{
	int i, Sum = 0 ;   
 
 	for(i = 1 ; i < Number ; i++)   
  	{
  		if(Number % i == 0)
		    Sum = Sum + i ;   
  	}    
 
 	if (Sum == Number) 
    	return 1;   
 	else   
    	return 0;   
}
/* C Program to Find Prime Number  */
int Prime_Number(int Number)
{
	int i, Count = 0;
	
	for (i = 2; i <= Number/2; i++)
   	{
    	if(Number%i == 0)
     	{
       		Count++;
     	} 
    }
   	if(Count == 0 && Number != 1 )
   		return 1;
   	else
   		return 0;
}

The result of the above c program; as follows:

Please Enter Number :- 2
2 is an Armstrong Number.
 2 is a Prime Number
 2 is not a Perfect Number

C Program to Check for Prime Armstrong or Perfect Number using Function

/**
 * C program to check prime, armstrong and perfect numbers using functions
 */
#include <stdio.h>
#include <math.h>
/* Function declarations */
int isPrime(int num);
int isArmstrong(int num);
int isPerfect(int num);
int main()
{
    int num;
    
    printf("Enter any number: ");
    scanf("%d", &num);
    
    // Call isPrime() functions
    if(isPrime(num))
    {
        printf("%d is Prime number.\n", num);
    }
    else
    {
        printf("%d is not Prime number.\n", num);
    }
    
    // Call isArmstrong() function
    if(isArmstrong(num))
    {
        printf("%d is Armstrong number.\n", num);
    }
    else
    {
        printf("%d is not Armstrong number.\n", num);
    }
    
    // Call isPerfect() function
    if(isPerfect(num))
    {
        printf("%d is Perfect number.\n", num);
    }
    else
    {
        printf("%d is not Perfect number.\n", num);
    }
    
    return 0;
}
/**
 * Check whether a number is prime or not. 
 * Returns 1 if the number is prime otherwise 0.
 */
int isPrime(int num) 
{
    int i;
    
    for(i=2; i<=num/2; i++)  
    {  
        /*  
         * If the number is divisible by any number  
         * other than 1 and self then it is not prime 
         */  
        if(num%i == 0)  
        {
            return 0;
        }  
    } 
    
    return 1; 
}
/**
 * Check whether a number is Armstrong number or not. 
 * Returns 1 if the number is Armstrong number otherwise 0.
 */
int isArmstrong(int num) 
{
    int lastDigit, sum, originalNum, digits;
    sum = 0;
    
    originalNum = num;
    /* Find total digits in num */
    digits = (int) log10(num) + 1;
    /*
     * Calculate sum of power of digits
     */
    while(num > 0)
    {
        // Extract the last digit
        lastDigit = num % 10;
        // Compute sum of power of last digit
        sum = sum + round(pow(lastDigit, digits));
        // Remove the last digit
        num = num / 10;
    }
    
    return (originalNum == sum);
}
/**
 * Check whether the number is perfect number or not. 
 * Returns 1 if the number is perfect otherwise 0.
 */
int isPerfect(int num) 
{
    int i, sum, n;
    sum = 0;
    n = num;
    
    for(i=1; i<n; i++)  
    {  
        /* If i is a divisor of num */  
        if(n%i == 0)  
        {  
            sum += i;  
        }  
    }
    
    return (num == sum);
}

The result of the above c program; as follows:

Enter any number: 9
9 is not Prime number.
9 is Armstrong number.
9 is not Perfect number.

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.