You are on page 1of 9

Question 1

import java.util.Scanner;
public class L3Q1 {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int num = 0;
int positive = 0;
int negative = 0;
int total = 0;
int count = 0;
double average;

System.out.print("Enter an int value, the program exits if the input is


0: ");
num = input.nextInt();

while (num != 0) {
total += num;
count++;
if (num > 0) {
positive++;
} else{
negative++;
}
num = input.nextInt();
}

average = total / count;

System.out.println("The number of positive is " + positive +


"\nThe number of negatives is " + negative +
"\nThe total is total " + total +
"\nThe average is " + average);
}

Output
Question 2
import java.util.Scanner;
public class L3Q2 {

public static void main(String[] args) {


final double KILOGRAM_POUND = 2.2;

System.out.println("Kilograms Pounds");

for (int i = 1; i <= 199; i += 2) {


System.out.printf("%-15d%6.1f\n", i, (i * KILOGRAM_POUND));
}
}

Output
Question 3

public class L3Q3 {

public static void main(String[] args) {


int totalCost = 0;
int tuition = 10000;
int TenYear = 0;

for (int year = 1; year <= 14; year++) {

tuition += (tuition * 0.05);

if (year > 10) {


totalCost += tuition;
}

if (year == 10) {
TenYear = tuition;
}
}

System.out.printf("Total tuition in ten years: $" + TenYear);

System.out.printf("\nTotal cost for four years' worth of tuition after


the tenth year: $" + totalCost);
}

Output
Question 4
import java.util.Scanner;
public class L3Q4 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int highestScore = 0;
String highestScoreName = "";

System.out.print("Enter the number of students: ");


int numberOfStudents = input.nextInt();

System.out.println("Enter each student’s name and score");


for (int i = 0; i < numberOfStudents; i++) {

System.out.print("Student: " + (i + 1) + "\n Name:


");
String name = input.next();

System.out.print(" Score: ");


int score = input.nextInt();

if (score > highestScore)


{
highestScore = score;
highestScoreName = name;
}
}
System.out.println("Student with the highest score: " +
highestScoreName);

Output
Question 5
import java.util.Scanner;
public class L3Q5 {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);

System.out.print("Enter the number of students: ");


int numberOfStudents = input.nextInt();

int score, highest = 0, highest2 = 0;


String name = "",
student1 = "",
student2 = "";

System.out.println("Enter each students' name and score:");


for (int i = 0; i < numberOfStudents; i++) {
System.out.print(
"Student: " + (i + 1) + "\n Name: ");
name = input.next();
System.out.print(" Score: ");
score = input.nextInt();

if (i == 0) {
highest = score;
student1 = name;
}

else if (i == 1 && score > highest) {


highest2 = highest;
highest = score;
student2 = student1;
student1 = name;
}

else if (i == 1) {
highest2 = score;
student2 = name;
}

else if (i > 1 && score > highest && score > highest2) {
highest2 = highest;
student2 = student1;
highest = score;
student1 = name;
}
else if (i > 1 && score > highest2) {
student2 = name;
highest2 = score;
}
}

System.out.println("The Higest Score of the student: " + student1 +


"\nThe Second Higest Score of the Student: " + student2);
}

Output
Question 6

public class L3Q6 {

public static void main(String[] args) {


final int NUMBERS_LINE = 10;
int count = 0;

for (int i = 100; i <= 200; i++) {


if (i % 5 == 0 ^ i % 6 == 0) {
count++;

if (count % NUMBERS_LINE == 0)
System.out.println(i);

else
System.out.print(i + " ");
}
}
System.out.println();
}

Output
Question 7

public class L3Q7 {

public static void main(String[] args) {


final double COMMISSION_SOUGHT = 30000;
double salesAmount = 0;
double commission, balance;

do {
balance = commission = 0;
salesAmount += 0.01;

if (salesAmount > 10000)


commission += (balance = salesAmount - 10000) * 0.12;

if (salesAmount > 5000)


commission += (balance -= balance - 5000) * 0.10;

if (salesAmount > 0)
commission += balance * 0.08;

} while (commission < COMMISSION_SOUGHT);

System.out.printf("Minimum sales to earn $30,000: $%.0f\n",


salesAmount);

}
}

Output
Question 8import java.util.Scanner;
public class L3Q8 {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
int largest = 0;
int occurrence = 0;
int number;

System.out.print("Enter number: ");


do {
number = input.nextInt();
if (number > largest) {
occurrence = 0;
largest = number;
}
if (number == largest) {
occurrence++;
}

} while (number != 0);

System.out.println("The largest number is " + largest);


System.out.println("The occurrence count is " + occurrence);

Output

You might also like