Description

Hello there...

Here we write the a new program in c programming language. The program is find that given number is Armstrong or Not. First of all, we know about that what is armstrong number. Armstrong number is which number that the sum of every digit of the cube is equal to the given number is called the Armstrong number, other wise we called the given number is Not Armstrong Number. So Let's get started the code. 

// Check Whether given number is Armstrong or not
// eg. 371 === (3*3*3) + (7*7*7) + (1*1*1) == 27 + 343 + 1 == 371 this number is armstrong 
// 123 === (1*1*1) + (2*2*2) + (3*3*3) == 1 + 8 + 27 == 36 this number is not armstrong

#include<stdio.h>

int main()
{
    int num ;
    printf("Enter the number : \n");
    scanf("%d",&num);

    int temp;
    temp = num;
    int lastDigit;
    int sum =0;

    while(num>0)
    {
        lastDigit = num%10;
        sum = sum + (lastDigit*lastDigit*lastDigit);
        num = num/10;
    }

    if(sum==temp)
    {
        printf("The given number %d is armstrong",temp);
    }
    else{
        printf("The given number %d is not armstrong",temp);
    }
    
    return 0;
}

 

Enter the number : 271
The given number 271 is not armstrong

  • Step 1: Initilized a variable num which store the value of given number by user
  • Step 2: Print a message from user with the help of printf() library funciton
  • Step 3: take the input from user with the help of scanf() library function
  • Step 4: Initilized a variable that's called temp is a temporary variable, which store the value of given number as a duplicate for use the last of the program
  • Step 5: Initilized a variable lastDigit which store the last digit of the given number 
  • Step 6: Initilized a variable sum and assigned the value is zero for store the sum of the cube of per digit
  • Step 7: Start a while loop, the condition is given as when num>0 that execute this code. Otherwise terminate the code
  • Step 8: In while loop body perform sum operation like that find the last digit (lastDigit=num%10) of the given number, store the sum of the sum of last digit cube (sum = sum + (lastDigit*lastDigit*lastDigit) and remove the last digit (num=num/10)
  • Step 9: Comparison to the sum and temp variable. Sum is store the value of the sum of given number and temp variable store the duplicate of the number, the condition is true the print that "The Given number 371 is Armstrong Number. Otherwise print that "The given Numbe 198 is Not a Armstrong Number.
  • Step 10: Exit;

 

Thank You So Much....

Comments

Login is mandatory to comment Please login

Recommended Posts

Star Pyramid Pattern in C programming

How to code the star pyramid pattern in c programming using for loop and simplied way.

Even or Odd in C | How to check given number is odd or even

How to check the given number is odd or even in C programming language.

Factorial program in C | Factorial given number by user | Factorial

Hello Guys... Here we write a program to calculate the factorial a number given by user.

Factorial Using function | C programming language

Here we calculate the factorial using function in c programming language.

Prime number program in c | Calculate given number is prime or Not | prime number

What is prime number? and how to calculate the given number is prime or not |

Largest Number | C programming language

Hello guys.. Today we write a program how to find the largest number ( greatest number ) in array c programming language.