Menu
×
Tutorials Ms Word Tutorial Ms Excel Tutorial Ms PowerPoint Tutorial C Language Tutorial C++ Tutorial C Sharp Tutorial Visual Basic Tutorial HTML Tutorial CSS Tutorial JavaScript Tutorial WordPress Tutorial
     ❯   

ADVERTISEMENT

Inputted Number is Strong or Not

#include<stdio.h>
#include<math.h>
int GetFactorial(int number);
main()
{
  int num,i,j,temp,sum=0;;
  printf("Enter a number to know whether it is armstrong or not\n");
  scanf("%d",&num);
  temp=num;
  while(num>0)
  {
    i=num%10;
    sum+=GetFactorial(i);
    num=num/10;
  }
  if(sum==temp)
  {
    printf("Giveb number %d is an Strong number\n",temp);
  }
  else
  {
    printf("Giveb number %d is not an Strong number since the sum of factorials of individual digits is %d\n",temp,sum);
  }
}
 
int GetFactorial(int number)
{
  int factorial=1,i;
  for(i=1;i<=number;i++)
  {
   factorial=factorial*i;
  }
  return factorial;
}
C Programming Examples
OUTPUT
Enter a number to know whether it is armstrong or not
28
Giveb number 28 is not an Strong number since the sum of factorials of individual digits is 40322
C Programming Examples

Below is an explanation of the code:

  1. Header Files:
   #include<stdio.h>
   #include<math.h>

The program includes necessary header files. stdio.h is for standard input and output functions, and math.h is for mathematical functions.

  1. Function Declaration:
   int GetFactorial(int number);

Declares a function named GetFactorial that calculates the factorial of a given number.

  1. Main Function:
   main()

Defines the main function, which is the entry point of execution.

  1. Variable Declaration:
   int num, i, j, temp, sum = 0;

Declares integer variables: num to store the user-inputted number, i for digit extraction, j (unused), temp to store the original number for later comparison, and sum to accumulate the factorials.

  1. User Input:
   printf("Enter a number to know whether it is Armstrong or not\n");
   scanf("%d", &num);

Prompts the user to enter a number and stores the input in the variable num.

  1. Digit Extraction and Factorial Summation:
   temp = num;
   while(num > 0)
   {
     i = num % 10;
     sum += GetFactorial(i);
     num = num / 10;
   }

Iterates through each digit of the input number, calculates the factorial of each digit using the GetFactorial function, and accumulates the factorials in the variable sum.

  1. Armstrong Number Check:
   if(sum == temp)
   {
     printf("Given number %d is an Armstrong number\n", temp);
   }
   else
   {
     printf("Given number %d is not an Armstrong number since the sum of factorials of individual digits is %d\n", temp, sum);
   }

Compares the sum of factorials (sum) with the original number (temp). If they are equal, it prints a message indicating that the number is an Armstrong number; otherwise, it indicates that the number is not an Armstrong number.

  1. Factorial Calculation Function:
   int GetFactorial(int number)
   {
     int factorial = 1, i;
     for(i = 1; i <= number; i++)
     {
       factorial = factorial * i;
     }
     return factorial;
   }

Defines the GetFactorial function, which calculates and returns the factorial of a given number using a for loop.

Spread the love
Scroll to Top
Scroll to Top