Comp Project Final
-
Upload
er-manika-vishnoi -
Category
Documents
-
view
223 -
download
0
Transcript of Comp Project Final
-
8/11/2019 Comp Project Final
1/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 1314 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
-
8/11/2019 Comp Project Final
2/87
23 Program 23
24 Program 24
25 Program 25
-
8/11/2019 Comp Project Final
3/87
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
-
8/11/2019 Comp Project Final
4/87
{
if(t%i==0)
{
c=0;
for(j=1;j0;k=k/10)
{
d=k%10;sum=sum+d;
}
t=t/i;
i--;
}
}
}
if(sum==s)
return 1;
-
8/11/2019 Comp Project Final
5/87
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");
}
}
}
-
8/11/2019 Comp Project Final
6/87
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;
-
8/11/2019 Comp Project Final
7/87
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");
}
}
-
8/11/2019 Comp Project Final
8/87
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
-
8/11/2019 Comp Project Final
9/87
}
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
-
8/11/2019 Comp Project Final
10/87
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
-
8/11/2019 Comp Project Final
11/87
}
void PrintMatrix()
{
int i,j;
for(i=0;i
-
8/11/2019 Comp Project Final
12/87
{
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();
}
}
-
8/11/2019 Comp Project Final
13/87
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
-
8/11/2019 Comp Project Final
14/87
s=a+b+c;
a=b;
b=c;
c=s;
d=0;
for(j=1;j
-
8/11/2019 Comp Project Final
15/87
System.out.println("INPUT :");
obj.input();
int j=obj.Series();
System.out.println("\nOUTPUT");
System.out.print ("Prime Tribonacci Series : ");
for(i=0;i
-
8/11/2019 Comp Project Final
16/87
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);
-
8/11/2019 Comp Project Final
17/87
}
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);
}
}
-
8/11/2019 Comp Project Final
18/87
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[];
-
8/11/2019 Comp Project Final
19/87
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
-
8/11/2019 Comp Project Final
20/87
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
-
8/11/2019 Comp Project Final
21/87
System.out.println("\nOUTPUT");
System.out.println("ARRAY AFTER INSERTION");
for(int j=0;j
-
8/11/2019 Comp Project Final
22/87
void disp2()
{
System.out.println("ARRAY AFTER DELETION");
for(int i=0;i
-
8/11/2019 Comp Project Final
23/87
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);
-
8/11/2019 Comp Project Final
24/87
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);
-
8/11/2019 Comp Project Final
25/87
}
}
static void change()
{
for(int i=0;i=65&&x
-
8/11/2019 Comp Project Final
26/87
{
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();
}
}
-
8/11/2019 Comp Project Final
27/87
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()
{
-
8/11/2019 Comp Project Final
28/87
m=0;
n=0;
}
public transarray(int mm,int nn)
{
m=mm;
n=nn;
for(int a=0;a
-
8/11/2019 Comp Project Final
29/87
arr[a][b]=d.nextInt ();
}
}
}
void transpose(transarray A)
{
for(int r=0;r
-
8/11/2019 Comp Project Final
30/87
}
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);
}
}
-
8/11/2019 Comp Project Final
31/87
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 ();
}
-
8/11/2019 Comp Project Final
32/87
static int check(int x)
{
int tri=1,k=0;
for(int a=2;ax)
{
k=0;break;
}
}
return k;
}
static void dispnum()
{
getnum();
System.out.println("\nOUTPUT:");
-
8/11/2019 Comp Project Final
33/87
-
8/11/2019 Comp Project Final
34/87
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
-
8/11/2019 Comp Project Final
35/87
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
-
8/11/2019 Comp Project Final
36/87
-
8/11/2019 Comp Project Final
37/87
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
-
8/11/2019 Comp Project Final
38/87
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
-
8/11/2019 Comp Project Final
39/87
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);
}}
-
8/11/2019 Comp Project Final
40/87
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'};
-
8/11/2019 Comp Project Final
41/87
String hexnum;
public change()
{
a=new int [15];
n=0;
for(int i=0;i
-
8/11/2019 Comp Project Final
42/87
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;
-
8/11/2019 Comp Project Final
43/87
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
-
8/11/2019 Comp Project Final
44/87
{
change ob=new change();
System.out.println("INPUT");
ob.input();
System.out.println("OUTPUT");
ob.hexadeci(n);
System.out.println();
ob.decihexa();
}
}
-
8/11/2019 Comp Project Final
45/87
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
-
8/11/2019 Comp Project Final
46/87
void sort()
{
int temp,small,pos;
for(int i=0;i
-
8/11/2019 Comp Project Final
47/87
{
if(L
-
8/11/2019 Comp Project Final
48/87
obj.BinSearch(a,0,(A.length-1));
}
}
-
8/11/2019 Comp Project Final
49/87
Program 15:PalPr ime 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
-
8/11/2019 Comp Project Final
50/87
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)
{
-
8/11/2019 Comp Project Final
51/87
int n1=101,nn;
for(int n2=2;n2
-
8/11/2019 Comp Project Final
52/87
}
}
}
else if(n==5)
{
int n1=10001,nn;
for(int n2=100;n2=2&&n
-
8/11/2019 Comp Project Final
53/87
}
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);
}
}
-
8/11/2019 Comp Project Final
54/87
Program 16: Bruns Constant. Twin primes are the prime numbers
with a difference of 2e.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 asBruns
Constant i.e.,
The class Primes below contains methods which help you work with
twin primes and Bruns constant.Study theclass 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
-
8/11/2019 Comp Project Final
55/87
return false;
}
}
return true;
}
public void twinPrimes(int lim)
{
for(int i=3;i
-
8/11/2019 Comp Project Final
56/87
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);
}
}
-
8/11/2019 Comp Project Final
57/87
Program 17: A super class Workerhas been defined to store the
details of a worker. Define a subclass Wagesto 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.
-
8/11/2019 Comp Project Final
58/87
-
8/11/2019 Comp Project Final
59/87
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()
{
-
8/11/2019 Comp Project Final
60/87
-
8/11/2019 Comp Project Final
61/87
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.
-
8/11/2019 Comp Project Final
62/87
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);
}
}
-
8/11/2019 Comp Project Final
63/87
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);
}
}
-
8/11/2019 Comp Project Final
64/87
public class Test
{
public static void main(String[]args)
{
Salary s1=new Salary(234,"Reva","Mgr",13000);
s1.calculate();
}
}
-
8/11/2019 Comp Project Final
65/87
Program 19:Write a program to print the sum of the following
series:-
Sum=1-x+x4/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
-
8/11/2019 Comp Project Final
66/87
n=d.nextInt ();
System.out.println("Enter the value of x");
x=d.nextInt ();
double s=0,a,b;
for(int j=1;j
-
8/11/2019 Comp Project Final
67/87
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
-
8/11/2019 Comp Project Final
68/87
}
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);
}
}
-
8/11/2019 Comp Project Final
69/87
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
-
8/11/2019 Comp Project Final
70/87
}
ar=new int[n-1];
}
void reverse()
{
int temp=0;
for(int a=0;a
-
8/11/2019 Comp Project Final
71/87
int b=d.nextInt();
for(int a=0;a
-
8/11/2019 Comp Project Final
72/87
int m=ar[a];
for(int i=1;i
-
8/11/2019 Comp Project Final
73/87
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(){
-
8/11/2019 Comp Project Final
74/87
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++ ];
-
8/11/2019 Comp Project Final
75/87
}
}
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();
}
}
-
8/11/2019 Comp Project Final
76/87
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()
{
-
8/11/2019 Comp Project Final
77/87
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)
{
-
8/11/2019 Comp Project Final
78/87
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();
}
}
-
8/11/2019 Comp Project Final
79/87
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 thecollection 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
-
8/11/2019 Comp Project Final
80/87
{
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
-
8/11/2019 Comp Project Final
81/87
}
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
-
8/11/2019 Comp Project Final
82/87
{
c3.arr[k]=val;
k++;
}
}
}
c3.setLength(k);
return c3;
}
public void display()
{
System.out.println("Common Elements");
for(int i=0;i=0; i--)
{
int highestIndex=i;
-
8/11/2019 Comp Project Final
83/87
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();
}
-
8/11/2019 Comp Project Final
84/87
}
-
8/11/2019 Comp Project Final
85/87
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;
-
8/11/2019 Comp Project Final
86/87
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=97 && asc
-
8/11/2019 Comp Project Final
87/87
{
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 : ");