Taking Averages Exercise
Additional Exercises
import java.util.Scanner; //allows scanners to be in the program
public class Main {
public static void main(String[] args) {
double num = 0; //keeps count num as the total
double x = 0; //x starts as 1 when user inputs one number keeps count
Scanner sc = new Scanner(System.in); //creates a scanner to allow for input
System.out.println("You must have an even number of cars! How many do you want to own? "); //asks user for input
int n = sc.nextInt(); //takes in integers
while(n % 2 == 0) { //while loop as long as asking integer is less than needed integer
System.out.println("# of Cars " + "("+ (int) x + ")"+":");
//takes in integers and goes to next one (sums up nums)
x += n; //adds 1 to x so once it reaches x=n it will exit loop
}
System.out.println("Number of Cars: " + x); //prints out the output/average
}}
Main.main(null);
import java.util.Scanner; //allows scanners to be in program
public class Main {
public static void main(String[] args) {
}}
Scanner in = new Scanner(System.in); //creates a scanner
System.out.print("Input 1st number: "); //asks user for input
int num1 = in.nextInt(); //saves number and goes to next int
System.out.print("Input 2nd number: "); //same as above
int num2 = in.nextInt();
System.out.println("The average of those numbers are " + (num1 + num2)/(2));
//prints out
//num you want to divide is however many numbers you want to average out