0% found this document useful (0 votes)
20 views47 pages

Computer Practical Og

The document contains multiple Java programs that perform various tasks, including identifying palindrome words in a sentence, checking for fascinating numbers, manipulating matrices, finding composite magic numbers, sorting names from two arrays, and determining if a number is evil based on its binary representation. Each program includes sample input and output to demonstrate functionality. The programs are designed to handle specific conditions and constraints, such as input size limits and character handling.

Uploaded by

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

Computer Practical Og

The document contains multiple Java programs that perform various tasks, including identifying palindrome words in a sentence, checking for fascinating numbers, manipulating matrices, finding composite magic numbers, sorting names from two arrays, and determining if a number is evil based on its binary representation. Each program includes sample input and output to demonstrate functionality. The programs are designed to handle specific conditions and constraints, such as input size limits and character handling.

Uploaded by

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

Program 1:

A palindrome is a word that may be read the same way in either direction. Accept a
sentence in uppercase which is terminated by either “.”, “?”, or “!”. Each word of the
sentence is separated by a single blank space.
1.Perform the following task.
2.Display the count of palindrome words in the sentence.
3.Display the palindrome words in the sentence.

Example :
Sample Input: NITIN ARORA UESE LIRIL SOAP.

Sample Output: NITIN ARORA LIRIL

Number of palindrome words: 3

Answer :

import java.util.*;
class palipracti
{
public void main()
{
int count=0,l,l1;
char c,c1;
String s1="",s2="";
Scanner in=new Scanner(System.in);
System.out.println("Enter a string");
String s=in.nextLine().toUpperCase();
l=s.length();
if(s.charAt(l-1)=='.'||s.charAt(l-1)=='?'||s.charAt(l-1)=='!')
{
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
{
s1=s1+c;
s1=s1.trim();
}
else
{
l1=s1.length();
for(int j=0;j<l1;j++)
{
c1=s1.charAt(j);
s2=c1+s2;
}
if(s1.equalsIgnoreCase(s2))
{
count++;
System.out.print(s2+" ");
}
s1="";
s2="";
}
}
System.out.println();
System.out.println("Number of Palindrome words : "+count);
}
else
System.out.println("INVALID OUTPUT");
}
}

OUTPUT:

Enter a string
Mom and dad enjoyed reading civic articles.

Palindrome Words: NITIN ARORA LIRIL

Number of Palindrome words: 3


Program 2:

Some number of 3 digits or more are called Fascinating numbers. These numbers when
multiplied by 2 and 3, and both these products on concatenating with the original
number, all digits from 1 to 9 are present exactly once, regardless of the number of
zeros.

Consider the number 192


192 x 1 = 192
192 x 2 = 384
192 x 3 = 576
Concatenating the results:192384576

It could be observed that 192384576 consists of all digits from 1 to 9 exactly once.
Hence, it could be concluded that 192 is a fascinating number.
Some examples of fascinating numbers are 192,219,273,327,1902,1920,2019 etc.
Write a program in java to input a number and check whether it is a fascinating
number or not.

Answer :

import java.util.*;
class Fascinating
{
public void main ()
{
int n,n2,n3,l,a=0,count=0,z=0;
char c,c1;
String s="";
Scanner in =new Scanner(System.in);
System. out.println("Enter the number");
n=in.nextInt();
n2=n*2;n3=n*3;
s=s+n+n2+n3;
l=s.length() ;
System.out.println(s);
for(int i=0;i<l;i++)
{
c=s.charAt(i);
for(int j=0;j<l;j++)
{
c1=s.charAt(j);
if(c1=='0') //To remove '0' formed in the output
z++;
else
if(c==c1)
count++;
}
}
if(count==9)
System.out.println("It is a fascinating number");
else
System.out.println("It is not a fascinating number");
}
}

OUTPUT:

Enter the number


219

219438657
It is a Fascinating Number.
Program 3:

Write a program in java which accepts elements in an array of order MxN, where M &
N < 20. Do the following task.
i) Accept the elements of the array and display the input matrix.
ii) Create an additional row and column in the matrix such that sum of elements of each
row and respective columns should be displayed st the terminal positions.
iii) Display the new matrix.

Sample Input:

1 2 3 4
5 6 7 8
9 10 11 12

Sample output:

1 2 3 4 10
5 6 7 8 26
9 10 11 12 32
15 8 21 24

Answer :

import java.util.*;
class Sum_of_Matrix
{
public void main()
{
int r,c,rsum=0,csum=0,i,j;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of rows and columns");
r=in.nextInt();
c=in.nextInt();
int a[][]=new int[r+1][c+1];
System.out.println("Enter the Matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
a[i][j]=in.nextInt();
}
//Input Matrix
System.out.println("Input Matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
//row sum
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
rsum=rsum+a[i][j];
csum=csum+a[j][i];
a[i][c]=rsum;
a[r][i]=csum;
}
rsum=0;
}
//column sum
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
csum=csum+a[j][i];
a[r][i]=csum;
}
csum=0;
}
// Final Matrix
System.out.println("Final Matrix");
for(i=0;i<=r;i++)
{
for(j=0;j<=c;j++)
if(i==r&&j==c)
System.out.println(" ");
else
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}

OUTPUT:

Enter the number of rows and columns


4
4
Enter the Matrix
1,5,9,3,5,7,9,6,2,4,6,5,4,1,2,2

Input Matrix:

1 5 9 3
5 7 8 6
2 4 6 5
4 1 2 2

Final Matrix:

1 5 9 3 18
5 7 8 6 26
2 4 6 5 17
4 1 2 2 9
12 17 25 16
Program 4:

A composite magic number is the integer which is composite as well as a magic


number.
A composite number has more than two factors.

For Example: 10, Factors are 1,2,5,10


A magic number is a number in which the eventual sum of digits is equal to 1.
For Example: 28 = 2+8 = 10 =1+0 = 1
Accept two positive integers m and n where m is less than n as user input. Display the
number composite magic numbers between m and n and output them along with the
frequency in the format.

Sample Input:
M=10, n=100

Sample output:
The composite magic numbers are: 10,28,46,55,64,82,91,100
The frequency of composite magic numbers is 8

Answer :

import java.util.*;
class compositemagic
{
int composite(int n)
{
int i,c=0;
{
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c>2)
return(1);
else
return(0);
}
}
int magic(int n)
{
int s=n,a,c=0;
while(n>9)
{
s=0;
while(n!=0)
{
a=n%10;
s=s+a;
n=n/10;
}
n=s;
}
if(s==1)
return(1);
else
return(0);
}
public void main()
{
Scanner in=new Scanner(System.in);
int x,y,count=0;
System.out.println("Enter the two numbers m & n for finding compositemagic");
int m=in.nextInt();
int n=in.nextInt();
compositemagic ob=new compositemagic();
System.out.println("The compositemagic numbers between "+m +" and " + n +" is ");
for(int i=m;i<=n;i++)
{
x=ob.composite(i);
y=ob.magic(i);
if(x==1&&y==1)
{
System.out.print(i+" ");
count++;
}
}
System.out.println();
System.out.println("The frequency is "+ count);
}
}

OUTPUT:

Enter the two numbers m & n for finding composite magic


30
130

The composite magic numbers between 30 and 130 are


46 55 64 82 91 100 118
The frequency of composite magic number is 7
Program 5:

Given a square matrices m[][] of order ‘N’. The maximum value possible foe N is 20.
Input the value for N and the positive integers in the matrix and perform the following
task.
a) Display the original matrix.
b)Print the row and column position of the largest element of the matrix. Print that row
and column position of the second largest element of the matrix.
c) Sort the elements of the rows in the ascending order and display the new matrix.

Sample input:

17 56 91
23 31 59
80 99 100

Sample output:

The largest element of the matrix is at 3,3.


The second largest element of the matrix is at 3,2.

Sorted matrix:

17 23 31
56 59 80
91 99 100

Answer :

import java.util.*;
class Highest
{
public void main()
{
int a=0,b=0,c1=0,d=0,temp,k=0,n=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the row and column");
int r=in.nextInt();
int c=in.nextInt();
int m[][]=new int[r][c];
n=r*c;
int arr[]=new int[n];
//INPUT MATRIX
System.out.println("Enter the matrix");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
m[i][j]=in.nextInt();
}
//ORIGINAL MATRIX
System.out.println("The input matrix is");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
System.out.print(m[i][j]+"\t");
System.out.println();
}
//Largest
int max=0,max2=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(max<m[i][j])
{
max=m[i][j];
a=i+1;
b=j+1;
}
}
}
m[a-1][b-1]=0;
//Second Largest
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(max2<m[i][j])
{
max2=m[i][j];
c1=i+1;
d=j+1;
}
}
}
System.out.println("The position of the largest element is "+a+","+b);
System.out.println("The position of the second largest element is "+c1+","+d);
m[a-1][b-1]=max;

//Entering elements from DDA to SDA


for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
arr[k]=m[i][j];
k++;
}
}
//Sorting (Bubble Sort)
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
//Elements from SDA to DDA
k=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
m[i][j]=arr[k];
k++;
}
}
//Displaying sorted array
System.out.println("The sorted matrix is ");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
System.out.print(m[i][j]+"\t");
System.out.println();
}
}
}
OUTPUT:

Enter the row and column


3
3
Enter the matrix
85,61,20,15,76,99,34,49,70

The input matrix is

85 61 20
15 76 99
34 49 70

The position of the largest element is 2,3


The position of the second largest element is 1,1

The sorted matrix is

15 20 34
49 61 70
76 85 99
Program 6:
An evil number is appositive whole number which has even number of 1’s in its binary
equivalent.
Binary equivalent of 9 is 1001 which contains even number of I’s.
A few evil numbers are 6,10,17,25,30…….
Design a program to accept a positive whole number and find the binary equialent of
the number and count the number of 1’s in it and display whether it is evil or not.

Answer :

import java.util.*;
class Evil
{
public void main()
{
Int b,s,sum=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number");
int n=in.nextInt();
b=n;
String a="";
while(n!=0)
{
s=n%2;
if(n%2==1)
sum++;
a=s+a;
n=n/2;
}
System.out.println("INPUT : "+b);
System.out.println("BINARY EQIVALENT : "+a);
System.out.println("NO OF ONE'S : "+sum);
if(sum%2==0)
System.out.println("OUTPUT : EVIL NUMBER");
else
System.out.println("OUTPUT : NOT AN EVIL NUMBER");
}
}

OUTPUT:
Enter the number
30
INPUT : 30
BINARY EQIVALENT : 11110
NO OF ONE'S :4
OUTPUT : EVIL NUMBER
Program 7:

Write a program that inputs the name of people into two different arrays A and B.
Array A has N number of names while array B has M number of names. Merge arrays
A and B into a single array such that the resulting array is sorted alphabetically.

Sample Input:
Enter number of names of array A, N=2
Enter number of names of array B, M=3

Sample Output:

A B Sorted Merged array


Divya Sriya Divya
Lena Gayathri Gaadha
Gaadha Gayathri
Lena
Sriya

Answer :

import java.util.*;
class Name_Sorting
{
public void main()
{
String temp;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the both arrays ");
int m=in.nextInt();
int n=in.nextInt();

String a[]=new String[m];


String b[]=new String[n];

System.out.println("Enter the elements in first array");


for(int i=0;i<m;i++)
a[i]=in.next().toUpperCase();

System.out.println("Enter the elements in second array");


for(int i=0;i<n;i++)
b[i]=in.next().toUpperCase();

String c[]=new String[m+n];


int k=0;
for(int i=0;i<(m+n);i++)
{
if(i<m)
c[i]=a[i];
else if(k!=n)
{
c[i]=b[k];
k++;
}
}

for(int i=0;i<(m+n)-1;i++)
{
for(int j=0;j<(m+n)-1-i;j++)
{
if(c[j].compareTo(c[j+1])>0)
{
temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
}
}
}
System.out.println("The sorted array is ");
for(int i=0;i<(m+n);i++)
System.out.println(c[i]);

}
}
OUTPUT:

Enter the size of the both arrays


2
3
Enter the elements in first array
AMBRO
JITHIN
Enter the elements in second array
YASSIR
KISHORE
SAM

The sorted array is


AMBRO
JITHIN
KISHORE
SAM
YASSIR
Program 8:

Write a program to declare a square matrix A[][] of order M x M, where M is a positive


integer and represents number of rows and columns. M should be greater than 3 and
less than Accept the value of M from user . Display an appropriate message for invalid
input.
Perform the following task:
1.Sort the boundary elements in ascending order using any standard sorting technique
and rearrange them in the matrix in clock wise manner.
2. Calculate the sum of both the diagonals.
3. Display the original matrix, rearranged matrix and only the diagonal elements of the
rearranged matrix with their sum.
Example:
Sample Input: M =4

7 2 1 6
3 5 0 4
8 11 10 20
15 12 13 14

Sample Output:

Original Matrix:
7 2 1 6
3 5 0 4
8 11 10 20
15 12 13 14

Rearranged matrix:
1 2 3 4
20 5 0 6
15 11 10 7
14 13 12 8

Answer :

import java.util.*;
class DDA_Sort
{
public void main()
{
int m,n,i,j,k,temp;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns");
m=in.nextInt();
int a[][]= new int[m][m];
System.out.println("Enter te array elements");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
a[i][j]=in.nextInt();
}
//INPUT MATRIX
System.out.println("The Input matrix is");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
//Entering elements from DDA to SDA
int p=2*(m+m)-4;
int b[]=new int[p];
k=0;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(i==0||j==0||i==m-1||j==m-1)
{
b[k]=a[i][j];
k++;
}
}
}
//Bubble sort for SDA
for(i=0;i<p-1;i++)
{
for(j=0;j<p-1-i;j++)
{
if(b[j]>b[j+1])
{
temp = b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
//Storing elements DDA
k=0;
for(j=0;j<m;j++)
a[0][j]=b[k++];

for(i=1;i<m-1;i++)
a[i][m-1]=b[k++];

for(j=m-1;j>=0;j--)
a[m-1][j]=b[k++];

for(i=m-2;i>=1;i--)
a[i][0]=b[k++];

//SORTED MATRIX
System.out.println("The sorted Matrix is");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}

OUTPUT:

Enter the number of rows of a square matrix


4
Enter te array elements
7,8,9,6,5,4,1,2,3,0,19,17,16,18,11,15

The Input matrix is


7 8 9 6
5 4 1 2
3 0 19 17
16 18 11 15

The sorted Matrix is


2 3 5 6
18 4 1 7
17 0 19 8
16 15 11 9
Program 9:

A company manufactures packing cartons in four sizes, ie. cartons to accommodate 6


boxes.
12 boxes, 24 boxes and 48 boxes.
Design a program to accept the number of boxes to be
packed (N) by the user (maximum up to 1000 boxes) and display the break-up of the
cartons used in descending order of capacity (i.e. preference should be given to the
highest capacity
waiable, and 1f boxes tef are less than 6, an extra carton of capacity 6 should be used.)
Test your program with the following data and some random data:
Example 1
INPUT : N= 726

OUTPUT: 48 × 15 = 720
6×1=6
Remaining boxes =0
Total number of boxes = 726
Total number of cartons = 16

Example 2
INPUT: N= 140

OUTPUT: 48 x 2 = 96
24 x 1 = 24
12 x 1 = 12
6x1=6
Remaining boxes 2 x 1 = 2
Total number of boxes = 140
Total number of cartons = 6

Example 3
INPUT:
N = 4296
OUTPUT: INVALID INPUT

Answer :

import java.util.*;
class Carton
{
public void main()
{
int m=0,m1=0,count=0,a=0,a1=0,b=0,b1=0,c=0,c1=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of boxes");
int n=in.nextInt();
int k=n;
if(n<=1000)
{
m=n/48;
m1=n%48;
if(m!=0)
System.out.println("48 * "+m+" = "+48*m);
}
if(m1!=0)
{
a=m1/24;
a1=m1%24;
if(a!=0)
System.out.println("24 * "+a+" = "+24*a);
}
if(a1!=0)
{
b=a1/12;
b1=a1%12;
if(b!=0)
System.out.println("12 * "+b+" = "+12*b);
}
if(b1!=0)
{
c=b1/6;
c1=b1%6;
if(c!=0)
System.out.println("6 * "+c+" = "+6*c);
}
if(c1>0)
{
System.out.println("Remaining no of boxes = " +c1+" * 1= " +c1*1);
count=m+a+b+c+1;
}
else
count=m+a+b+c;
System.out.println("The total no of boxes = "+k);
System.out.println("The total no of cartons = "+count);
}
}
OUTPUT:

Enter the number of boxes


799

48 * 16 = 768
24 * 1 = 24
6*1=6
Remaining no of boxes = 1 * 1= 1
The total no of boxes = 799
The total no of cartons = 19
Program 10:

Caesar Cipher is an encryption technique which is implemented as ROT12(‘rotate by


13 places’).It is a simple letter substitution cipher that replaces a letter with the letter 13
places after it in the alphabets, with the other characters remaining unchanged.
ROT13
A/a B/b C/c D/d E/e F/f G/g H/ I/i J/j K/k L/l M/m
h
↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕
N/ O/o P/p Q/q R/r S/s T/t U/u V/v W/w X/x Y/y Z/z
n

Sample Input:
Hello ! How are you?

Sample Output:
The Cipher text is:
Uryyb! Ubj ner lbh?

Answer :

import java.util.*;
class Ceaser_Cipher
{
public void main ()
{
int n,r=0, l;
char c,b;
String s;
Scanner in =new Scanner(System.in);
System.out.println("Enter the text to be ciphered");
s=in.nextLine();
l=s.length();
for(int i=0;i<l;i++)
{
c=s.charAt(i);
n=(int)(c);
if(c!=' ')
{
if(n>=65&&n<=90)
{
if(n<78)
r=n+13;
else if(n>77)
r=n-13;
}
else if(n>=97&&n<=122)
{
if(n<110)
r=n+13;
else if(n>109)
r=n-13;
}
b=(char)(r);
System.out.print(b);
}
else
System.out.print(" ");
}
}
}

OUTPUT:

Enter the text to be ciphered


Hai hello
Unv uryyb
Program 11:

Write a program to accept a sentence as input. The words in the string are to be
separated by a blank. Each word must be in uppercase. The sentence is terminated by
either “.”, “?” or “!”.
a) Obtain the length of the sentence.
b) Arrange the sentence I alphabetical order of the words.

Sample Input:
NECESSITY IS THE MOTHER OF INVENTION

Sample Output:
Length : 6
INVENTION IS MOTHER NECESSITY OF THE

Answer:

import java.util.*;
class alphabetical_order
{
public void main()
{
String s,s1,temp;
int l,count=0,a,j=0,i;
char c;
Scanner in =new Scanner (System.in);
System.out.println("Enter The Sentence");
s=in.nextLine().toUpperCase();
s=s+" ";
l=s.length();
if(s.charAt(l-1)=='.'||s.charAt(l-1)=='?'||s.charAt(l-1)=='!')
{
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(Character.isWhitespace(c))
count++;
}
String s2[]=new String[count];
s1="";
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
s1=s1+c;
else
{
s2[j]=s1;
s1="";
j++;
}
}
s1="";
for(i=0;i<count;i++)
{
for(j=0;j<(count-1-i);j++)
{
if(s2[j].compareTo(s2[j+1])>0)
{
temp=s2[j];
s2[j]=s2[j+1];
s2[j+1]=temp;
}
}
}
System.out.println("Length :"+count);
for(i=0;i<count;i++)
System.out.print(s2[i]+" ");
}
else
System.out.println("INVALID OUTPUT");
}
}

OUTPUT:

Enter The Sentence


The sunset is absolutely breathtaking!
Length : 6
ABSOLUTELY BREATHTAKING IS SUNSET THE
Program 12:

Write a program to accept a sentence which may be terminated by either “.”, “?”, or
“!” only . The words may be separated by more than one blank space and are in
UPPER CASE.
Perform the following tasks:
a) Find the number of words beginning and ending with a vowel.
b) Place the words which begin and end with a vowel at the beginning, followed by
the remaining words as they occur in the sentence.

Example:
Sample Input:
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE
Sample Output:
Number of words beginning and ending with a vowel = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL

Sample Input:
HOW ARE YOU@
Sample Output:
INVALID INPUT

Answer :

import java.util.*;
class vowel
{
public void main()
{
int count=0;
String s1="",s2="",s3="";
Scanner in=new Scanner(System.in);
System.out.println("Enter a sentence");
String s=in.nextLine().toUpperCase();
s=s+" ";
int l=s.length();
if(s.charAt(l-1)=='.'||s.charAt(l-1)=='?'||s.charAt(l-1)=='!')
{
char c,c1,c2;
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
{
s1=s1+c;
s1=s1.trim();
}
else
{
int l1=s1.length();
c1=s1.charAt(0);
c2=s1.charAt(l1-1);
if((c1=='A'||c1=='E'||c1=='I'||c1=='O'||c1=='U') && (c2=='A'||c2=='E'||c2=='I'||
c2=='O'||c2=='U'))
{
count++;
s2=s2+" "+s1;
s2=s2.trim();
}
else
{
s3=s3+" "+s1;
s3=s3.trim();
}
s1="";
}
}
System.out.println("Number of words beginning and ending with a vowel = "+count);
System.out.println("The rearranged sentence "+s2+" "+s3);
}
else
System.out.println("INVALID INPUT");
}
}

OUTPUT:

Enter a sentence

You must aim to be a better person tomorrow than you are today
Number of words beginning and ending with a vowel = 2
The rearranged sentence A ARE YOU MUST AIM TO BE BETTER PERSON
TOMORROW THAN YOU TODAY
13. A stack is a kind of data structure which can store elements with the restriction that
an element can be added or removed from the top only. It follows LIFO nature.(Last In
First Out).
The details of the class stack is given below:
Class name : Stack
Data Members/Instance Variables:
st[] : the array to hold the names
size : the maximum capacity of the string array.
top: the index of the topmost element of the stack.
ctr: to count the number of elements of the stack.
Member Functions:
Stack(int cap): constructor to initialize size = cap and top =-1
void pushname(String n) : to push a name onto the stack. If the stack is full, display the
message “OVERFLOW”.
String popname() : removes a name from the top of the stack and returns it. If the stack
is empty, display the message “UNDERFLOW”
void display(): Display the elememts of the stack.
Specify the class stack giving details of the constructors(), void pushname(String n),
String popname() and void display()

Answer :

import java.util.*;
class stack
{
int size,top,ctr;
String st[]=new String[10];
Scanner in=new Scanner(System.in);
stack(int cap)
{
for(int i=0;i<10;i++)
st[i]="";
size=cap;
top=-1;
ctr=0;
}
void pushname(String n)
{
if(top==(size-1))
System.out.println("OVERFLOW");
else
{
top++;
st[top]=n;
ctr++;
}
}
String val="";
String popname()
{
if(top==-1)
System.out.println("UNDERFLOW");
else
{
val=st[top];
top--;
}
return(val);
}
void display()
{
System.out.println("The stack elements are :");
for(int i=top;i>=0;i--)
System.out.println(st[i]);
}
}
14.Queue is an entity which can hold a maximum of 100 integers. The queue enables the
user to add integers from the rear and remove integers from the front. Define a class
queue with the following details.
Class Name: Queue
Data Members/ Instance Variables
Que[] : array to hold the integer elements.
size : stores the size of the array.
front : to point the index of the front.
rear : to point the index of the rear.
Member Functions
Queue(int mm) : constructor to initialize the data size =mm, front =0, rear = 0
void delete (int v) : to add integer from the rear if possible else display the message
“Overflow”.
Int delete() : returns elements from front if present, otherwise displays the
message :Underflow” and return -9999
void display() : displays the array elements.
Specify the class Queue giving details of above methods.

Answer :

import java.util.*;
class Queue
{
int Que[]=new int[100];
int size,front,rear,val;;
Queue(int mm)
{
size=mm;
front=0;
rear=0;
}
void Insert (int v)
{
if(rear==size-1)
System.out.println("Overflow");
else
if(front==-1&&rear==-1)
{
front=0;
rear=0;
}
else
{
Que[rear]=v;
rear=rear+1;
}
}
int Delete()
{
if(front==-1&&rear==-1)
{
System.out.println("Underflow");
return(9999);
}
else
val=Que[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
front++;
return(val);
}
void display()
{
if(front==-1&&rear==-1)
System.out.println("Queue is empty");
else
{
System.out.println("The elements of the queue are: ");
for(int i=front;i<=rear;i++)
System.out.print(Que[i]+" ");
System.out.println();
}
}
}
Program 15:

Write a program to accept n names and perform Linear Search.

Answer :

import java.util.*;
class Linearsearch_name
{
public void main()
{
int i,n,f=0;
String ns="";
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
n=in.nextInt();
String a[]=new String[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
a[i]=in.nextLine();
System.out.println("Enter the element to be searched");
ns=in.nextLine();
for(i=0;i<n;i++)
{
if(a[i].equalsIgnoreCase(ns))
{
f=1;
break;
}
}
if(f==1)
System.out.println("Item found in "+(i+1)+" position");
else
System.out.println("Item not found");
}
}
OUTPUT:

Enter the size of the array


4
Enter the array elements
Anamika
Susan
Karthika
Vrinda
Enter the element to be searched
Vrinda
Item found in 4th position
Program 16:

Write a program to accept n numbers and perform Binary Search.

Answer :

import java.util.*;
class Binarysearch
{
public void main()
{
int lb,ub,mid=0,ns,f=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=in.nextInt();
int a[]=new int[n];
System.out.println("Enter the array elements");
for(int i=0;i<n;i++)
a[i]=in.nextInt();
lb=0;
ub=n-1;
System.out.println("Enter the element to be searched");
ns=in.nextInt();
while(lb<=ub)
{
mid=lb+ub/2;
if(a[mid]>ns)
ub=mid-1;
if(a[mid]<ns)
lb=mid+1;
if(a[mid]==ns)
{
f=1;
break;
}
}
if(f==1)
System.out.println("Item found in "+(mid+1)+" position");
else
System.out.println("Item not found");
}
}
OUTPUT:

Enter the size of the array


5
Enter the array elements
1
3
5
7
9
Enter the element to be searched
5
Item found in 3 position
Program 17:

Write a program to accept n names in an array and sort the elements in descending
order using Bubble sort technique.

Answer :

import java.util.*;
class Bubble_Sort_name
{
public void main()
{
String temp;
int i;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=in.nextInt();
String a[]=new String[n];
System.out.println("Enter the array elements");
for( i=0;i<n;i++)
a[i]=in.nextLine();
for(i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(a[j].compareTo(a[j+1])<0)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("the sorted elements are....");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
OUTPUT:

Enter the size of the array


5
Enter the array elements
Anamika
Karthika
Susan
Manju
Isha

The sorted elements are


Susan
Manju
Karthika
Isha
Anamika
Program 18:

Write a program to accept n numbers of double type and sort the elements in ascending
order by using Selection Sort technique.

Answer :

import java.util.*;
class Selection_Sort
{
public void main()
{
double temp,min;
int i;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=in.nextInt();
double a[]=new double[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
a[i]=in.nextDouble();
for( i=0;i<n;i++)
{
min=i;
for(int j=i+1;j<n-1;j++)
{
if(a[j]<a[min])
min=j;
}
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
System.out.println("The sorted elements are....");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
OUTPUT:

Enter the size of the array


5
Enter the array elements
9
1
4
8
3
The sorted elements are....
1
4
8
9
3
Program 19:

Write a program to accept n integer elements in an array and sort the elements using
insertion sort technique

Answer :

import java.util.*;
class Insertion_Sort
{
public void main()
{
int temp,i,j;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=in.nextInt();
int a[]=new int[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
a[i]=in.nextInt();
//Sorting of the element
for( i=1;i<n;i++)
{
j=i-1;
while(j>-1)
{
if(a[j]>a[i])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
i--;
}
j--;
}
}
System.out.println("The sorted elements are ");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
OUTPUT:

Enter the size of the array


6
Enter the array elements
9
3
4
7
1
5
The sorted elements are
1
3
4
5
7
9
Program 20:

Write a program to accept n numbers in an array and perform sorting in ascending


orders.

Answer :

import java.util.*;
class Bubble_Sort_number
{
public void main()
{
int temp,i;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=in.nextInt();
int a[]=new int[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
a[i]=in.nextInt();
for( i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("The sorted elements are....");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
OUTPUT:

Enter the size of the array


6
Enter the array elements
9
1
5
3
7
0
The sorted elements are
0
1
3
5
7
9

You might also like