Computer Practical 2-1
Computer Practical 2-1
Computer Practical 2-1
COMPUTER SCIENCE
PRACTICAL
JAVA Programs
By Mohak Seth
INDEX
1. LOOPING
a. Sphenic Number..........................................................................3
b. Smith Number....................................................................................5
c. Kaprekar Number........................................................................7
2. RECURSION
a. Decimal Number to Octal Equivalent.................................................9
b. Perfect Number...................................................................................11
c. Disarium Number..........................................................................13
d. Find HCF and LCM.........................................................................15
3. STRINGS
a. SwapSort.............................................................................................16
b. Rearrange…................................................................................18
c. Palindrome…..................................................................................20
4. ARRAYS
a. Symmetric…...............................................................................21
b. Date and Month…..........................................................................23
c. Insertion Sort…...............................................................................25
5. FILE HANDLING
a. Marks Updation.............................................................................27
b. Shopkeeper’s Stock….....................................................................33
LOOPING
Question 1
Write a program to check whether a number is a Sphenic number or
not.
A Sphenic Number is a positive integer n which is product of exactly
three distinct prime numbers.
import java.util.*;
class Sphenic
{
int n;
Sphenic()
{ n=
0;
}
void accept()
{
Scanner in=new Scanner(System.in);
System.out.println(“Enter a positive Integer”);
n=in.nextInt();
}
int primeFact()
{
int c=0,d=0,i,m=n;
for(i=2;i<=m;)
{
if(m%i==0)
{ c+
+;
m=m/i;
if(c>1)
return 0;
d++;
}
else
{ i+
+;c=0;
}
}
return d;
}
void check()
{
if(primeFact()==3)
{
System.out.println(“Sphenic Number”);
}
else
System.out.println(“Not a Sphenic Number”);
}
static void main()
{
Sphenic obj=new Sphenic();
obj.accept();
obj.check();
}
}
Output
Question 2
import java.util.*;
class Smith
{
int sumDig(int n)
{
int s=0;
while(n>0)
{
s=s+n%10;
n=n/10;
}
return s;
}
int sumPrimeFact(int n)
{
int i=2, sum=0;
while(n>1)
{
if(n%i==0)
{
sum=sum+sumDig(i);
n=n/i;
}
else
i++;
}
return sum;
}
boolean isComposite(int n)
{
int c=0;
for(int i=1; i<=n; i++)
{
if(n%i==0)
{ c+
+;
}
}
if(c>2)
return true;
else
return false;
}
public static void main()
{
Smith ob=new Smith();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Number : ");
int n=sc.nextInt();
if(ob.isComposite(n) == false)
{
System.out.println("You have entered a non-Composite Number. Please enter a
composite number");
}
else
{
int a=ob.sumDig(n);
int b=ob.sumPrimeFact(n);
System.out.println("Sum of Digit = "+a);
System.out.println("Sum of Prime Factor = "+b);
if(a==b)
System.out.print(n+" is a Smith Number");
else
System.out.print(n+" is Not a Smith Number");
System.out.println("");
}
}
}
Output
Enter a Number : 22
Sum of Digit = 4
Sum of Prime Factor = 4
22 is a Smith Number
Enter a Number : 5
You have entered a non-Composite Number. Please enter a composite number
Enter a Number : 99
Sum of Digit = 18
Sum of Prime Factor = 9
99 is Not a Smith Number
Question 3
import java.util.*;
class Kaprekar_Num
{
int p;
int q;
void input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter two numbers");
p=in.nextInt();
q=in.nextInt();
if(p>q&&p>5000&&q>5000)
{
System.out.println("ERROR!!");
System.exit(0);
}
}
boolean Kaprekar(int n)
{
if(n==1)
return true;
int sq_n=n*n;
int count_digits=0;
while(sq_n!=0)
{
count_digits++;
sq_n=sq_n/10;
}
sq_n=n*n;
for(int i=1;i<count_digits;i++)
{
int e=(int)Math.pow(10,i);
if(e==n)
continue;
int sum=(sq_n/e)+(sq_n%e);
if(sum==n)
return true;
}
return false;
}
void display()
{
for(int i=p;i<=q;i++)
{
if(Kaprekar(i))
System.out.print(i+” “);
}
}
public static void main()
{
Kaprekar_Num obj=new Kaprekar_Num();
obj.input();
obj.display();
}
}
Output
Recursion
Question 4
import java.io.*;
class DeciOct
{
int n,oct;
DeciOct()
{
n = 0;
oct = 0;
}
void getNum(int num)
{
n = num;
}
void deciOct()
{
if(n > 0)
{
int rem = n % 8;
n /= 8;
deciOct();
oct = oct * 10 + rem;
}
}
void show()
{
System.out.println("Decimal number: " + n);
deciOct();
System.out.println("Octal equivalent: " + oct);
}
public static void main()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("N = ");
int num = Integer.parseInt(br.readLine());
Output
N = 42
Decimal number: 42
Octal Equivalent: 52
N = 67
Decimal number: 67
Octal Equivalent: 103
N = 18
Decimal number: 18
Octal Equivalent: 22
Question 5
import java.util.*;
class Perfect
{
int num;
Perfect(int nn)
{
num=nn;
}
public int sumofFact(int i)
{
int n=num;
if(i>n/2)
return 1;
else
{
if(n %i==0)
return i +sumofFact(i+1);
else
return sumofFact(i+1);
}
}
public void check()
{
int s=sumofFact(2);
if(s==num)
System.out.println(num+" is a perfect number");
else
System.out.println(num+" is not a perfect number");
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n= sc.nextInt();
Perfect obj=new Perfect(n);
obj.check();
}
}
Output
Enter a number
6
6 is a perfect number
Enter a number
28
28 is a perfect number
Enter a number
98
98 is not a perfect number
Question 6
import java.util.*;
class Disarium
{
int num;
int size;
Disarium(int nn)
{
num = nn;
size = 0;
}
void countDigit()
{
int len = (""+num).length();
size = len;
}
int sumofDigits(int n, int p)
{
if(n==0)
return 0;
else
return (int)Math.pow((n%10),p) + sumofDigits((n/10), p-1);
}
void check()
{
if(num == sumofDigits(num, size))
System.out.println(num + " is a Disarium Number");
else
System.out.println(num + " is not a Disarium Number");
}
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a number");
int n = in.nextInt();
Disarium obj = new Disarium(n);
obj.countDigit();
obj.check();
}
}
Output
Enter a number
135
135 is a Disarium Number
Enter a number
89
89 is a Disarium Number
Enter a number
518
518 is a Disarium Number
Enter a number
66
66 is not a Disarium Number
Question 7
Write a program in Java to find the HCF and LCM of a number using
recursive technique.
Example: 90 and 144
HCF=18
LCM=720
import java.io.*;
class HCFLCM
{
public static int gcd(int p, int q)
{
if(q == 0)
return p;
return gcd(q, p % q);
}
public static void main()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("First number: ");
int a = Integer.parseInt(br.readLine());
System.out.print("Second number: ");
int b = Integer.parseInt(br.readLine());
int hcf = gcd(a, b);
int lcm = a * b / hcf;
System.out.println("HCF: " + hcf);
System.out.println("LCM: " + lcm);
}
}
Output
First number: 50
Second number: 20
HCF: 10
LCM: 100
First number: 72
Second number: 90
HCF: 18
LCM: 360
STRINGS
Question 8
import java.util.*;
public class SwapSort
{
String wrd,swapwrd,sortwrd;
int len;
static Scanner x=new Scanner(System.in);
SwapSort()
{
swapwrd="";
sortwrd="";
}
void readword()
{
System.out.println("Enter word in Upper case");
wrd=x.next();
len=wrd.length();
}
void swapchar()
{
swapwrd=wrd.charAt(len-1) + wrd.substring(1,len-1) + wrd.charAt(0);
}
void sortword()
{
char c;
for(int i=65;i<=90;i++)
{
for(int j=0;j<len;j++)
{
c=wrd.charAt(j);
if(c==i)
sortwrd += c;
}
}
}
void display()
{
System.out.println("Original word = " + wrd);
System.out.println("Swapped word = " + swapwrd);
System.out.println("Sorted word = " + sortwrd);
}
static void main()
{
SwapSort x=new SwapSort();
x.readword();
x.swapchar();
x.sortword();
x.display();
}
}
Output
Question 9
import java.util.*;
public class Rearrange
{
String wrd,newwrd;
static Scanner x=new Scanner(System.in);
Rearrange()
{
wrd="";
newwrd="";
}
void readword()
{
System.out.println("Enter a word" );
wrd=x.next();
wrd=wrd.toUpperCase();
}
void freq_vow_con()
{
int s=0,s1=0;
char ch;
for(int i=0;i<wrd.length();i++)
{
ch=wrd.charAt(i);
if("AEIOU".indexOf(ch)!=-1)
s++;
}
s1= wrd.length()-s;
System.out.println("Vowels = "+ s);
System.out.println("Consonants = " + s1);
}
void arrange()
{
char ch;
String p="",q="";
for(int i=0;i<wrd.length();i++)
{
ch=wrd.charAt(i);
if("AEIOU".indexOf(ch)!=-1)
p +=ch;
else
q +=ch;
}
newwrd= p+q;
}
void display()
{
System.out.println("Original word = "+ wrd);
System.out.println("Rearranged word = "+ newwrd);
}
static void main()
{
Rearrange obj=new Rearrange();
obj.readword();
obj.freq_vow_con();
obj.arrange();
obj.display();
}
}
Output
Enter a word
ANITEJ
Vowels = 3
Consonants = 3
Original word = ANITEJ
Rearranged word = AIENTJ
Enter a word
COMPUTER
Vowels = 3
Consonants = 5
Original word = COMPUTER
Rearranged word = OUECMPTR
Question 10
import java.util.*;
public class RecursivePalindromeJava
{
public static boolean checkPalindrome(String str)
{
if(str.length() == 0 || str.length() == 1)
return true;
if(str.charAt(0) == str.charAt(str.length() - 1))
return checkPalindrome(str.substring(1, str.length() - 1));
return false;
}
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a string : ");
String strInput = sc.nextLine();
if(checkPalindrome(strInput))
{
System.out.println(strInput + " is a palindrome");
}
else
{
System.out.println(strInput + " is not a palindrome");
}
}
}
Output
Arrays
Question 11
import java.io.*;
class Symmetric
{
public static void main()throws IOException
{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("Matrix size: ");
int m = Integer.parseInt(br.readLine());
int ld = 0;
int rd = 0;
boolean flag = true;
if(m <2 || m >= 10)
{
System.out.println("Size out of range!");
return;
}
int a[][] = new int[m][m];
System.out.println("Enter matrix elements:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
a[i][j] = Integer.parseInt(br.readLine());
if(i == j)
ld += a[i][j];
if(i + j == m - 1)
rd += a[i][j];
}
}
System.out.println("Original Matrix:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
System.out.print(a[i][j] + "\t");
if(a[i][j] != a[j][i])
flag = false;
}
System.out.println();
}
if(flag)
System.out.println("The matrix is symmetric.");
else
System.out.println("The matrix is not symmetric.");
System.out.println("Sum of left diagonal elements: " + ld);
System.out.println("Sum of right diagonal elements: " + rd);
}
}
Output
Matrix size: 2
Enter matrix elements:
1
2
3
4
Original Matrix:
1 2
3 4
Matrix size: 2
Enter matrix elements:
1
2
2
1
Original Matrix:
1 2
2 1
Question 12
Design a class convert to find the date and the month from a given day
number for a particular year.
Example: If day number is 64 and the year is 2020, then the
corresponding date would be:
March 4, 2020 i.e. (31 + 29 + 4 = 64)
import java.util.*;
class Convert_arr
{
int n,d,m,y;
Convert_arr( )
{ n=
0;
y=0;
}
void accept()
{
Scanner x=new Scanner(System.in) ;
System.out.println(("Enter day number and year")); ;
n=x.nextInt() ;
y=x.nextInt() ;
}
void day_to_date()
{
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(y%4==0)
a[2]=29;
int s=0, c=0;
while(s<n)
s=s+a[c++];
s=s-a[--c];
d=n-s;
m=c;
}
void display()
{
String x[]={"","JANUARY","FEBRUARY","MARCH","APRIL","MAY",
"JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
System.out.print("\n Day Number: " + n);
System.out.print("\n Date: " );
System.out.print(x[m]+" " +d + ", " + y);
System.out.println("");
}
static void main()
{
Convert_arr obj =new Convert_arr();
obj.accept( ) ;
obj.day_to_date();
obj.display();
}
}
Output
343
2020
145
2011
109
2004
Question 13
import java.util.*;
class InsertionSort
{
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i)
{
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main()
{
Scanner in=new Scanner(System.in);
int arr[] = new int[5];
System.out.println("Enter 5 numbers");
for(int i=0;i<5;i++)
{
arr[i]=in.nextInt();
}
InsertionSort ob = new InsertionSort();
ob.sort(arr);
printArray(arr);
}
}
Output
Enter 5 numbers
01469
Enter 5 numbers
98
78
53
32
1 32 53 78 98
Enter 5 numbers
33
11
48
87
68
11 33 48 68 87
File handling
Question 14
import java.io.*;
import java.util.*;
public class BinFiles
{
String name; int age; double marks;
static Scanner in = new Scanner(System.in);
static Scanner sc = new Scanner(System.in);
void addrecord()throws IOException
{
DataOutputStream out = new DataOutputStream(new
FileOutputStream("Stud.dat",true));
try
{
for(int i = 1 ; i<=4;i++)
{
System.out.println("enter a name, age and marks ");
name = in.nextLine();
age = sc.nextInt();
marks = sc.nextDouble();
out.writeUTF(name);
out.writeInt(age);
out.writeDouble(marks);
}
out.close();
}
catch(IOException e)
{
System.out.print(e);
}
}
void print()throws IOException
{
//reading data from the file
boolean eofile = false;
try
{
DataInputStream in = new DataInputStream(new FileInputStream("Stud.dat"));
do
{
try
{
name = in.readUTF();
age = in.readInt();
marks = in.readDouble();
//if(marks>=90) System.out.println(name+"\t"+age+"\
t"+marks);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
in.close();
}
catch(FileNotFoundException ee)
{
System.out.println("File not found");
}
}
void update()throws IOException
{
DataOutputStream out1 = new DataOutputStream(new FileOutputStream("temp.dat"));
String nm;double mm;
System.out.println("Enter name for search");
nm = in.nextLine();
System.out.println("Enter changed marks ");
mm = sc.nextDouble();
boolean eofile = false;
try
{
DataInputStream in1 = new DataInputStream(new FileInputStream("Stud.dat"));
do
{
try
{
name = in1.readUTF();
age = in1.readInt();
marks = in1.readDouble();
if(name.compareTo(nm)==0)
marks=mm;
out1.writeUTF(name);
out1.writeInt(age);
out1.writeDouble(marks);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
in1.close();
out1.close();
}
catch(FileNotFoundException eee)
{
System.out.println("File does not exists ");
}
DataInputStream in2 = new DataInputStream(new FileInputStream("temp.dat"));
DataOutputStream out2 = new DataOutputStream(new FileOutputStream("Stud.dat"));
eofile=false;
do
{
try
{
name = in2.readUTF();
age = in2.readInt();
marks = in2.readDouble();
out2.writeUTF(name);
out2.writeInt(age);
out2.writeDouble(marks);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
out2.close();
in2.close();
}
public static void main()throws IOException
{
int ch=0;
BinFiles obj = new BinFiles();
do
{
System.out.println("1 . Add records ");
System.out.println("2 . Update records ");
System.out.println("3 . Display records ");
System.out.println("4 . Exit");
System.out.println("Enter your choice ");
ch = sc.nextInt();
switch(ch)
{
case 1:obj.addrecord();
break;
case 2:obj.update();
break;
case 3:obj.print();
break;
case 4:System.exit(0);
break;
default :System.out.println("Wrong choice entered ");
}
}
while(ch!= 4);
}
}
Output
Jake
25
85
Amy
24
100
Terry
30
98
Raymond
35
100
1 . Add records
2 . Update records
3 . Display records
4 . Exit
Jake 25 85.0
Amy 24 100.0
Terry 30 98.0
Raymond 35 100.0
1 . Add records
2 . Update records
3 . Display records
4 . Exit
Jake
95
1 . Add records
2 . Update records
3 . Display records
4 . Exit
Jake 25 95.0
Amy 24 100.0
Terry 30 98.0
Raymond 35 100.0
1 . Add records
2 . Update records
3 . Display records
4 . Exit
Question 15
import java.io.*;
import java.util.*;
public class BinFiles2
{
long itcode;String itname;double price;int qty;
static Scanner in = new Scanner(System.in);
static Scanner sc = new Scanner(System.in);
void input()throws IOException
{
DataOutputStream out = new DataOutputStream(new
FileOutputStream("Stock.dat",true));
try
{
for(int i = 1 ; i<=2;i++)
{
System.out.println("enter an item Code, item Name, price and quantity ");
itcode = in.nextLong();
itname = sc.nextLine();
price = sc.nextDouble();
qty=sc.nextInt();
out.writeLong(itcode);
out.writeUTF(itname);
out.writeDouble(price);
out.writeInt(qty);
}
out.close();
}
catch(IOException e)
{
System.out.print(e);
}
}
void update()throws IOException
{
catch(EOFException e)
{
eofile = true;
}
}
while(!eofile);
out2.close();
in2.close();
}
void print()throws IOException
{
boolean eofile=false;
{
try
{
DataInputStream in1 = new DataInputStream(new FileInputStream("Stock.dat"));
do
{
try
{
itcode= in1.readLong();
itname = in1.readUTF();
price = in1.readDouble();
qty=in1.readInt();
System.out.println(itcode+"\t"+itname+"\t"+price+"\t"+qty);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
in1.close();
}
catch(FileNotFoundException eee)
{
System.out.println("File does not exist");
}
}
}
public static void main()
{
int ch;
BinFiles2 obj=new BinFiles2();
do
{
System.out.println("1 . Add records ");
System.out.println("2 . Update records ");
Output
1 . Add records
2 . Update records
3 . Display records
4 . Exit
1102345
CHIPS
20
10
48190
DAIRY MILK
80
22
1 . Add records
2 . Update records
3 . Display records
4 . Exit
1102345 CHIPS 20 10
48190 DAIRY MILK 80 22
1 . Add records
2 . Update records
3 . Display records
4 . Exit
THANK
YOU!