Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Week 1 Level 1 Array

The document contains Java programs that demonstrate various functionalities such as checking voting eligibility based on age, analyzing numbers for positivity and evenness, generating multiplication tables, calculating the sum of entered numbers, and finding factors of a number. Each program utilizes arrays and loops to process user input and display results. The programs also include error handling for invalid inputs and provide outputs in a structured format.

Uploaded by

bb7431
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week 1 Level 1 Array

The document contains Java programs that demonstrate various functionalities such as checking voting eligibility based on age, analyzing numbers for positivity and evenness, generating multiplication tables, calculating the sum of entered numbers, and finding factors of a number. Each program utilizes arrays and loops to process user input and display results. The programs also include error handling for invalid inputs and provide outputs in a structured format.

Uploaded by

bb7431
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Week 1 Level 1 Array

1.

Import java.util.Scanner;

Public class VotingEligibility {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

Int[] ages = new int[10];

For (int I = 0; I < ages.length; i++) {

System.out.print(“Enter the age of student “ + (I + 1) + “: “);

Ages[i] = scanner.nextInt();

For (int age : ages) {

If (age < 0) {

System.out.println(“Invalid age entered.”);

} else if (age >= 18) {

System.out.println(“The student with the age “ + age + “ can


vote.”);

} else {

System.out.println(“The student with the age “ + age + “ cannot


vote.”);

}
2.

Import java.util.Scanner;

Public class NumberAnalysis {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

Int[] numbers = new int[5];

For (int I = 0; I < numbers.length; i++) {

System.out.print(“Enter number “ + (I + 1) + “: “);

Numbers[i] = scanner.nextInt();

For (int num : numbers) {

If (num > 0) {

If (num % 2 == 0) {

System.out.println(“The number “ + num + “ is positive and


even.”);

} else {

System.out.println(“The number “ + num + “ is positive and


odd.”);

} else if (num < 0) {

System.out.println(“The number “ + num + “ is negative.”);

} else {

System.out.println(“The number is zero.”);

}
}

If (numbers[0] > numbers[4]) {

System.out.println(“The first number is greater than the last


number.”);

} else if (numbers[0] < numbers[4]) {

System.out.println(“The first number is less than the last number.”);

} else {

System.out.println(“The first and last numbers are equal.”);

3.

Import java.util.Scanner;

Public class MultiplicationTable {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter a number: “);

Int number = scanner.nextInt();

Int[] table = new int[10];

For (int I = 0; I < 10; i++) {

Table[i] = number * (I + 1);

For (int I = 0; I < 10; i++) {


System.out.println(number + “ * “ + (I + 1) + “ = “ + table[i]);

4.

Import java.util.Scanner;

Public class ArraySum {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

Double[] numbers = new double[10];

Double total = 0.0;

Int index = 0;

While (true) {

System.out.print(“Enter a number (0 or negative to stop): “);

Double input = scanner.nextDouble();

If (input <= 0 || index == 10) {

Break;

Numbers[index] = input;

Index++;

System.out.println(“Numbers entered:”);

For (int I = 0; I < index; i++) {


System.out.print(numbers[i] + “ “);

Total += numbers[i];

System.out.println(“\nTotal sum: “ + total);

5.

Import java.util.Scanner;

Public class MultiplicationTableRange {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter a number: “);

Int number = scanner.nextInt();

Int[] multiplicationResult = new int[4];

For (int I = 0; I < 4; i++) {

multiplicationResult[i] = number * (I + 6);

For (int I = 0; I < 4; i++) {

System.out.println(number + “ * “ + (I + 6) + “ = “ +
multiplicationResult[i]);

}
6.

Import java.util.Scanner;

Public class MeanHeightCalculator {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

Double[] heights = new double[11];

Double sum = 0.0;

For (int I = 0; I < heights.length; i++) {

System.out.print(“Enter height of player “ + (I + 1) + “: “);

Heights[i] = scanner.nextDouble();

Sum += heights[i];

Double meanHeight = sum / heights.length;

System.out.println(“Mean height of the football team: “ + meanHeight);

7.

Import java.util.Scanner;

Public class OddEvenArrays {


Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter a natural number: “);

Int number = scanner.nextInt();

If (number < 1) {

System.out.println(“Error: Please enter a natural number (greater


than 0).”);

Return;

Int[] evenNumbers = new int[number / 2 + 1];

Int[] oddNumbers = new int[number / 2 + 1];

Int evenIndex = 0, oddIndex = 0;

For (int I = 1; I <= number; i++) {

If (I % 2 == 0) {

evenNumbers[evenIndex++] = I;

} else {

oddNumbers[oddIndex++] = I;

System.out.print(“Even numbers: “);

For (int I = 0; I < evenIndex; i++) {

System.out.print(evenNumbers[i] + “ “);

System.out.println();

System.out.print(“Odd numbers: “);

For (int I = 0; I < oddIndex; i++) {

System.out.print(oddNumbers[i] + “ “);
}

System.out.println();

8.

Import java.util.Scanner;

Public class FactorFinder {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter a number: “);

Int number = scanner.nextInt();

Int maxFactor = 10;

Int[] factors = new int[maxFactor];

Int index = 0;

For (int I = 1; I <= number; i++) {

If (number % I == 0) {

If (index == maxFactor) {

maxFactor *= 2;

int[] temp = new int[maxFactor];

System.arraycopy(factors, 0, temp, 0, factors.length);

Factors = temp;

Factors[index++] = I;
}

System.out.print(“Factors of “ + number + “: “);

For (int I = 0; I < index; i++) {

System.out.print(factors[i] + “ “);

System.out.println();

9.

Import java.util.Scanner;

Public class MatrixTo1DArray {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter the number of rows: “);

Int rows = scanner.nextInt();

System.out.print(“Enter the number of columns: “);

Int cols = scanner.nextInt();

Int[][] matrix = new int[rows][cols];

System.out.println(“Enter matrix elements:”);

For (int I = 0; I < rows; i++) {

For (int j = 0; j < cols; j++) {

System.out.print(“Element [“ + I + “][“ + j + “]: “);


Matrix[i][j] = scanner.nextInt();

Int[] array = new int[rows * cols];

Int index = 0;

For (int I = 0; I < rows; i++) {

For (int j = 0; j < cols; j++) {

Array[index++] = matrix[i][j];

System.out.println(“1D Array representation:”);

For (int num : array) {

System.out.print(num + “ “);

System.out.println();

10.

Import java.util.Scanner;

Public class FizzBuzz {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter a positive integer: “);


Int number = scanner.nextInt();

If (number < 1) {

System.out.println(“Error: Please enter a positive integer.”);

Return;

String[] results = new String[number + 1];

For (int I = 0; I <= number; i++) {

If (I % 3 == 0 && I % 5 == 0) {

Results[i] = “FizzBuzz”;

} else if (I % 3 == 0) {

Results[i] = “Fizz”;

} else if (I % 5 == 0) {

Results[i] = “Buzz”;

} else {

Results[i] = String.valueOf(i);

For (int I = 0; I <= number; i++) {

System.out.println(“Position “ + I + “ = “ + results[i]);

You might also like