'Armstrong number in Java
This is my code, it is not producing any output. As I have started my loop from 100 so according to the logic used I should get answer as 153. But nothing is coming. Please help.
// Program to find the first Angstrom Number and display it!
public static void main(String[] args) {
int sum = 0;
int y, z;
System.out.println("Starting program");
for (int i = 100; i < 1000; i++) {
sum += (i % 10) * (i % 10) * (i % 10);
y = i / 10;
sum += (y % 10) * (y % 10) * (y % 10);
z = y / 10;
sum += z * z * z;
if (sum == i) {
System.out.println("The first Angstrom number is " + i);
break;
}
}
}
Solution 1:[1]
You should reset the sum in every step:
for (int i = 100; i < 1000; i++) {
sum = 0;
sum += (i%10) * (i%10) * (i%10);
....
}
Solution 2:[2]
You can find Armstrong number between two numbers by using this logic. Just change the values according to you need.
public class Armstrong {
public static void main(String[] args) {
int low = 999, high = 99999;
for(int number = low + 1; number < high; ++number) {
int digits = 0;
int result = 0;
int originalNumber = number;
// number of digits calculation
while (originalNumber != 0) {
originalNumber /= 10;
++digits;
}
originalNumber = number;
// result contains sum of nth power of its digits
while (originalNumber != 0) {
int remainder = originalNumber % 10;
result += Math.pow(remainder, digits);
originalNumber /= 10;
}
if (result == number)
System.out.print(number + " ");
}
}
}
Output of this program is:
1634 8208 9474 54748 92727 93084
Solution 3:[3]
public static int Power(int out,int res)
{
int count=res;
int temp=1;
while(count!=0)
{
temp=temp*out;
count--;
}
return temp;
}
public static int count1(int num)
{
int count=0;
while(num!=0)
{
num=num/10;
count++;
}
return count;
}
public static int isArmstrong(int num)
{
int out;
int sum=0;
int res=count1(num);
while(num!=0)
{
out=num%10;
sum=sum+Power(out,res);
num=num/10;
}
return sum;
}
public static void main(String[] args)
{
int start=10;
int end=100000;
for(int i=start;i<=end;i++)
{
int result=isArmstrong(i);
if(result==i)
{
System.out.println(i);
}
Solution 4:[4]
public class armstrongNumber
{
public void isArmstrong(String n)
{
char[] s=n.toCharArray();
int size=s.length;
int sum=0;
for(char num:s)
{int temp=1;
int i=Integer.parseInt(Character.toString(num));
for(int j=0;j<=size-1;j++)
{ temp *=i;}
sum +=temp;
}
if(sum==Integer.parseInt(n))
{
System.out.println(n+" is an Armstrong Number");
}
else
{
System.out.println(n+" is not an Armstrong Number");
}
}
public static void main(String[] args)
{
armstrongNumber am= new armstrongNumber();
am.isArmstrong("2");
am.isArmstrong("153");
am.isArmstrong("1634");
am.isArmstrong("231");
}
}
Solution 5:[5]
public class Armstrong {
public static int findArmStrong(int x, int y) {
if(x== getArmstrongSum(x)) return x;
else if(y== getArmstrongSum(y)) return y;
else return -1;
}
public static int getArmstrongSum(int num) {
int pow = String.valueOf(num).length();
return IntStream.iterate(num, i -> i / 10)
.limit(pow)
.map(i -> (int) Math.pow(i % 10, 3))
.sum();
}
public static void main(String[] args) {
System.out.println(findArmStrong(153, 154));
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Salem |
Solution 2 | Ijlal Hussain |
Solution 3 | Sushant27 |
Solution 4 | |
Solution 5 | Suraj Rao |