Java "For" loop output repeats -
i beginner trying program java code produce output numbers prime factor of 2 , five.
for example, if input 8, output should 2 4 5 8. however, whenever print output, result 2 5 4 5 8 5.
please advice on have gone wrong. thank you
import java.util.scanner; class twofive { public static void main(string [] args) { scanner sc = new scanner(system.in); system.out.print("enter n:"); int n = sc.nextint(); double num = 0; double num2 = 0; (int = 1; (((math.pow(2,i))<= n) || ((math.pow(5,i)) <=n) || (((math.pow(2,i))<= n) && ((math.pow(5,i)) <=n))) ; i++) { if (( math.pow(2,i)) <= n) num = (math.pow(2,i)); int convert = (int) num;{ system.out.print(convert + " "); } if ((math.pow(5,i)) <= n) num2 = (math.pow(5,i)); int convert2 = (int) num2; {system.out.print(convert2 + " "); } } } }
after review @amedeevangasse 's comment, need fix braces.
public static void main(string [] args) { scanner sc = new scanner(system.in); system.out.print("enter n:"); int n = sc.nextint(); double num = 0; double num2 = 0; (int = 1; math.pow(2,i))<= n) || ((math.pow(5,i)) <=n) || (((math.pow(2,i))<= n) && ((math.pow(5,i)) <=n))) ; i++) { if (( math.pow(2,i)) <= n) { num = (math.pow(2,i)); int convert = (int) num; system.out.print(convert + " "); } if ((math.pow(5,i)) <= n) { num2 = (math.pow(5,i)); int convert2 = (int) num2; system.out.print(convert2 + " "); } } }
you should review logic in for
loop , if
statements. full of unneeded redundancies.
Comments
Post a Comment