Java assignmant #4
System.out.println(“”);
First task:
Scanner reader = new Scanner(System.in);
for(int i = 1; i < 10; i++)
System.out.println(i);
Second task:
Scanner reader = new Scanner(System.in);
for(char i = 'a'; i <= 'z'; i++)
System.out.println(i);
Third task:
Scanner reader = new Scanner(System.in);
for (int i = 1; i <= 50; i++)
System.out.println("Please enter a three digit number");
int num = reader.nextInt();
int ones = num % 10;
int tens = (num / 10) % 10;
int hundreds = num / 100;
if (ones != tens && ones != hundreds && tens != hundreds)
System.out.println(num);
Fourth task:
First part:
Scanner reader = new Scanner(System.in);
int num1, num2;
System.out.println("Please enter the first number");
num1 = reader.nextInt();
System.out.println("Please enter the second number");
num2 = reader.nextInt();
for (int i = num1; i <= num2; i++)
System.out.println(i + " ");
Second part:
Scanner reader = new Scanner(System.in);
int num1, num2;
System.out.println("Please enter the first number");
num1 = reader.nextInt();
System.out.println("Please enter the second number");
num2 = reader.nextInt();
if (num1 < num2)
for (int i = num1; i <= num2; i++)
System.out.println(i + " ");
else
for (int i = num2; i <= num1; i++)
System.out.println(i + " ");
}
Fifth task:
Scanner reader = new Scanner(System.in);
for (int i = 1; i <= 25; i++)
System.out.println("Please enter a two digit number");
int num = reader.nextInt();
int ones = num%10;
int tens = num/10;
if (ones%2 == 0 && tens%2 == 0)
System.out.println("The two digits are even");
else
if (ones%2 != 0 && tens%2 != 0)
System.out.println("Both digits are uneven");
else
if (tens%2 != 0 && ones%2 == 0)
System.out.println("The first digit is uneven and the second digit is even");
else
System.out.println("The first digit is even and the second digit is uneven");
}
Sixth task:
Scanner reader = new Scanner(System.in);
int num;
System.out.println("Enter a number between 10 and 20: ");
num = reader.nextInt();
System.out.println("Numbers close to your number are: ");
for (int i = 0; i < 30; i++);
int random = (int) (Math.random() * 11) + 10;
if (Math.abs(random - num) <= 4)
System.out.print(random + " ");
Seventh task:
Scanner reader = new Scanner(System.in);
int math, physics, science, average;
boolean passed = false;
for(int i = 1; i <= 58; i++)
System.out.println("Please enter your grades in math");
math = reader.nextInt();
if (math < 90)
System.out.println("You cannot advance to stage b");
else
System.out.println("Please enter your physics grade");
physics = reader.nextInt();
if (physics < 90)
{
System.out.println("You cannot advance to stage b");
else
System.out.println("Please enter your science grade");
science = reader.nextInt();
if (science < 90)
System.out.println("You cannot advance to stage b");
else
average = (math + physics + science) / 3;
if (average < 92)
System.out.println("You cannot advance to stage b");
else
System.out.println(i + " " + average);
Eighth task:
Scanner reader = new Scanner(System.in);
int counter = 0;
int sum = 0;
for (int i = 1; i <= 3; i++) {
System.out.println("Please enter a number");
int num = reader.nextInt();
sum += num;
int average = sum / 3;
System.out.println("The average of the three numbers is: " + average);
if (average < 0) {
counter += 1;
Ninth task:
Scanner scanner = new Scanner(System.in);
int total = 20;
int both = 0;
int one = 0;
int none = 0;
for (int i = 1; i <= total; i++) {
System.out.println("Enter the grade for the first submission for student " + i + ":");
int grade1 = scanner.nextInt();
System.out.println("Enter the grade for the second submission for student " + i + ":");
int grade2 = scanner.nextInt();
if (grade1 != -1 && grade2 != -1)
both++;
else if (grade1 != -1 || grade2 != -1)
one++;
}
else
none++;
System.out.println("Number of students who submitted both assignments: " + both);
System.out.println("Number of students who submitted only one assignment: " + one);
System.out.println("Number of students who did not submit any assignments: " + none);
Tenth task:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of examinees: ");
int totalExaminees = scanner.nextInt();
int passedExaminees = 0;
for (int i = 1; i <= totalExaminees; i++)
System.out.print("Enter the number of mistakes in temperament for examinee" + i +);
int mistakesTemperament = scanner.nextInt();
System.out.print("Enter the number of mistakes in other questions for examinee" + i);
int mistakesOtherQuestions = scanner.nextInt();
if (mistakesTemperament <= 0 && mistakesOtherQuestions <= 3)
passedExaminees+=1;
double passPercentage = (double) passedExaminees / totalExaminees * 100;
System.out.println("Number of examinees who passed the exam: " + passedExaminees);
System.out.println("Pass percentage: " + passPercentage + "%");
Eleventh task:
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[30];
int SOETDN = 0;
for (int i = 0; i < 30; i++) {
System.out.print("Enter an integer for number " + (i + 1) + ": ");
numbers[i] = scanner.nextInt();
if (numbers[i] > 9 && numbers[i] < 100 && numbers[i] % 2 == 0) {
SOETDN += numbers[i];
System.out.println("The sum of even two-digit numbers is: " + SOETDN);
Twelve task:
Scanner scanner = new Scanner(System.in);
int count = 0;
double sum = 0;
double num;
System.out.println("Enter positive numbers");
while (scanner.hasNextDouble()) {
num = scanner.nextDouble();
if (num >= 0) {
sum += num;
count++;
} else {
break;
if (count == 0) {
System.out.println("No valid numbers entered. Cannot calculate median.");
return;
double[] numbers = new double[count];
scanner = new Scanner(System.in); // Reset the scanner to read input again
for (int i = 0; i < count; i++) {
numbers[i] = scanner.nextDouble();
}
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
double temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
double median;
if (count % 2 == 0) {
median = (numbers[count / 2 - 1] + numbers[count / 2]) / 2.0;
} else {
median = numbers[count / 2];
System.out.println("The median is: " + median);