0% found this document useful (0 votes)
11 views

S.aneesh Week 1-Level-1 Arrays

The document contains multiple Java programs that demonstrate various functionalities such as checking voting eligibility based on age, determining positive or negative numbers, generating multiplication tables, summing an array of numbers, and calculating factors of a number. Each program includes user input handling and outputs results based on the provided data. The examples illustrate basic programming concepts like loops, conditionals, and array manipulation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

S.aneesh Week 1-Level-1 Arrays

The document contains multiple Java programs that demonstrate various functionalities such as checking voting eligibility based on age, determining positive or negative numbers, generating multiplication tables, summing an array of numbers, and calculating factors of a number. Each program includes user input handling and outputs results based on the provided data. The examples illustrate basic programming concepts like loops, conditionals, and array manipulation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1

import java.util.Scanner;

public class VotingEligibility {

public static void main(String[] args) {


// Create an array to store the age of 10 students
int[] studentAges = new int[10];

// Create a scanner object to take input from the user


Scanner scanner = new Scanner(System.in);

// Loop to take input for each student's age


for (int i = 0; i < studentAges.length; i++) {
System.out.print("Enter the age of student " + (i + 1) + ": ");
studentAges[i] = scanner.nextInt();

// Check for invalid age input


if (studentAges[i] < 0) {
System.out.println("Invalid age! Age cannot be negative.");
} else if (studentAges[i] >= 18) {
System.out.println("The student with the age " + studentAges[i] + " can vote.");
} else {
System.out.println("The student with the age " + studentAges[i] + " cannot vote.");
}
}

// Close the scanner object


scanner.close();
}
}

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 {

public static void main(String[] args) {


// Create an array of 5 elements to store the numbers
int[] numbers = new int[5];

// Create a scanner object to take input from the user


Scanner scanner = new Scanner(System.in);

// Loop to take input for each number


for (int i = 0; i < numbers.length; i++) {
System.out.print("Enter number " + (i + 1) + ": ");
numbers[i] = scanner.nextInt();

// Check if the number is positive, negative, or zero


if (numbers[i] > 0) {
// Further check if the positive number is even or odd
if (numbers[i] % 2 == 0) {
System.out.println(numbers[i] + " is positive and even.");
} else {
System.out.println(numbers[i] + " is positive and odd.");
}
} else if (numbers[i] < 0) {
System.out.println(numbers[i] + " is negative.");
} else {
System.out.println(numbers[i] + " is zero.");
}
}

// Compare the first and last elements of the array


if (numbers[0] == numbers[4]) {
System.out.println("The first and last numbers are equal.");
} else if (numbers[0] > numbers[4]) {
System.out.println("The first number is greater than the last number.");
} else {
System.out.println("The first number is less than the last number.");
}

// Close the scanner object


scanner.close();
}
}

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;

public class MultiplicationTable {

public static void main(String[] args) {


// Create a scanner object to take input from the user
Scanner scanner = new Scanner(System.in);

// Get the number for the multiplication table


System.out.print("Enter a number to print its multiplication table: ");
int number = scanner.nextInt();

// Define an integer array to store the results of multiplication


int[] multiplicationTable = new int[10];

// Loop to calculate and store multiplication results from 1 to 10


for (int i = 1; i <= 10; i++) {
multiplicationTable[i - 1] = number * i;
}

// Display the multiplication table


System.out.println("Multiplication table for " + number + ":");
for (int i = 0; i < multiplicationTable.length; i++) {
System.out.println(number + " * " + (i + 1) + " = " + multiplicationTable[i]);
}

// Close the scanner object


scanner.close();
}
}

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;

public class ArraySum {


public static void main(String[] args) {
// Declare an array to store up to 10 double values
double[] numbers = new double[10];

// Variable to store the total sum, initialized to 0.0


double total = 0.0;

// Variable to keep track of the number of entries


int index = 0;

// Scanner object to read input from user


Scanner scanner = new Scanner(System.in);

// 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 user enters 0 or a negative number, break the loop


if (userInput <= 0) {
break;
}

// 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;
}
}

// Display all the numbers in the array


System.out.println("Entered numbers:");
for (int i = 0; i < index; i++) {
System.out.println(numbers[i]);
total += numbers[i]; // Add each number to the total
}

// Display the total sum of all the numbers


System.out.println("Total sum: " + total);

// Close the scanner to avoid resource leak


scanner.close();
}
}

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;

public class MultiplicationTable {


public static void main(String[] args) {
// Scanner object to read user input
Scanner scanner = new Scanner(System.in);

// Taking integer input for the number


System.out.print("Enter a number: ");
int number = scanner.nextInt();

// Define an array to store multiplication results


int[] multiplicationResult = new int[4];

// Loop through numbers 6 to 9 and calculate multiplication table


for (int i = 6; i <= 9; i++) {
multiplicationResult[i - 6] = number * i; // Store result in the array
}

// Display the results in the format number * i = result


for (int i = 6; i <= 9; i++) {
System.out.println(number + " * " + i + " = " + multiplicationResult[i - 6]);
}

// Close the scanner to avoid resource leaks


scanner.close();
}
}

Output:
Enter a number: 7
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63

6
import java.util.Scanner;

public class FootballTeamHeight {


public static void main(String[] args) {
// Create an array to store the heights of 11 players
double[] heights = new double[11];
// Initialize a variable to store the sum of heights
double sum = 0.0;

// Scanner object to take input from the user


Scanner scanner = new Scanner(System.in);

// Get the heights of the players


System.out.println("Enter the heights of 11 players:");
for (int i = 0; i < 11; i++) {
System.out.print("Player " + (i + 1) + " height: ");
heights[i] = scanner.nextDouble();
sum += heights[i]; // Add each player's height to the sum
}

// Calculate the mean height


double mean = sum / 11;

// Display the mean height


System.out.println("The mean height of the football team is: " + mean);

// Close the scanner to prevent resource leak


scanner.close();
}
}

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;

public class OddEvenArrays {


public static void main(String[] args) {
// Create a scanner to take input from the user
Scanner scanner = new Scanner(System.in);

// Prompt the user for input


System.out.print("Enter a number: ");
int number = scanner.nextInt();

// Check if the input is a natural number


if (number <= 0) {
System.out.println("Error: Please enter a natural number greater than 0.");
scanner.close();
return;
}

// Create arrays for odd and even numbers


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

// Initialize index variables for odd and even numbers


int evenIndex = 0;
int oddIndex = 0;

// Use a for loop to iterate from 1 to the number


for (int i = 1; i <= number; i++) {
if (i % 2 == 0) {
evenNumbers[evenIndex++] = i; // If the number is even, store it in evenNumbers
array
} else {
oddNumbers[oddIndex++] = i; // If the number is odd, store it in oddNumbers array
}
}

// Print the even numbers array


System.out.print("Even numbers: ");
for (int i = 0; i < evenIndex; i++) {
System.out.print(evenNumbers[i] + " ");
}
System.out.println(); // For a newline after even numbers
// Print the odd numbers array
System.out.print("Odd numbers: ");
for (int i = 0; i < oddIndex; i++) {
System.out.print(oddNumbers[i] + " ");
}
System.out.println(); // For a newline after odd numbers

// Close the scanner


scanner.close();
}
}

Output:
Enter a number: 7
Even numbers: 2 4 6
Odd numbers: 1 3 5 7

8
import java.util.Scanner;

public class FactorsOfNumber {


public static void main(String[] args) {
// Create a scanner object to take input from the user
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter a number


System.out.print("Enter a number: ");
int number = scanner.nextInt();

// Initialize the maxFactor and factors array


int maxFactor = 10;
int[] factors = new int[maxFactor];
int index = 0;

// Find the factors of the number


for (int i = 1; i <= number; i++) {
if (number % i == 0) {
// If i is a factor, store it in the array
if (index == maxFactor) {
// Resize the array if needed
maxFactor *= 2;
int[] temp = new int[maxFactor];
System.arraycopy(factors, 0, temp, 0, factors.length);
factors = temp;
}
factors[index++] = i;
}
}

// Display the factors


System.out.print("Factors of " + number + ": ");
for (int i = 0; i < index; i++) {
System.out.print(factors[i] + " ");
}
System.out.println(); // Print a newline after the factors

// Close the scanner


scanner.close();
}
}

Output:
Enter a number: 7
Factors of 7: 1 7

9
import java.util.Scanner;

public class Copy2DArrayTo1D {


public static void main(String[] args) {
// Create a scanner object to take input from the user
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter the number of rows and columns


System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int cols = scanner.nextInt();

// Create a 2D array (Matrix) with the given number of rows and columns
int[][] matrix = new int[rows][cols];

// Take input for the 2D array elements


System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element at [" + i + "][" + j + "]: ");
matrix[i][j] = scanner.nextInt();
}
}

// Create a 1D array to store the elements of the 2D array


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

// Index variable to insert elements into the 1D array


int index = 0;

// Copy the elements of the 2D array into the 1D array


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[index++] = matrix[i][j]; // Copy each element into the 1D array and increment
index
}
}

// Display the original 2D array


System.out.println("\nThe original 2D array (Matrix) is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}

// Display the 1D array with the copied elements


System.out.print("\nThe 1D array with copied elements is: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println(); // Print a newline at the end

// Close the scanner


scanner.close();
}
}

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

The original 2D array (Matrix) is:


112
345
365

10
import java.util.Scanner;

public class FizzBuzz {


public static void main(String[] args) {
// Create a scanner object to take input from the user
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter a number


System.out.print("Enter a positive integer: ");
int number = scanner.nextInt();

// Check if the entered number is positive


if (number <= 0) {
System.out.println("Please enter a positive integer.");
scanner.close();
return;
}

// Create a String array to store results of FizzBuzz


String[] results = new String[number + 1]; // +1 to include the number itself in the array

// Loop from 0 to the entered number


for (int i = 0; i <= number; i++) {
if (i % 3 == 0 && i % 5 == 0) {
results[i] = "FizzBuzz"; // If multiple of both 3 and 5
} else if (i % 3 == 0) {
results[i] = "Fizz"; // If multiple of 3
} else if (i % 5 == 0) {
results[i] = "Buzz"; // If multiple of 5
} else {
results[i] = Integer.toString(i); // Otherwise, save the number itself
}
}

// Display the results in the format: Position X = value


for (int i = 0; i <= number; i++) {
System.out.println("Position " + i + " = " + results[i]);
}

// Close the scanner


scanner.close();
}
}

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

You might also like