First one, I know that using break; is a bad coding habit, how would I remove it from this code?
[FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Lab07A
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]static[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] main(String[] args)
{
[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Variables
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] a;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] b;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] divisor;
Scanner keyboard = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Scanner(System.in);
[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// BEGIN LAB
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"Enter two integers (both > 0) to find their LCM: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
a = keyboard.nextInt();
b = keyboard.nextInt();
divisor = 1;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (divisor <= a * b)
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (divisor % a == 0 && divisor % b == 0)
{
System.out.println(a + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" and "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + b + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" have LCM: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] + divisor);
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]break[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New];
}
divisor++;
}
}
}[/FONT]
Second, I need to print all the prime numbers between the two inputted integers. I got it to work with smaller integers, but once it goes over 100's it just does everything wrong, so how would I rewrite this to do it correctly?
[FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] java.util.Scanner;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Lab07B
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]static[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] main(String[] args)
{
[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Variables
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] start;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] end;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] found_divisor_count = 0;
[/FONT][FONT=Courier New]Scanner keyboard = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Scanner(System.in);
[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// BEGIN LAB
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"What is the start interger: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
start = keyboard.nextInt();
System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"What is the end integer (greater than start): "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
end = keyboard.nextInt();
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (start <= end)
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (start % 2 != 0 && start % 3 != 0 || start == 3)
{
System.out.println(start + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" is a prime number."[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
}
start++;
}
}
}
[/FONT]
[FONT=Courier New][/FONT]---------- Post added at 10:49 AM ---------- Previous post was at 09:43 AM ----------
EDIT:
Just added some code to the Primenumbers, is there a shorter block of code?
[FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]import[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] java.util.Scanner;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]class[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Lab07B
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]public[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]static[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]void[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] main(String[] args)
{
[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// Variables
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] start;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] end;
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] found_divisor_count = 0;
Scanner keyboard = [/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]new[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] Scanner(System.in);
[/FONT][FONT=Courier New][COLOR=#fa6400][FONT=Courier New][COLOR=#fa6400]// BEGIN LAB
[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"What is the start interger: "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
start = keyboard.nextInt();
System.out.print([/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]"What is the end integer (greater than start): "[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
end = keyboard.nextInt();
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]while[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (start <= end)
{
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (start%2 != 0 && start%3 != 0 && start%4 != 0 && start%5 != 0 && start%7 != 0 || start == 2 || start == 3 || start == 5 || start == 7)
{
System.out.println(start + [/FONT][FONT=Courier New][COLOR=#00cb00][FONT=Courier New][COLOR=#00cb00]" is a prime number."[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
}
start++;
}
}
}
[/FONT]


Sign In
Create Account


Back to top









