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
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