Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions