loops - JAVA How to return list of elements with amstrong numbers from interval [100; 999] -
i new in java , have got assigment armstrong numbers. created new class armstrongnumber.java initialized method website: http://www.programmingsimplified.com/java/source-code/java-program-armstrong-number
now in class main method created method calling armstrongnumber class , have return armstrong number interval [100 till 999].
there stuck .
public static void armtrongnumbs() { armstrongnumber returnobj = new armstrongnumber(); // here m calling class. int start = 100; int end = 999; for(int = start; i<= end; i++) { number = + number; returnobj.armstrong(number); } //returnobj.armstrong(); }
how loop return armstrong numbers?
edit: armstrongnumber class
class armstrongnumber { public void armstrong(int number) { int n, sum = 0, temp, remainder, digits = 0; scanner in = new scanner(system.in); system.out.println("input number check if armstrong number"); n = in.nextint(); temp = n; // count number of digits while (temp != 0) { digits++; temp = temp/10; } temp = n; while (temp != 0) { remainder = temp%10; sum = sum + power(remainder, digits); temp = temp/10; } if (n == sum) system.out.println(n + " armstrong number."); else system.out.println(n + " not armstrong number."); } static int power(int n, int r) { int c, p = 1; (c = 1; c <= r; c++) p = p*n; return p; } }
based on requirement, need logic of armstrongnumber.java
, mold suit per requirements.
you need use following code , can stop worrying using armstrongnumber.java
package hello; public class abc { public static void main(string[] args) { int n, sum, temp, remainder, digits; int start = 100; int end = 999; (int = start; <= end; i++) { sum = 0; digits = 0; temp = i; // count number of digits while (temp != 0) { digits++; temp = temp / 10; } temp = i; while (temp != 0) { remainder = temp % 10; sum = sum + power(remainder, digits); temp = temp / 10; } if (i == sum) system.out.println(i + " armstrong number."); } } static int power(int n, int r) { int c, p = 1; (c = 1; c <= r; c++) p = p * n; return p; } }
here can see, how sum , digits initialised 0 every number , rest of logic same. can verify 153, 370, 371, 407 printed armstrong numbers.
hope helps
Comments
Post a Comment