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):
- 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.
- Read a sequence of integer inputs and print the smallest and largest of the inputs.
- 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started