0% found this document useful (0 votes)
13 views22 pages

Gurleen Java

The document is a Java practical file containing various programming exercises submitted by Gurleen Kaur for the BCA 5th semester. It includes programs for basic operations, area calculations, temperature conversion, quadratic equations, and array manipulations among others. Each program is accompanied by code snippets demonstrating the implementation of the specified tasks.

Uploaded by

gurleenkaur12410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views22 pages

Gurleen Java

The document is a Java practical file containing various programming exercises submitted by Gurleen Kaur for the BCA 5th semester. It includes programs for basic operations, area calculations, temperature conversion, quadratic equations, and array manipulations among others. Each program is accompanied by code snippets demonstrating the implementation of the specified tasks.

Uploaded by

gurleenkaur12410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

PCTE GROUP OF INSTITUTES

BCA 5th Semester

Java Practical File


Submitted by-Gurleen kaur
Roll No- 2213591
Submitted to- Ms. Sonali Pruthi
INDEX

1. Write a program to perform following operations on two numbers input by


the user: 1) Addition 2) subtraction 3) multiplication 4) division

2. Write a Java program to print result of the following operations. 1. -15


+58 * 45 2. (35+8) % 6 3. 24 + -5*3 / 7 4. 15 + 18 / 3 * 2 - 9 % 3

3. Write a Java program to compute area of: 1) Circle2) rectangle 3) triangle


4) square

4. Write a program to convert temperature from Fahrenheit to Celsius degree


using Java.

5. Write a program through Java that reads a number in inches, converts it to


meters.

6. Design a program in Java to solve quadratic equations using if, if else

7. Write a Java program to determine greatest number of three numbers.

8. Write program that gets a number from the user and generates an integer between
1 and 7 subsequently should display the name of the weekday as per that number

9. Construct a Java program to find the number of days in a month.


10. Write a program to sum values of a Single Dimensional array.

11. Design & execute a program in Java to sort a numeric array and a string array.

12. Calculate the average value of array elements through Java Program.

13. Write a Java program to test if an array contains a specific value.


1.Write a program to perform following operations on two numbers input by the user: 1) Addition 2)
subtraction 3) multiplication 4) division
import java.util.Scanner;

public class BasicOperations {


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

System.out.print("Enter the first number: ");


double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();

// Addition
double sum = num1 + num2;
System.out.println("Addition: " + sum);

// Subtraction
double difference = num1 - num2;
System.out.println("Subtraction: " + difference);

// Multiplication
double product = num1 * num2;
System.out.println("Multiplication: " + product);

// Division
if (num2 != 0) {
double quotient = num1 / num2;
System.out.println("Division: " + quotient);
} else {
System.out.println("Error: Division by zero is not allowed.");
}

scanner.close();
System.out.println("BY GURLEEN");
}

}
2.Write a Java program to print result of the following operations. 1. -15 +58 * 45 2. (35+8) % 6 3. 24 + -5*3 /
7 4. 15 + 18 / 3 * 2 - 9 % 3
public class OperationsResults {
public static void main(String[] args) {
System.out.println("-15 + 58 * 45 = " + (-15 + 58 * 45));
System.out.println("(35 + 8) % 6 = " + ((35 + 8) % 6));
System.out.println("24 + (-5 * 3) / 7 = " + (24 + (-5 * 3) / 7));
System.out.println("15 + 18 / 3 * 2 - 9 % 3 = " + (15 + 18 / 3 * 2 - 9 % 3)); System.out.println("by gurleen");
}
}
3.Write a Java program to compute area of: 1) Circle2) rectangle 3) triangle 4) square
import java.util.Scanner;

public class AreaCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Circle

System.out.print("Enter radius of the circle: ");

double radius = scanner.nextDouble();

double circleArea = Math.PI * radius * radius;

System.out.println("Area of the circle: " + circleArea);

// Rectangle

System.out.print("Enter width of the rectangle: ");

double width = scanner.nextDouble();

System.out.print("Enter height of the rectangle: ");

double height = scanner.nextDouble();

double rectangleArea = width * height;

System.out.println("Area of the rectangle: " + rectangleArea);

// Triangle

System.out.print("Enter base of the triangle: ");

double base = scanner.nextDouble();

System.out.print("Enter height of the triangle: ");

double triangleHeight = scanner.nextDouble();

double triangleArea = 0.5 * base * triangleHeight;

System.out.println("Area of the triangle: " + triangleArea);

// Square

System.out.print("Enter side length of the square: ");

double side = scanner.nextDouble();

double squareArea = side * side;

System.out.println("Area of the square: " + squareArea);

scanner.close();

}
4.Write a program to convert temperature from Fahrenheit to Celsius degree using Java.
import java.util.Scanner;

public class FahrenheitToCelsius {


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

// Prompt the user to enter temperature in Fahrenheit


System.out.print("Enter temperature in Fahrenheit: ");
double fahrenheit = scanner.nextDouble();

// Convert Fahrenheit to Celsius


double celsius = (fahrenheit - 32) * 5 / 9;

// Print the result


System.out.println("Temperature in Celsius: " + celsius);

// Extra line as requested


System.out.println("by gurleen");

// Close the scanner


scanner.close();
}
}
5. Write a program through Java that reads a number in inches, converts it to meters.
import java.util.Scanner;

public class InchesToMeters {


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

System.out.print("Enter length in inches: ");


double inches = scanner.nextDouble();
double meters = inches * 0.0254;
System.out.println("Length in meters: " + meters);

scanner.close();
}
}
6. Design a program in Java to solve quadratic equations using if, if else
import java.util.Scanner;

public class QuadraticSolver {


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

System.out.print("Enter coefficient a: ");


double a = scanner.nextDouble();
System.out.print("Enter coefficient b: ");
double b = scanner.nextDouble();
System.out.print("Enter coefficient c: ");
double c = scanner.nextDouble();

double discriminant = b * b - 4 * a * c;

if (discriminant > 0) {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("Roots are: " + root1 + " and " + root2); } else if (discriminant == 0) {
double root = -b / (2 * a);
System.out.println("Root is: " + root);
} else {
System.out.println("No real roots.");
}

scanner.close();
}
}
7. Write a Java program to determine greatest number

of three numbers.

import java.util.Scanner;

public class GreatestOfThree {


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

System.out.print("Enter the first number: ");


int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
System.out.print("Enter the third number: ");
int num3 = scanner.nextInt();

if (num1 >= num2 && num1 >= num3) {


System.out.println("Greatest number is: " + num1);
} else if (num2 >= num1 && num2 >= num3) {
System.out.println("Greatest number is: " + num2);
} else {
System.out.println("Greatest number is: " + num3);
}
System.out.println("By gurleen");
scanner.close();
}
}
8. Write program that gets a number from the user and generates an integer between 1 and 7 subsequently should
display the name of the weekday as per that number

import java.util.Scanner;

public class WeekdayGenerator {


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

System.out.print("Enter a number (1-7): ");


int day = scanner.nextInt();

switch (day) {
case 1: System.out.println("Sunday"); break;
case 2: System.out.println("Monday"); break;
case 3: System.out.println("Tuesday"); break;
case 4: System.out.println("Wednesday"); break;
case 5: System.out.println("Thursday"); break;
case 6: System.out.println("Friday"); break;
case 7: System.out.println("Saturday"); break;
default: System.out.println("Invalid number. Please enter a number between 1 and 7.");
}
System.out.println("By gurleen");
scanner.close();
}
}
9. Construct a Java program to find the number of days in a month.

import java.util.Scanner;

public class DaysInMonth {


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

System.out.print("Enter month number (1-12): ");


int month = scanner.nextInt();

System.out.print("Enter year: ");


int year = scanner.nextInt();

int days;

switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
case 2:
// Check for leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { days = 29;
} else {
days = 28;
}
break;
default:
days = 0; // Invalid month
}

if (days > 0) {
System.out.println("Number of days: " + days);
} else {
System.out.println("Invalid month.");
}
System.out.println("By gurleen");
scanner.close();
}
}
10. Write a program to sum values of a Single Dimensional array.
import java.util.Scanner;

public class ArraySum {


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

System.out.print("Enter the size of the array: ");


int size = scanner.nextInt();
int[] array = new int[size];

System.out.println("Enter " + size + " integers:");


for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}

int sum = 0;
for (int i = 0; i < size; i++) {
sum += array[i];
}

System.out.println("Sum of array elements: " + sum);


System.out.println("By gurleen");
scanner.close();
}
}
11. Design &amp; execute a program in Java to sort a numeric array and a string array.
import java.util.Arrays;
import java.util.Scanner;

public class ArraySorting {


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

// Numeric Array
System.out.print("Enter the size of the numeric array: ");
int size = scanner.nextInt();
int[] numArray = new int[size];

System.out.println("Enter " + size + " integers:");


for (int i = 0; i < size; i++) {
numArray[i] = scanner.nextInt();
}

Arrays.sort(numArray);
System.out.println("Sorted numeric array: " + Arrays.toString(numArray));

// String Array
System.out.print("Enter the size of the string array: ");
int strSize = scanner.nextInt();
scanner.nextLine(); // Consume newline
String[] strArray = new String[strSize];

System.out.println("Enter " + strSize + " strings:");


for (int i = 0; i < strSize; i++) {
strArray[i] = scanner.nextLine();
}

Arrays.sort(strArray);
System.out.println("Sorted string array: " + Arrays.toString(strArray)); System.out.println("By gurleen");
scanner.close();
}
}
12. Calculate the average value of array elements through Java Program.
import java.util.Scanner;

public class ArrayAverage {


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

System.out.print("Enter the size of the array: ");


int size = scanner.nextInt();
int[] array = new int[size];

System.out.println("Enter " + size + " integers:");


for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}

int sum = 0;
for (int i = 0; i < size; i++) {
sum += array[i];
}

double average = (double) sum / size;


System.out.println("Average value of array elements: " + average); System.out.println("By gurleen");
scanner.close();
}
}
13. Write a Java program to test if an array contains a specific value.
import java.util.Scanner;

public class ArrayContains {


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

System.out.print("Enter the size of the array: ");


int size = scanner.nextInt();
int[] array = new int[size];

System.out.println("Enter " + size + " integers:");


for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}

System.out.print("Enter value to search for: ");


int value = scanner.nextInt();

boolean found = false;


for (int i = 0; i < size; i++) {
if (array[i] == value) {
found = true;
break;
}
}

if (found) {
System.out.println("Value found in the array.");
} else {
System.out.println("Value not found in the array.");
}
System.out.println("By gurleen");
scanner.close();
}
}
14. Find the index of an array element by writing a program in Java.
import java.util.Scanner;

public class FindIndex {


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

System.out.print("Enter the size of the array: ");


int size = scanner.nextInt();
int[] array = new int[size];

System.out.println("Enter " + size + " integers:");


for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}

System.out.print("Enter the element to find: ");


int element = scanner.nextInt();

int index = -1; // Default if not found


for (int i = 0; i < size; i++) {
if (array[i] == element) {
index = i;
break;
}
}

if (index != -1) {
System.out.println("Element found at index: " + index);
} else {
System.out.println("Element not found.");
}

System.out.println("by gurleen");
scanner.close();
}
}
15. Write a Java program to remove a specific element from an array.

import java.util.Scanner;

public class RemoveElement {


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

System.out.print("Enter the size of the array: ");


int size = scanner.nextInt();
int[] array = new int[size];

System.out.println("Enter " + size + " integers:");


for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}

System.out.print("Enter the element to remove: ");


int element = scanner.nextInt();

int[] newArray = new int[size - 1];


int index = 0;

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


if (array[i] != element) {
newArray[index++] = array[i];
}
}

System.out.println("Array after removal:");


for (int i = 0; i < newArray.length; i++) {
System.out.print(newArray[i] + " ");
}

System.out.println("\nby gurleen");
scanner.close();
}
}
16. Design a program to copy an array by iterating the array.
import java.util.Scanner;

public class CopyArray {


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

System.out.print("Enter the size of the array: ");


int size = scanner.nextInt();
int[] originalArray = new int[size];

System.out.println("Enter " + size + " integers:");


for (int i = 0; i < size; i++) {
originalArray[i] = scanner.nextInt();
}

int[] copiedArray = new int[size];


for (int i = 0; i < size; i++) {
copiedArray[i] = originalArray[i];
}

System.out.println("Copied array:");
for (int i = 0; i < copiedArray.length; i++) {
System.out.print(copiedArray[i] + " ");
}

System.out.println("\nby gurleen");
scanner.close();
}
}
17. Write a Java program to insert an element (on a specific position) into Multidimensional array.

import java.util.Scanner;

public class InsertInMultidimensionalArray {


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 columns = scanner.nextInt();
int[][] array = new int[rows][columns];

System.out.println("Enter elements for the array:");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = scanner.nextInt();
}
}

System.out.print("Enter row index to insert into: ");


int rowIndex = scanner.nextInt();
System.out.print("Enter column index to insert into: ");
int colIndex = scanner.nextInt();
System.out.print("Enter the value to insert: ");
int value = scanner.nextInt();

if (rowIndex < rows && colIndex < columns) {


array[rowIndex][colIndex] = value;
} else {
System.out.println("Invalid index.");
}

System.out.println("Array after insertion:");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}

System.out.println("by gurleen");
scanner.close();
}
}
18. Write a program to perform following operations on strings: 1) Compare two strings. 2) Count string length. 3) Convert
upper case to lower case &amp; vice versa. 4) Concatenate two strings. 5) Print a substring.

import java.util.Scanner;

public class StringOperations {


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

System.out.print("Enter first string: ");


String str1 = scanner.nextLine();
System.out.print("Enter second string: ");
String str2 = scanner.nextLine();

// 1) Compare two strings


System.out.println("Strings are equal: " + str1.equals(str2));

// 2) Count string length


System.out.println("Length of first string: " + str1.length());

// 3) Convert upper case to lower case & vice versa


System.out.println("First string in uppercase: " + str1.toUpperCase()); System.out.println("First string in
lowercase: " + str1.toLowerCase());

// 4) Concatenate two strings


System.out.println("Concatenated string: " + str1.concat(str2));

// 5) Print a substring
System.out.print("Enter start index for substring: ");
int startIndex = scanner.nextInt();
System.out.print("Enter end index for substring: ");
int endIndex = scanner.nextInt();
scanner.nextLine();
if (startIndex >= 0 && endIndex <= str1.length() && startIndex < endIndex) { System.out.println("Substring: " +
str1.substring(startIndex, endIndex)); } else {
System.out.println("Invalid indices.");
}

System.out.println("by gurleen");
scanner.close();
}
}
19. Developed Program &amp; design a method to find the smallest number among three numbers.

import java.util.Scanner;

public class SmallestNumber {


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

System.out.print("Enter the first number: ");


int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
System.out.print("Enter the third number: ");
int num3 = scanner.nextInt();

int smallest = num1;

if (num2 < smallest) {


smallest = num2;
}
if (num3 < smallest) {
smallest = num3;
}

System.out.println("The smallest number is: " + smallest);


System.out.println("by gurleen");
scanner.close();
}
}
20. Compute the average of three numbers through a Java Program.
import java.util.Scanner;

public class AverageOfThree {


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

System.out.print("Enter the first number: ");


double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
System.out.print("Enter the third number: ");
double num3 = scanner.nextDouble();
double average = (num1 + num2 + num3) / 3;

System.out.println("The average is: " + average);


System.out.println("by gurleen");
scanner.close();
}
}

You might also like