Check a given number is an Armstrong Number

Armstrong Number:- A Number is an Armstrong Number if the sum of cubes of digits of the number is equal to Given Number.

Example:-

153, 307, 407....

Problem statement:- Program to check whether a given number is an Armstrong number or not.

Sample Input/Output:-

Sample Input First: 250                         Sample Output First: It is not an Armstrong Number

Sample Input Second: 153                    Sample Output SecondIt is an Armstrong Number

Explanation:- 

For output 1st:  sum of cubes of digits(250)=2³+5³+0³=8+125+0=133 ≠ Given Number

For output 2nd: sum of cubes of digits(153)=1³+5³+3³=1+125+27=153  = Given Number

Data requirement:-

   Input Data:- num

  Output Data:-String Output

  Additional Data:- num2, sum, rem

Program in C
  
Here is the source code of the C Program to find the sum of first n natural numbers.

Code:

#include<stdio.h>
int main()
{
    int num;
    printf("Enter a number:");
    scanf("%d",&num);
    int num2=num;
    int sum=0;
   while(num!=0)
   {
       int rem=num%10;
       num=num/10;
       sum=sum+rem*rem*rem;
   }
   if(sum==num2)
    printf("It is an Armstrong Number");
   else
    printf("It is not an Armstrong Number");
}

Input/Output:
Enter a number:250
It is not an Armstrong Number

Program in C++
  
Here is the source code of the C++ Program to find the sum of first n natural numbers.

Code:

#include<iostream>
using namespace std;
int main()
{
    int num;
    cout<<"Enter a number:";
    cin>>num;
    int num2=num;
    int sum=0;
   while(num!=0)
   {
       int rem=num%10;
       num=num/10;
       sum=sum+rem*rem*rem;
   }
   if(sum==num2)
    cout<<"It is an Armstrong Number";
   else
    cout<<"It is not an Armstrong Number";
}

Input/Output:
Enter a number:153
It is an Armstrong Number

Program in Java
  
Here is the source code of the Java Program to find the sum of first n natural numbers.

Code:

import java.util.Scanner;
public class ArmstrongNumberNumberOrNot {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num;
    System.out.println("Enter a number:");
    num=cs.nextInt();
    int num2=num;
    int sum=0;
   while(num!=0)
   {
       int rem=num%10;
       num=num/10;
       sum=sum+rem*rem*rem;
   }
   if(sum==num2)
    System.out.println("It is an Armstrong Number");
   else
    System.out.println("It is not an Armstrong Number");
cs.close();
}
}

Input/Output:
Enter a number:
407
It is an Armstrong Number


Program in Python
  
Here is the source code of the Python Program to find the sum of first n natural numbers.

Code:

'num=int(input("Enter a number:"))
num2=num
sum=0
while(num!=0):
   rem=num%10
   num=int(num/10)
   sum=sum+rem*rem*rem
if sum==num2:
   print("It is an Armstrong Number")
else:
   print("It is not an Armstrong Number")

Input/Output:
Enter a number:555
It is not an Armstrong Number






Post a Comment

0 Comments