Unit 1 Programs JAVA

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

UNIT-1

PART-B
1. Implement a program in Java that interchanges the odd and even
components in an array
public class AraayInterchange {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6};

System.out.println("Original Array:");
for (int num : array) {
System.out.print(num + " ");
}

// Interchange odd and even components


for (int i = 0; i < array.length - 1; i += 2) {
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}

System.out.println("\nArray after interchanging odd and even


components:");
for (int num : array) {
System.out.print(num + " ");
}
}
}

OUTPUT:
Original Array:
123456
Array after interchanging odd and even components:
214365
2. Write a Java program to sort set of names stored in an array in
alphabetical order.
public class SortNamesWithoutMethod {
public static void main(String[] args) {
String[] names = {"John", "Alice", "Bob", "Eve", "Charlie"};

System.out.println("Original Names:");
for (String name : names) {
System.out.println(name);
}

// Sorting the names without using methods


int n = names.length;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (names[i].compareTo(names[j]) > 0) {
// Swap names[i] and names[j]
String temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}

System.out.println("\nNames in Alphabetical Order:");


for (String name : names) {
System.out.println(name);
}
}
}
OUTPUT:
Original Names:
John
Alice
Bob
Eve
Charlie

Names in Alphabetical Order:


Alice
Bob
Charlie
Eve
John
3. Explain how two numbers can be swapped with and without temporary
variables. Write a java program for each
With Temporary Variable:
public class SwapWithTempVariable {
public static void main(String[] args) {
int num1 = 5;
int num2 = 10;

System.out.println("Before swapping: num1 = " + num1 + ", num2 =


" + num2);

int temp = num1;


num1 = num2;
num2 = temp;

System.out.println("After swapping: num1 = " + num1 + ", num2 =


" + num2);
}
}
Without Temporary Variable (Using Arithmetic):
public class SwapWithoutTempVariable {
public static void main(String[] args) {
int num1 = 5;
int num2 = 10;

System.out.println("Before swapping: num1 = " + num1 + ", num2 =


" + num2);

num1 = num1 + num2;


num2 = num1 - num2;
num1 = num1 - num2;

System.out.println("After swapping: num1 = " + num1 + ", num2 =


" + num2);
}
}
OUTPUT:
Before swapping: num1 = 5, num2 = 10
After swapping: num1 = 10, num2 = 5
4. Consider the following series of number:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc.
Write a Java program to print the above series by getting the numbers of
values to be printed from the user
import java.util.Scanner;

public class FibonacciSeries {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of terms in the Fibonacci series: ");


int n = scanner.nextInt();

int first = 0, second = 1;

System.out.println("Fibonacci Series:");

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


System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}

scanner.close();
}
}
OUTPUT:
Enter the number of terms in the Fibonacci series: 6
Fibonacci Series:
011235
5. Create a simple Java program to implement basic Calculator operations
import java.util.Scanner;

public class BasicCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Basic Calculator");
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);

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


double num2 = scanner.nextDouble();

double result = 0;

switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error: Division by zero");
System.exit(1);
}
break;
default:
System.out.println("Error: Invalid operator");
System.exit(1);
}

System.out.println("Result: " + result);

scanner.close();
}
}
OUTPUT:
Basic Calculator
Enter first number: 10
Enter operator (+, -, *, /): *
Enter second number: 5
Result: 50.0
PART-C
1. How would you find a given two one dimensional arrays A and B which
are sorted in ascending order. Write a Java program to merge them into a
single sorted array, see that is contains every item from array A and B, in
ascending order.
public class MergeSortedArrays {
public static void main(String[] args) {
int[] A = {1, 3, 5, 7, 9};
int[] B = {2, 4, 6, 8, 10};

int[] mergedArray = mergeArrays(A, B);

System.out.println("Merged and Sorted Array:");


for (int num : mergedArray) {
System.out.print(num + " ");
}
}

public static int[] mergeArrays(int[] A, int[] B) {


int m = A.length;
int n = B.length;
int[] mergedArray = new int[m + n];

int i = 0, j = 0, k = 0;

// Merge A and B into mergedArray in ascending order


while (i < m && j < n) {
if (A[i] < B[j]) {
mergedArray[k++] = A[i++];
} else {
mergedArray[k++] = B[j++];
}
}

// If there are remaining elements in A or B, copy them to


mergedArray
while (i < m) {
mergedArray[k++] = A[i++];
}

while (j < n) {
mergedArray[k++] = B[j++];
}

return mergedArray;
}
}
OUTPUT:
Merged and Sorted Array:
1 2 3 4 5 6 7 8 9 10
2. i. Can you make the keyword public to private in main() method? Justify
(3)
ii. Write a java program using array to perform
Sum of all values in an array (6)
Find the greatest number in an array(6)
i.
 In Java, the main() method is typically declared as public
static void main(String[] args), and it must be public because
it serves as the entry point for the Java application. When the
Java Virtual Machine (JVM) starts an application, it looks
for the public static void main(String[] args) method to begin
execution. Making it public allows the JVM to access and
execute this method.
 Changing the access modifier from public to private in the
main() method would result in a compilation error, and the
program would not execute. It's necessary for the main()
method to be accessible from outside the class to start the
program.
ii. Calculate Sum of Values in an Array
public class SumOfArrayValues {
public static void main(String[] args) {
int[] numbers = {10, 7, 15, 23, 4, 17, 9};

int sum = 0;

for (int num : numbers) {


sum += num;
}

System.out.println("Array: " +
java.util.Arrays.toString(numbers));
System.out.println("Sum of all values: " + sum);
}
}
OUTPUT:
Array: [10, 7, 15, 23, 4, 17, 9]
Sum of all values: 85

Find Greatest Number in an Array


java
public class FindGreatestNumberInArray {
public static void main(String[] args) {
int[] numbers = {10, 7, 15, 23, 4, 17, 9};

int max = numbers[0]; // Assume the first element as the


maximum

for (int num : numbers) {


if (num > max) {
max = num;
}
}

System.out.println("Array: " +
java.util.Arrays.toString(numbers));
System.out.println("Greatest number: " + max);
}
}
OUTPUT:
Array: [10, 7, 15, 23, 4, 17, 9]
Greatest number: 23
3. Develop a Java Program to find
i) Largest of three numbers using Ternary Operator (8)
ii) Smallest of three numbers using Ternary Operator (7)
Largest of Three Numbers using Ternary Operator:
public class LargestOfThreeNumbers {
public static void main(String[] args) {
int num1 = 25;
int num2 = 10;
int num3 = 35;

int largest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) :
((num2 > num3) ? num2 : num3);

System.out.println("Largest of three numbers: " + largest);


}
}
Smallest of Three Numbers using Ternary Operator:
public class SmallestOfThreeNumbers {
public static void main(String[] args) {
int num1 = 25;
int num2 = 10;
int num3 = 35;

int smallest = (num1 < num2) ? ((num1 < num3) ? num1 : num3) :
((num2 < num3) ? num2 : num3);

System.out.println("Smallest of three numbers: " + smallest);


}
}
4. Develop a Java program to design the following pattern using any control
flow statements
*
**
***
****
*****

public class PyramidPattern {


public static void main(String[] args) {
int rows = 5; // Change the number of rows as needed

// Outer loop for the number of rows


for (int i = 1; i <= rows; i++) {
// Inner loop to print spaces before asterisks
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}

// Inner loop to print asterisks


for (int k = 1; k <= i; k++) {
System.out.print("* ");
}

// Move to the next line after printing each row


System.out.println();
}
}
}

OUTPUT:
*
**
***
****
*****

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy