Notification texts go here Contact Us Buy Now!

Complete the code segment to check whether the number is an Armstrong number or not. Armstrong Number:

Complete the code segment to check whether the number is an Armstrong number or not. Armstrong Number:

 Complete the code segment to check whether the number is an Armstrong number or not.

Armstrong Number:

A positive number is called an Armstrong number if it is equal to the sum of cubes of its digits for example 153 = 13+53+33, 370, 371, 407, etc.


import java.util.Scanner;

public class Exercise1_4 {

    public static void main(String[] args) {

   Scanner sc = new Scanner(System.in);

   int n=sc.nextInt();

           int result=0;


//Use while loop check the number is Armstrong or not.

//store the output(1 or 0) in result variable.

int n2 = n;

int sum=0 , cube;

while(n2>0)

        {

         cube = n2%10;

       

         sum=sum+( cube* cube* cube);

         

          n2 = n2/10;

         

        }


if(n == sum)

        {

          result = 1;

        }

else

        {

          result = 0;

        }

          

          System.out.print(result);

 }

}

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.