Question: Need help in java: Write a program with loops to solve each of the following (comment each block of code): Compute the sum of all

Need help in java:

Write a program with loops to solve each of the following (comment each block of code):

  1. Compute the sum of all odd numbers between a and b (inclusive), where a and b are inputs. Prompt the user for a and b. Display the sum.
  2. Read a sequence of integer inputs and print the smallest and largest of the inputs.
  3. Prompt for a line of input as a string and display the string, with all vowels replaced by an underscore.

This is my code so far, I have complete part 1, but I am struggling with the second and third part, after I type in the sequence of numbers of the second part nothing happens after. I enter them:

Scanner in = new Scanner(System.in);

int a, b;

int sum = 0;

//get all inputs

System.out.println("Enter a: ");

a = in.nextInt();

System.out.println("Enter b: ");

b = in.nextInt();

// compute sum of all odd numbers

if (a>b) {

System.out.println("The value of a is less than b");

} else {

for(int i = a; i <= b; i++) {

if((i%2) == 1) {

sum+=i;

}

} System.out.print("The sum is "+sum+ ".");

}

//STEP 2

// input sequence of numbers

System.out.println(" Enter the sequence of inputs :");

int input_num=in.nextInt();

int largest_num=input_num;

int smallest_num=input_num;

String end="";

while(in.hasNextInt()) {

int input=in.nextInt();

//get smallest input

if(input

smallest_num=input;

}

//get largest input

if(input>largest_num) {

largest_num=input;

}

}

System.out.println("Smallest number = "+smallest_num);

System.out.println("Largest number = "+largest_num);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!