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

Java Lab

Uploaded by

Divyanshu Kumar
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)
16 views

Java Lab

Uploaded by

Divyanshu Kumar
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/ 7

1)

a -> Arithmetic Operation


b> Compound Simple
System.out.print("Enter the number of times interest is compounded per year: ");
int compoundFrequency = scanner.nextInt();
double simpleInterest = (principal * annualInterestRate * years) / 100.0;
double compoundInterest = principal * Math.pow(1 + (annualInterestRate / (100.0 *
compoundFrequency)), compoundFrequency * years) - principal;
c> swap two number with temp and without temp variable
2)
Quadratic
System.out.print("Enter the coefficient 'a': "); double a = scanner.nextDouble();
System.out.print("Enter the coefficient 'b': "); double b = scanner.nextDouble();
System.out.print("Enter the 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("Two real solutions:");
System.out.println("Root 1: " + root1);
System.out.println("Root 2: " + root2);
}
else if (discriminant == 0)
{
double root = -b / (2 * a);
System.out.println("One real solution:");
System.out.println("Root: " + root);
}
else {
System.out.println("No real solutions. The discriminant is negative.");
}

PRime
for (int num = 2; num <= maxNumber; num++) {
boolean isPrime = true;
for (int i=2; i <= num/2; i++)
{
if ( num % i == 0)
{
isPrime = false;
break;
}}
if ( isPrime == true )
System.out.println(num);
}

Factorial
if (number < 0)
{
System.out.println("Factorial is not defined for negative numbers.");
}
else
{
int factorial = 1;
for (int i = 1; i <= number; i++)
{
factorial=factorial*i;
}
System.out.println("Factorial of " + number + " is " + factorial);
}

3-)
a)
import java.util.Scanner;
import java.util.Arrays;
public class linaerBinary{
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
System.out.println("enter length of array ");
int n= sc.nextInt();
System.out.println("enter elements of array ");
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i]= sc.nextInt();
}
System.out.println("Key ");
int key =sc.nextInt();
int ans =-1;
for(int i=0;i<n;i++){
if(arr[i] == key){
ans=i;
}

}
if(ans != -1)
System.out.println(key+" is found at index "+ ans);
else
System.out.println(key+" is not found" );

Arrays.sort(arr);
int binary = bin(arr,n,key);
if(binary !=-1){
System.out.println(key+" is found at index "+ binary);
}
else
System.out.println(key+" is not found" );

sc.close();
}
private static int bin(int[] arr , int n, int key){
int low= 0;
int high = n-1;
while(low<=high){
int mid= low+(high-low)/2;
if(arr[mid] == key) {
return mid;
}
else if(arr[mid] > key){
high = mid-1;

}
else
low= mid+1;
}
return -1;
}

}
(b)Bubble
import java.util.Scanner;
import java.util.Arrays;
public class bubble{
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
System.out.println("enter length of array ");
int n= sc.nextInt();
System.out.println("enter elements of array ");
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i]= sc.nextInt();
}
bubblesort(arr,n);
sc.close();
}
private static void bubblesort(int[] arr , int n){
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(arr[j]> arr[j+1]){
int temp =arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println("sorted array is : ");
for(int i=0;i<n;i++){
System.out.println(+arr[i]+ " ");
}
}

}
c)SmallestLargest
int[] arr = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
} int largest = arr[0];
int smallest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) { largest = arr[i];
}
if (arr[i] < smallest)
{
smallest = arr[i];
}}

5)Strings

import java.util.Scanner;
public class string{
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
System.out.println("enter first string :");
String str1=sc.nextLine();

reverse(str1);
palindrome(str1);

System.out.println("enter second string :");

String str2=sc.nextLine();

if(str1.equals(str2)){
System.out.println("The two strings are equal ");
}
else
System.out.println("The two strings are not equal");
sc.close();
}

public static void reverse(String str){


String reversed = new StringBuffer(str).reverse().toString();
System.out.println("Reversed id " + reversed +"\n");
}
public static void palindrome(String str){
String rev= new StringBuffer(str).reverse().toString();
if(rev.equals(str))
System.out.println("Palindrome");
else
System.out.println("Not a palindrome");
}
}

4)
import java.util.Scanner;
public class reverse{
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
System.out.println("enter rows of matrix A and B ");
int row=sc.nextInt();
System.out.println("enter columns of matrix A and B ");
int col=sc.nextInt();

int[][] matrixA=new int[row][col];


int[][] matrixB=new int[row][col];

System.out.println("enter elements of matrix A ");


for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
matrixA[i][j] = sc.nextInt();
}
}
System.out.println("enter elements of matrix B ");
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
matrixB[i][j] = sc.nextInt();
}
}

int[][] sum = new int[row][col];


int[][] prod = new int[row][col];

for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
sum[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
System.out.println("Sum is : ");
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
System.out.println(sum[i][j] + " ");
}
System.out.println();
}

for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
for(int k=0;k<col;k++){
prod[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
System.out.println("Product is : ");
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
System.out.println(prod[i][j] + " ");
}
System.out.println();
}

System.out.println(determinant(matrixA));

sc.close();
}
public static int determinant(int[][] matrix){
if(matrix.length !=2 || matrix[0].length !=2 || matrix[1].length !=2)
return 0;
else
return matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0];
}

You might also like