Question

In: Computer Science

Java Program 1Use a loop to add up the odd numbers between 100 and 200. 2Use...

Java Program

1Use a loop to add up the odd numbers between 100 and 200.

2Use a loop to determine if a number is prime. Recall: a number is prime if its only factors are 1 and itself.

3. Nested Loops:

}Write a nested loop that finds the largest prime number smaller than 125.

Solutions

Expert Solution

Question 1:
//TestCode1.java
public class TestCode1 {
    public static void main(String[] args)
    {
        int sum = 0;
        for(int i = 100;i<=200;i++){
            if(i%2==1){
                sum += i;
            }
        }
    }
}


Question 2:
import java.util.Scanner;
public class TestCode2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n;
        System.out.print("Enter a number : ");
        n = scanner.nextInt();
        boolean flag = true;
        for(int i = 2;i<n;i++){
            if(n%i == 0){
                flag = false;
            }
        }
        if (flag) {
            System.out.println("It is a prime number.");
        }
        else{
            System.out.println("It is a composite number.");
        }
    }
}


Question 3:
public class TestCode3 {
    public static void main(String[] args) {
        for(int j = 124;j>=1;j--) {
            boolean flag = true;
            int n = j;
            for (int i = 2; i < n; i++) {
                if (n % i == 0) {
                    flag = false;
                }
            }
            if(flag){
                System.out.println(n);
                break;
            }
        }

    }
}

Related Solutions

Java Write a program that displays all the numbers from 100 to 200 that are divisible...
Java Write a program that displays all the numbers from 100 to 200 that are divisible by 5 or 6, but not both Make sure all instructions and outputs for the user are explicit
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Given the following program(Java); we are asked to do the following 1. Add a loop in...
Given the following program(Java); we are asked to do the following 1. Add a loop in the main to enqueue 12 items of your choice. 2. Be sure to implement some form of error checking that lets you know if the loop tries to add too many items to the queue. Error message: "Unexpected overflow" 3. Add a loop to dequeue items and print them on their own line with their location. Location = ? item = ? package Khatrijavaarrayqueue;...
TO DO in JAVA: The program that runs is TrackInsurance. Open this up and add five...
TO DO in JAVA: The program that runs is TrackInsurance. Open this up and add five instances of your ArtInsurance class at the noted location in the main method. Use whatever data you like. TrackInsurance(below) package itp120mod6; import java.util.*; public class TrackInsurance extends Object { public static Scanner scan = new Scanner(System.in); // method that runs first public static void main(String[] args) throws Exception { // make an ArrayList of customers and insurance policies ArrayList cust = new ArrayList(); //...
1.write a small program using a loop to add a series of numbers 2.write a function...
1.write a small program using a loop to add a series of numbers 2.write a function called "main" that performs several given steps. Be sure to call the main() function so that its code executes In python and doesn't have to be long. just long enough to do what it says. Thank you.
Write a program which uses a while loop to add the following numbers: 5, 52, 31,...
Write a program which uses a while loop to add the following numbers: 5, 52, 31, and 65 and output the sum of those numbers. Your output should look like this: The sum of (list the numbers) is ( give the result). //In C language Upload a screen shot of your program and the results of running the program.
Python Exercise 2 A program is to display all odd numbers between 10 and 3000. The...
Python Exercise 2 A program is to display all odd numbers between 10 and 3000. The starting value and end value are to be assigned by the programmer, not requested from user.
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
Use a For loop to compute the sum of all the odd numbers from 1 through...
Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500. Print your name 7 times using a While loop. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"}; Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop. Reverse the logic for zyBook Challenge activity 4.6.1 (Nested loops: Indent text) so that the first line is indented userNum...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT