Question
Use a java program that does the following:
. (10 points) Write a program as follows a. Prompt the user to input two positive integers: nl and n2 (nl should be less than n2) b. If the user enters the negative number(s), convert it/them to positive number(s) c. If nl is greater than n2, swap them. d. Use a while loop to output all the even numbers between nl and n2 e. Use a while loop to output the sum of all the odd numbers between n and n2 r. Use a while loop to output the sum of squares of all the odd numbers between nl and n2 g. Use a while loop to output all the lower case letters Sample run Enter two numbers (First number must be less than the second number) Enter the first number:-21 Enter the second number:S Even integers between 5 and 21 are: 6 8 10 12 14 16 8 20 Sum of odd integers betveen 5 and 21- 117 Sum of the squares of odd integers betveen 5 and 21-1761 Lower case letters arerabedefghijkinnoPqEstuvwxy z 2. (10 points) Driving Cost Program Drivers are concerned with the mileage obtained by their One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. De velop a program that will input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for cach tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls. Here is a sample input/output dialog: Enter the gallons used C-1 to end): 12.8 Enter the ales driven: 287 The miles/gallon for this tank was 22- Enter the gallons used C-1 to end): 10.3 Enter the miles driven: 200 The miles/gallon for this tank was 19- Enter the gallons used C-1 to end): 5 Enter the miles driven: 120 The miles/gallon for this tank was 24.0 Enter the gallons used C-1 to end):-1 The overall average miles/gallon was 21.6
media%2F35d%2F35de3e88-4b2f-4d46-9969-08
media%2Fd44%2Fd44b2ffa-dbd0-402b-9968-10
media%2Fdff%2Fdff961ef-a9dd-4930-8e83-25
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//1.

import java.util.Scanner;

public class {

public static void main(String[] args) {

  

int n1, n2, swap,num, oddsum, sumsq;

Scanner s = new Scanner(System.in);

char c;

System.out.println("Enter First Number: ");

n1 = s.nextInt();

  

System.out.println("Enter Second Number: ");

n2 = s.nextInt();

s.close();

if(n1<0)

{

n1=-n1;

}

else if(n2<0)

{

n2=-n2;

}

if (n1>n2)

{   

swap=n1;

n1=n2;

n2=swap;

}

oddsum=0;

num=n1;

sumsq=0;

while (num >= n1 && num <= n2){

if (num % 2 != 0)

{

oddsum = oddsum + num; //sum of odd

sumsq=sumsq+num*num;

}

num++;

}

System.out.println("Sum of odd integere is "+ oddsum);

System.out.println("Sum of SQUARES OF odd integers is "+ sumsq);

c='a';

System.out.println("Lowercase letters are ");

while(c<='z')

{

System.out.printf("%s ", c);

c++;

}

}

}

// 2

import java.util.Scanner;

public class {

public static void main(String[] args) {

  

float g,m; //g for gallons and m for miles

float avg,sum=0,n=0,mpg;

Scanner s = new Scanner(System.in);

while(0)

{

System.out.println("Enter the gallons used(-1 to end): ");

g = s.nextFloat();

if (g==-1)

break;

System.out.println("Enter the miles drives: ");

m = s.nextFloat();

mpg=g/m;

System.out.println("The miles per gallon for this tank is: "+mpg);

n++;

sum=sum+mpg;

}

avg-sum/n;

System.out.println("The overall average miles/gallon was "+avg;);

}

}

  

//3

import java.util.Scanner;

public class {

public static void main(String[] args) {

  

int A, B;

int A1, B1;

float ga,gb;

Scanner s = new Scanner(System.in);

System.out.println("Enter current student numbers of school A: ");

A = s.nextInt();

System.out.println("Enter current student numbers of school B: ");

B = s.nextInt();

System.out.println("Enter the growth rate of school A(percentage): ");

ga = s.nextFloat();

System.out.println("Enter the growth rate of school B(percentage): ");

gb = s.nextFloat();

a1=a;

b1=b;

for(int x=0;a1<b1;x++)

{

a1=a1+a1*ga;

b1=b1+b1*ga;

}

System.out.println("After"+x + "years the students numbersof A will be greater than or equal to B");

System.out.println("After"+x + "years the students numbers of A"+a1);

System.out.println("After"+x + "years the students numbers of B"+b1);

}

}

//4

import java.util.Scanner;

public class {

public static void main(String[] args) {

float gal,sum=0;   

int x;

Scanner s = new Scanner(System.in);

while(x!=0)

{

System.out.println("87 - Regular $2.45");

System.out.println("89 - Plus $2.89");

System.out.println("93 - Premium $3.09");

System.out.println("O - exit");

x=s.nextint();

switch(x)

{case 87 :

System.out.println("Enter the gallons used");

gal=s.nextFloat();

if(gal<=0)

System.out.println("Gallons used must be greater than 0, try again");

else

{

System.out.println("the price is"+gal*2.45);

sum=sum+gal;

}

break;

case 89 :

System.out.println("Enter the gallons used");

gal=s.nextFloat();

if(gal<=0)

System.out.println("Gallons used must be greater than 0, try again");

else

{

System.out.println("the price is"+gal*2.89);

sum=sum+gal;

}

break;

case 93 :

System.out.println("Enter the gallons used");

gal=s.nextFloat();

if(gal<=0)

System.out.println("Gallons used must be greater than 0, try again");

else

{

System.out.println("the price is"+gal*3.09);

sum=sum+gal;

}

break;

case 0:

System.out.println("Total sales is"+sum);

break;

default:

System.out.println("invalid response. Try agian");

}

}

}

}

Add a comment
Know the answer?
Add Answer to:
Use a java program that does the following: . (10 points) Write a program as follows...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT