Question: I need to modify the below java program to do the following: Output all the even numbers between n1 and n2 inclusively using a do...

I need to modify the below java program to do the following:

Output all the even numbers between n1 and n2 inclusively using a do... while loop.

Output the sum of all the odd numbers between n1 and n2 inclusively using a for loop.

import java.util.*; //use a Scanner object to represent the keyboard

public class WhileLoops { public static void main(String[]args) { // declare the variables int sum=0; int sSquares=0; int n1=0; int n2=0; String alphabet = ("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"); // create a Scanner object to represent the keyboard Scanner input = new Scanner(System.in); // User Information System.out.print("Enter n1: "); n1=input.nextInt(); System.out.print("Enter n2: "); n2=input.nextInt(); if(n1>n2) { System.out.print("n1 cannot be greater than n2. Try Again: "); n1=input.nextInt(); } else{ System.out.println("Even numbers: "); while( n1 <= n2 ) { if( n1%2 != 1 ) { System.out.println( n1 ); sSquares+=( n1*n1 ); } if( n1%2 == 1 ) { sum+=n1; } n1++; } System.out.println("Sum Of odd numbers: "+sum ); System.out.println("Numbers and squares between 1 and 10" ); int i=1; while( i <= 10 ) { System.out.println( i+" "+(i*i)); i++; } System.out.println("Sum Of square even numbers: "+sSquares); } System.out.println("Lower case letters are:"); System.out.println( alphabet ); }// end main }// end class

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!