mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
moved the class from others papckage to default.
and implemeted the following review comments. Remove the package Please provide a description for checkIfANumberIsAmstrongOrNot function Provide a description for what actually is an Armstrong number at the top along with an example fixes #96
This commit is contained in:
47
Armstrong.java
Normal file
47
Armstrong.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A utility to check if a given number is armstrong or not. Armstrong number is
|
||||||
|
* a number that is equal to the sum of cubes of its digits for example 0, 1,
|
||||||
|
* 153, 370, 371, 407 etc. For example 153 = 1^3 + 5^3 +3^3
|
||||||
|
*
|
||||||
|
* @author mani manasa mylavarapu
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Armstrong {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scan = new Scanner(System.in);
|
||||||
|
System.out.println("please enter the number");
|
||||||
|
int n = scan.nextInt();
|
||||||
|
boolean isArmstrong = checkIfANumberIsAmstrongOrNot(n);
|
||||||
|
if (isArmstrong) {
|
||||||
|
System.out.println("the number is armstrong");
|
||||||
|
} else {
|
||||||
|
System.out.println("the number is not armstrong");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether a given number is an armstrong number or not. Armstrong
|
||||||
|
* number is a number that is equal to the sum of cubes of its digits for
|
||||||
|
* example 0, 1, 153, 370, 371, 407 etc.
|
||||||
|
*
|
||||||
|
* @param number
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public static boolean checkIfANumberIsAmstrongOrNot(int number) {
|
||||||
|
int remainder, sum = 0, temp = 0;
|
||||||
|
temp = number;
|
||||||
|
while (number > 0) {
|
||||||
|
remainder = number % 10;
|
||||||
|
sum = sum + (remainder * remainder * remainder);
|
||||||
|
number = number / 10;
|
||||||
|
}
|
||||||
|
if (sum == temp) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,14 @@
|
|||||||
package Others;
|
package Others;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* To check if a given number is armstrong or not.
|
* A utility to check if a given number is armstrong or not. Armstrong number is
|
||||||
|
* a number that is equal to the sum of cubes of its digits for example 0, 1,
|
||||||
|
* 153, 370, 371, 407 etc. For example 153 = 1^3 + 5^3 +3^3
|
||||||
|
*
|
||||||
* @author mani manasa mylavarapu
|
* @author mani manasa mylavarapu
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Armstrong {
|
public class Armstrong {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@ -12,19 +16,24 @@ public class Armstrong {
|
|||||||
System.out.println("please enter the number");
|
System.out.println("please enter the number");
|
||||||
int n = scan.nextInt();
|
int n = scan.nextInt();
|
||||||
boolean isArmstrong = checkIfANumberIsAmstrongOrNot(n);
|
boolean isArmstrong = checkIfANumberIsAmstrongOrNot(n);
|
||||||
if(isArmstrong)
|
if (isArmstrong) {
|
||||||
{
|
|
||||||
System.out.println("the number is armstrong");
|
System.out.println("the number is armstrong");
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
System.out.println("the number is not armstrong");
|
System.out.println("the number is not armstrong");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether a given number is an armstrong number or not. Armstrong
|
||||||
|
* number is a number that is equal to the sum of cubes of its digits for
|
||||||
|
* example 0, 1, 153, 370, 371, 407 etc.
|
||||||
|
*
|
||||||
|
* @param number
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
public static boolean checkIfANumberIsAmstrongOrNot(int number) {
|
public static boolean checkIfANumberIsAmstrongOrNot(int number) {
|
||||||
int remainder, sum = 0,temp=0;
|
int remainder, sum = 0, temp = 0;
|
||||||
temp=number;
|
temp = number;
|
||||||
while (number > 0) {
|
while (number > 0) {
|
||||||
remainder = number % 10;
|
remainder = number % 10;
|
||||||
sum = sum + (remainder * remainder * remainder);
|
sum = sum + (remainder * remainder * remainder);
|
||||||
|
Reference in New Issue
Block a user