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
  • Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of...

    Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a C++ 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 each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls....

  • Drivers are concerned with the mileage their automobiles get. One driver has kept track of several...

    Drivers are concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording the miles driven and gallons used for each tankful. Develop a C# app that will input the miles driven and gallons used (both as integers) for each tankful. The app should calculate and display the miles per gallon obtained for each tankful and display the combined miles per gallon obtained for all tankfuls up to this point. All averaging...

  • Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankful of gasoline by recording miles driven and gallons used for each tankful. Develop a C++ program that inputs the miles driven and gallons us

    Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankful of gasoline by recording miles driven and gallons used for each tankful. Develop a C++ program that inputs the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point.

  • 7.11 Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track...

    7.11 Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording the number of miles driven and the number of gallons used for each tankful. Develop a script that will take as input the miles driven and gallons used (both as integers) for each tankful. The script should calculate and output HTML5 text that displays the number of miles per gallon obtained for each tankful and the combined...

  • Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of...

    Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tanks of gasoline by recording miles driven and gallons used for each tank. Develop a program using a while loop that will input the miles driven and the gallons used for each tank. The program should calculate and display the miles per gallon obtained for each tank. After processing all input information, the program should calculate and print the overall miles per gallon...

  • Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of...

    Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankful of gasoline by recording miles driven and gallons used for each tankful. Develop a C++ program that inputs the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankful up to this point. (Use the five-step problem solving methodology)...

  • C++ please Question 4: 125 points] Write and test a program that reads two positive integers...

    C++ please Question 4: 125 points] Write and test a program that reads two positive integers nl and n2 with n2 > nl. The program then calculates the sum of the prime numbers, using is prime () function developed above, between nl and n2 (inclusive) A Sample input Enter values for nl and n2 (nl<n2): 3 9 Output: Thé Sum of prime numbers between 3 and 9 (inclusive) is 15

  • Programming Language is Java Lab 1: Programming Exercises Input/Process/Output 1) Run the Numbers. Write a program...

    Programming Language is Java Lab 1: Programming Exercises Input/Process/Output 1) Run the Numbers. Write a program with two input values. The program should display the sum, difference, quotient, product, and average of the two numbers. 2) Jake’s Problem. Jake has a car with an 8-gallon fuel tank. Jake fills his tank with gas and drives 60 miles to a friend’s house. When he gets to his friend’s house, he has 6 gallons left in his fuel tank. Write a program...

  • Write program PSEUDOCODE that prompts the user for their name and then will compute a car's...

    Write program PSEUDOCODE that prompts the user for their name and then will compute a car's miles per gallon given input of miles driven and gallons of gas used. It should also convert those given numbers into metric and show kilometers driven, liters used, and kilometers per liter. All with well formatted output. Submit: C++ Programming Warm-Up Exercise PSEUDOCODE for the algorithm that compute's a car's miles per gallon given input of miles and convert these numbers into metric and...

  • Write a Java program to prompt for inputting an integer N, then enter N integers (a...

    Write a Java program to prompt for inputting an integer N, then enter N integers (a loop is needed), print the numbers user entered, and the amount of even and odd numbers (zero is even number). (1) Prompt for the user to input an integer and output the integer. (1 pts) Enter an integer: You entered: 5 (2) Prompt for the user to input N integers, output the numbers entered and the amount of even and odd numbers (9 pts)...

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