Final Project File Class 12
Final Project File Class 12
PRACTICAL FILE
SESSION
2023-2024
Write a program in Java to enter a natural number. Display all the possible
combinations of consecutive natural numbers which adds up to give the sum equal to
the original number.
import java.util.*;
int i,j,k,n,sum;
n=sc.nextInt();
for(i=1;i<=n/2;i++)
sum=0;
for(j=i;j<=n/2;j++)
sum=sum+j;
if(sum==n)
break;
if(j<=n/2+1)
for(k=i;k<=j;k++)
System.out.println(k+"");
System.out.println();
}
}
}
PROGRAM
Happy number is a number in which the eventual sum of square of its digit is equal to
1.Write a program to check if a given number is a happy number or not. Specify the
method Number(int n) that accepts a number from the main method. It returns 1(one)
or 0(zero) accordingly.
import java.util.*;
int p,s=0,d;
p=n;
while(p>9)
s=0;
while(p>0)
d=p%10;
s= s+d*d;
p=p/10;
p=s;
return(s);
int k,m;
System.out.println("Enter the number");
m=sc.nextInt();
k=ob.Number(m);
if(k==1)
else
}
PROGRAM.
Write a program to accept a string and pass it to method void Replace (String wd).The
method further replaces each letter by the next letter in opposite case. The conversion
follows round robin system (i.e., ‘Z’ follows ‘a’ and ‘z’ follows ‘A’).
import java.util.*;
int i,p,k;
char chr;
k=wd.length();
for(i=0;i<k;i++)
chr=wd.charAt(i);
if(chr>='a'&&chr<='y')
p=(int)(chr-31);
chr=(char)(p);
else if(chr=='z')
p=(int)(chr-57);
chr=(char)(p);
else if(chr>='A'&&chr<='Y')
p=(int)(chr+33);
chr=(char)(p);
else if(chr=='Z')
p=(int)(chr+7);
chr=(char)(p);
System.out.print(chr);
String str;
System.out.println("Enter a word");
str=sc.next();
ob.Replace(str);
}
PROGRAM
Write a program to accept two words and check whether they are Anagram or not. The words
that are made with the combinations of the letters present in the original words are called
Anagram.
import java.util.*;
int p1,p2,i,j,as1,as2,s1=0,s2=0;
String str1,str2,str3="",str4="";
char chr1,chr2;
str1=sc.next();
str1=str1.toUpperCase();
str2=sc.next();
str2=str2.toUpperCase();
p1=str1.length();
p2=str2.length();
if(p1==p2)
for(i=65;i<=90;i++)
for(j=0;j<p2;j++)
chr1=str1.charAt(j);
chr2=str2.charAt(j);
as1=(int)chr1;
as2=(int)chr2;
if(as1==i)
str3=str3+chr1;
s1=s1+as1;
if(as2==i)
str4=str4+chr2;
s2=s2+as2; } } }
if(str3.equals(str4)&&(s1==s2))
else
else
}
PROGRAM
Write a class sentence to store a sentence and another class Duplicate to replace the duplicate
characters.The details of the classes are given below.
Class Name : Sentence
Data members/Instance variables : to store a sentence in the string variable of
protected type.
Members functions/Methods
Sentence( ) : constructor to assign string variable
void Accept( ) : to accept a sentence in the variable str
void Display( ) : to display the sentence after performing the
task .
Class Name : Duplicate
Data members/Instance variables :
p : to store the length of sentence of str
Member functions/methods :
Void remove Duplicate( ) : to remove the duplicate characters of each
word in Sequence so that it should have only
one occurrence. The new sentence should
possess only one space
import java.util.*;
Sentence()
str="";
void Accept()
str=sc.nextLine();
void Display()
System.out.println(str);
}// end of base class
int len;
void removeDuplicate()
str=str+"";
len=str.length();
char b,c;
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;
System.out.println("Original String:"+str);
System.out.println("Modified String:"+mdstr);
}
import java.util.*;
String str=sc.nextLine();
int len=str.length();
if(len<=3||len>=100)
System.out.println("INVALID LENGTH");
return;
for(int i=0;i<len;i++)
char ch=str.charAt(i);
if((ch>='A'&&ch<='M')||(ch>='a'&&ch<='m'))
sb.append((char)(ch+13));
else if((ch>='N'&&ch<='Z')||(ch>='n'&&ch<='z'))
sb.append((char)(ch-13));
else
sb.append(ch);
String cipher=sb.toString();
System.out.println(cipher);
}
PROGRAM
A square matrix is said to be a magic square, if the sum of each row, each column and each
diagonal is same. Write a program to enter an integer number N. Create a magic square of size
N x N.. Finally, print the elements of the matrix as magic square.
import java.util.*;
int i,j,k,n,t;
n=sc.nextInt();
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++;
{
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;
j--;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
PROGRAM
import java.util.*;
int i,j,k,ld=0,rd=0;
for(i=0;i<rc;i++)
for(j=0;j<rc;j++)
System.out.print(arr[i][j]+"");
System.out.println();
for(i=0;i<rc;i++)
ld=ld+arr[i][i];
k=rc-1;
for(i=0;i<rc;i++)
rd=rd+arr[i][k];
k=k-1;
}
int i,j,rc;
rc=sc.nextInt();
for(i=0;i<rc;i++)
for(j=0;j<rc;j++)
m[i][j]=sc.nextInt();
ob.Diagonal(m,rc);
}
PROGRAM
Write a program in Java to enter a natural number. Display all the possible
combinations of consecutive natural numbers which adds up to give the sum equal to
the original number.
import java.util.*;
int i,j,k,n,sum;
n=sc.nextInt();
for(i=1;i<=n/2;i++)
sum=0;
for(j=i;j<=n/2;j++)
sum=sum+j;
if(sum==n)
break;
if(j<=n/2+1)
for(k=i;k<=j;k++)
System.out.println(k+"");
System.out.println();
}
}
}
PROGRAM
Write a menu driven program to perform the given task as per the user’s choice :
Arrange the digits of the entered number in ascending order and display the result.
(ii) a number is said to be a ‘Magic Number’, if eventual sum of the digits of the number is
equal to 1 , otherwise not . Enter a number and check whether it is a magic number or not.
import java.util.*;
int i,j,p,r,ch,n;
ch=sc.nextInt();
switch(ch)
case 1:
System.out.println("Enter a number");
n=sc.nextInt();
for(i=0;i<10;i++)
p=n;
while(p!=0)
{
r=p%10;
if(r==i)
System.out.print(r+",");
p=p/10;
System.out.println();
break;
case 2:
int a ,b,c,s;
System.out.println("Enter a number");
n=sc.nextInt();
s=n;
while(s>9)
n=s;s=0;
while(n>10)
a=n/10;
b=a*10;
c=n-b;
s=s+c;
n=a;
if(s==1)
System.out.println("Magic Number");
else
break;
default:
}
PROGRAM
Write a program to input a String and reverse it by using recursive function . Display the new
String formed after reversal.
import java.util.*;
if(p<0)
return(rev);
else
rev=rev+s.charAt(p);
return(reverse(s,p-1,rev));
} }
String st=sc.nextLine();
String res=reverse(st,st.length()-1,"");
}}
PROGRAM
Write a program ( Using Scanner Class ) to enter a token / word in a mixed case and display the
new token after deleting all the vowels. Finally arrange the new token in alphabetical order of
letters.
import java.util.*;
inti,j,p,q;
char ch,ch1;
String st,st2="",st3="";
System.out.println("Enter a word");
st=sc.next();
p=st.length();
for(i=0;i<p;i++)
ch=st.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
continue;
st2=st2+ch;
q=st2.length();
for(i=65;i<=90;i++)
for(j=0;j<q;j++)
{
ch1=st2.charAt(j);
if(ch1==(char)i||ch1==(char)(i+32))
st3=st3+ch1;
}
PROGRAM
Write a program in java to accept a decimal number ( base 10 ) . Convert the decimal number to a
hexadecimal number and display the result.
import java.util.*;
int i=0,c=0,n,r;
n=sc.nextInt();
while(n>0)
r=n%16;
ar[i]=r;
n=n/16;
i++;
c++;
for(i=c-1;i>=0;i--)
if((ar[i]>=10)&&(ar[i]<=15))
if(ar[i]==10)
System.out.println("A");
if(ar[i]==11)
System.out.println("B");
if(ar[i]==12)
System.out.println("C");
if(ar[i]==13)
System.out.println("D");
if(ar[i]==14)
System.out.println("E");
if(ar[i]==15)
System.out.println("F");
else
System.out.print(ar[i]);
System.out.println();
}
PROGRAM
Write a program in java to accept a string from the user. Count and display the number of uppercase
characters , the number of lowercase characters , and the number of vowels as per the User’s choice
import java.util.*;
int i,p,up=0,low=0,v=0,ch;
String str;
char chr;
System.out.println("Enter a string");
str=sc.nextLine();
p=sc.nextInt();
ch=sc.nextInt();
switch(ch)
case 1:
for(i=0;i<p;i++)
chr=(str.charAt(i));
if(Character.isUpperCase(chr))
up=up+1;
break;
case 2:
for(i=0;i<p;i++)
chr=(str.charAt(i));
if(Character.isLowerCase(chr))
low=low+1;
break;
case 3:
for(i=0;i<p;i++)
chr=(str.charAt(i));
if((chr=='A')||(chr=='E')||(chr=='I')||(chr=='O')||(chr=='U')||(chr=='a')||(chr=='e')||
(chr=='i')||(chr=='o')||(chr=='u'))
v=v+1;
break;
default:
System.out.println("Wrong choice!!!");
}
PROGRAM
Using scanner class , write a program to enter a text line ending with a space and terminated with a
full stop ( . ) . Display the frequency of the word ‘The / the’ in the given text line. However the word
‘The / the’ should not be a substring of another word .
import java.util.*;
public class Frequency
{
public static void main(String args[])
int c=0;
String st;
while(true)
st=sc.next();
if(st.equals("The")||st.equals("the"))
c=c+1;
if(st.equals("."))
break;
}}
PROGRAM
A class Myarray contains an array of n integers (n<=100) that are already arranged in ascending
order. The subscripts of the array elements vary from 0 to n-1. Some of the members functions/
methods of Myarray are given below:
Class Name : Myarray
Data members/Instance varibles :
arr : an array of n integers.
n : size of the array .
Member functions/ methods :
Myarray() : Constructor to initialize 0 in each memory location
of
the array arr [].
voidreadarray( ) : read n integers that are arranged in ascending order.
voiddisplayarray( ) : displays n integers.
intbinarysearch(int value) : searches for the ‘value’ in the array using the binary
search technique. It returns the subscript of the array
Element if the value is found , otherwise it
Returns – 999.
Specify the class Myarray giving the details of the void displayarray( ) and intbinarysearch( int value)
only . You may assume that the other functions/methods are written for you . You do not need to write
the main function.
Import java.util.*;
intar[]=new int[100];
intn,i;
Myarray(int nm)
n=nm;
for(i=0;i<100;i++)
ar[i]=0;
voidreadarray()
for(i=0;i<n;i++)
System.out.println("Enter a number");
ar[i]=sc.nextInt();
voiddisplayarray()
for(i=0;i<n;i++)
System.out.print(ar[i]+"");
System.out.println();
intbinary_search(int value)
intlb,ub,mid,found=0,ret;
lb=0;
ub=n-1;
mid=0;
while(found==0&&lb<=ub)
mid=(lb+ub)/2;
if(value<ar[mid])
ub=mid-1;
if(value>ar[mid])
lb=mid+1;
if(value==ar[mid])
found=1;
if(found==1)
ret=value;
else
ret=-999;
return(ret);
}
PROGRAM
Write a program to input ‘n’ integer numbers ( positive or negative ) in a Single Dimensional Array
(SDA ). Pass the array to a function sort ( int arr[ ] ) . Arrange all the numbers in ascending order and
display the result.
import java.util.*;
int p=arr.length;
int i,j,t;
for(i=0;i<(p-1);i++)
for(j=i+1;j<p;j++)
if(arr[i]>arr[j])
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
for(i=0;i<p;i++)
System.out.println(arr[i]);
}
public static void main(String args[])
int n,i;
n=sc.nextInt();
for(i=0;i<n;i++)
m[i]=sc.nextInt();
ob.sort(m,n);
}
PROGRAM
Design a program which accepts your date of birth in mm ddyyyy format . Check whether the date is
a valid date or not . If it is valid , display “ Valid Data” . It also computes and displays the day number
of the year for the date of birth. If it is invalid , display “Invalid Data” and terminate the program.
importjava.util.*;
inti,d,m,y,f=1,s=0;
int nod[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
d=sc.nextInt();
m=sc.nextInt();
y=sc.nextInt();
if(m>12)
f=0;
if(m==2)
if(y%4==0)
if(d>29)
f=0;
else
if(d>28)
f=0;
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
{
if(d>31)
f=0;
else
if(d>30)
f=0;
if(f==1)
System.out.println("Date is valid");
i=1;
while(i<m)
s=s+nod[i];
i++;
s=s+d;
else
System.out.println("Invalid date");
}}
PROGRAM
A Gold Batch Number is a positiveeven integer that can be expressed as the sum of two primes.
Note : All integer number s greater than 4 are goldbatch numbers.
import java.util.*;
int c=0;
for(int i=1;i<=num;i++)
if(num%i==0)
c++;
return c==2;
int n= sc.nextInt();
if(n<=9||n>=50)
return;
if(n%2!=0)
{
return;
int a=3;
int b=0;
while(a<=n/2)
b=n-a;
if(isPrime(a)&&isPrime(b))
System.out.println(a+","+b);
a+=2;
PROGRAM
Checks whether the ISBN code is valid or not.
Sample Input :
0201103311
Sample Output :
Sum= 55
LEAVES NO REMAINDER – VALID ISBN CODE
import java.util.*;
public class ISBN
String s;
int sum=0,l,i;
char c;
s=sc.nextLine();
l=s.length();
if(l==10)
for(i=10;i>1;i--)
c=s.charAt(10-i);
if((c=='x'||c=='X'))
sum=sum+(10*i);
else
sum=sum+(Integer.parseInt(c+"")*i);
}
}
System.out.println("SUM="+sum);
if(sum%11==0)
else
System.out.println("INVALID INPUT");
PROGRAM
import java.util.*;
int n=sc.nextInt();
if(n<1||n>1000)
System.out.println("INVALID INPUT");
return;
int cartonSizes[]={48,24,12,6};
int total=0;
int t=n;
for(int i=0;i<cartonSizes.length;i++)
int cartonCount=t/cartonSizes[i];
t=t%cartonSizes[i];
total+=cartonCount;
if(cartonCount!=0)
System.out.println(cartonSizes[i]+"*"+cartonCount+"="+(cartonSizes[i]*cartonCount));
}}
if(t!=0){
System.out.println("Remaining boxes="+t+"*1+"+t);
total++;
else{
System.out.println("Remaining boxes=0");
PROGRAM
Write a program to input a String and reverse the original String by using recursive function.
Display the new String.
import java.util.*;
class strrev
{
if(I<0)
return(rev);
else
rev=rev+s.charAt(I);
return(reverse(s,I-1,rev));
String st=sc.next();
String res=reverse(st,st.length()-1,"");
}
PROGRAM
A bank intends to design a program to display the denominations of an input amount , up to 5 digits.
The available denomination with the bank are of rupees 2000,500,200,100,50,20,10 and 1.
Design a program to accept the amount from the user and display the break-up in descending order of
denominations (i.e.preference should be given to the highest denomination available ) along with the
total number of votes.
import java.util.*;
int amt=sc.nextInt();
if(amt>99999)
System.out.println("Invalid Amount");
return;
String amtlnWords=getAmtlnWords(amt);
System.out.println("amtln words");
System.out.println("Denomionations");
int notes[]={2000,500,200,100,50,20,10,1};
int t=amt;
for(int i=0;i<notes.length;i++)
{
int c=t/notes[i];
if(c!=0)
System.out.println(notes[i]+"\t*\t"+c+"\t=\t"+(c*notes[i]));
t=t%notes[i];
while(amt!=0)
int d=amt%10;
amt/=10;
switch(d)
case 0:
sb.insert(0,"Zero");
break;
case 1:
sb.insert(0,"One");
break;
case 2:
sb.insert(0,"Two");
break;
case 3:
sb.insert(0,"Three");
break;
case 4:
sb.insert(0,"Four");
break;
case 5:
sb.insert(0,"Five");
break;
case 6:
sb.insert(0,"Six");
break;
case 7:
sb.insert(0,"Seven");
break;
case 8:
sb.insert(0,"Eight");
break;
case 9:
sb.insert(0,"Nine");
break;
default:
System.out.println("Invaild digit");
return sb.toString();
}
PROGRAM
Write a program to declare a matrix num[][]of order(M x N) where ‘M’ is the number of rows and ‘N’
is the number of columns such that the values of both ‘M’ and ‘N’ should be greater than 2 and less
than 10. Allow the user to input positive integers into this matrix. Perform following task on the
matrix:
(ii) Sort each row of the matrix in ascending order using any standard sorting technique.
import java.util.*;
{
public static void main()
int m,n,j,k,i,t;
m=sc.nextInt();
n=sc.nextInt();
if(m<=2&&m>=20||n<=2&&n>=20)
else
for(i=0;i<m;i++)
for(j=0;j<n;j++)
num[i][j]=sc.nextInt();
System.out.println("Original Matrix");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
System.out.println(num[i][j]+"\t");
System.out.println();
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
for(k=0;k<(n-1-j);k++)
if(num[i][k]>num[i][k+1])
t=num[i][k];
num[i][k]=num[i][k+1];
num[i][k+1]=t;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
System.out.println(num[i][j]+"\t");
System.out.println();
}
PROGRAM
Write a program by using class Employee to store the details of an employee and another Class
Overtime to Calculate the amount paid for working more than the normal working Hours.The details
of the classes are given below:
Class name : Employee
Data members/ instance variable :
Empc: to store employee code
Bpay: to store basic salary n double type
Member fuctions/ methods:
Employee() : constructor to assign 0 to empc and Bpay
Employee(int c, double b): constructor to assign ‘c’ to empc and b to Bpay
Void Dislplay() : to display empc and Bpay
Class name : overtime
Data members / instance variables:
Nd: to staore number(int) ofextra hours worked
Member function/methods:
Overtime(int n, float r)
Double calculate()
Void Shoe()
Use concept of inheritance.
import java.util.*;
int impc;
double bpay;
Employee()
impc=0;
bpay=0;
impc=c;
bpay=b;
void display()
System.out.println("Employee code="+impc);
System.out.println("Basic pay=rs"+bpay);
int nd;
double rate;
overtime(int n,double r)
nd=n;
rate=r;
double calculate()
double ts;
ts=bpay+(nd*rate);
return(ts);
void show()
display();
double x=calculate();
System.out.println("Salary"+x);
}
PROGRAM
A class called evenseries has been definedto find the smallest value of integer , n such that :
import java.util.*;
class EvenSeries
int n,i;
double s;
EvenSeries()
s=0;
n=0;
void accept()
n=sc.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++)
p=Math.pow(2,i);
k=fact(i);
s=s+(double)p/k;
if (s>2.0&&s<7.0)
break;
}//end of i loop
}//end of display()
PROGRAM
M (N) = 1: IF N =1= 0: IF any prime factor of p distinct prime factors.Write a program to input a
positive natural number N and output M (N). The program should continue till the user wants.Check
your program for N=78,N=34,N=12,N=17,N=666,N=327,N=29 and so on.
import java.util.*;
int j,p,n,c;
c=0;
double m;
m=0.0;
System.out.println("Enter a number");
n=sc.nextInt();
if(n==1)
System.exit(1);
j=2;p=0;
while(j<=n)
if(n%j==0)
c++;
n=n/j;
p++;
if(p>1)
m=0;
break;
else
j++;
p=0;
if(p<=1)
m=(Math.pow(-1,c));
}
PROGRAM
Write a program to input a set of ‘n’ numbers (ascending orders)in a single Dimension Array.
Enter a number and search whether the number and search whether the number is present or not
by using binary search technique. If the number is present then display a message ”Element
found” otherwise, display “Element is not found”.
import java.util.*;
class B_Search
int mid;
mid=(l+h)/2;
if(l>h||f==1)
return(f);
else
if(ele==x[mid])
f=1;
if(ele<x[mid])
l=mid-1;
if(ele>x[mid])
l=mid+1;
return(bsearch(x,l,h,ele,f));
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
int k=sc.nextInt();
int l=0,h=n-1;
int f=0;
int res=bsearch(a,l,h,k,f);
if(res==1)
System.out.println("Element is found");
else
}}
PROGRAM
Write a program in java to input number N (1<=N<=200) and display all the Harshad Number
Niven Number in the range from 1 to N then(both inclusive) .
import java.util.*;
class HarshadNumber
System.out.print("Enter a number:");
int n=sc.nextInt();
int c=n,d,sum=0;
while(c>0)
d=c%10;
sum=sum+d;
c=c/10;
if(n%sum==0)
else
}
PROGRAM
Write a program to input two decimal numbers m,n (m>0,n>0 and they are long integer
type).Convert the decimal number into its equivalent binary numbers.,Add the binary numbers
and display their sum in binary form.
import java.util.*;
class Binary
double bin=0;
int c=0;
while(dec>0)
bin=bin+(dec%2)*Math.pow(10,c++);
dec=dec/2;
return(bin);
long sum,carry=0,bit1,bit2,ct=0;
double total=0.0;
while(bin1!=0)
bit1=bin1%10;
bit2=bin2%10;
if((bit1+bit2+ct)%2==0)
sum=1;
else
sum=1;
if((bit1+bit2+ct)>=2)
ct=1;
else
ct=0;
total=total+sum*Math.pow(10,carry++);
bin1=bin1/10;
bin2=bin2/10;
total=total+ct*Math.pow(10,carry);
return(total);
long m,n,t;
m=sc.nextInt();
n=sc.nextInt();
bin1=(long)ob.toBinary(m);
bin2=(long)ob.toBinary(n);
bin=ob.addBinary(bin1,bin2);
t=(long)bin;
System.out.println("Binary of"+m+":"+bin1);
System.out.println("Binary of"+n+":"+bin2);
}
CONCLUSION
This project has been a great tutor as far as practical aspects of programming are concerned. I Planned
the program with my classmates and then discussed it with my teachers. After drafting the source
code, I punched in the computer followed by debugging and testing it both at home and school.
Subsequently, I was able to get an error free code to put in my project.
This has not only drilled me in the subject but also taught me things like teamwork, team
management, and research work and presentation skills. These are the lesson for my life, which will
always stays with me and help in my career.
BIBLIOGRAPHY