In: Computer Science
Have a pretty good idea on how to solve this program, I'm just not very accustomed to formatting well in Java for these types of problems. In java, you have full control of what's printed, I just need to get better at it, so I thought I'd post it here to receive an answer to learn from. Thanks for everything guys.
This program must use the Exclusive OR Operator ^ in java, it must also use a while loop. The way it wants to be formatted is also listed.
Write a program that uses a while loop to detect and print multiples of 19 or 23, but not both. Use the exclusive or operator as on page 93. The program should start examining integers at 100, and examine successively larger integers until 15 multiples have been detected. Correct multiples should be printed right aligned in fields that are 6 characters wide, but with only five multiples per line. The total of all correct multiples should be reported after 15 multiples have been printed.
Code - Main.java
//class Main
public class Main
{
//starting of the program
public static void main(String[] args) {
//variable declare
int n=100,countMultiple =
0,countMultipleInLine=0;
//while loop
do{
//using Exclusive OR Operator ^ to check for the
multiple of 19 and 23
if(((n%19 == 0)^(n%23 == 0)) == true){
//print the multiple
System.out.print(n+" ");
//increment the countMultiple variable
countMultiple++;
//increment count of multiple in a line
countMultipleInLine++;
//if the countMultipleInLine is 5 then again set it to
0 and go to next line
if(countMultipleInLine == 5){
//go to next line
System.out.println();
//initialize countMultipleInLine to 0 again
countMultipleInLine = 0;
}
}
//increment n
n++;
}while(countMultiple !=15);//exit while loop when 15
multiple are found
}
}
Screenshots -