You are on page 1of 11

1.

Write a program to enter few integer elements in an array and find the largest and
smallest element present in that array.

Program:-
import java.util.*;

class MyArray{

int a[],n;

MyArray(int size){

n=size;

a=new int[n];

void input()

Scanner sc=new Scanner(System.in);

System.out.print("Enter the elements: ");

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

a[i]=sc.nextInt();

void find()

int max,min;

min=max=a[0];

for(int i=1;i<n;i++)

if(a[i]>max)

max=a[i];

else if(a[i]<min)

min=a[i];

System.out.println("Largest value is: "+max);

System.out.print("Smallest value is: "+min);

} PAGE NO 1
}

class Main

{ public static void main(String args[])

int size;

Scanner sc=new Scanner(System.in);

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

size=sc.nextInt();

MyArray ob=new MyArray(size);

ob.input();

ob.find();

OUTPUT:-

PAGE NO 2
2 . Write a program which finds the greatest of three numbers using the concept of command line
arguments.

PROGRAM:-

class Largest

public static void main(String args[])

int a,b,c;

a=Integer.parseInt(args[0]);

b=Integer.parseInt(args[1]);

c=Integer.parseInt(args[2]);

int g=a>b?(a>c?a:c):(b>c?b:c);

System.out.print("Largest value is: "+g);

OUTPUT:-

PAGE NO 3
3 . Write a program to enter integer values in an array and check whether an element is present in
the array or not using linear search. Assume that each element is present in the array exactly once.

PROGRAM:-

import java.util.*;

class LSearch

int a[],n,num;

Scanner sc=new Scanner(System.in);

void input()

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

n=sc.nextInt();

a=new int[n];

System.out.print("Enter the elements: ");

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

a[i]=sc.nextInt();

System.out.print("Enter the number you want to search: ");

num=sc.nextInt();

void search()

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

if(a[i]==num)

System.out.print("Element found at index "+i);

return;

System.out.print("Element not found"); PAGE NO 4


}

public static void main(String args[])

LSearch ob=new LSearch();

ob.input();

ob.search();

OUTPUT:-

PAGE NO 5
4. Write a program to sort the elements of an array using selection sort.

PROGRAM:-

import java.util.*;

class SSort

int a[],n;

Scanner sc=new Scanner(System.in);

void input()

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

n=sc.nextInt();

a=new int[n];

System.out.print("Enter the elements: ");

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

a[i]=sc.nextInt();

void s_sort()

int t,min;

for(int i=0;i<n-1;i++)

min=i;

for(int j=i+1;j<n;j++)

if(a[min]>a[j])

min=j;

t=a[min]; PAGE NO 6
a[min]=a[i];

a[i]=t;

System.out.print("After sorting the elements are:");

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

System.out.print(" "+a[i]);

public static void main(String args[])

SSort ob=new SSort();

ob.input();

ob.s_sort();

OUTPUT:-

PAGE NO 7
5. Write a program to merge two sorted arrays. The resultant array must also be in sorted order.

PROGRAM:-

import java.util.*;

class Merge {

int a[],b[],c[],n1,n2,n3;

Scanner sc=new Scanner(System.in);

Merge(int s1,int s2) {

n1=s1;

n2=s2;

n3=n1+n2;

a=new int[n1];

b=new int[n2];

c=new int[n3]; }

void input() {

System.out.print("Enter the elements of 1st array: ");

for(int i=0;i<n1;i++)

a[i]=sc.nextInt();

System.out.print("Enter the elements of 2nd array: ");

for(int i=0;i<n2;i++)

b[i]=sc.nextInt(); }

void merge() {

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

while(i<n1 && j<n2)

if(a[i]<b[j])

c[k++]=a[i++];

else

c[k++]=b[j++];

} PAGE NO 8
while(i<n1)

c[k++]=a[i++];

while(j<n2)

c[k++]=b[j++];

System.out.print("The elements of 3rd array are:");

for(i=0;i<n3;i++)

System.out.print(" "+c[i]);} }

class MergeMain {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

int s1,s2;

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

s1=sc.nextInt();

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

s2=sc.nextInt();

Merge ob=new Merge(s1,s2);

ob.input();

ob.merge(); } }

OUTPUT:-

PAGE NO 9
6. Write a program to enter few integer elements in a matrix, find & store its transpose in another
matrix and display the result.

PROGRAM:-

import java.util.*;

class Transpose {

int a[][],b[][],r1,c1,r2,c2;

Scanner sc=new Scanner(System.in);

Transpose(int r,int c) {

r1=c2=r; c1=r2=c;

a=new int[r1][c1]; b=new int[r2][c2]; }

void input() {

System.out.print("Enter the elements: ");

for(int i=0;i<r1;i++)

for(int j=0;j<c1;j++)

a[i][j]=sc.nextInt(); }

void transpose() {

for(int i=0;i<r1;i++)

for(int j=0;j<c1;j++)

b[j][i]=a[i][j];

System.out.println("After transpose the elements of the matrix are:");

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

for(int j=0;j<c2;j++) {

System.out.print(" "+b[i][j]); }

System.out.println(); } } }

class TransposeMain {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);

int r,c;

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

r=sc.nextInt();

System.out.print("Enter the number of columns: "); PAGE NO 10


c=sc.nextInt();

Transpose ob=new Transpose(r,c);

ob.input();

ob.transpose();

OUTPUT:-

PAGE NO 11

You might also like