package armstrong;
import java.util.Scanner;
public class armstrong {
static int tr;
static double tri=0;
public static void main(String[] args) {
System.out.println("enter a number");
int s;
Scanner sc = new Scanner(System.in);
s=sc.nextInt();
int b=s;
do
{
tr=s%10;
tri=tri+Math.pow(tr,3);
s=s/10;
}
while(s!=0);
if(tri==b)
System.out.println("the number is armstrong");
else
System.out.println("not armstrong");
}
}
here i introduced a variable 'b' because 's' will get modified during the do while loop. is there any way to use 's' rather than storing the value to another variable
You can use this program to get Armstrong number:
import java.util.Scanner;
class ArmstrongNumberChecker {
public static void main(String[] args) {
int c = 0, a, temp,n;
System.out.print("enter a number:");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
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");
}
}
}
Yes there is. Do the following:
tr = s;
do
{
tri=tri+Math.pow(tr%10,3);
tr=tr/10;
}
while(tr!=0);
And then,
if(tri==s)
System.out.println("the number is armstrong");
else
System.out.println("not armstrong");
Notice, I directly used tr%10 in the computation for this.