in java
Use a for loop and nested if statements to determine if a number is even or prime
Use the numbers 30 to 60
Hint: you may need to review what is a prime number
Output:
30 . . . even
31 . . . prime number
32 . . . even
33 . . .
34 . . . even
35 . . .
36 . . . even
37 . . . prime number
etc.
package test.java;
public class TestPrime {
public static void main(String[] args){
int startNumber = 30;
int endNumber = 60;
boolean isNonPrime = false;
for(int i = startNumber;
i<=endNumber; i++){ //This will loop from 30 to 60
if(i==2){ //As 2
is the only even prime number;though will nnot be needed in this
case, just used to keep up with prime number logic
System.out.println(i+"...prime number");
}else
if(i%2==0){//This will check if the number is even i.e divisibility
by 2
System.out.println(i+"...even");
}else{
isNonPrime = false;
/* Check prime number by checking if the number
has any factor from 2 to (number/2)
* e.g if the number is 39, it will divide 39
starting from 2 to 39/2 = 19 to check if any number divides
39
* If no number divides that means the number is
prime else it is non prime*/
for(int k = 2; k<=i/2;k++){
if(i%k==0){
isNonPrime
= true;
break;//breaking out of the loop as we already found a factor for
the number indicating it is a non prime number
}
}
if(isNonPrime){
System.out.println(i+"...");
}else{
System.out.println(i+"...prime number");
}
}
}
}
}
Explanation:
Comments are given within the code . The check for prime number for 2 has also been given in the code , however that will not be need in our case as we are iterating through number 30 to 60.
The program output will be like below:
30...even
31...prime number
32...even
33...
34...even
35...
36...even
37...prime number
38...even
39...
40...even
41...prime number
42...even
43...prime number
44...even
45...
46...even
47...prime number
48...even
49...
50...even
51...
52...even
53...prime number
54...even
55...
56...even
57...
58...even
59...prime number
60...even
in java Use a for loop and nested if statements to determine if a number is...
Create in Java: 1. Create a (nested) for loop that given a number prints a line with the number repeated as many times as the number itself, for example: 22 333 4444 000.- 2.Modify the nested for loop to specify in each line if the numbers are even or odd. For example: 1 (odd) 22 (even) 333 (odd) 4444 (even) Dona.... 3. Modify the nested for loop to only print odd numbers. For example: 333 55555 annan..
I know how to do number one just not number 2. Please answer number 2 only Write a java program that will print out following pattern. Use nested for loop. 2. Add a method to the above program that generates takes two numbers as parameters and prints out all the numbers from number 1 to number 2 separated by using a while loop. (Do not print the last ,'). For example: Calling method1(9,51) should print: 9, 10, 11, 12, 13,...
Write a python nested for loop that prints out the following pattern 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33...
ANOVA 1. The data represent the number of pods of soybean plants for various plant types. An agricultural researcher wants to determine if the mean numbers of pods for each plot type are equal. Use significance level 0.05 Libert No till Chisel plowed34 32 34 31 30 36 31 37 24 23 41 40 32 34 39 37 27 37 42 34 38 39 30 35 27 ANOVA 1. The data represent the number of pods of soybean plants for...
Java use for loop (nested for loop) if/else possible on blue J. (row *col)%2 for 0's and 1's. Write a program to generate the below table: . The table should be able to vary in size based on a class constant called SIZE; minimum of 1, maximum of 9. . The O's and 1's can and should be generated by evaluating a mathematical expression using the row and column numbers Think about the operators: +-* / % . The output...
Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the user for the size of the multiplication table (from 2x2 to 10x10). Use a validation loop to display a warning if the number is less than 2 or greater than 10 and prompt the user to enter the data again until they enter a valid number Put a...
Java program The file enrollments.txt is a text file with each line containing a positive integer that represents the enrollment in a section of CIS 103. The highest value is 36. Write a program to read the data from the file into an array, then calculate and display the number of sections, the average class size, the minimum class size, and the maximum class size. enrollments.txt 36 36 36 34 35 25 34 31 29 19 35 33 36 32...
True or false 27. Java compiler does not ignore white spaces such as spaces, tabs, or blank lines in a Java program. 28. The same variable name(s) can be declared and used within different methods. 29. In Java, 3*'b'+1 is a valid expression. 30. In Java x++ is equivalent to x=x+1. 31. In Java, int x-2.5; is a valid statement. 32. The data type of the Java expression (int) 2.5 is double. 33. A method in Java can return more...
I need it in JAVA Write a program that randomly populates an array of size 100, sorts it, and then finds the median. The random numbers must be from 0-99 and all integer values. Also since there is an even set of numbers the median is found by taking the mean of the two middle numbers (In other words the 50th and 51st). You have to code your own sorting method. You may not use a built in java sorter....
1. The service departments at Tionesta Ford Lincoln Mercury and Sheffield Motors Inc., two of the four Applewood Auto Group dealerships, were both open 24 working days last month. Listed below is the number of vehicles serviced last month at the two dealerships. Construct dot plots and report summary statistics to compare the two dealerships. Tionesta Ford Lincoln Mercury Friday Saturday Thursday Wednesday Tuesday Monday 26 28 39 27 33 23 35 32 33 28 32 30 32 27 31...