You are on page 1of 87

S.No.

TOPIC REMARKS
1 Program 1
2 Program 2
3 Program 3
4 Program 4
5 Program 5
6 Program 6
7 Program 7
8 Program 8
9 Program 9
10 Program 10
11 Program 11
12 Program 12
13 Program 13
14 Program 14
15 Program 15
16 Program 16
17 Program 17
18 Program 18
19 Program 19
20 Program 20
21 Program 21
22 Program 22


23 Program 23
24 Program 24
25 Program 25




Program 1: Write a program to enter a number at run time and
check if it is a Smith Number or not.
PROGRAM
import java.util.*;
class SmithNumber
{
static int n;
void input()
{
Scanner d=new Scanner(System.in);
System.out.println("Enter Number for checking");
n=d.nextInt ();
}
int CheckSmith()
{
int i,d,c,j,k,s=0,sum=0,t=n;
for(i=n;i>0;i=i/10)
{
d=i%10;
s=s+d;
}
t=n;
for(i=2;i<=n;i++)


{
if(t%i==0)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
{
for(k=i;k>0;k=k/10)
{
d=k%10;
sum=sum+d;
}
t=t/i;
i--;
}
}
}
if(sum==s)
return 1;


else
return 0;
}
public static void main(String args[])
{
SmithNumber obj=new SmithNumber();
System.out.println("INPUT :");
obj.input();
System.out.println("\nOUTPUT :");
int ch=obj.CheckSmith();
if(ch==1)
{
System.out.println(n+" is a Smith Number");
}
else
{
System.out.println(n+" is not a Smith Number");
}
}
}


Program 2: Write a program to enter a number at run time and
check if it is a Happy Number or not by using logical functions.
PROGRAM
import java.util.*;
class HappyNumber
{
static int n;
void input()
{
Scanner d=new Scanner(System.in);
System.out.println("INPUT :");
System.out.println("Enter Number for checking");
n=d.nextInt ();
}
int Happy(int num)
{
int i,s=0,d=0;
while(num>=10)
{
s=0;
for(i=num;i>0;i/=10)
{
d=i%10;


s=s+(d*d);
}
num=s;
}
if(s==1)
return 1;
else
return 0;
}
public static void main(String args[])
{
HappyNumber obj=new HappyNumber();
obj.input();
int ch=obj.Happy(n);
System.out.println("\nOUTPUT");
if(ch==1)
System.out.println(n+" is a Happy Number");
else
System.out.println(n+" is not a Happy Number");
}
}



Program 3: Write a program to print all special numbers between 1
to 1000.
PROGRAM
import java.util.*;
class SpecialNumber
{
int CheckSpecial(int num)
{
int i,d,f,j,s=0;
for(i=num;i>0;i=i/10)
{
d=i%10;
f=1;
for(j=1;j<=d;j++)
{
f=f*j;
}
s=s+f;
}
if(s==num)
return 1;
else
return 0;


}
public static void main(String args[])
{
int i,ch;
SpecialNumber obj=new SpecialNumber();
System.out.println("OUTPUT :");
System.out.print ("\nSpecial Numbers between 1-1000 are : ");
for(i=1;i<=1000;i++)
{
ch=obj.CheckSpecial(i);
if(ch==1)
System.out.print (i+" ");
}
System.out.println();
}
}


Program 4: Write a program to enter a Double -Dimensional array
at run time and print all elements of Upper Triangle.
PROGRAM
import java.util.*;
class UpperTriangle
{
static int size;
static int arr[][];
void input()
{
int i,j;
Scanner d=new Scanner(System.in);
System.out.println("Enter size of grid for square matrix");
size=d.nextInt ();
arr=new int [size][size];
System.out.println("Enter Elements of Matrix");
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
arr[i][j]=d.nextInt ();
}
}


}
void PrintMatrix()
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
System.out.print (arr[i][j]);
}
System.out.println();
}
}
void PrintTriangle()
{
int i,j,k;
for(i=0;i<size;i++)
{
for(k=0;k<i;k++)
{
System.out.print (" ");
}
for(j=i;j<size;j++)


{
System.out.print (arr[i][j]);
}
System.out.println();
}
}
public static void main(String args[])
{
UpperTriangle obj=new UpperTriangle();
System.out.println("INPUT :");
obj.input();
System.out.println("\nINPUTTED MATRIX");
obj.PrintMatrix();
System.out.println("\nOUTPUT :");
System.out.println("Upper Triangle");
obj.PrintTriangle();
}
}


Program 5: Write a program to store all Prime Tribonacci numbers
in an array and print all its elements.
PROGRAM
import java.util.*;
class PrimeTribonacci
{
static int num;
static int series[];
void input()
{
Scanner d=new Scanner(System.in);
System.out.println("Enter Number of terms");
num=d.nextInt ();
series=new int [num];
}
int Series()
{
int j,i=0,d=0,a=0,s=0,b=1,c=2,m=4;
series[0]=c;
i++;
while(m<=num)
{
s=0;


s=a+b+c;
a=b;
b=c;
c=s;
d=0;
for(j=1;j<=s;j++)
{
if(s%j==0)
d++;
}
if(d==2)
{
series[i]=s;
i++;
}
m++;
}
return i;
}
public static void main(String args[])
{
int i;
PrimeTribonacci obj=new PrimeTribonacci();


System.out.println("INPUT :");
obj.input();
int j=obj.Series();
System.out.println("\nOUTPUT");
System.out.print ("Prime Tribonacci Series : ");
for(i=0;i<j;i++)
{
System.out.print (series[i]+"\t");
}
System.out.println();
}
}


Program 6: Write a program to enter a Decimal Number at run
time and print its Binary Equivalent.
PROGRAM
import java.util.*;
class Binary
{
static int num;
static String bin=new String();
void input()
{
Scanner d=new Scanner(System.in);
System.out.println("Enter Number to print Binary Equivalent of");
num=d.nextInt ();
}
void binarychange(int n)
{
int i,d,s=0;
do
{
s=n%2;
bin=s+bin;
n=n/2;
}while(n!=0);


}
public static void main(String args[])
{
Binary obj=new Binary();
System.out.println("INPUT :");
obj.input();
obj.binarychange(num);
System.out.println("\nOUTPUT :");
System.out.println("Binary Equivalent of "+num+" is: "+bin);
}
}




Program 7: A class Rearrange has been defined to insert and delete
elements from array. Some of the members of class are given below:
Class name : Rearrange
Data members/instance variables
a[] :int eger type array
n :size of array(int eger)
pos1 :position of insertion (int eger)
pos2 :position of deletion (int eger)
ite :item to be inserted (int eger)
Member functions
void enter() :To enter size , array elements and to display the entered
elements.
void insert() :To accept element (item) to be inserted , position of
insertion and insert element at the position of insertion.
void disp1() :To display array after item is inserted.
void disp2() :To display array after item is removed.
void remov() :To accept the position of deletion and delete item to be
deleted.

PROGRAM
import java.util.*;
class rearrange
{
static int a[];


int b[];
int c[];
static int n;
int pos1,pos2,item;
void enter()
{
Scanner d=new Scanner(System.in);
System.out.println("INPUT");
System.out.println("Enter the size of array");
n=d.nextInt ();
a=new int [n];
System.out.println("Enter elements of array");
for(int b=0;b<n;b++)
{
a[b]=d.nextInt ();
}
System.out.println("Inputted Array");
for(int b=0;b<n;b++)
{
System.out.print (a[b]+" ");
}
System.out.println();
}


void insert()
{
b=new int [n+1];
Scanner d=new Scanner(System.in);
System.out.println("Enter the element and position where it is to be entered");
item=d.nextInt ();
pos1=d.nextInt ();
int i;
for(i=0;i<n;i++)
{
if((pos1-1)==i)
break;
else
b[i]=a[i];
}
b[i]=item;
for(int j=i+1;j<n+1;j++)
{
b[j]=a[j-1];
}
}
void disp1()
{


System.out.println("\nOUTPUT");
System.out.println("ARRAY AFTER INSERTION");
for(int j=0;j<n+1;j++)
System.out.print (b[j]+" ");
System.out.println();
}
void remov()
{
Scanner d=new Scanner(System.in);
c=new int [n];
System.out.println("\n");
System.out.println("ENTER THE POSITION OF DELETION");
pos2=d.nextInt ();
int k=0;
for(int i=0;i<n+1;i++)
{
if(i!=(pos2-1))
{
c[k]=b[i];
k++;
}
}
}


void disp2()
{
System.out.println("ARRAY AFTER DELETION");
for(int i=0;i<(n);i++)
{
System.out.print (c[i]+" ");
}
}
public static void main(String[]args)
{
rearrange obj=new rearrange();
obj.enter();
obj.insert();
obj.disp1();
obj.remov();
obj.disp2();
}
}



Program 8: A class modify is defined with following features
Class name : Modify
Data members
St :To store a string
len :To store length of the string
Member functions
void read() :To accept the String in uppercase
voidputin(int,char) :To insert a character at the specified position in string
and display the changed string.
void takeout(int) :To remove character from specified position in the
string
void change() :To replace each character in org string with character
which is at distance of 2.

Program:
import java.util.*;
class Modify
{
static String St="";
static StringBuffer res;
static int len=0;
static void read()
{
Scanner d=new Scanner(System.in);


System.out.println("Enter a string");
St=d.next();
St=St.toUpperCase();
System.out.println("String in uppercase is");
System.out.println(St);
len=St.length();
}
static void putin(int a,char ch)
{
ch=Character.toUpperCase(ch);
res= new StringBuffer(St);
if(a>=len)
res.append(ch);
else if(a>=0)
res.setCharAt(a-1,ch);
System.out.println(res);
}
static void takeout(int a)
{
if(a>=0)
{
res.delete(a-1,a);
System.out.println(res);


}
}
static void change()
{
for(int i=0;i<(len-1);i++)
{
char ch=res.charAt(i);
int x=(int)ch;
if(x>=65&&x<=88)
{
ch=(char)(x+2);
res.setCharAt(i,ch);
}
else
{
x=x-24;
ch=(char)x;
res.setCharAt(i,ch);
}
}
System.out.println(res);
}
public static void main(String[]args)


{
Scanner d=new Scanner(System.in);
char cm;
Modify ob=new Modify();
read();
System.out.println("ENTER THE ELEMENT AND POSITION WHERE IT IS TO
BE ENTERED");
cm=d.next().charAt(0);
Character.toUpperCase(cm);
int a=d.nextInt();
putin(a,cm);
System.out.println("Enter the position from element is to removed");
int pos=d.nextInt();
takeout(pos);
System.out.println("String with each element shifted two forward");
change();
}
}






Program 9: A transpose of any matrix is found by int erchanging
rows with columns. A class Transarray contains 2d array of order
[m]*[n].Details of this class Transarray is given below
Class name :Transarray
Data members
arr[][] :Store the matrix elements
m :Store the number of rows
n :Store the number of columns
Members Functions
Transarray () :default constructor
Transarray (int mm, int nn) :To initialize the size of matrix m=mm , n=mn
void fillarray() :to enter the elements of the matrix
void transpose(Transarray A) :to find the transpose of the given matrix
void disparray() :Display array in matrix form

PROGRAM
import java.util.*;
class transarray
{
int arr[][]=new int [20][20];
int m,n;
public transarray()
{


m=0;
n=0;
}
public transarray(int mm,int nn)
{
m=mm;
n=nn;
for(int a=0;a<20;a++)
{
for(int b=0;b<20;b++)
{
arr[a][b]=0;
}
}
}
void fillarray()
{
Scanner d=new Scanner(System.in);
System.out.println ("Enter elements of array: ");
for(int a=0;a<m;a++)
{
for(int b=0;b<n;b++)
{


arr[a][b]=d.nextInt ();
}
}
}
void transpose(transarray A)
{
for(int r=0;r<m;r++)
{
for(int c=0;c<n;c++)
{
A.arr[c][r]=arr[r][c];
}
}
}
void disparray(transarray B)
{
for(int r=0;r<m;r++)
{
for(int c=0;c<n;c++)
{
System.out.print (B.arr[r][c]+"\t");
}
System.out.println();}


}
public static void main()
{
Scanner d=new Scanner(System.in);
System.out.println ("INPUT ");
System.out.println("Enter the dimensions of array");
int a=d.nextInt ();
int b=d.nextInt ();
transarray c=new transarray();
transarray obj=new transarray(a,b);
obj.fillarray();
System.out.println("\nOUTPUT:");
System.out.println("Transposed Matrix");
obj.transpose(c);
obj.disparray(c);
}
}



Program 10: A class Numbers contains the following data members
and members functions to check for triangular numbers.[A
triangular numbers is formed by the addition of a consecutive
sequence of int egers starting from 1]
Class name :Numbers
Data members :
n :int egers to be checked for whether it is triangular or not
Member function methods
void getnum() :To accept int eger n
int check(int ) :To check if n is triangular
void dispnum() :To display message whether number is triangular or not
Program:
import java.util.*;
class Numbers
{
static int n;
static void getnum()
{
Scanner d=new Scanner(System.in);
System.out.println("INPUT:");
System.out.println("Enter the number to be checked");
n=d.nextInt ();
}


static int check(int x)
{
int tri=1,k=0;
for(int a=2;a<=x;a++)
{
tri=tri+a;
if(tri==x)
{
k=1;
break;
}
else if(tri>x)
{
k=0;
break;
}
}
return k;
}
static void dispnum()
{
getnum();
System.out.println("\nOUTPUT:");


int m=check(n);
if(m==1)
System.out.println("Number is triangular");
else
System.out.println("Number is not triangular");
}
public static void main(String[]args)
{
dispnum();
}
}



Program 11: A class Telcall () calculates the monthly phone bill of
consumer. Some of the members are given below:
Class Name :Telcall()
Data members
Phno :phone number
Name :name of the consumer
N :number of calls made
Amt :bill amount

Member Functions
Telcall() :Parameterised constructor to assign values to data
Members
Void compute() :To calculate phone bill according to the given slabs
Void dispdata() :To display details in specified format
Number of Calls
Rate
1 -100 Rs
500/- rental charge only 101-200
Rs 1.00 per call + rental charge
201-300 Rs 1.20
per call + rental charge
Above 300 Rs
1.50 per call +rental charge
Phone Number Name Total calls
Amount


PROGRAM:
import java.util.*;
class telcall
{
long phno;
String name;
int n;
double amt;
public telcall(long p,String s,int c,double a)
{
phno=p;
name=s;
n=c;
amt=a;
}
public void compute()
{
if(n<=100)
amt=500;
else if(n<=200)
amt=(n-100)*1.00 + 500;
else if(n<=300)
amt=(100*1.00)+((n-200)*1.20+500);


else
amt=(100*1.00)+(100*1.20)+((n-300)*1.50+500);
}
public void dispdata()
{
System.out.println("Phone Number\tName\t\t\tTotal Calls\tAmount");
System.out.println(" "+phno+"\t"+name+"\t\t\t"+n+"\t"+amt);
}
public static void main(String[]args)
{
Scanner d=new Scanner(System.in);
System.out.println("Enter your name");
String na=d.nextLine();
System.out.println("Enter your phone number");
long p=d.nextLong();
System.out.println("Enter number of phone calls");
int c=d.nextInt ();
double a=0.00;
telcall ob=new telcall(p,na,c,a);
ob.compute();
ob.dispdata();
}
}


Program 12: Write a program to enter of countries and their
corresponding capitals at runtime. If user enters a particular
country name to be searched in the list. If the country name exists it
should display the country name along with its capitals. Otherwise
it should display
Country name does not exist
Program:
import java.util.*;
class countries
{
String coun[];
String capitals[];
int n;
void input()
{
Scanner d=new Scanner(System.in);
System.out.println("INPUT");
System.out.println("Enter the number of countries you want to enter");
n=d.nextInt ();
coun=new String[n];
capitals=new String[n];
System.out.println("Enter the name of countries along with their capitals");
for(int j=0;j<=(n-1);j++)
{


System.out.println("Country name");
coun[j]=d.next();
System.out.println("Capital");
capitals[j]=d.next();
}
}
void search(String lm)
{
int i=0;
int k=0;
for(i=0;i<n;i++)
{
if(coun[i].equals(lm))
{
k=1;
break;
}
}
System.out.println("\nOUTPUT");
if(k==1)
{
System.out.println("Search Found!");
System.out.println("Country name "+coun[i]);


System.out.println("Capital "+capitals[i]);
}
else
System.out.println("Country name does not exist");
}
public static void main(String[]args)
{
countries ob=new countries();
ob.input();
Scanner d=new Scanner(System.in);
System.out.println("Enter the name of country to be searched");
String s=d.nextLine();
ob.search(s);
}
}


Program 13: Design a class change to convert a decimal number to
its equivalent in base 16 and to convert it back to its decimal form.
Eg.(i)The decimal number 35 is 23 in base 16.(ii)The number 107 is 6B in base 16.
Some of the members of the class are given below:
Class name : change
Data members/instance variables:
a[] : int eger type array
n : int eger to be converted to base 16
Member functions/methods:
change( ) : Constructor to assign 0 to instance variables
void input( ) : accepts int eger to be converted to base 16
void hexadeci(int ): to convert decimal int eger n to hexadecimal form
void decihexa( ) : to convert hexadecimal number back to decimal form.

Program:
import java.util.*;
class change
{
int a[];
static int n;
private final char[] DIGIT_TABLE={ '0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z'};


String hexnum;
public change()
{
a=new int [15];
n=0;
for(int i=0;i<15;i++)
a[i]=0;
}
public void hexadeci(int n1)
{
if(n<0)
{
System.out.print ("-");
n1=-n1;
hexnum="";
}
printRecursively(n1);
}
private void printRecursively(int n1)
{
if(n1>=16)
{
printRecursively(n1/16);
}
System.out.print (DIGIT_TABLE[n1%16]);


if(hexnum==null)
hexnum=String.valueOf(DIGIT_TABLE[n1%16]);
else
hexnum=hexnum+DIGIT_TABLE[n1%16];
}
public void input()
{
Scanner d= new Scanner(System.in);
System.out.println("Enter Integer:");
try
{
n= d.nextInt ();
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println();
}

public void decihexa()
{
int len =hexnum.length();
char ch;
int hexplacevalue=0,hexmul=1;


long decnum=0;
int n1=0,k=0;
for(int i=len-1;i>=0;i--)
{
ch=hexnum.charAt(i);
switch(ch)
{
case 'A': n1=10;break;
case 'B': n1=11;break;
case 'C': n1=12;break;
case 'D': n1=13;break;
case 'E': n1=14;break;
case 'F': n1=15;break;
default: n1=Integer.parseInt(String.valueOf(ch));break;
}
if(hexplacevalue==0)
decnum=decnum*10+1*n1;
else
decnum=hexmul*n1+decnum;
hexmul*=16;
hexplacevalue++;
}
System.out.println(hexnum+" Converted back to decimal as "+decnum);
}
public static void main(String[]args)throws Exception


{
change ob=new change();
System.out.println("INPUT");
ob.input();
System.out.println("OUTPUT");
ob.hexadeci(n);
System.out.println();
ob.decihexa();
}
}



Program 14: Write a program to enter all the elements of an integer
array at runtime. Now, by using binary search method (recursive
function) find a particular value entered by the user.

Program:
import java.util.*;
public class BinarySearchRecursive
{

static int A[]; int size=0,M=0;
void input()throws Exception
{
Scanner d= new Scanner(System.in);
System.out.print("Enter the size of the Array to be Searched : ");
size=d.nextInt();
A=new int[size];
System.out.println("Enter elements of the array");
for(int i=0;i<size;i++)
{
A[i]=d.nextInt();
}
}



void sort()
{
int temp,small,pos;
for(int i=0;i<A.length;i++)
{
small=A[i];
pos=i;
for(int j=i+1;j<A.length;j++)
if(A[j]<small)
{
small=A[j];
pos=j;
}
temp=A[i];
A[i]=A[pos];
A[pos]=temp;
}
System.out.println("\nSorted Array is : ");
for(int j=0;j<A.length;j++)
System.out.print(A[j]+" ");
}

void BinSearch(int S,int L,int U)


{
if(L<=U)
{
M=(int)(U+L)/2;
if(A[M]<S)
BinSearch(S,(M+1),U);
else if(A[M]>S)
BinSearch(S,L,(M-1));
else if(A[M]==S)
System.out.println(S+" found at Position "+(M+1));
}
else
System.out.println(S+" not found");
}

public static void main(String args[])throws Exception
{
Scanner d= new Scanner(System.in);
BinarySearchRecursive obj=new BinarySearchRecursive();
obj.input();
obj.sort();
System.out.print("\nEnter the Element to be Searched : ");
int a=d.nextInt();


obj.BinSearch(a,0,(A.length-1));
}
}



Program 15: PalPrime Numbers. Palprime numbers are palindromic
prime numbers i.e.,the prime numbers that are palindrome also.
Class Name: PalPrime
Member functions:
GenPalPrime(int n) : The passed number n tells the number of digits in the
palprime to be generated. Acceptable values for n are 2-5
You may define additional methods,if needed.

Program:
import java.util.*;
class PalPrime
{
public static boolean IsPrime(int n)
{
int mid = n/2;
for(int i=2;i<=mid;i++)
{
if(n%i==0)
return false;
}
return true;
}
public static boolean IsPal(int n)
{


int n1,rev=0,r;
n1=n;
while(n1!=0)
{
r=n1%10;
rev=rev*10+r;
n1=n1/10;
}
if(rev==n)
return true;
else
return false;
}
public static void GenPalPrime(int n)
{
int count=0;
if(n==2)
{
System.out.println("11");
count++;
}
else if(n==3)
{


int n1=101,nn;
for(int n2=2;n2<=9;n2++)
if(IsPrime(n2)==true)
{
nn=n1+n2*10;
if(IsPrime(nn)==true&&IsPal(nn)==true)
{
System.out.println(nn);
count++;
}
}
}
else if(n==4)
{
int n1=1001,nn;
for(int n2=10;n2<=99;n2++)
if(IsPrime(n2)==true)
{
nn=n1+n2*10;
if(IsPrime(nn)==true&&IsPal(nn)==true)
{
System.out.println(nn);
count++;


}
}
}
else if(n==5)
{
int n1=10001,nn;
for(int n2=100;n2<=999;n2++)
if(IsPrime(n2)==true)
{
nn=n1+n2*10;
if(IsPrime(nn)==true&&IsPal(nn)==true)
{
System.out.println(nn);
count++;
}
}
}
else
System.out.println("Value of n should be within range 2...5");
if(n>=2&&n<=5&&count==0)
{
System.out.println("No palprime numbers of this width.");
}


}
public static void main(String[]args)throws Exception
{
Scanner d= new Scanner(System.in);
System.out.println("INPUT:");
System.out.println("Enter the width of palprime numbers(ie no. of digits in a
palprime number )");
int n=d.nextInt ();
System.out.println("\nOUTPUT:");
System.out.println("Palprime numbers are: ");
GenPalPrime(n);
}
}



Program 16: Bruns Constant. Twin primes are the prime numbers
with a difference of 2 e.g.,(3,5),(5,7),(7,11),(11,13),(17,19),(29,31).etc.
The sum of reciprocals of the twin primes converges to a sum, know as Bruns
Constant i.e.,
The class Primes below contains methods which help you work with
twin primes and Bruns constant.Study the class and then answer
the questions that follow:
Class : Primes
Public Member Functions :
Boolean Is Prime(int n) :returns true if n is a prime number, false otherwise
void twinPrimes(int lim) : print s out twin primes in pairs.
double BrunConstant(int lim) : calculates and returns Bruns constant below lim

Program :
import java.util.*;
class Primes
{
public boolean IsPrime(int n)
{
int mid=n/2;
for(int i=2;i<=mid;i++)
{
if(n%i==0)
{


return false;
}
}
return true;
}
public void twinPrimes(int lim)
{
for(int i=3;i<=lim;i+=2)
{
if(IsPrime(i)==true&&IsPrime(i+2)==true)
{
System.out.println("("+i+","+(i+2)+")");
}
}
}
public double BrunConstant(int lim)
{
double s=0;
for(int i=3;i<=lim;i=i+2)
{
if(IsPrime(i)==true&&IsPrime(i+2)==true)
{
s+=(1/(float)(i)+1/(float)(i+2));


System.out.print ("+(1/"+i+"+1/"+(i+2)+")");
}
}
System.out.println();
return s;
}
public static void main(String[]args)throws Exception
{
Primes pr=new Primes();
Scanner d= new Scanner(System.in);
int n=0;
System.out.println("INPUT:");
System.out.print ("Enter limit: ");
n=d.nextInt ();
System.out.println("OUTPUT");
double brun=pr.BrunConstant(n);
System.out.println("Brun's constant below limit i.e. "+n+" is :"+brun);
}
}



Program 17: A super class Worker has been defined to store the
details of a worker. Define a subclass Wages to compute the monthly
wage for the worker. The details/specifications of both the classes
are given below:
Class name Worker
Data Members/instance variables :
Name : to store the name of the worker
Basic : to store the basic pay in decimals
Member functions
Worker(.) : Parameterized constructor to assign values to the instance
variables .
void display : to display the workers details
Class Name Wages
Data members/instance variables:
hrs : stores the hours worked
rate :stores rate per hour
wage :stores the overall wage of the worker
Member functions:
Wages() :Parameterized constructor assign values to the instance
variables of both the classes
double overtime() :Calculates and returns the overtime amount as
(hours*rate)
void display :Calculates the wage using the wage=overtime
amount+Basic pay and displays it along with other
details.


Program:
class Worker
{
String Name;
double Basic;
Worker(String nm,double bs)
{
Name =nm;
Basic=bs;
}
void display()
{
System.out.println("NAME :"+Name);
System.out.println("BASIC :"+Basic);
}
}
class Wages extends Worker
{
int hrs;
double rate;
double wage;
Wages(String nm,double bas,int hr,double rt)
{


super(nm,bas);
hrs=hr;
rate =rt;
}
double overtime()
{
return hrs*rate;
}
void display()
{
System.out.println("INPUT:");
super.display();
wage= overtime()+Basic;
System.out.println("\nOUTPUT:");
System.out.println("Hours Worked :"+ hrs);
System.out.println("Rate :"+rate);
System.out.println("Wage :"+wage);
}
}
public class finalres
{
public static void main()
{


Wages w=new Wages("ABC",1500.00,15,7.0);
w.display();
}
}



Program 18 : A class Employee contains employee details and
another class Salary calculates the employees net salary.The details
of the two classes are given below:
Class Name : Employee
Data members
empNo : stores the employees number
empName : stores the employees name
empDesign : stores the employees designation

Member functions :
Employee() : default constructor
Employee(..) : parameterized constructor to assign values to employee
number ,name and designation.
void display() : display the employees details
Class name : Salary
Data member
basic : float variable to store the basic pay
Member functions :
Salary(.) : parameterized constructor assign values to data members
void calculate() : calculate the employee salary according to following rules:
DA =10%of basic, HRA=15%of basic, Salary =basic +DA+HRA, PF =8% of
Salary, Net salary=Salary- PF
Display the employee details and the Net salary.



Program:
class Employee
{
int empNo;
String empName;
String empDesig;
public Employee()
{
empNo=0;
}
public Employee(int eno,String ename,String edesig)
{
empNo=eno;
empName=ename;
empDesig=edesig;
}
public void display()
{
System.out.println("Employee No.:"+ empNo);
System.out.println("Employee Name :"+empName);
System.out.println("Employee Designation:"+empDesig);
}
}


class Salary extends Employee
{
float basic;
public Salary(int eno,String enm,String edg,float bas)
{
super(eno,enm,edg);
basic=bas;
}
public void calculate()
{
float salary,netsalary;
float DA,HRA,PF;
DA=basic*0.10f;
HRA=basic*0.15f;
PF=basic*0.08f;
salary=basic+DA+HRA;
netsalary=salary-PF;
System.out.println("INPUT:");
super.display();
System.out.println("\nOUTPUT:");
System.out.println("Net salary: "+netsalary);
}
}


public class Test
{
public static void main(String[]args)
{
Salary s1=new Salary(234,"Reva","Mgr",13000);
s1.calculate();
}
}



Program 19:Write a program to print the sum of the following
series:-
Sum=1-x+x
4
/3!-x
7
/6!+x
10
/9!.........................upto n terms.

Program:
import java.util.*;
class series
{
static int fact(int c)
{
int f=1;
for(int i=1;i<=c;i++)
{
f=f*i;
}
return f;
}
public static void main(String[]args)throws Exception
{
Scanner d= new Scanner(System.in);
int x,n;
System.out.println("INPUT:");
System.out.println("Enter the Number of elements");


n=d.nextInt ();
System.out.println("Enter the value of x");
x=d.nextInt ();
double s=0,a,b;
for(int j=1;j<=n-2;j++)
{
a=fact(j*3);
b=Math.pow(x,((j*3)+1));
s=s+(b/a);
if(j==2)
{
s=-s;
}
}
s=s+1-x;
System.out.println("\nOUTPUT");
System.out.println("Sum of the given series is"+ s);
}
}



Program 20:Write a program to print the primorial of 1-100
numbers.For Example: 5#= 2*3*5=30

Program:
import java.util.*;
public class primorial
{
public int num=0;
}
public void findprimorial(int num)
{
int res=2,flag=0;
String str="2";
for(int i=3;i<=num;i++)
{
flag=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
flag=1;
break;
}


}
if(flag!=1)
{
res*=i;
str=str+ "*" +i;
}
}
System.out.println(num +"= " +str +"=" +res);
}
public static void main()
{
primorial ob=new primorial();
Scanner d= new Scanner(System.in);
System.out.println("INPUT");
System.out.println("Enter a number");
int n= d.nextInt();
System.out.println("\nOUTPUT");
ob.findprimorial(n);
}
}




PROGRAM NO. 21 : Write a program to enter all the array
elements at run time in an input function named void input().Do the
following tasks using different functions.
Reverse(): to reverse all the array elements by using the same array.
Delete(): to delete one elements from the array by specifying the position.
Prime(): to print all prime elements from the array.
Program:
import java.util.*;
class arrays
{
int n;
int arr[];
int ar[];
void input()
{
Scanner d= new Scanner(System.in);
System.out.println("Enter the size of array");
n=d.nextInt();
arr=new int[n];
System.out.println("Enter the elements of array");
for(int a=0;a<n;a++)
{
arr[a]=d.nextInt();


}
ar=new int[n-1];
}
void reverse()
{
int temp=0;
for(int a=0;a<n;a++)
{
temp=arr[a];
arr[a]=arr[n-a-1];
arr[n-a-1]=temp;
}
for(int a=0;a<n;a++)
{
System.out.print(arr[a]+"\t");
}
}
void delete()
{
System.out.println();
int k=0;
Scanner d=new Scanner(System.in);
System.out.println("Enter the position from array to be deleted");


int b=d.nextInt();
for(int a=0;a<n;a++)
{
if((b-1)!=a)
{
ar[k]=arr[a];
k++;
}
}
System.out.println("\nOUTPUT");
System.out.println("The new array is");
for(int a=0;a<(n-1);a++)
{
System.out.print(ar[a]+"\t");
}
}
void prime()
{
System.out.println();
System.out.println("The prime elements of the array are");
for(int a=0;a<(n-1);a++)
{
int c=0;


int m=ar[a];
for(int i=1;i<=m;i++)
{
if((m%i)==0)
c++;
}
if(c==2)
{
System.out.print(m+"\t");
}
}
}
public static void main(String args[])
{
arrays ob=new arrays();
System.out.println("INPUT");
ob.input();
ob.reverse();
ob.delete();
ob.prime();
}
}


Program 22:A class Mixer has been defined to merge two sorted integer arrays
in ascending order.Some of the members of the class are given below:
Data Members:
int arr[] :to store elements of the array
int n :to store size
Member functions:
Mixer(int nn) :constructor to assign n=nn.
void accept() :to accept the elements of the array in
ascending order without any duplicates.
Mixer mix(Mixer A) :to merge the current object array elements with the
parameterized array elements and return the resultant object.
void display() :to display the elements of the array.

Program
import java.util.*;
class Mixer{
private int arr[ ];
private int n;
public Mixer( int nn ){
n = nn;
arr = new int[ n ];
}
public void accept(){


Scanner sc = new Scanner( System.in );
for( int i=0; i< n; i++ ){
arr[ i ] = sc.nextInt();
}
}
public Mixer mix( Mixer A ){
Mixer ans = new Mixer( this.arr.length + A.arr.length );
int p1, p2, p3;
p1 = p2 = p3 = 0;
while( p1 < this.arr.length && p2 < A.arr.length ){
if( this.arr[ p1 ] < A.arr[ p2 ] ){
ans.arr[ p3++ ] = this.arr[ p1++ ];
}else{
ans.arr[ p3++ ] = A.arr[ p2++ ];
}
}
if( p1 < this.arr.length ){
for( int i = p1 ; p1 < this.arr.length; i++ ){
ans.arr[ p3++ ] = this.arr[ p1++ ];
}
}else if( p2 < A.arr.length ){
for( int i = p2 ; p2 < A.arr.length; i++ ){
ans.arr[ p3++ ] = this.arr[ p2++ ];


}
}
return ans;
}
public void display(){
for( int i=0; i < arr.length; i++ ){
System.out.print( arr[i] + " " );
}
System.out.println();
}
public static void main( String args[] ){
Mixer obj1 = new Mixer(5);
Mixer obj2 = new Mixer(3);
obj1.accept();
obj2.accept();
Mixer obj3 = obj1.mix( obj2 );
obj3.display();
}
}



Program 23: A magic number is a number in which the eventual sum
of digits if the number is equal to 1.
Design a class magic to check if a given number is a magic number. Some of the
members of the class are given below:
CLASS NAME : Magic
DATA MEMBERS:
n :stores the number
MEMBER FUNCTIONS:
Magic() :constructor to assign 0 to n
void getnum(int nn) :to assign parameter value to the number, n=nn
int Sum_of_digits(int) :returns the sum of the digits of a number
void ismagic() :checks if the given number is a magic number by calling
the function Sum_of_digits(int) and displays appropriate
message.
Specify the class Magic giving details of the constructor, void getnum(int), int
Sum_of_digits(int) and void ismagic().

Program:
import java.util.*;
class Magic
{
int n;
Magic()
{


n=0;
}
void getnum(int nn)
{
n=nn;
}
int sum_of_digits(int num)
{
int d=0, sum=0;
while(num>0)
{
d=num%10;
sum=sum+d;
num=num/10;
}
return sum;
}
void ismagic()
{
int num;
int sum= sum_of_digits(n);
while(sum>9)
{


num=sum;
sum=sum_of_digits(num);
}
if(sum==1)
System.out.println(n + " is a Magic Number.");
else
System.out.println(n+ " is not a Magic Number.");
}
public static void main(String[]args) throws Exception
{
Scanner d= new Scanner(System.in);
Magic ob = new Magic();
System.out.println("Enter Number");
int a= d.nextInt();
ob.getnum(a);
ob.ismagic();
}
}


Program 24: A class Collection contains an array of 100 integers.
Using the following class description create an array with common
elements from two integer arrays. Some of the members if the class
are given below:
CLASS NAME :Collection
DATA MEMBERS:
arr[] : integer array
len :length of the array
MEMBER FUNCTIONS:
Collection() :default constructor
Collection(int) :Parameterized constructor to assign the length of
the array
void inparr() :to accept the array elements
Collection common(Collection) :returns a Collection containing the common
elements of current Collection object and the
collection object passed as parameter
void arrange :sort the array element of the object containing
common elements in ascending order using any
sorting technique
void display() :displays the array elements
Specify the class Collection giving details of the constructors, void
inparr() and void arrange(), Collection common(Collection).
Program:
import java.util.*;
public class Collection


{
int arr[];
int len;
public Collection()
{
arr= new int[100];
len=100;
}
public Collection(int size)
{
arr=new int[size];
len=size;
}
public void inputarr()
{
Scanner d= new Scanner(System.in);
int val=0;
for(int i=0; i<len; ++i)
{
System.out.println("Enter Element " + (i+1));
try
{
val=d.nextInt();


}
catch(Exception e)
{
System.out.println("error in reading/ invalid integer");
}
arr[i]= val;
}
}
private void setLength(int le)
{
len=le;
}
public Collection common(Collection c2)
{
Collection c3= new Collection();
int val;
int k=0;
for(int i=0; i<len; ++i)
{
val=arr[i];
for(int j=0; j< c2.arr.length; ++j)
{
if(val==c2.arr[j])


{
c3.arr[k]=val;
k++;
}
}
}
c3.setLength(k);
return c3;
}
public void display()
{
System.out.println("Common Elements");
for(int i=0;i<len; i++)
{
System.out.print(arr[i]+ "\t");
}
System.out.println();
}
public void arrange()
{
for(int i=len-1; i>=0; i--)
{
int highestIndex=i;


for(int j=1; j>=0; j--)
{
if(arr[j]>arr[highestIndex])
highestIndex=j;
}
int temp=arr[i];
arr[i]=arr[highestIndex];
arr[highestIndex]= temp;
}
}
public static void main(String[]args) throws Exception
{
Collection c=new Collection();
Collection c1=new Collection(5);
Collection c4=new Collection(5);
System.out.println("INPUT");
c1.inputarr();
c4.inputarr();
Collection c5=c1.common(c4);
c5.arrange();
System.out.println("\nOUTPUT:");
c5.display();
}


}



Program 25: Design a class Change to perform string related
operations. The details of the class are given below:
CLASS NAME :Change
DATA MEMBERS:
str : stores the word
newstr :stores the changed word
len :stores the length of the word
MEMBER FUNCTIONS:
Change() :default constructor
void inputword() :to accept a word
char caseconvert(char ch) :converts the case of the character and returns it
void recchange(int) :extracts character using recursive technique and
changes its case using caseconvert() and forms a new
word
void display() :displays both the words
Specify the class Change giving details of the constructor, and
member functions void inputword(), char caseconvert(char ch), void
recchange(it) and void display().
Program:
import java.util.*;
class Change
{
String str, newstr;
int len;


Change()
{
str=new String();
newstr=new String();
len=0;
}
void inputword()
{
Scanner d= new Scanner(System.in);
str= d.nextLine();
len=str.length();
}
char caseconvert(char ch)
{
int asc= (int) ch;
if(asc>=65 && asc<=90)
return (char) (ch+32);
else if(asc>=97 && asc<=122)
return (char) (ch-32);
else
return ch;
}
void recchange(int n)


{
if(n==len)
return;
else
{
newstr = newstr+ caseconvert(str.charAt(n));
recchange(n+1);
}
}
void display()
{
System.out.println("String : " + str);
System.out.println("Changed String : " + newstr);
}
public static void main(String[]args)
{
Change object= new Change();
System.out.println("Enter Word : ");
object.inputword();
object.recchange(0);
object.display();
}
}

You might also like