S.aneesh Week 1-Level-1 Arrays
S.aneesh Week 1-Level-1 Arrays
import java.util.Scanner;
Output:
Enter the age of student 1: 18
The student with the age 18 can vote.
Enter the age of student 2: 15
The student with the age 15 cannot vote.
Enter the age of student 3: 21
The student with the age 21 can vote.
2
import java.util.Scanner;
public class NumberCheck {
Output:
Enter number 1: 20
20 is positive and even.
Enter number 2: 7
7 is positive and odd.
Enter number 3: 15
15 is positive and odd.
3
import java.util.Scanner;
Output:
Enter a number to print its multiplication table: 7
Multiplication table for 7:
7*1=7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
4
import java.util.Scanner;
// Infinite loop for accepting user input until break condition is met
while (true) {
// Ask user to enter a number
System.out.print("Enter a number: ");
double userInput = scanner.nextDouble();
// If the array has space (index < 10), store the value in the array
if (index < 10) {
numbers[index] = userInput;
index++;
} else {
// If the array is full, break the loop
System.out.println("Array is full, cannot accept more numbers.");
break;
}
}
Output:
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
Enter a number: 6
Enter a number: 67
Enter a number: 8
Enter a number: 9
Enter a number: 1
Enter a number: 1
Array is full, cannot accept more numbers.
Entered numbers:
1.0
2.0
3.0
4.0
5.0
6.0
67.0
8.0
9.0
1.0
Total sum: 106.0
5
import java.util.Scanner;
Output:
Enter a number: 7
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
6
import java.util.Scanner;
Output:
Enter the heights of 11 players:
Player 1 height: 1.80
Player 2 height: 2.00
Player 3 height: 1.78
Player 4 height: 1.23
Player 5 height: 1.45
Player 6 height: 1.87
Player 7 height: 1.59
Player 8 height: 1.69
Player 9 height: 1.76
Player 10 height: 1.83
Player 11 height: 1.67
The mean height of the football team is: 1.6972727272727275
7
import java.util.Scanner;
Output:
Enter a number: 7
Even numbers: 2 4 6
Odd numbers: 1 3 5 7
8
import java.util.Scanner;
Output:
Enter a number: 7
Factors of 7: 1 7
9
import java.util.Scanner;
// Create a 2D array (Matrix) with the given number of rows and columns
int[][] matrix = new int[rows][cols];
Output:
Enter the number of rows: 3
Enter the number of columns: 3
Enter the elements of the matrix:
Element at [0][0]: 1
Element at [0][1]: 1
Element at [0][2]: 2
Element at [1][0]: 3
Element at [1][1]: 4
Element at [1][2]: 5
Element at [2][0]: 3
Element at [2][1]:
6
Element at [2][2]: 5
10
import java.util.Scanner;
Output:
Enter a positive integer: 7
Position 0 = FizzBuzz
Position 1 = 1
Position 2 = 2
Position 3 = Fizz
Position 4 = 4
Position 5 = Buzz
Position 6 = Fizz
Position 7 = 7