java - Why do I get wrong output? -
i new java , trying write simple code. here description: write program prompts user number x. print numbers 1 x. however, in place of multiples of 4 print “qqqq”. in place of multiples of 7, print “seven”. if number divisible both 4 , 7 print “qqqqseven”. means if input 4, output should 1, 2, 3, (qqqq), ...but 1(qqqq), 2(qqqq), 3(qqqq), 4(qqqq)..... can me , let me know doing wrong? appreciated. you.
public static void main(string args[]) { //print method system.out.println("enter number upto want print: "); scanner input = new scanner(system.in); int x; x = input.nextint(); for(int i=1; <= x; i++) { system.out.println(i); //if x multiples of 4 if (x % 4 == 0) system.out.println("qqqq"); //if x multiples of 7 if (x % 7 == 0) system.out.println("seven"); //if x divisible 4 , 7 if (x % 4 == 0 && x % 7 == 0) system.out.println("qqqqseven"); } }
}
the idea here use if condition specific least specific. in case specific condition divisor of 4 , 7, followed divisor of 4, divisor fo 7 , least specific 1 means else. if arrage if conditions in order you'll result.
note: practice close scanner or resources open. :)
import java.util.scanner; public class testprogram { public static void main(string[] args) { system.out.println("enter number upto want print: "); scanner input = new scanner(system.in); int x; x = input.nextint(); (int = 1; <= x; i++) { if(i%4 == 0 && i%7 == 0) { system.out.println("qqqqseven"); } else if(i%4 == 0) { system.out.println("qqqq"); } else if(i%7 == 0){ system.out.println("seven"); } else { system.out.println(i); } } input.close(); } }
Comments
Post a Comment