Answers for "code for armstrong number in java"

2

Armstrong number in java

// Check whether a given number is armstrong number or not
import java.util.Scanner;
public class ArmstrongNumber
{
   public static void main(String[] args)
   {
      int x, y, z = 0, temp;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a number: ");
      x = sc.nextInt();
      temp = x;
      while(x > 0)
      {
         y = x % 10;
         x = x / 10;
         z = z + (y * y * y);
      }
      if(temp == z)
      {
         System.out.println(temp + " is an Armstrong Number.");
      }
      else
      {
         System.out.println(temp + " is not an Armstrong Number.");
      }
      sc.close();
   }
}
Posted by: Guest on December-29-2020
2

armstrong number in java

int c=0,a,temp;  
    int n=153;//It is the number to check armstrong  
    temp=n;  
    while(n>0)  
    {  
    a=n%10;  
    n=n/10;  
    c=c+(a*a*a);  
    }  
    if(temp==c)  
    System.out.println("armstrong number");   
    else  
        System.out.println("Not armstrong number");
Posted by: Guest on March-11-2020
0

armstrong number in java

import java.util.Scanner;

public class Armstrong
{
  public static void main(String args[])
  {
    int num,temp,c=0;
  	Scanner in=new Scanner(System.in);
  	num=in.nextInt();
  	temp=num;
  	while(num!=0)
  	{
    	int d=num%10; //extracting last digit
    	c+=d*d*d;
    	num/=10; // removing last digit
  	}
  	if(temp==c)
  	{
    	System.out.println("Number is Armstrong");
  	}
  	else
  		System.out.println("Number is not Armstrong");
  }
}
Posted by: Guest on August-20-2020
0

armstrong numbers problem java

import java.util.Scanner;

/*
 *@author: Mayank Manoj Raicha
 * Armstrong Number in Java: A positive number is called armstrong number if it is equal to the sum of cubes of its digits 
 * for example 0, 1, 153, 370, 371, 407 etc.
 * 153 = (1*1*1)+(5*5*5)+(3*3*3)  = 1+125+27 = 153
 */
public class ArmstrongNumberExample {

	public static void main(String[] args) {
		
		int sum = 0;
		
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the number: ");
		int input = in.nextInt(); //1634
		String val = String.valueOf(input);
		char[] charArray = val.toCharArray();  //charArray[0] = "1" , charArray[1] = "6", charArray[2] = "3", charArray[3] = "4"
		int[] numArray = new int[charArray.length]; //Declaring this array to store the result of getPowerOfNumber() method for each digit.
		
		//for each char element calculate the power of number and store it in the "cubedNumArray" array.
		for(int i=0; i<charArray.length; i++) {
			numArray[i] = getPowerOfNumber(Integer.parseInt(String.valueOf(charArray[i])), charArray.length);
			sum = sum + numArray[i];
		}
		
      //Compare if the resulting sum is equal to the original input.
		if(sum == input) {
			System.out.println("Entered number is an Armstrong number.");
		}else {
			System.out.println("Entered number is NOT an Armstrong number.");
		}
		
		in.close();
	}

  	//Calculate & Return the value of the first argument raised to the power of the second argument
	public static int getPowerOfNumber(int num, int count) {
		return (int) Math.pow(num, count);
	}
}
Posted by: Guest on May-27-2021

Code answers related to "code for armstrong number in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language