Computer Science, asked by socayow375, 2 months ago

Write a program in JAVA to enter a number and check and print whether the number is Armstrong or not.

★ A number is said to be Armstrong, if the sum of the cubes of the digits is equal to the original number.​

Answers

Answered by Bᴇʏᴏɴᴅᴇʀ
92

Answer:-

Program to check for Armstrong number in Java:-

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

\pink{\bigstar} \large\underline{\underline{\bf\green{C{o}de:-}}}

import java.util.*;

public class Armstrong

{

public static void main(String args[])

{

Scanner in = new Scanner (System.in);

int n,num,a,s=0;

System.out.println("Enter a Number:");

n=in.nextInt();

num=n;

while(n>0)

{

a=n%10;

s=s+a*a*a;

n=n/10;

}

if(num==s)

System.out.println("It is an Armstrong Number");

else

System.out.println("It is NOT an Armstrong Number");

}

}

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

\pink{\bigstar} \large\underline{\underline{\bf\green{Out{p}ut:-}}}

★ Refer to the attachment for Output.

Input value in the above Output is 153.

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

\red{\bigstar} Explore More:-

✯ Here, is the list of all 3 digit Armstrong numbers.

  • 153

  • 370

  • 371

  • 407

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

✯ In 4 digit Armstrong numbers like 1634, 8208 and 9474 the sum of the power of four of the digits is equal to the original number.

Attachments:
Answered by BrainlyProgrammer
5

Answer:

//Java Program to check if a number is Armstrong number or not.

import java.util.*;

public class Armstrong {

public static void main(String[] args) {

Scanner sc=new Scanner (System.in);

long n=sc.nextInt(); //Enter a number

int c=0,s=0;long n1=n;

while (n!=0)

{

c++;

n/=10;

}

n=n1;

while (n1!=0)

{

s+=Math.pow(n1%10,c);

n1/=10;

}

if(s==n)

System.out.println("Armstrong");

}

}

Variable Description:-

  • n:- to Accept input
  • c:- counter variable
  • s:- sum variable
  • n1:- to make the copy of the original number.

• Output Attached.

Attachments:
Similar questions