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

Computer Practical

The document contains 30 programming questions on various Java concepts like arrays, strings, inheritance, recursion, file handling, and more. Each question includes the problem statement and sample code to solve the problem. The programs cover concepts like palindrome checking, Fibonacci series, complex numbers, object passing between classes, and array operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Computer Practical

The document contains 30 programming questions on various Java concepts like arrays, strings, inheritance, recursion, file handling, and more. Each question includes the problem statement and sample code to solve the problem. The programs cover concepts like palindrome checking, Fibonacci series, complex numbers, object passing between classes, and array operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Contents:

1 Palindrome-Array
2 Positive Palin-sub method
3 Magic number-sub method
4 Fibonacci Sum-sub method
5 File Handling
6 File Handling 2
7 Employee Object Passing-two objects passed 8
Employee Object Passing-one object passed 9
Complex number-Object Passing
10 Array-Data Structure
11 Queue Array-Data Structure
12 Digit\t\tStar-sub method
13 AlphaArrange-String
14 Telephone Bill-Rate
15 Matrix Equality-DDA
16 Transpose-DDA
17 Students marks-Inheritance
18 Perimeter&Area-Inheritance
19 Conversion-DecOct- sub method
20 GCD-Recursion
21 PowerBase-Recursion
22 Circular QueueArray-Data Structure
23 Stacked Array-Data Structure
24 ArrangeCapital&Country-String
25 Format Fration-Sub method
26 PalinD-DDA
27 Link List
28 Stacked Link List
29 PrimeLinkArr-Linklist and Array connection

Programs:

Q1
//check and display the palindrome numbers from an array
import java.util.*;
class Palindrome
{
int a[],s;
void store()
{
Scanner kc=new Scanner(System.in);
a=new int[15];
int i;
System.out.println("Enter 15 numbers.");
for(i=0;i<15;i++)
a[i]=kc.nextInt();
kc.close();
}

boolean check(int n)
{
int d,dup=n,rev=0;
while(n!=0)
{
d=n%10;
rev=rev*10+d;
n/=10;
}
if(rev==dup)
return true;
else
return false;
}

void disp()
{
int i;s=0;
boolean b;
for(i=0;i<15;i++)
{
b=check(a[i]);
if(b==true)
{
s++;
System.out.println(a[i]);
}
}
System.out.println("There are "+s+" plaindrome numbers found.");
}
public static void main(String[] args)
{
Palindrome kk=new Palindrome();
kk.store();
kk.disp();
}
}
========================================================================
=========
Q2
//to check the positive number is palindrome or not
import java.util.Scanner;
class Palin
{
int num,revnum;
Palin()
{
num=0;
revnum=0;
}

void accept()
{
Scanner kc=new Scanner(System.in);
System.out.println("Enter a number");
num=kc.nextInt();
kc.close();
}

int reverse(int y)
{
if(y>0)
{
int d=y%10;
revnum=revnum*10+d;
reverse(y/10);
}
return revnum;
}

void check()
{
if(num>0)
{
revnum=reverse(num);
if(num==revnum)
System.out.println(num+" is a Palindrome number.");
else
System.out.println(num+" is not a Palindrome number.");
}
else
System.out.println("The number is not a positive number.");
}

public static void main(String[] args)


{
Palin ob=new Palin();
ob.accept();
ob.check();
}
}
========================================================================
=========
Q3
//to check whether a number is magic number or not
import java.util.*;
class Magic
{
int n;
Magic()
{
n=0;
}
void getnum(int n)
{
this.n=n;//this keyword gives proirity to the instance variable
}
int sum_of_digits(int x)
{
int s=0;
while(x!=0)
{
s=s+(x%10);
x/=10;
}
return s;
}
void isMagic()
{
while(n>9)
{
int sum=sum_of_digits(n);
n=sum;
}
if(n==1)
System.out.println("The number entered is a magic number.");
else
System.out.println("The number entered is not a Magic number.");
}
public static void main(String[] args) {
Scanner kc=new Scanner(System.in);
Magic kk=new Magic();
System.out.println("Enter a number.");
int n=kc.nextInt();
kc.close();
kk.getnum(n);
kk.isMagic();
}
}
========================================================================
=========
Q4
//to calculate the sum of nth fibonacci numbers
import java.util.Scanner;
class Sum
{
int n,s;
Sum(int n)
{
s=0;
this.n=n;//this gives priority to the instance variable
}
int fibo(int p)
{
if(p==1)
return 0;
else if(p==2)
return 1;
else
return fibo(p-1)+fibo(p-2);
}
void add()
{
for(int i=0;i<=n;i++)
s=s+fibo(i);
System.out.println("The sum of the fibanacci numbers is "+s);
}
public static void main(String[] args)
{
Scanner kc=new Scanner(System.in);
System.out.println("Enter the number of terms.");
int n=kc.nextInt();
kc.close();
Sum kk=new Sum(n);
kk.add();
}
}
========================================================================
===================
Q5
//to store 5 names of the students and then display them from the file.
import java.io.*;
class File1
{
public static void main(String[] args)throws IOException
{
BufferedReader hk=new BufferedReader(new InputStreamReader(System.in));
PrintWriter kk=new PrintWriter(new BufferedWriter(new FileWriter("Names.txt")));
String n;
System.out.println("Enter 5 names.");
for(int i=0;i<5;i++)
{
n=hk.readLine();
kk.println(n);
}
hk.close();
kk.close();
}
}
//to read the names from the file Names.txt
import java.io.*;
class File2
{
public static void main(String args[])throws IOException
{
BufferedReader hk=new BufferedReader(new FileReader("Names.txt"));
for(int i=0;i<5;i++)
System.out.println(hk.readLine());
hk.close();
}
}
========================================================================
=====================
Q6
//binary file
//to take 5 names and roll numbers and store in a file
import java.io.*;
class BFile1
{
public static void main(String args[])throws IOException
{
BufferedReader hk=new BufferedReader(new InputStreamReader(System.in));
PrintWriter kk=new PrintWriter(new BufferedWriter(new FileWriter("NamesR.txt")));
PrintWriter kc=new PrintWriter(new BufferedWriter(new FileWriter("Roll.txt")));
String n;
int r;
System.out.println("Enter 5 names and roll numbers.");
for(int i=0;i<5;i++)
{
System.out.print("Enter the name: ");
n=hk.readLine();
kk.println(n);
System.out.print("\nEnter the roll number: ");
r=Integer.parseInt(hk.readLine());
kc.println(r);
}
System.out.println(" ");
hk.close();
kk.close();
kc.close();
}
}

//binary file2
//to read the names from the file NamesR.txt
import java.io.*;
class BFile2
{
public static void main(String args[])throws IOException
{
BufferedReader hk=new BufferedReader(new FileReader("NamesR.txt"));
BufferedReader kk=new BufferedReader(new FileReader("Roll.txt"));
System.out.println("Names\t\tRoll Number");;
for(int i=0;i<5;i++)
System.out.println(hk.readLine()+"\t\t"+kk.readLine());
hk.close();
kk.close();
}
}
========================================================================
=====================
Q7
//use object passing to compare the salaries of 2 employees
import java.util.Scanner;
class Emp1
{
String nm;
int sal;
void store()
{
Scanner kc=new Scanner(System.in);
System.out.println("Enter the name and the salary.");
nm=kc.nextLine();
sal=kc.nextInt();
//kc.close();
}

void compare(Emp1 kk, Emp1 hk)


{
if(kk.sal>hk.sal)
System.out.println(kk.nm+" receives more salary.");
else if(kk.sal<hk.sal)
System.out.println(hk.nm+" receives more salary.");
else
System.out.println("Both receive the same salary.");
}

public static void main(String[] args)


{
Emp1 kk=new Emp1();
kk.store();
Emp1 hk=new Emp1();
hk.store();
kk.compare(kk, hk);
}
}
========================================================================
=====================
Q8
//use object passing to compare the salaries of 2 employees
import java.util.Scanner;
class Emp2
{
String nm;
int sal;
void store()
{
Scanner kc=new Scanner(System.in);
System.out.println("Enter the name and the salary.");
nm=kc.nextLine();
sal=kc.nextInt();
//kc.close();
}

void compare(Emp2 hk)


{
if(sal>hk.sal)
System.out.println(nm+" receives more salary.");
else if(sal<hk.sal)
System.out.println(hk.nm+" receives more salary.");
else
System.out.println("Both receive the same salary.");
}
public static void main(String[] args)
{
Emp2 kk=new Emp2();
kk.store();
Emp2 hk=new Emp2();
hk.store();
kk.compare(hk);
}
}
========================================================================
=====================
Q9
//take 2 copmlex numbers as input and check their sum
import java.util.*;
class Complex
{
int i,r;
Complex()
{
i=0;
r=0;
}

void store()
{
Scanner kc=new Scanner(System.in);
System.out.println("Enter the complex number.");
System.out.print("Real part: ");
r=kc.nextInt();
System.out.print("Imaginary part: ");
i=kc.nextInt();
//kc.close();
}

Complex add(Complex b)
{
Complex k=new Complex();
k.r=r+b.r;
k.i=i+b.i;
return k;
}
void display()
{
System.out.println("The sum of the two complex numbers is "+r+"+"+i+"i");
}

public static void main(String[] args)


{
Complex a,b,sum;
a=new Complex();
b=new Complex();
//sum=new Complex();//only caries the address
a.store();
b.store();
sum=a.add(b);
sum.display();
}
}
========================================================================
=====================
Q10
//Write a program in java to insert, delete and display elements of Array according to users
input.
import java.util.Scanner;
class Array
{
int a[],top;
Array()
{
a=new int[15];
top=-1;
}

void insert(int v, int p)


{
int ip=p-1,i;//v=value to be inserted
if(top==-1&&p==1)
{
System.out.println("first value is inserted");
a[++top]=v;
}
else if(top==a.length-1)
System.out.println("Overflow");
else if(ip>top+1)
System.out.println("\n\nNot a valid position");
else
{
for(i=top;i>=ip;i--)
a[i+1]=a[i];
a[ip]=v;
top++;
System.out.println("\nInsertion is successful");
}

}
void del(int p)
{
int ip=p-1;int i;
if(top==-1)
System.out.println("Under flow");
else if(ip>top)
System.out.println("\n\nSorry, not a valid situation");
else
{
for(i=ip;i<top;i++)
a[i]=a[i+1];
System.out.println("\nThe Positional value you have given is successfully deleted");
top--;
}
}

void traverse()
{
int i;
if(top==-1)
System.out.println("Underflow");
else
{
System.out.println("The numbers of the array are: ");
for(i=0;i<=top;i++)
System.out.print(a[i]+" ");
System.out.println("\nTotal: "+(top+1)+" numbers found");
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);int ch;
Array ob=new Array();
char k;
do
{
System.out.println("1 for insertion 2 for deletion 3 for traverse");
ch=sc.nextInt();
switch (ch)
{
case 1:
int v,p;
System.out.println("Enter value");
v=sc.nextInt();
System.out.println("Enter postion");
p=sc.nextInt();
ob.insert(v, p);
break;
case 2:
if(ob.top==-1)
ob.del(0);
else
{
System.out.println("Enter position");
p=sc.nextInt();
ob.del(p);
}
break;
case 3:
ob.traverse();
break;
default:
System.out.println("Sorry, your option could not be understood");
break;
}
System.out.println("\nPress m to get main menu");
k=sc.next().charAt(0);
}while(k=='M'||k=='m');
sc.close();
}
}
========================================================================
=====================
Q11
//implement queue technique
import java.util.Scanner;
class Queue_Array
{
int a[],r,f;
Queue_Array()
{
a=new int[50];
r=-1;
f=-1;
}

void insert(int v)
{
if(r==-1 && f==-1)//indicates that the array is empty
{
a[++r]=v;
f++;
System.out.println("\nFirst Value Inserted Successfully.");
}
else if(r==a.length-1)//indicates that the array is fully occcupied
System.out.println("\n\nOverFlow.");
else
{
a[++r]=v;
System.out.println("\n\nValue insertion is successfull.");
}
}

void delete()
{
if(f==-1 && r==-1)
System.out.println("\n\nUnderFlow");
else if(f==r)
{
System.out.println("\n"+a[f]+" has been deleted.");
f=-1;
r=-1;
}
else
{
System.out.println("\n"+a[f]+" has been deleted.");
f++;
}
}

void traverse()
{
if(f==-1 && r==-1)
System.out.println("\n\nUnderFlow.");
else
{
System.out.println("\n\n\nThe values of the queue:");
for(int i=f;i<=r;i++)
System.out.print(+a[i]+" ");
System.out.println("\n\nThere are "+(r-f+1)+" values found.");
}
}

public static void main(String args[])


{
Scanner kc=new Scanner(System.in);
Queue_Array kk=new Queue_Array();
char k;
do
{
System.out.println("Enter 1 for Insertion\nEnter 2 for Deletion\nEnter 3 for Traverse.");
int ch=kc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter the value to be inserted.");
int v=kc.nextInt();
kk.insert(v);
break;

case 2:
kk.delete();
break;
case 3:
kk.traverse();
break;

default:
System.out.println("Invalid Input.");
break;
}
System.out.println("\n\nPress m to get main menu");
k=kc.next().charAt(0);
}while(k=='M'||k=='m');
System.out.println("\nThank You for using our services.");
kc.close();
}
}
========================================================================
=====================
Q12
//to show the digits in one column and the same number of stars in another column
import java.util.Scanner;
class Pattern
{
int n;
Pattern(int n)
{
this.n=n;//this keyword gives priority to the instance variable
}

void disp(int d)
{
System.out.print(d+"\t\t");
for(int i=1;i<=d;i++)
System.out.print("*");
System.out.print("\n");
}

void generate()
{
int d,c=0,dup=n;
while(dup!=0)
{
c++;
dup/=10;
}
dup=n;
while(dup!=0)
{
d=dup/(int)Math.pow(10,--c);
disp(d);
dup%=(int)Math.pow(10,c);
}
}

public static void main(String[] args)


{
Scanner kc=new Scanner(System.in);
System.out.println("Enter a number.");
int n=kc.nextInt();
kc.close();
System.out.println("Digit\t\tStars");
Pattern kk=new Pattern(n);
kk.generate();
}
}
========================================================================
=====================
Q13
//arrange the 10 alphabets of the word in ascending order
import java.util.Scanner;
class Alpha
{
String str;
Alpha()
{
str="";
}

void readword()
{
Scanner kc=new Scanner(System.in);
System.out.println("Enter the word of 10 alphabets.");
str=kc.next();
kc.close();
}

void arrange()
{
char[] arr=str.toCharArray();
int i,j;
char newValue;
for(i=0;i<arr.length;i++)
{
newValue=arr[i];
j=i;
while(j<0 && arr[j-1]>newValue)
{
arr[j]=arr[j-1];
j--;
}
arr[j]=newValue;
}
str=new String(arr);
}

void display()
{
System.out.println("The word in ascending order is "+str);
}

public static void main(String[] args) {


Alpha kk=new Alpha();
kk.display();
kk.arrange();
kk.display();
}
}

2nd technique
//arrange the words of the string in ascending order
import java.util.Scanner;
class AlphaArrange
{
String s, w[];
int wl[];
void readData(String s)
{
this.s=s;
}

void store()
{
int c=0;
String k="";
s=s.trim()+" ";
for(int i=0;i<s.length();i++)
{
if (s.charAt(i)==32)
c++;
}
w=new String[c];
wl=new int[c];
c=0;
for(int i=0;i<s.length();i++)
{
if (s.charAt(i)!=32)
k=k+s.charAt(i);
else
{
w[c]=k;
wl[c]=k.length();
c++;
k="";
}
}
}

void arrange()
{
for(int j=0;j<wl.length;j++)
for(int i=0;i<wl.length-1;i++)
if(wl[i]>wl[i++])
{
String k=w[i];
w[i]=w[i++];
w[i++]=k;

int l=wl[i];
wl[i]=wl[i++];
wl[i++]=l;
}
}

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

public static void main(String args[])


{
Scanner kc=new Scanner(System.in);
System.out.println("Enter a string.");
String s=kc.nextLine();
kc.close();
AlphaArrange kk=new AlphaArrange();
kk.readData(s);
kk.store();
kk.arrange();
kk.disp();
}
}
========================================================================
=====================
Q14
//to calculate the telephone bill according to the slab
import java.util.Scanner;
class TelCal
{
String ph, nm;
int n;
double amt;
TelCal(String ph,String nm,int n)
{
this.ph=ph;
this.nm=nm;
this.n=n;//this keyword gives priority to the instance variable
amt=0.0;
}
void compute()
{
if(n<=100)
amt=500.0;
else if(n>100 && n<=200)
amt=((n-100)*1.0)+500.0;
else if(n>200 && n<=300)
amt=((n-200)*1.2)+600.0;
else
amt=((n-300)*1.5)+720.0;
}

void dispdata()
{
System.out.println("Phone Number\tName\t\tTotal Calls\tBill Amount");
System.out.println(ph+"\t"+nm+"\t\t"+n+"\t\t"+amt);
}

public static void main(String[] args)


{
Scanner kc=new Scanner(System.in);
System.out.print("Enter the phone number: ");
String ph=kc.nextLine();
System.out.print("Enter the name: ");
String nm=kc.nextLine();
System.out.print("Enter the number of calls: ");
int n=kc.nextInt();
kc.close();
TelCal kk=new TelCal(ph, nm, n);
kk.compute();
kk.dispdata();
}
}
========================================================================
=====================
Q15
//to check whether both the matrices are equal or not
import java.util.Scanner;
class EqMat
{
int a[][],n,m;
EqMat(int n,int m)
{
this.n=n;
this.m=m;//this keyword gives priority to the instance variable
a=new int[m][n];
}

void readarray()
{
Scanner kc=new Scanner(System.in);
System.out.println("Enter the elements: ");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
a[i][j]=kc.nextInt();
}

int check(EqMat P, EqMat Q)


{
int flag=1;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(P.a[i][j]!=Q.a[i][j])
{
flag=0;
break;
}
return flag;
}

void print()
{
System.out.println("The elements of the matrix are:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
System.out.print(a[i][j]+"\t");
System.out.println(" ");
}
}

public static void main(String[] args)


{
Scanner kc=new Scanner(System.in);
EqMat P,Q;
System.out.println("First Matrix.");
System.out.print("Enter the number of rows: ");
int m=kc.nextInt();
System.out.print("Enter the number of column: ");
int n=kc.nextInt();
P=new EqMat(m,n);
P.readarray();
System.out.println("Second Matrix.");
System.out.print("Enter the number of rows: ");
m=kc.nextInt();
System.out.print("Enter the number of column: ");
n=kc.nextInt();
Q=new EqMat(m,n);
Q.readarray();
System.out.println("\nFirst Matrix:");
P.print();
System.out.println("\nSecond Matrix:");
Q.print();
int k=P.check(P, Q);
if(k==1)
System.out.println("\nThe two matrices are equal.");
else
System.out.println("\nThe two matrices are not equal.");
kc.close();
}
}
========================================================================
=====================
Q16
//to find the transpose of the array
import java.util.Scanner;
class Transarray
{
int arr[][],m,n;
Transarray(int mm, int nn)
{
m=mm;
n=nn;
arr=new int[m][n];
}
void fillarray()
{
Scanner kc=new Scanner(System.in);
System.out.println("Enter the elements of the matrix.");
for(int r=0;r<m;r++)
for(int c=0;c<n;c++)
arr[r][c]=kc.nextInt();
kc.close();
}

void transpose(Transarray A)
{
for(int r=0;r<m;r++)
for(int c=0;c<n;c++)
arr[r][c]=A.arr[c][r];
}

void display()
{
for(int r=0;r<m;r++)
{
for(int c=0;c<n;c++)
System.out.print(arr[r][c]+"\t");
System.out.println("\t");
}
}

public static void main(String[] args)


{
Scanner kc=new Scanner(System.in);
System.out.println("Enter the number of rows and columns.");
System.out.println("(The numbers should be less than 21)");
System.out.print("r=");
int mm=kc.nextInt();
System.out.print("c=");
int nn=kc.nextInt();
Transarray A=new Transarray(mm, nn);
Transarray T=new Transarray(mm, nn);
A.fillarray();
System.out.println("\nThe original matrix is: ");
A.display();
T.transpose(A);
System.out.println("\nThe transpose matrix is: ");
T.display();
kc.close();
}
}
========================================================================
=====================
Q17
//part one of the program Q17
//to store the personal data of the student
import java.io.*;
class Student
{
String name, gender;
int age;
void inpdetails1()throws IOException
{
BufferedReader hk=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the student's details.");
System.out.print("Enter the name: ");
name=hk.readLine();
System.out.print("Enter the gender: ");
gender=hk.readLine();
System.out.print("Enter the age: ");
age=Integer.parseInt(hk.readLine());
}
void show1()
{
System.out.println("Name: "+name);
System.out.println("Gender: "+gender);
System.out.println("Age: "+age);
}
}
//part two of the program Q17
//to input the marks of the students.
import java.io.*;
class Marks extends Student
{
int regnum,marks;
String subject;
void inpdetails2()throws IOException
{
BufferedReader hk=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the exam details.");
System.out.print("Enter the registration number: ");
regnum=Integer.parseInt(hk.readLine());
System.out.print("Enter the subject: ");
subject=hk.readLine();
System.out.print("Enter the marks: ");
marks=Integer.parseInt(hk.readLine());
}

void show2()
{
System.out.println("Registration Number: "+regnum);
System.out.println("Subject: "+subject);
System.out.println("Marks: "+marks);
}

public static void main(String[] args)throws IOException


{
Marks kk=new Marks();
kk.inpdetails1();
kk.inpdetails2();
System.out.println("\n\nStudent Database\n");
kk.show1();
kk.show2();
}
}
========================================================================
=====================
Q18
//to calculate the perimeter
//part one of the calculation of the perimeter program
import java.lang.*;
class Perimeter
{
double a, b;
Perimeter(int a,int b)
{
this.a=a;
this.b=b;
}
double calculate()
{
return (2*(a+b));
}

void show()
{
System.out.println("\nThe perimeter of the parallelogram with length "+a+" and breadth
"+b+" is "+calculate()+".");
}
}

//to calculate the area of the parallelogram


//part two
import java.util.Scanner;
class Area extends Perimeter
{
double h,area;
Area(int a, int b, int h)
{
super(a,b);
this.h=h;//this keyword gives priority to the instance variable
area=0.0;
}

void doarea()
{
area=b*h;
}

void show()
{
super.show();
System.out.println("With height "+h+", the area of the parallelogram is "+area);
}

public static void main(String[] args)


{
Scanner kc=new Scanner(System.in);
System.out.println("Enter the dimensions of the parallelogram.");
System.out.print("Length= ");
int l=kc.nextInt();
System.out.print("Breadth= ");
int b=kc.nextInt();
System.out.print("Height= ");
int h=kc.nextInt();
kc.close();
Area kk=new Area(l,b,h);
kk.doarea();
kk.show();
}
}
========================================================================
=====================
Q19
//to convert the decimal number to its equivalent octal number
import java.util.Scanner;
class DeciOct
{
int n,oct,p;
DeciOct()
{
n=0;
oct=0;
p=0;
}

void getnum(int nn)


{
n=nn;
}

void deci_oct()
{
if(n>0)
{
oct=oct+(n%8)*(int)Math.pow(10, p);
n/=8;
p++;
deci_oct();
}
}
void show()
{
deci_oct();
System.out.println("The Ocatal equivalent of the number is "+oct);
}

public static void main(String[] args)


{
Scanner kc=new Scanner(System.in);
System.out.println("Enter a Decimal number.");
int nn=kc.nextInt();
kc.close();
DeciOct kk=new DeciOct();
kk.getnum(nn);
kk.show();
}
}
========================================================================
=====================
Q20
//to calculate the Greatest Common Divisor of a number
import java.util.Scanner;
class GCD
{
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b, a%b);
}

public static void main(String[] args)


{
Scanner kc=new Scanner(System.in);
System.out.println("\nWelcome to the GCD wizard.");
System.out.println("\nEnter two numbers.");
int a=kc.nextInt();
int b=kc.nextInt();
kc.close();
GCD kk=new GCD();
System.out.println("\nThe Greatest Common Divisor of the two numbers is "+kk.gcd(a, b));
}
}
========================================================================
=====================
Q21
//to calculate the power of the number
import java.util.Scanner;
class Power
{
int cal(int b,int p)
{
if(p==1)
return b;
else
return (b*cal(b,p-1));
}

public static void main(String[] args) {


Scanner kc=new Scanner(System.in);
Power kk=new Power();
System.out.println("Enter the base.");
int b=kc.nextInt();
System.out.println("Enter the power.");
int p=kc.nextInt();
System.out.println("The answer is "+kk.cal(b,p));
kc.close();
}
}
========================================================================
=====================
Q22
//implement circular queue technique
import java.util.Scanner;
class CQueue_Array
{
int a[],r,f;
CQueue_Array()
{
a=new int[5];
r=-1;
f=-1;
}

void insert(int v)
{
if(f!=0 && r==a.length-1)
{
r=a.length-f;
for(int i=0;f<a.length;i++,f++)
a[i]=a[f];
f=0;
}
if(r==-1 && f==-1)//indicates that the array is empty
{
a[++r]=v;
f++;
System.out.println("\nFirst Value Inserted Successfully.");
}
else if(f==0 && r==a.length-1)//indicates that the array is fully occcupied
System.out.println("\n\nOverFlow.");
else
{
a[++r]=v;
System.out.println("\n\nValue insertion is successfull.");
}
}

void delete()
{
if(f==-1 && r==-1)
System.out.println("\n\nUnderFlow");
else if(f==r)
{
System.out.println("\n"+a[f]+" has been deleted.");
f=-1;
r=-1;
}
else
{
System.out.println("\n"+a[f]+" has been deleted.");
a[f]=0;
f++;
}
}

void traverse()
{
if(f==-1 && r==-1)
System.out.println("\n\nUnderFlow.");
else
for(int i=f;i<=r;i++)
System.out.print(+a[i]+" ");
}

public static void main(String args[])


{
Scanner kc=new Scanner(System.in);
CQueue_Array kk=new CQueue_Array();
char k;
do
{
System.out.println("Enter 1 for Insertion\nEnter 2 for Deletion\nEnter 3 for Traverse.");
int ch=kc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter the value to be inserted.");
int v=kc.nextInt();
kk.insert(v);
break;

case 2:
kk.delete();
break;

case 3:
kk.traverse();
break;

default:
System.out.println("Invalid Input.");
break;
}
System.out.println("\n\nPress m to get main menu");
k=kc.next().charAt(0);
}while(k=='M'||k=='m');
System.out.println("\nThank You for using our services.");
kc.close();
}
}
========================================================================
=====================
Q23
import java.util.Scanner;
class Stack
{
int a[],top;
Stack()
{
a=new int[100];
top=-1;
}

void insert_stackArray(int v)
{
if(top==-1)
{
System.out.println("First Value to be inserted.");
a[0]=v;
top++;
System.out.println("\n\nValue insertion successful.");
}
else if(top==a.length-1)
System.out.println("OverFlow.");
else
{
a[top++]=v;
System.out.println("\n\nValue insertion successful.");
}
}
void delete_stackArray()
{
if(top==-1)
System.out.println("UnderFlow.");
else
{
System.out.println(a[top]+" has been deleted.");
top--;
}
}

void traverse_stackArray()
{
for(int i=0;i<=top;i++)
System.out.print(a[i]+" ");
System.out.println("\n\nThere are "+(top+1)+" values.");
}

public static void main(String args[])


{
Scanner kc=new Scanner(System.in);
Stack kk=new Stack();
System.out.println("Enter 1 for Insertion.\nEnter 2 for Deletion.\nEnter 3 for Traverse.");
int ch=kc.nextInt();
int c;
do
{
switch(ch)
{
case 1:
System.out.println("Enter the value to be inserted.");
int v=kc.nextInt();
kk.insert_stackArray(v);
break;

case 2:
kk.delete_stackArray();
break;

case 3:
kk.traverse_stackArray();
break;
default:
System.out.println("\nInvalid Input.");
}
System.out.println("Enter 0 for Main Menu.");
c=kc.nextInt();
}while(c!=0);
System.out.println("\nThank You for using our services.");
kc.close();
}
}
========================================================================
=====================
Q24
//store and arrange the names and capitals of 15 countries alphabetically
import java.util.Scanner;
class Arrange
{
String nm[],cap[];
Arrange()
{
nm=new String[15];
cap=new String[15];
}

void readData()
{
Scanner kc=new Scanner(System.in);
for(int i=0;i<15;i++)
{
System.out.println("Enter the country name.");
nm[i]=kc.nextLine().trim().toUpperCase();
System.out.println("Enter the capital.");
cap[i]=kc.nextLine().trim().toUpperCase();
}
kc.close();
}

void arrange()
{
String k;
for(int i=0;i<15;i++)
for(int j=0;j<14-i;j++)
if(nm[j].charAt(0)>nm[j+1].charAt(0))
{
k=nm[j];
nm[j]=nm[j+1];
nm[j+1]=k;

k=cap[j];
cap[j]=cap[j+1];
cap[j+1]=k;
}
}

void disp()
{
System.out.println("Country\t\tCapital");
for(int i=0;i<15;i++)
System.out.println(nm[i]+"\t\t"+cap[i]);
}

public static void main(String args[])


{
Arrange kk=new Arrange();
kk.readData();
kk.arrange();
kk.disp();
}
}
========================================================================
=====================
Q25
//to display the number in fraction format
import java.util.Scanner;
class Fraction
{
int num, den;
Fraction(int n, int d)
{
num=n;
den=d;
}

int hcf(int n,int d)


{
int h=0;
for(int i=0;i<=Math.min(n,d);i++)
if(n%i==0 && d%i==0)
h=i;
return h;
}
void reduce()
{
int h=hcf(num,den);
if(h>0)
{
num=num/h;
den/=h;
}
}

void displayFraction()
{
System.out.println("The reduced fraction is "+num+"/"+den);
}

public static void main(String args[])


{
Scanner kc=new Scanner(System.in);
System.out.println("Welcome to RedHat Services.");
System.out.println("Enter the numerator value.");
int n=kc.nextInt();
System.out.println("Enter the denominator value.");
int d=kc.nextInt();
Fraction kk=new Fraction(n,d);
kk.reduce();
kk.displayFraction();
System.out.println("Thank you for using our services.\nHave a nice day. :)");
kc.close();
}
}
========================================================================
=====================
Q26
//fill the DDA with first 20 palindrome numbers columnwise
//import java.lang.*;
class PalinD
{
int val[][],r,c;
PalinD()
{
val=new int[4][5];
r=0;
c=0;
}

int isPalin(int p)
{
int dup=p,rev=0,d;
while(dup>0)
{
d=dup%10;
rev=rev*10+d;
dup/=10;
}
if(rev==p)
return 1;
else
return 0;
}

void fill()
{
int n=1;
for(c=0;c<5;c++)
{
for(r=0;r<4;)
{
if(isPalin(n)==1)
{
//System.out.println(n);
val[r][c]=n;
//n++;
r++;
}
n++;
}
}
}
void display()
{
System.out.println("The Palindrome numbers are:");
for(r=0;r<4;r++)
{
for(c=0;c<5;c++)
System.out.print(val[r][c]+"\t");
System.out.println("");
}
}

public static void main(String args[])


{
PalinD kk=new PalinD();
System.out.println("Welcome to Krishna's Services");
kk.fill();
kk.display();
}
}
========================================================================
=====================
Q27
//create linklist to store values
public class Node
{
int info;
Node next;
Node()
{
info=0;
next=null;
}
}

import java.util.Scanner;
public class LinkList
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Node fresh=null, ptr, prev=null, start=null;
int i;
System.out.println("Enter some numbers");
do
{
fresh=new Node();
System.out.println("Enter a number");
fresh.info=sc.nextInt();
if(start==null)
{
start=fresh;
}
else
{
prev.next=fresh;
}
prev=fresh;
System.out.println("\n\npress 1 to continue");
i=sc.nextInt();
}while(i==1);
System.out.println("\n\nHere are the numbers you have given: ");
System.out.println("Number\tAddress");
for(ptr=start;ptr!=null;ptr=ptr.next)
{
System.out.println(ptr.info+"\t"+ptr.next);
}
sc.close();
}
}
========================================================================
=====================
Q28
//insertion, deletion in stacked link list
public class Node
{
int info;
Node next;
Node()
{
info=0;
next=null;
}
}
import java.io.IOException;
import java.util.*;
class SLL
{
int counter=0;
Node start,fresh,ptr;
SLL()
{
start=null;
fresh=null;
ptr=null;
}

void insert(int v)
{
fresh=new Node();
fresh.info=v;
if(start==null)
start=fresh;
else
{
for(ptr=start;ptr.next!=null;ptr=ptr.next);
ptr.next=fresh;
fresh.next=null;
}
counter++;
System.out.println("\nNumber inserted successfully.");
}

void del()
{
if(start==null)
{
System.out.println("\n\nUnderflow");
System.out.println("\nThe linked list is empty.");
}
else if(start.next==null)
{
counter=0;
System.out.println("\n\n");
System.out.println(start.info+" has been deleted.");
start=null;
System.out.println("\nThe linked list is empty. You have deleted all the values. \nThank
you.");
}
else
{
for(ptr=start;ptr.next.next!=null;ptr=ptr.next);
System.out.println("\n\n"+ptr.next.info+" has been deleted.");
ptr.next=null;
counter--;
System.out.println("\nThere are total "+counter + " number of values remain in the linked
list.");
}
}

void traverse()
{
System.out.println();
if(start==null)
System.out.println("\n\nUnderflow");
for(ptr=start;ptr!=null;ptr=ptr.next)
System.out.println(ptr.info+ " ");
System.out.println("\nNumber of values found: "+counter);
}
//runner method for insertion, deleting and traverse for stacked link list
public static void main(String args[])throws IOException
{
Scanner sc=new Scanner(System.in);
int ch=0,n;
char x;
SLL ob=new SLL();
System.out.println("Welcome to the Integer Stacked Link List Wizard.\n");
do
{
System.out.println("Enter 1 for inserting a value.");
System.out.println("Enter 2 for deleting the last value in the stack.");
System.out.println("Enter 3 to display the current list.");
System.out.println("Enter any other value to exit the program.");
System.out.println("Enter your choice: ");
try
{
ch=sc.nextInt();
}
catch(InputMismatchException e)
{
System.out.println("\nThank you for your time. Have a good day. :)");
System.exit(0);
}
switch(ch)
{
case 1:
System.out.println("Enter a number");
n= sc.nextInt();
ob.insert(n);
break;

case 2:
ob.del();
break;

case 3:
ob.traverse();
break;
}
System.out.println("\n\nPress 'A' to come to main menu. ");
x=sc.next().charAt(0);
}while(x=='A'||x=='a');
sc.close();
}
}
========================================================================
=====================
Q29
//write a program top create an array in order to store 25 numbers
//then create a link list to store only the prime numbers present within the array //then
dislpay the values of the link list and also display how many prime numbers found public
class Node
{
int info;
Node next;
Node()
{
info=0;
next=null;
}
}
//connection between array and link list
import java.util.Scanner;
class PrimeLinkArr
{
public static void main(String args[])
{
Scanner kc=new Scanner(System.in);
int a[]=new int[25];
int n,k;
Node fresh=null,start=null,prev=null,ptr=null;
System.out.println("Enter 25 numbers");
for(int i=0;i<25;i++)
a[i]=kc.nextInt();
kc.close();
for(int i=0;i<25;i++)
{
n=a[i];
k=0;
for(int j=1;j<=n;j++)
if(n%j==0)
k++;
if(k==2)
{
fresh=new Node();
fresh.info=n;
if(start==null)
start=fresh;
else
prev.next=fresh;
prev=fresh;
}
}
if(start==null)
System.out.println("Sorry, No prime numbers found.");
else
{
k=0;
System.out.println("Prime Numbers\tAddress");
for(ptr=start;ptr!=null;ptr=ptr.next)
{
System.out.println(ptr.info+"\t\t"+ptr);
k++;
}
System.out.println("Thank You for using our services.\nHave a nice day :)");
}
}
}
========================================================================
=====================

You might also like