Computer Project Final
Computer Project Final
Program 1
/**
* This program returns all the Smith numbers in the range of 1 to n
* A Smith number id if the sum of the digits and the sum of prime factors are same.
*/
import java.util.*;
public class Smith
{public int sum(int a)
{int d, s2=0;
while(a>0)
{d=a%10;
s2=s2+d;
a=a/10;
}
return(s2);
}
public void main()
{
Scanner in=new Scanner(System.in);
int n,s1=0,i,n1,c=0,j,s=0;
System.out.println("Enter limit");
n=in.nextInt();
Smith ob=new Smith();
for(j=1;j<=n;j++)
{
s1=0;
s=0;
1
s1=ob.sum(j);
n1=j;
i=2;
while(n1>1)
{if(n1%1==0)
{n1=n1/i;
s=s+ob.sum(i);
}
else
i++;
}
if(s==s)
System.out.println("Smith number= "+j);
}
}
}
2
Program 2
/**
* This program finds out the smallest value of n such that
* 4/2!+8/3!+16/4!+.........2^n/n!>=s where 2.0<s<7.0
* and also prints the sum of the series
*/
import java.util.*;
class Series
{int n,i;
double s;
Series()
{s=0;
n=0;
}
void accept()
{ Scanner in=new Scanner(System.in);
System.out.println("Enter value of n");
n=in.nextInt();
}
long fact(long g)
{
long f=1;
int j;
for(j=1;j<=g;j++)
f=f*j;
return(f);
}
void display()
{double p,k;
for(i=2;i<=n;i++)
3
{
p=Math.pow(2,i);
k=fact(i);
s=s+(double)p/k;
if(s>2.0&&s<7.0)
{break;
}
}
}
}
4
Program 3
/**
* This program converts a decimal number to its equivalent in base16 and vice versa
*/
import java.util.*;
class Change
{
int a[]=new int[50];
int n,i;
Change()
{
for(i=0;i<50;i++)
{
a[i]=0;
}
n=0;
}
void input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a decimal number");
n=in.nextInt();
}
void decihexa(String str)
{
int a=0,len,p=0,k=0;
double d=0,s=0;
String str1="";
char ch;
len=str.length();
5
p=len;
for(i=0;i<len;i++)
{ ch=str.charAt(i);
if(ch>='A')
{k=(int)ch-55;
}
else
{ str1=str1+ch;
k=Integer.parseInt(str1);
str1="";
}
d=Math.pow(16,(p-1))*k;
s=s+d;
p--;
}
System.out.println("The decimal equivalent of hexadecimal number is"+str+"is");
System.out.print((int)s);
}
void hexadeci()
{
int i=0,c=0,r,t;
String str2="";
System.out.print("The hexadecimal equivalent of decimal number"+n+"is");
while(n>0)
{
r=n%16;
a[i]=r;
n=n/16;
i++;
c++;
6
}
for(i=c-1;i>=0;i--)
{
if(a[i]>=10)
{ t=a[i]-10;
t=65+t;
str2=str2+(char)t;
}
else
str2=str2+(char)(48+a[i]);
}
System.out.print(str2);
System.out.println();
decihexa(str2);
}
}
7
Program 4
/**
* This program finds out if a number is a Magic number
* A magic number is a number where the eventual sum of digits of the number is 1
*/
import java.util.*;
class Magic
{ int ar[]=new int[150];
int n,i;
Magic()
{
for(i=0;i<150;i++)
ar[i]=0;
}
Magic(int nx)
{
n=nx;
}
void input_numbers()
{ Scanner in=new Scanner(System.in);
for(i=0;i<n;i++)
{System.out.println("Enter a number");
ar[i]=in.nextInt();
}
}
void find_print_magic()
{
int p,s=0,d;
for(i=0;i<n;i++)
8
{
p=ar[i];
while(p>9)
{s=0;
while(p>0)
{d=p%10;
s=s+d;
p=p/10;
}
p=s;
}
if(s==1)
{System.out.println(ar[i]+"is a Magic number");
}
}
}
}
9
Strings
Program 1
/**
* This program accepts the names of N teams,where 2<n<9 and displays them
* in a vertical order, side by side with a horizontal tab space
*/
import java.io.*;
class TeamNames
{ public void main()throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int n,i,j,max;
System.out.println("Enter number of teams");
n=Integer.parseInt(in.readLine());
if(n<=2||n>=9)
System.out.println("Invalid Input");
else
{
String name[]=new String[n];
int len[]=new int[n];
for(i=0;i<n;i++)
{
name[i]=in.readLine();
len[i]=name[i].length();
}
max=0;
for(i=0;i<n;i++)
if(len[i]>max)
max=len[i];
10
for(i=0;i<max;i++)
{ for(j=0;j<n;j++)
{ if(i<len[j])
System.out.print(name[j].charAt(i)+"\t");
else
System.out.print("\t");
}
System.out.println();
}
}
}
}
11
Program 2
/**
* This programs exhanges the first and last letter of every word of a sentence
*/
import java.util.*;
class Exchange
{String sent,rev;
int size;
Exchange()
{
sent="";
rev="";
size=0;
}
void readsentence()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a sentence to reverse the words");
sent=in.nextLine();
}
void exfirstlast()
{
String s="",st;
int l;
char c;
size=sent.length();
for(int i=0;i<size;i++)
{
c=sent.charAt(i);
12
if(c!=' ' && c!='.')
s=s+c;
else
{ l=s.length();
if(l>1)
{
st=s.charAt(l-1)+s.substring(1,l-1)+s.charAt(0);
rev=rev+' '+st;
}
else
rev=rev+' '+s;
s="";
}
}
}
void display()
{ System.out.println("Original String");
System.out.println(sent);
System.out.println("reversed Stting");
System.out.println(rev);
}
public void main()
{ Exchange ob=new Exchange();
ob.readsentence();
ob.exfirstlast();
ob.display();
}
13
Program 3
/**
* This program converts a sentence by arranging the words in the order of their length.
* The sentence can be terminated only with a '.','?' or '!' and has to be in UPPERCASE
*/
import java.util.*;
14
strArr[j] = strArr[j+1];
strArr[j+1] = t;
}
}
}
return sb.toString().trim();
}
if (str.charAt(len - 1) != '.'
&& str.charAt(len - 1) != '?'
&& str.charAt(len - 1) != '!') {
System.out.println("INVALID INPUT");
return;
} String sortedStr = sortString(str.substring(0, len - 1));
System.out.println(str);
System.out.println(sortedStr); } }
15
Program 4
/**
* This program converts the non-palindrome words of a sentence into palindrome words
* by concatenating the word by its reverse.
* The String should terminate only by '.','!', or '?' and should be in uppercase
*/
import java.util.*;
return palin;
}
16
i--;
}
return sb.toString();
}
17
while (st.hasMoreTokens()) {
String word = st.nextToken();
boolean isPalinWord = isPalindrome(word);
if (isPalinWord) {
sb.append(word);
}
else {
String palinWord = makePalindrome(word);
sb.append(palinWord);
}
sb.append(" ");
}
System.out.println();
System.out.println(ipStr);
System.out.println(convertedStr);
}
}
18
Program 5
/**
* This program helps to encrypt a message using Ceaser Cypher(rotate by 13 places)
* The string must be greater than length 3 and smaller than 100
*/
import java.util.Scanner;
19
sb.append(ch);
}
}
20
Program 6
/**
* This program finds out the number of words which start and end with a vowel.
* It places the words which start and end with vowels at the beginning and
* places the rest of the words as they occur in the sentence.
*/
import java.util.*;
21
int c = 0;
while (st.hasMoreTokens()) {
String word = st.nextToken();
int wordLen = word.length();
if (isVowel(word.charAt(0))
&& isVowel(word.charAt(wordLen - 1))) {
c++;
sbVowel.append(word);
sbVowel.append(" ");
}
else {
sb.append(word);
sb.append(" ");
}
}
String newStr = sbVowel.toString() + sb.toString();
22
2D Arrays
Program 1
import java.util.*;
/**
* This program is a switch case program in which you can either print the
* sum ofleft diagonal or the right diagonal or print their difference.
*/
public class Diagonals
{//start of class
public void main()
{//start of void main
Scanner in=new Scanner(System.in);
System.out.println("What do you want to do");
System.out.println("1.Left diagonal \n 2.Right Diagonal \n 3.Difference between their
sums");
int a=in.nextInt();
int arr[][]=new int[4][4]; int suml=0; int sumr=0;
for(int i=0;i<4;i++)
{for(int j=0;j<4;j++)
{ arr[i][j]=in.nextInt();
}
}
switch(a)
{
case 1:
//left diagonal
for(int i=0;i<4;i++)
{for(int j=0;j<4;j++)
23
{ if(i==j)
{suml=suml+arr[i][j];
}}
}
System.out.println(suml);
break;
case 2:
//right diagonal
for(int i=3; i>=0;i--)
{for(int j=3;j>=0;j--)
{ if((i+j)==(4-1))
{sumr=sumr+arr[i][j];
}
}
}
System.out.println(sumr);
break;
case 3:
System.out.println(suml-sumr);
}
}
}
24
Program 2
/**
* This program finds the highest and the lowest number in a 2D nxn array
*/
import java.util.Scanner;
public class HighestLowest
{public void main()
{ Scanner in=new Scanner(System.in);
System.out.println("Enter the size of array n");
int n=in.nextInt();
int arr[][]=new int[n][n];
int min; int max;
System.out.println("Enter the numbers in the array");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=in.nextInt(); //input
}
}
min=arr[0][0];
max=arr[0][0];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(arr[i][j]<min)
{
min=arr[i][j];
}
25
if(arr[i][j]>max)
{
max=arr[i][j];
}
}
}
System.out.println("The highest number is= "+max);
System.out.println("The lowest number is= "+min);
}
}
26
Program 3
/**
* This programs displays the sum of the border elements in a nxn matrix
*/
import java.util.Scanner;
public class BorderElements
{public void main()
{ Scanner in=new Scanner(System.in);
System.out.println("Enter the value for the size of array");
int n=in.nextInt();
int m[][]=new int[n][n];
int s1=0; int s2=0;
System.out.println("Enter elements in the array");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
m[i][j]=in.nextInt();
}}
for(int i=0;i<n;i++)
{for(int j=0;j<n;j++)
{ s1=s1+m[i][j];
}
}
for(int i=1;i<n-1;i++)
{
for(int j=1;j<n-1;j++)
{
s2=s2+m[i][j]; }}
System.out.println("The sum of the border elements are "+(s1-s2)); }}
27
Program 4
/**
* A square matrix is said to be a magic square if sum of each row, column and diagonal is
the same.
* User can enter the dimension of the matrix and a magic square will be created
*/
import java.util.*;
public class magicNumber
{//start of class
public void main()
{//start of main mehod
Scanner in=new Scanner(System.in);
int i,j,k,n,t;
System.out.println("Enter matrix dimension of a magic square");
System.out.println("Enter number of rows");
n=in.nextInt();
int a[][]=new int[n][n]; //creating array
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
a[i][j]=0;
}
if(n%2!=0)
{
i=0; j=n/2; k=1;
while(k<=n*n)
{
a[i][j]=k++;
i--;
j++;
28
if(i<0&&j>n-1)
{
i=i+2;
j--;
}
if(i<0)
i=n-1;
if(j>n-1)
j=0;
if(a[i][j]>0)
{
i=i+2;
j--;
}
}
}
else
{k=0;
for( i=0;i<n;i++)
{for(j=0;j<n;j++)
a[i][j]=++k;
}
j=n-1;
for(i=0;i<n/2;i++)
{t=a[i][i];
a[i][i]=a[j][j];
a[j][j]=t;
t=a[i][j];
a[i][j]=a[j][i];
a[j][i]=t;
29
j--;
}
}
System.out.println("Magic square of size "+n+" x" +n+ "as shown below");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}//end of class
//end of program
30
Program 5
import java.util.*;
/**
* This program arranges numbers in an ascending order in a 4x4 matrix
*/
public class Arrange
{public void main()
{Scanner in=new Scanner(System.in);
int i,j,k,t;
int m[][]=new int[4][4];
System.out.println("Enter the elements in the first array of 4x4 matrix");
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
{
m[i][j]=in.nextInt();
}
System.out.println("The elements of first matrix 4x4 matrix are ");
for(i=0;i<4;i++)
{ for(j=0;j<4;j++)
{System.out.print(m[i][j]+" ");
}
System.out.println();
}
for(i=0;i<4;i++)
{for(j=0;j<3;j++)
{for(k=0;k<3-j;k++)
{if(m[i][k]>m[i][k+1])
{t=m[i][k];
m[i][k]=m[i][k+1];
m[i][k+1]=t;
31
}
}
}
}
System.out.println("The elements after arrange each row in ascending order is= ");
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
{System.out.print(m[i][j]+ " ");
}
System.out.println();
}
}
}
}
32
Program 6
import java.util.*;
/**
* This program fills a square matrix(nxn) in a circular way(clockwise) with natural numbers
*/
public class circularFill
{public void main()
{Scanner in=new Scanner(System.in);
int n, r1,r2,c1,c2,k=1,i,j;
System.out.println("Enter the size of the matrix");
n=in.nextInt();
int a[][]=new int[n][n];
c1=0;c2=n-1; r1=0;r2=n-1;
do
{ for(i=c1;i<=c2;i++)
{a[r1][i]=k;
k++;
}
for(j=r1+1;j<=r2;j++)
{ a[j][c2]=k;
k++;
}
for(i=c2-1;i>=c1;i--)
{a[r2][i]=k;
k++;
}
for(j=r2-1;j>=r1+1;j--)
{a[j][c1]=k;
k++;
}
33
c1++;
c2--;
r1++;
r2--;
}
while(k<=n*n);
System.out.println("Circulae matrix ");
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}
34
Inheritance
Program1
/**
* This program takes inputs and prints student related information like name,roll no, date of
birth, marks in various subjects
* total percentage and grade of student.
*/
import java.util.*;
public class Student
{Scanner in=new Scanner(System.in);
String name;
int dob;
int r;
void inputdata()
{ System.out.println("Enter name");
name=in.nextLine();
System.out.println("Enter date of birth");
dob=in.nextInt();
System.out.println("Enter roll no");
r=in.nextInt();
}
void printdata()
{ System.out.println("Name: "+name);
System.out.println("Date of birth" +dob);
System.out.println("Roll number "+ r);
}
} //end of base class
class Marks extends Student
{ //derived class
35
int p,c,m,cts,e,tot=0;
float per=0;
char gd;
void readata()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter marks in Physics");
p=in.nextInt();
System.out.println("Enter marks in Chemistry");
c=in.nextInt();
System.out.println("Enter marks in Math");
m=in.nextInt();
System.out.println("Enter marks in Computer SC");
cts=in.nextInt();
System.out.println("Enter marks in English");
e=in.nextInt();
}
void compute()
{ tot=p+c+m+cts+e;
per=(tot*100)/500;
if(per>=90)
gd='A';
if(per>=60 && per<90)
gd='B';
if(per>=40&&per<60)
gd='C';
else if(per<40)
gd='D';
}
void showdata()
36
{printdata();
System.out.println("Marks in physics= "+p);
System.out.println("Marks in Chemistry= "+c);
System.out.println("Marks in Maths= "+m);
System.out.println("Marks in Computer SC= "+cts);
System.out.println("Marks in English"+e);
System.out.println("Percentage Marks"+per);
System.out.println("Grade obtained "+gd);
}
}//end of derived class
37
Program 2
/**
* This program removes duplicate repeated letters in a string.
* example: Ccccommputterrrr issss a subbbbjjjeeectttt
* Output: Computer is a subject
*/
import java.util.*;
class Sentence
{ protected String str;
Sentence()
{
str="";
}
void Accept()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter name with duplicate letters");
str=in.nextLine();
}
void Display()
{System.out.println(str);
}
} //end of base class
class Duplicate extends Sentence
{//derived class
int len;
void removeDuplicate()
{str=str+"";
len=str.length();
char b,c;
38
char st[]=new char[len];
int i,p;
String mdstr="";
p=len;
for(i=0;i<p;i++)
{
c=str.charAt(i);
st[i]=c;
}
for(i=0;i<p-1;i++)
{
c=st[i];
b=st[i+1];
if(b==c)
continue;
else
mdstr=mdstr+c;
}
39
Program 3
/**
* This program is about a Library who issues books on a rental basis at 2% charge on cost
price per day. The book can
* be retained for 7 days without fine but if it extends that a fine will be charged.
*/
import java.util.*;
public class Library
{ String name;
String author;
double p;
Library(String nm,String au,double pr)
{
name=nm;
author=au;
p=pr;
}
void display()
{ System.out.println("Title = " +name );
System.out.println("Author "+author);
System.out.println("Price"+p);
}
}//end of base class
class Compute extends Library
{ int d;
double f;
Compute(String n,String a,double prc, int dy)
{
super(n,a,prc);
d=dy;
f=0.0D;
40
}
void fine()
{
if(d>7)
{
if((d-7)<=5)
f=2*(d-7);
else
if((d-7)>5&&(d-7)<=10)
f=3*(d-7);
else
f=5*(d-7);
}
}
void display()
{
super.display();
double total;
System.out.println("No of days taken to return the book "+d);
System.out.println("Fine charged "+f);
total=(2.0/100.0*p)+f;
System.out.println("Total amount to be paid"+total);
}
}
41
Program 4
/**
* This program stores names and ranks of 30 students and finds the highest rank along with
the name
*/
import java.util.*;
class Record
{String name[]=new String[30];
int rnk[]=new int[30];
Record()
{
for(int i=0;i<30;i++)
{name[i]="";
rnk[i]=0;
}
}
void readValues()
{Scanner in=new Scanner (System.in);
for(int i=0;i<30;i++)
{
System.out.println("Enter name");
name[i]=in.next();
System.out.println("Enter student");
rnk[i]=in.nextInt();
}
}
void display()
{
System.out.println("Name\t"+"Rank");
for(int i=0;i<50;i++)
{
42
System.out.println(name[i]+"\t"+rnk[i]);
}
}
}//end of base class
class Rank extends Record
{
int index;
Rank()
{index=0;
}
void highest()
{for(int i=0;i<30;i++)
{if(rnk[i]<rnk[index])
index=i;
}
}
void display()
{ super.display();
System.out.println("Topmost rank: "+rnk[index]);
System.out.println("Name with topmost rank" +name[index]);
}
}//end of derived class
43
Program 5
/**
* This program calculates the perimeter and area of a parallelogram
*/
import java.util.*;
class Perimeter
{double a,b;
Perimeter(double x,double y)
{a=x;
b=y;
}
double calculate()
{double z;
z=2*(a+b);
return(z);
}
void show()
{System.out.println("Length of parallelogram"+a);
System.out.println("Breadth of parallelogram"+b);
System.out.println("Perimeter of parallelogram" +calculate());
}
}//end of base class
class Area extends Perimeter
{ double h;
double area;
Area(double m, double n, double ht)
{
super(m,n);
h=ht;
}
44
void doarea()
{area=b*h;
}
void show ()
{
super.show();
System.out.println("Height of the parallelogram "+h);
System.out.println("Area of parallelogram "+area);
}
}//end of derived class
45
Program 6
/**
* This program stores details of employees and calculates the extra amount paid for working
more than normal hours
*/
import java.util.*;
class Employee
{ int empc;
double bpay;
Employee()
{empc=0;
bpay=0;
}
void Employee(int c, double b)
{
empc=c;
bpay=b;
}
void Display()
{
System.out.println("Employee code= "+empc);
System.out.println("Basic pay= "+bpay);
}
} //end of base class
class Overtime extends Employee
{
int nd;
double rate;
Overtime(int n, double r)
{
46
nd=n;
rate=r;
}
double Calculate()
{
double ts;
ts=bpay+(nd*rate);
return(ts);
}
void Show()
{
Display();
double x=Calculate();
System.out.println("Number of days worked"+nd);
System.out.println("Amount paid for extra hours"+(nd*rate));
System.out.println("Salary= "+x);
}
}
47
Data Structures
i)Queue
/**
* This is a queue program. It allows user to add elements(from rear) and remove
elements(from front)from an array of size 100
*/
public class Queue
{ int st[]=new int[100];
int capacity;
int f;
int r;
Queue(int m)
{ capacity=m;
f=-1;
r=-1;
}
void pushvalue(int v)
{ if(r==capacity-1)
System.out.println("Overflow");
else
{ if(f==-1 && r==-1)
{f=0; r=0;
}
else
r=r+1;
st[r]=v;
}
}
int popvalue()
48
{
int v;
if(r==-1&&f==-1)
return(-9999);
else
{v=st[f];
if(f==r)
{f=-1;
r=-1;
}
else
f=f=+1;
return(v);
}
}
void display()
{
if(f==-1 && r==-1)
System.out.println("List Underflows");
else
{ System.out.println("Elements of the list");
for(int i=f;i<=r;i++)
System.out.println(st[i]);
}
}
}
49
ii)Stack
/**
* This is a Stack program which pushes and pops String from the top only. It also counts
number of elements in stack
*/
import java.util.*;
public class Stack
{ String st[]=new String[50];
int size;
int top;
int ctr;
Stack()
{ top=-1;
ctr=0;
}
Stack(int cap)
{
size=cap;
}
void pushname(String n)
{
if(top ==size -1)
System.out.println("Overflow");
else
{
top++;
st[top]=n;
ctr++;
}
50
}
String popname()
{
String v;
if(top==-1)
{
System.out.println("Underflow");
return("");
}
else
{
v=st[top];
top--;
ctr--;
return(v);
}
}
void display()
{
if(top==-1)
System.out.println("Underflow");
else
for(int i=top; i>0;i--)
{
System.out.println(st[i]);
System.out.println("Number of elements in the stack \t" +ctr);
}
}
}
51
Recursion
Program1
/**
* This program helps to take an input of a number and reverse the digits of the number using
recirsive function
*/
import java.util.*;
public class reverseDigit
{ public int rev(int n,int r)
{
if(n==0)
return(r);
else
{r=r*10+n%10;
return(rev(n/10,r));
}
}
public void main()
{ Scanner in=new Scanner(System.in);
System.out.println("Enter a number to be reversed");
int num=in.nextInt();
int res=rev(num,0);
System.out.println("Number after reversing the digits"+res);
}
}
52
Program 2
/**
* This program converts a decimal number to its Binary equivalent using recursive function
*/
import java.util.*;
class Binary
{ public int recbin(int n,String s)
{if(n==0)
return(Integer.parseInt(s));
else
s=Integer.toString(n%2)+s;
return(recbin(n/2,s));
}
public void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a decimal number to be converted to binary");
int num=in.nextInt();
int res=recbin(num, "");
System.out.println("Binary equivalent "+res);
}
}
53
Program 3
/**
* Reverses the String by using Recursive function
*/
import java.util.*;
public class reverseString
{
public String reverse(String s, int p, String rev)
{
if(p<0)
return(rev);
else
{ rev=rev+s.charAt(p);
return(reverse(s,p-1,rev));
}
}
public void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a string to be reversed");
String st=in.nextLine();
String res=reverse(st,st.length()-1,"");
System.out.println("String after reversing= "+res);
}
}
54
Program 4
/**
* This program finds the factorial for the number using recursive function
*/
import java.util.*;
public class Factorial
{ int n,f;
Factorial()
{
int n=0;
f=0;
}
int fact(int num)
{ n=num;
if(n==0)
return(1);
else
return(n*fact(n-1));
}
void get(int x)
{ n=x;
f=fact(n);
System.out.println("The factorial of "+x+"is "+f);}
public void main()
{ int m;
Scanner in=new Scanner(System.in);
System.out.println("Enter a number for its factorial");
m=in.nextInt();
Factorial ob=new Factorial();
ob.get(m); }}
55
Program 5
/**
* This program displays the prime numbers from a list of numbers given by user by using
recursive function
*/
import java.util.*;
public class Prime
{
Scanner in=new Scanner(System.in);
int limit;
int arr[]=new int[150];
Prime()
{
int i;
for(i=0;i<150;i++)
{
arr[i]=0;
}
}
void readList()
{
int j;
System.out.println("Enter a number of elements to store");
limit=in.nextInt();
System.out.println("Enter elements");
for(j=0;j<limit;j++)
{
arr[j]=in.nextInt();
}
}
int Isprime(int num,int j,int f)
56
{
if(j==num)
return (f);
else
{if(num%j==0)
f=0;
return(Isprime(num,++j,f));
}
}
void PrintPrime()
{
int v;
for(int i=0;i<limit;i++)
{
v=Isprime(arr[i],2,1);
if(v==1)
System.out.println("Number"+arr[i]+"is prime");
}
}
}
57
Program 6
/**
* This program checks whether the given positive number is a palindrome or not(if number
and its reverse are same)
*/
import java.util.*;
public class Palin
{ int num;
int revnum;
Scanner in=new Scanner(System.in);
Palin()
{
num=0;
revnum=0;
}
void accept()
{
System.out.println("Enter a number");
num=in.nextInt();
}
int reverse(int y)
{
if(y==0)
return revnum;
else
{revnum=revnum*10+y%10;
return reverse (y/10);
}
}
void check()
58
{
if(num==reverse(num))
System.out.println(num+ "Is a palindrome number");
else
System.out.println(num+" Is not a palindrome number");
}
public void main()
{
Palin ob=new Palin();
ob.accept();
ob.check();
}
}
59