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 that uses three input elements to enter values for tank size, miles traveled and gallons left. The program should calculate and display how many miles Jake can drive on a full tank of gas.
(Note: Be sure to use input elements to accept the values 8, 60, and 6 rather than hard coding then into your solution.)
3) Correct Change.
Write a program to assist a cashier with determining correct change. The program should have one input, the number of cents to return to the customer. The output should be the appropriate change in quarters, dimes, nickels and cents.
Hint: Consider how integer division (\) and integer remainder (Mod) can be used as part of your solution.
Answers:
Lab1.
1)Run the Numbers.
raw code:
//import
import java.util.Scanner;
public class Run_The_Numbers{//class name
public static void main(String[] args) { //main
method
Scanner scnr= new
Scanner(System.in);//creating an scanner object
//
System.out.println("Enter the first
value:");//taking the first input into variable called a
int a= scnr.nextInt();
System.out.println("Enter the
second value:");
int b=scnr.nextInt();//taking the
first input into variable called b
System.out.println("Sum: "+(a+b));
//adding the inputs
System.out.println("Difference:
"+(a-b));//subtracting the second input from first input
System.out.println("Product:
"+(a*b));//multiplying the values
System.out.println("Quotient:
"+(a/b));//dividing a by b
System.out.println("Average:
"+((a+b)/2));// adding the both inputs and dividing it by 2 get
average
}
}
code in editor:
output:

2) Jake’s Problem.
Raw code:
//imports
import java.util.Scanner;
public class Jakes_problem {//class name
public static void main(String[] args) {//main
method
Scanner scnr= new
Scanner(System.in);//creating scanner object
System.out.println("Enter the
tanksize: ");
int
tank_size=scnr.nextInt();//taking the input from user for the
tank_size
System.out.println("Enter the miles
Travelled:");
int
miles_travelled=scnr.nextInt();//taking the input from user for the
total_miles he travelled for friends house
System.out.println("Enter the
gallons left in tank");
int
gallons_left=scnr.nextInt();//taking the input from user for the
gallons of gas left in the tank after he reached his friends
house
int gallons_consumed= tank_size-
gallons_left;//to calculate the gallons consumed to travel to his
friend house
int miles_per_gallon=
miles_travelled/gallons_consumed;//to calculate the miles can be
covered using one gallon of fuel
int total;
total=tank_size*miles_per_gallon;
//to calculate the total miles can be covered with the full tank of
fuel
System.out.println("Total miles can
be covered using the Full Tank:"+total);
}
}
code in editor:

output:

3) Correct Change.
raw code:
// Importing packages
import java.util.*;
public class CorrectChange{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
// Creating Scanner object for reading input from user.
System.out.print("Enter no of cents: ");
int n = s.nextInt(); // Taking no
of cents from user
int arr[] = new int[4]; // Creating
int array 'arr'.It's automatically intialized with '0's
// arr[0] -> "Quarters" ; arr[1]
-> "Dimes" ; arr[2] -> "Nickels" ; arr[3] -> "cents"
while(n>0){ // This loop will
iterate until the n value is greater then 0
if
((n/25)>=1){ // If the 'n' is divisible by 25
arr[0] = arr[0] + (n/25); // Here (n/25) gives
the no of 'Quarters'.Incrementing the no of Quarters
n = n%25; // Updating the n value with mod
operator.It's Nothing but updating the n value with remainder of
n/25.
continue;
}
else if
((n/10)>=1) { // If the 'n' is divisible by 10
arr[1] = arr[1] + (n/10); // Here (n/10) gives
the no of 'Dimes'.Incrementing the no of Dimes
n = n%10; // Updating the n value with mod
operator.It's Nothing but updating the n value with remainder of
n/10.
continue;
}
else if
((n/5)>=1) { // If the 'n' is divisible by 5
arr[2] = arr[2] + (n/5); // Here (n/5) gives the
no of 'Nickels'.Incrementing the no of Nickels
n = n%5; // Updating the n value with mod
operator.It's Nothing but updating the n value with remainder of
n/5.
continue;
}
else{
arr[3]++; // Incrementing the no of cents
n--; // Decrementing the n vlaue.
}
}
System.out.println( arr[0] + "
Quarters\n" + arr[1] + " Dimes\n" + arr[2] + " Nickels\n" + arr[3]
+ " cents\n" );
}
}
Code in editor:

output:

Programming Language is Java Lab 1: Programming Exercises Input/Process/Output 1) Run the Numbers. Write a program...
IN C++ Programming Problem 1: Write a program that calculates a truck's gas mileage and cost for a road trip. Be creative with the output, think of it as a “report”, give it a title at the top and use formatting where appropriate. The program should set the number of gallons of gas that the truck can hold to 31, and the number of miles it can be driving on a full tank to 651. The program should display the...
Write a program called CountCoins.java that prompts the user for the input file name (you can copy the getInputScanner() method given in the ProcessFile assignment) then reads the file. The file contains a series of pairs of tokens, where each pair begins with an integer and is followed by the type of coin, which will be “pennies” (1 cent each), “nickels” (5 cents each), “dimes” (10 cents each), or “quarters” (25 cents each), case-insensitively. Add up the cash values of...
Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes
(In Python 3) Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes So...
Write a program in Java that simulates a vending machine: The vending machine sells three types of food: 1) Potato chips $1.25; 2) Cookies $0.85; 3) Candies $0.95. The program will prompt for the buyer to enter the amount in quarters (25 cents), dimes (10 cents), and nickels (5 cents). The program will then present a selection menu for the foods and prompt the buyer to enter the amount of quarters, dimes and nickels. The machine would then proceed to...
write a program in java that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and a dollar, in a 5-cent increments(25,30,35.....,90,95,or 100), and the machine accepts only a single dollar bill to pay for the item. for example a possible dialogue with the user might be "Enter price of item (from 25 cents to a dollar, in 5-cent increments):45 you boughtan item for 45 cents and gave me...
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...
Write a Python program that does the following: Obtains the following input from a user: Mileage at beginning of measurement period Mileage at end of measurement period Gallons of fuel consumed Calculates and displays: Miles driven Miles per gallon Kilometers driven Liters of fuel consumed Kilometers per liter Incorporate some Selection structure logic into it: If a vehicle gets less than 15 miles per gallon, display the message: "Your vehicle has criminally low fuel efficiency." If it gets 15...
Objectives:
Use the while loop in a program
Use the do-while loop in a second program
Use the for loop in a third program
Instructions:
Part A. Code a for loop to print the Celsius temperatures for
Fahrenheit temperatures 25 to 125. C = 5/9(F – 32).Output should be
in a table format:
Fahrenheit Celsius
25 ?
. .
. . . .
. .
Turn in the source and the output from Part A.
Part B. Code a while...
Write a Python program that does the following: Obtains the following input from a user: Mileage at beginning of measurement period Mileage at end of measurement period Gallons of fuel consumed Calculates and displays: Miles driven Miles per gallon Kilometers driven Liters of fuel consumed Kilometers per liter Also incorporate some selection structure logic into it. If a vehicle gets less than 15 miles per gallon, display the message: "Your vehicle has criminally low fuel efficiency." If it gets...