Computer Project ISC Class 12
Computer Project ISC Class 12
Computer
Science
Project-1
Source Code:
import java.util.*;
class BaseConversion
{ public static void main(String agrs[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
int num=sc.nextInt();
int num1=num;
System.out.println("Enter the Source base:");
int sb=sc.nextInt();
System.out.println("Enter the required
base:");
int b=sc.nextInt();
if(sb==b){
System.out.println("Base Conversion of
"+num1+" from base "+sb+" to base "+b+" is:
"+num);
}
else if(!(b==2 || b==10 || b==8 ||
b==16))
{
System.out.println("Invalid Base:");
}
else
{
if(sb==10)//From Decimal
{
if(b==2)//from decimal to binary
{
int d;
String s=new String();
while(num!=0)
{
d=num%2;
num=num/2;
s=s+d;
}
System.out.println("Base
Conversion of "+num1+" from base "+sb+" to base
"+b+" is: "+s);
}
else if(b==8)//decimal to octal
{
int rem;
String octal="";
char
octalchars[]={'0','1','2','3','4','5','6','7'};
while(num!=0)
{
rem=num%8;
octal=octalchars[rem]+octal;
num=num/8;
}
System.out.println("Base
Conversion of "+num1+" from base "+sb+" to base
"+b+" is: "+octal);
}
else if(b==16)//decimal to hexadecimal
{
int rem;
String hex="";
char
hexchars[]={'0','1','2','3','4','5','6','7','8','9
','A','B','C','D','E','F'};
while (num!=0)
{
rem=num%16;
hex=hexchars[rem]+hex;
num=num/16;
}
System.out.println("Base
Conversion of "+num1+" from base "+sb+" to base
"+b+" is: "+hex);
}
}
else if(sb==8)//From Octal
{
if(b==2)//octal to binary
{
int i,decimalVal=0;
long BinaryVal=0;
for(i=0;num!=0;i++)
{
decimalVal=(int)(decimalVal+(num%10)*Math.pow(8,i)
);
num=num/10;
}
for(i=1;decimalVal!=0;i=i*10)
{
BinaryVal=BinaryVal+(decimalVal%2)*i;
decimalVal=decimalVal/2;
}
System.out.println("Base
Conversion of "+num1+" from base "+sb+" to base
"+b+" is: "+BinaryVal);
}
else if(b==10)//octal to decimal
{
int decimal=0;
int n=0;
while(true)
{
if(num==0)
{
break;
}
else
{
int temp=num%10;
decimal+=temp*Math.pow(8,n);
num=num/10;
n++;
}
}
System.out.println("Base
Conversion of "+num1+" from base "+sb+" to base
"+b+" is: "+decimal);
}
else if(b==16)//octal to
hexadecimal
{
int dec=0,i=0,t,rem;
char
a[]={'0','1','2','3','4','5','6','7','8','9','A','
B','C','D','E','F'};
String hexdec="";
while(num!=0)
{
dec+=(num%10)*(int)Math.pow(8,i);
i++;
num=num/10;
}
while(dec!=0)
{
rem=dec%16;
hexdec=a[rem]+hexdec;
dec=dec/16;
}
System.out.println("Base
Conversion of "+num1+" from base "+sb+" to base
"+b+" is: "+hexdec);
}
}
else if(sb==2)//From Binary
{
if(b==8)//binary to octal
{
//from binary to decimal then
decimal to octal
int rem,fem,deci=0,i=0;
String octal="";
char
octalchars[]={'0','1','2','3','4','5','6','7'};
while(num!=0)//binary to
decimal
{
rem=num%10;
num=num/10;
deci+=rem*Math.pow(2,i);
++i;
}
while(deci!=0)//decimal to
octal
{
fem=deci%8;
octal=octalchars[fem]+octal;
deci=deci/8;
}
System.out.println("Base
Conversion of "+num1+" from base "+sb+" to base
"+b+" is: "+octal);
}
else if(b==10)//binary to decimal
{
int rem,deci=0,i=0;
while(num!=0)
{
rem=num%10;
num=num/10;
deci+=rem*Math.pow(2,i);
++i;
}
System.out.println("Base
Conversion of "+num1+" from base "+sb+" to base
"+b+" is: "+deci);
}
else if(b==16)//binary to
hexadecimal
{
//from binary to decimal then
decimal to octal then octal to hexadecimal
int rem,fem,deci=0,i=0;
String octal="";
char
octalchars[]={'0','1','2','3','4','5','6','7'};
while(num!=0)//binary to
decimal
{
rem=num%10;
num=num/10;
deci+=rem*Math.pow(2,i);
++i;
}
while(deci!=0)//decimal to
octal
{
fem=deci%8;
octal=octalchars[fem]+octal;
deci=deci/8;
}
int
z=Integer.parseInt(octal);//converting octal num
from string to integer
int dec=0,t,qem;
char
a[]={'0','1','2','3','4','5','6','7','8','9','A','
B','C','D','E','F'};
String hexdec="";
while(z!=0)//octal to
hexadecimal
{
dec+=(z%10)*(int)Math.pow(8,i);
i++;
z=z/10;
}
while(dec!=0)
{
qem=dec%16;
hexdec=a[qem]+hexdec;
dec=dec/16;
}
System.out.println(z);
System.out.println("Base
Conversion of "+num1+" from base "+sb+" to base
"+b+" is: "+hexdec);
}
}
else if(sb==16)//From Hexadecimal
{
if(b==2)//hexadecimal to Binary
{
String
f=Integer.toString(num);
int g=Integer.parseInt(f,2);
System.out.println("Base
Conversion of "+num1+" from base "+sb+" to base
"+b+" is: "+g);
}
if(b==8)//hexadecimal to octal
{
String
f=Integer.toString(num);
int g=Integer.parseInt(f,8);
System.out.println("Base
Conversion of "+num1+" from base "+sb+" to base
"+b+" is: "+g);
}
if(b==10)//hexadecimal to decimal
{
String
f=Integer.toString(num);
int g=Integer.parseInt(f,16);
System.out.println("Base
Conversion of "+num1+" from base "+sb+" to base
"+b+" is: "+g);
}
}
}
}
}
Outputs:
Decimal to binary.
Octal to Decimal conversion.
Encoder
A Computer Works on a Binary form of Numbers. Hence, the number
entered in decimal or any other numerical needs to be converted
into binary. We use a encoder to serve the purpose.
Hence, a logic circuit that is used to convert a number from any base
to binary is known as Encoder.
Applications of Encoder
An Encoder has the following applications:
• Encoder is used to convert the codes from one form to another.
• Encoder can be used to convert linear motion (i.e., speed,
velocity, acceleration etc.) into digital form.
Unlike encoder and decoder, there are n selection lines and 2n input
lines. So, there is a total of 2n possible combinations of inputs. A
multiplexer is also treated as Mux.
Applications of Multiplexer
• For using two ways selection in tiggering device.
• For sharing common bus for address and data transmission
among the devices.
• Parallel to serial conversion
• In input signal conditioning device.
Source Code:
import java.util.*;
public class RotateMat90
{
public static void main(String agrs[])
{
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Dimension");
int d=sc.nextInt();
int arr[][]=new int[d][d];
System.out.println("Enter the elements");
for(i=0;i<d;i++)
{
for( j=0;j<d;j++)
{
arr[i][j]=sc.nextInt();
}
}
System.out.println("The Matrix is:");
for( i=0;i<d;i++)
{
for(j=0;j<d;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
//Transposing the Matrix
int tran[][]=new int[d][d];
for(i=0;i<d;i++)
{
for(j=0;j<d;j++)
{
tran[j][i]=arr[i][j];
}
}
//reversing every row of tranpose Matrix
for(i=0;i<d;i++)
{
int start=0;
int end=d-1;
while(start<end)
{
int temp=tran[i][start];
tran[i][start]=tran[i][end];
tran[i][end]=temp;
start++;
end--;
}
}
System.out.println("90 degree clockwise
rotation of the Matrix is");
for( i=0;i<d;i++)
{
for(j=0;j<d;j++)
{
System.out.print(tran[i][j]+" ");
}
System.out.println();
}
}
}
Outputs:
Output-1.
Output-2.
Question 4
Write a Program in java to find the Saddle point in a
matrix given by the user.
A saddle point is an element of the matrix such that it
is the minimum element in its row and maximum in
its column vice-versa.
Example:
Input: 1 2 3 Output: Saddle point is 7
4 5 6
7 8 9
Source Code:
import java.util.Scanner;
public class Saddle_point {
int col_max = 0;
int col = 0;
for (int j = 1; j < n; j++)
{
if (arr[i][j] < row_min)
{
row_min = arr[i][j];
col = j;
}
}
for (int k = 0; k < n; k++)
{
if (row_min < arr[k][col])
{
col_max = 0;
break;
} else
col_max = row_min;
}
if (col_max != 0)
System.out.println("saddle point is " +
col_max);
}
}
Outputs:
Output-1.
Output-2.
Question 5.
Write a program in java to Swap the odd row and the
even row of a Matrix given by the user.
Example:
Input: 1 2 3 Output: 1 2 3
4 5 6 7 8 9
7 8 9 4 5 6
(The 3rd Row of the Matrix is Swapped with
the 2nd Row of the Matrix.)
Source Code:
import java.util.*;
public class OddEvenRowSwap
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[][] = new int[4][4];
Output-1.
Question 6
Write a Program in java to input an array of elements
and check if the number is present or not using Binary
Search and using Recursion.
Example:
Input: Array : {'1','2','3','4','5','6','7','8'}
From : 0th Index
To : 7th Index
Number : 4
Output: “PRESENT”
Source Code:
class BinaryRecu
{
static String BinSearch(int arr[],int
lowest,int highest,int num)
{
if(lowest>highest)
return("Not Present");
else
if(num==arr[(lowest+highest)/2])
return("Found");
else
if(num<arr[(lowest+highest)/2])
return(BinSearch(arr,lowest,(lowest+highest)/2-
1,num));
else
if(num>arr[(lowest+highest)/2])
return(BinSearch(arr,(lowest+highest)/2+1,highest,
num));
else
return("Not Present");
}
}
Output:
Question 7
a) Print the first n numbers of the Pell series using
Recursion. The term n will be given by the user
Source Code:
import java.util.*;
class Pell_Recursion
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the range:");
int s=sc.nextInt();
for(int i = 0; i <s; i++)
System.out.println(pell(i));
}
public static int pell(int n){
if(n == 0 || n == 1)
return n;
else
return 2 * pell(n - 1) + pell(n - 2);
}
}
Outputs:
Output-1.
Output-2.
b) Print the first n numbers of the Lucas series using
Recursion. The term n will be given by the user
Source Code:
import java.util.*;
class LucasSeries
{
public static int lucas(int n)
{
int a=2,b=1,c=0,i;
if(n==0)
return 2;
if(n==1)
return 1;
for(i=3;i<=n;i++)
{
c=a+b;
System.out.println(c+" ");
a=b;
b=c;
}
return b;
}
public static void main(String agrs[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your range:");
int n=sc.nextInt();
System.out.println("The First "+n+" Lucas
Numbers are:");
System.out.println("2"+"\t");//printing
the first 2 values of the series
System.out.println("1"+"\t");
lucas(n);
}
}
Output:
Output-1
Question 8
Write a program in java to take 2 Matrix from user
and perform Matrix Multiplication to them.
Source Code:
import java.util.*;
class MatrixMultiply
{
public static void main(String agrs[])
{
int i,j,k,size;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the
Matrix's:");
size=sc.nextInt();
int arr1[][]=new int[size][size];
int arr2[][]=new int[size][size];
int c[][]=new int[size][size];
System.out.println("Enter Matrix 1:");
for(i=0;i<size;i++){
for(j=0;j<size;j++){
arr1[i][j]=sc.nextInt();
}
}
System.out.println("Enter Matrix 2:");
for(i=0;i<size;i++){
for(j=0;j<size;j++){
arr2[i][j]=sc.nextInt();
}
}
System.out.println("Matrix 1:");
for(i=0;i<size;i++){
for(j=0;j<size;j++){
System.out.print(arr1[i][j]+" ");
}
System.out.println();
}
System.out.println("Matrix 2:");
for(i=0;i<size;i++){
for(j=0;j<size;j++){
System.out.print(arr2[i][j]+" ");
}
System.out.println();
}
System.out.println("Matrix
Multiplication:");
//Multiplication of the Matrices
for(i=0;i<size;i++){
for(j=0;j<size;j++){
c[i][j]=0;
for(k=0;k<size;k++){
c[i][j]+=arr1[i][k]*arr2[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
Output:
Output-1