0% found this document useful (0 votes)
528 views39 pages

Computer Project ISC Class 12

The document contains source code for a Java program that performs base conversion. The program allows a user to input a number in any base and converts it to any other base. It includes code to handle conversions between the bases 2, 8, 10, and 16. The document also discusses encoders, decoders, and multiplexers at a high level. It provides block diagrams and explains that encoders convert numbers to binary and decoders perform the reverse operation.

Uploaded by

Ankan Misra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
528 views39 pages

Computer Project ISC Class 12

The document contains source code for a Java program that performs base conversion. The program allows a user to input a number in any base and converts it to any other base. It includes code to handle conversions between the bases 2, 8, 10, and 16. The document also discusses encoders, decoders, and multiplexers at a high level. It provides block diagrams and explains that encoders convert numbers to binary and decoders perform the reverse operation.

Uploaded by

Ankan Misra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

ISC-2023

Computer
Science
Project-1

Name : Ankan Misra


Class : XII
Section : A
Roll Num : 09
Session : 2022-2023
Question 1
Write a program in java in which the user will input a
number of any base and convert the number in any
base that the user wants.
Example:
Input: Number = 10011
Original base =2
Base to be converted = 10
Output:
Base Conversion of 48 from base 10 to base 2 is: 19

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.

Decimal to Hexadecimal converison.


Question 2.
Show the Working of Encoder, Decoder, Mux.

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.

Block Diagram of an Encoder.

An Encoder is a Combinational circuit that performs the reverse


operation of Decoder. It has maximum of 2n input lines and ‘n’
output lines; hence it encodes the information from 2n inputs into n-
bit code. It will produce a binary code equivalent to the input, which
is active High. Therefore, the encoder encodes 2n input lines with ‘n’
bits.
Working Principle of Encoder
If you want to convert hexadecimal number 9 into binary then put on
the witch marked with 9. Whose binary equivalent number is 1001.
You may observe that only gates A and D will get high (1) input
signal. All other input signals to the gates will remain low (0). Hence,
only gates A and D will result in high (1) which represent the binary
pattern 1001 of the hexadecimal digit 9.

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.

Logic gate of an Encoder.


Decoder
The combinational circuit that changes the binary information into
2N output lines is known as Decoders. The binary information is
passed in the form of N input lines. The output lines define the 2N-bit
code for the binary information. In simple words,
the Decoder performs the reverse operation of the Encoder. At a
time, only one input line is activated for simplicity. The produced 2N-
bit output code is equivalent to the binary information.

Block Diagram of a Decoder.

The name “Decoder” means to translate or decoded coded


information from one format into another, so a digital decoder
transforms a set of digital input signals into an equivalent decimal
code at its output.

Working Principle of Decoder


At first the input signals according to the bits for the input binary
value to be converted into octal (say, A=1, B=1, C=0). In this
situation, only gate 6 will activate to produce a high output because
only this gate will receive all high inputs signals. One or more input
signals to other AND gates will remain low resulting them in low
output. Hence, 6 is the octal of binary number 110.
Application of Decoder
A Decoder can be used for the following application
• Instruction decoding in Microprocessor.
• Selecting different input/output devices in Microprocessor.
• Enabling different rows of memory addresses.

Logic gate of a Decoder.


Multiplexer
A multiplexer is a combinational circuit that has 2n input lines and a
single output line. Simply, the multiplexer is a multi-input and single-
output combinational circuit. The binary information is received from
the input lines and directed to the output line. On the basis of the
values of the selection lines, one of these data inputs will be
connected to the output.

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.

Block Diagram of a Multiplexer.

The multiplexer, shortened to “MUX” or “MPX”, is a combinational


logic circuit designed to switch one of several input lines through to a
single common output line by the application of a control signal.
Multiplexers operate like very fast acting multiple position rotary
switches connecting or controlling multiple input lines called
“channels” one at a time to the output.
Working principle of Multiplexer
With the reference to the logic circuit, the lines A, B and C are
referred to as the selector switches which ensure the selection of a
signal. In order to select data D5 the state of the selector switches A,
B and C must be 101 (Binary of 5) respectively. In this situation, only
gate 5 will have all the inputs high to produce a high output. The
output of all other AND gates will remain low. This high output
enables the OR gate to result in high signal showing the selection of
data D5.

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.

Logic gate of Multiplexer.


Question 3
Write a program in java in which the user will input a
2-Dimensional Matrix and to rotate the matrix by 900.
Example:
Input: 1 2 3 Output: 7 4 1
4 5 6 8 5 2
7 8 9 9 6 3

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 {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);

System.out.println("enter the size of your


matrix");
int n = s.nextInt();
int arr[][] = new int[n][n];

System.out.println("enter the elements:-");


for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
arr[i][j] = s.nextInt();
}
System.out.println("The Matrix You enetered
is");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}

int col_max = 0;

for (int i = 0; i < n; i++)


{
int row_min = arr[i][0]; //smallest of row

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];

System.out.println("Enter elements of 4x4 the


Matrix ");
for (int i = 0; i < 4; i++) {
System.out.println("Enter Row "+ (i+1) +
" :");
for (int j = 0; j < 4; j++) {
arr[i][j] = in.nextInt();
}
}

System.out.println("The Matrix is:");


for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
System.out.println("Swapped the 1st row with
the 2nd row");
//Swap 0th and 1st rows
for (int j = 0; j < 4; j++) {
int t = arr[0][j];
arr[0][j] = arr[1][j];
arr[1][j] = t;
}
System.out.println("Swapped Matrix:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
Outputs:

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

You might also like