COMPUTER
SCIENCE
PROJECT
NAME : PABAN SAHA
CLASS : XI
SECTION: B
ROLL NO : 15
FACINATING NUMBER
QUESTION:
WRITE A PROGRAM IN BLUE J TO CHECK A NUMBER IS
FACINATING NUMBER OR NOT
SOLUTION:
ALGORITHM:
STEP 1 Start
STEP 2 x:=0
STEP 3 Print "Enter a number"
STEP 4 Read x;
STEP 5 count=0
STEP 6 i=1
STEP 7 if i<=9, go to step 8 or else go to step 24
STEP 8 j=i
STEP 9 a=[Link](x*2)
STEP 10 b=[Link](x*3)
STEP 11 c=[Link](x)
STEP 12 t=[Link](c+a+b)
STEP 13 digit:=0
STEP 14 c=0
STEP 15 If t>0, go to step 16 or else go to step 21
STEP 16 digit=t%10
STEP 17 t=t/10
STEP 18 if digit==j, go to step 19 or else go to step 20
STEP 19 c++
STEP 20 Go to step 15
STEP 21 If c==1, go to step 22 or else go to step 23
STEP 22 count ++
STEP 23 i++. Go to step 7
STEP 24 If count==9, go to step 25 or else go to step 26
STEP 25 Print "The given number is fascinating number"
STEP 26 Print "The given number is not a fascinating number"
STEP 27 Stop
SOURCE CODE:
import [Link].*;
class Fascinating
{
int x;
void accept() throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter a number");
x=[Link]([Link]());
}
int getNumber()
{
String a=[Link](x*2), b=[Link](x*3),
c=[Link](x);
return [Link](c+a+b);
}
int checkDigit(int j)
{
int t=getNumber(), digit, c=0;
while(t>0)
{
digit=t%10;
t=t/10;
if(digit==j)
{
c++;
}
}
return c;
}
void isFascinating()
{
int count=0;
for(int i=1;i<=9;i++)
{
if(checkDigit(i)==1)
{
count++;
}
}
if(count==9)
{
[Link]("The given number is fascinating number");
}
else
{
[Link]("The given number is not a fascinating number");
}
}
public static void main() throws IOException
{
Fascinating obj = new Fascinating();
[Link]();
[Link]();
}
}
Output:
VARIABLE DESCRIPTION:
Data Type Variable Name Description
int x To accept a number from the user
String a To store double of the number
String b To store triple of the number
String c To store string form of number
int j To parse a value to the function
int digit To store the digits
int c To count the frequency of each digit
int count To count the number of digits whose frequency is 1
int i To act as counter variable
int t To act as a temp variable
HAPPY NUMBER
QUESTION:
WRITE A PROGRAM IN BLUE J TO CHECK A NUMBER IS HAPPY
NUMBER OR NOT
SOLUTION:
ALGORITHM
INPUT: A number
OUTPUT: Whether the number is a happy number or not
PROCESS:
Step 1: [Taking the input]
Read n [the number to be checked]
Step 2: [Checking for Happy Number]
Set sum<-0
[Checking for happy number]
While sum≠1 and sum≠4 repeat
Set sum<-0
While n>0 repeat
Set r<-n mod 10
Set sum<-sum+(r×r)
Set n<-n/10
[End of ‘while’ loop]
Set n<-sum
[End of ‘while’ loop]
[Printing the result]
If sum=1
Print "The number is a Happy Number"
Else
Print "The number is not a Happy Number"
[End of ‘if-else’]
Step 3: Stop.
PROGRAM:
import [Link];
public class Happy
{
int n;
Happy()
{
n=0;
}
void getnum(int nn)
{
n=nn;
}
int sum_sq_digits(int x)
{
if(x==0)
{
return 0;
}
else
{
int rem=x%10;
return (rem * rem)+sum_sq_digits(x/10);
}
}
void ishappy()
{
int x=n;
while(x!=1 && x!=4)
{
x=sum_sq_digits(x);
}
if(x==1)
{
[Link](n+" IS A HAPPY NUMBER");
}
else
{
[Link](n+" IS NOT A HAPPY NUMBER");
}
}
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int a=[Link]();
Happy ob1=new Happy( );
[Link](a);
[Link]();
}
}
OUTPUT:
VARIABLE DESCRIPTION:
SL. NO VARIABLE DATATYPE DESCRIPTION
1 A Integer For taking input
2 N Integer The number
3 Rem Integer For Extracting
Last Digit
PROGRAM 9: MATRIX SUBTRACTION
QUESTION:
WRITE A PROGRAM IN BLUE J TO SUBTRACT 2 MATRIXES USING 2D
ARRAY
SOLUTION:
ALGORITHM:
Step 1: start
Step 2: accept two matrix
Step 3: input all the rows and columns of both matrix
Step 4: mat3[i][j]=mat1[i][j]-mat2[i][j];
Step 5: print a third matrix to display the sustraction
Step 6: stop
SOURCE CODE:
import [Link].*;
class twarray{
public static void main()throws IOException{
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter the number of rows for matrix 1 ");
int row1=[Link]([Link]()); //to enter the first row
[Link]("Enter the number of columns matrix 1");
int col1=[Link]([Link]());//to enter the first column
int mat1[][]=new int[row1][col1];//to input the first matrix
[Link]("Enter the elments for first matrix");//to enter the
elements
for(int i=0;i<row1;i++){
for(int j=0;j<col1;j++){
mat1[i][j]=[Link]([Link]());}}
for(int i=0;i<row1;i++){
for(int j=0;j<col1;j++){
[Link](mat1[i][j]+" ");}//to display the elements of first
matrix
[Link]();}
[Link]("Enter the number of rows for matrix 2 ");
int row2=[Link]([Link]());
[Link]("Enter the number of columns matrix 2");
int col2=[Link]([Link]());//to enter the second column
int mat2[][]=new int[row2][col2];//to input the second matrix
[Link]("Enter the elments for Second matrix");
for(int i=0;i<row2;i++){
for(int j=0;j<row2;j++){ mat2[i][j]=[Link]([Link]());}}
for(int i=0;i<row2;i++){
for(int j=0;j<col2;j++){
[Link](mat2[i][j]+" ");}
[Link]();}
if(row1==row2 && col1==col2){
int mat3[][]=new int[row2][col2];
for(int i=0;i<row2;i++){
for(int j=0;j<col2;j++){
mat3[i][j]=mat1[i][j]-mat2[i][j];}}//to calculate the difference
[Link]("Result elments are :");//to calculate the result elements
for(int i=0;i<row2;i++){
for(int j=0;j<col2;j++){
[Link](mat3[i][j]+" ");}//to display the difference
[Link]();}
else{
[Link]("Invaild");
}
}
}
OUTPUT:
VARIABLE DESCRIPTION
VARIABLE NAME DATA TYPE DESCRIPTION
Mat1[] Int To enter matrix 1
Mat2[] Int To enter matrix 2
Row Int To enter the row of 1st matrix
col Int To enter the column of 1 st
matrix
Row1 int To enter the row of 2nd matrix
Col1 Int To enter the column of 2nd
matrix
i Int To run the for loop
j int To run the for loop
TRANSPOSE A MATRIX
QUESTION:
WRITE A PROGRAM IN BLUE J TO FIND TRANSPOSE OF A MATRIX
SOLUTION:
ALGORITHM:
Step 1: Start
Step 2: Declare matrix a[m][n] of order mxn
Step 3: Read matrix a[m][n] from User
Step 4: Declare matrix b[m][n] of order mxn
Step 5: // Transposing the Matrix
5.1: Declare variables i, j
5.2: Set i=0, j=0
5.3: Repeat until i < n
5.3.1: Repeat until j < m
[Link]: b[i][j] = a[j][i]
[Link]: j=j+1 // Increment j by 1
5.3.2: i=i+1 // Increment i by 1
5.4: Print matrix b
// The matrix b is the transpose of a and can be printed now
Step 6: Stop
SOURCE CODE:
import [Link].*;
class transpose{
public static void main(){
Scanner sc=new Scanner([Link]);
[Link]("Enter the number of rows for matrix 1 ");
int row1=[Link]();//to accept the first row
[Link]("Enter the number of columns matrix 1");
int col1=[Link]();//to accept the first column
int mat1[][]=new int[row1][col1];//to accept the first matrix
[Link]("Enter the elments for first matrix");//to display the
elements
for(int i=0;i<row1;i++){
for(int j=0;j<col1;j++){
mat1[i][j]=[Link]();}}
int mat2[][]=new int[col1][row1];//to accept the second matrix
for(int i=0;i<col1;i++){
for(int j=0;j<row1;j++){
mat2[i][j]=mat1[j][i];}}
[Link]("Before");//to display the matrix before reverse
for(int i=0;i<row1;i++){
for(int j=0;j<col1;j++){
[Link](mat1[i][j]+" ");} } [Link]("After");//to
display the matrix after reverse
for(int i=0;i<col1;i++){
for(int j=0;j<row1;j++){
[Link](mat2[i][j]+" ");
[Link]();}}}
OUTPUT:
VARIABLE DESCRIPTION:
Variable name data type description
Mat1[][] int To enter a matrix
Mat2[][] int To dispay the matrix after
transpose
i int To run the loop
j int To run the foe loop
Row1 int To enter the row
Col1 int To enter the column
MIRROR OF A MATRIX
QUESTION:
WRITE A PROGRAM IN BLUE J TO FIND MIRROR IMAGE OF A
MATRIX
SOLUTION:
ALGORITHM:
Step 1: Start
Step 2: Declare matrix A[m][n]
Step 3: Read m, n
Step 4: Take input the matrix elements
Step 5: Traverse each column of the matrix in reverse way
Step 6: Display the elements
Step 7: Stop
SOURCE CODE:
import [Link].*;
class MatrixImage
int a[ ][ ];
int i, j, m;
Scanner ob=new Scanner([Link]);
public void show( )
[Link]("Enter size of an Array from 2 to 20 ");
m=[Link]( );
if(m>1 && m<=20)
a=new int[m][m];
for(i=0;i<m;i++)
for(j=0;j<m;j++)
[Link]("Enter value for a[i][j] ");
a[i][j]=[Link]( );
}
}
[Link]("Original Matrix is ");
[Link]( );
for(i=0;i<m;i++)
for(j=0;j<m;j++)
[Link](a[i][j]+" ");
[Link]( );
[Link]( );
[Link]("Image Matrix is ");
[Link]( );
for(i=0;i<m;i++)
for(j=m-1;j>=0;j--)
[Link](a[i][j]+" ");
[Link]( );
else [Link]("SIZE OUT OFF RANGE");
public static void main(String arg[])
{
MatrixImage obj = new MatrixImage( );
[Link]();
OUTPUT:
VARIABLE DESCRIPTION:
Variable name data type description
a[][] int To enter a matrix
i int To run the loop
j int To run the foe loop
m int To enter the row
n int To enter the column
To Display a Frequency of Each Character in Entered String
QUESTION:
WRITE A PROGRAM IN BLUE J TO DISPLAY A FREQUENCY OF EACH
CHARACTER IN ENTERED STRING
SOLUTION:
ALGORITHM:
STEP 1 - START
STEP 2 - INPUT str
STEP 3 - l=[Link]()
STEP 4 - PRINT str
STEP 5 - IF i=0 THEN GOTO STEP 4 OTHERWISE GOTO STEP 22
STEP 6 - char a=[Link](i)
STEP 7 - IF ii=0 THEN GOTO STEP 4 OTHERWISE GOTO STEP 22
STEP 8 - char b = [Link](ii)
STEP 9 - IF a==b GOTO STEP 10
STEP 10 - freq=freq+1
STEP 11 - ii++ & IF ii<1 GOTO STEP 8
STEP 12 - i++ & IF i<1 GOTO STEP 6
STEP 13 - DISPLAY a+" occurs "+freq+" times"
STEP 14 – END
SOURCE CODE:
import [Link].*;
class Frequency
{private int i,a1,l,p,j,freq;
public Frequency() //default constructor
{p=0;
freq=0; // initialise instance variables
}
public void count(String str) //counting character frquency
{int ii;
l=[Link]();
[Link](str);
for(i=0;i<l;i++)
{char a=[Link](i);
for(ii=0;ii<l;ii++)
{char b = [Link](ii);
if (a==b)
freq=freq+1;
}
[Link](a+" occurs "+freq+" times"); //displaying frequency
freq=0;
}}
public static void main(String args[]) throws IOException //main function
{BufferedReader br =new BufferedReader(new
InputStreamReader([Link]));
[Link]("enter string");
String str = [Link]();
Frequency x = new Frequency();
[Link](str);
}}
OUTPUT:
VARIABLE DESCRIPTION:
Variable Name Data Type Description
br BufferedReader BufferedReader object
i Int Loop variable
a1 Int Instance
L Int Length
p Int Instance
freq Int Frequency
ii Int Loop
a Char Character at index i
b Char Character at index ii
str String Input string
x Frequency Frequency object
To Decode the Entered String
QUESTION:
WRITE A PROGRAM IN BLUE J FIND A WORD IN ENTERED STRING
SOLUTION:
ALGORITHM:
STEP 1 - START
STEP 2 - INPUT name, n
STEP 3 - l=[Link]()
STEP 4 - PRINT original string is "+name
STEP 5 - IF i=0 THEN GOTO STEP 6
STEP 6 - char c1=[Link](i)
STEP 7 - c=(int)c1
STEP 8 - IF n>0 THEN GOTO STEP 9 THERWISE GOTO STEP 12
STEP 9 - IF (c+n)<=90 THEN GOTO STEP 10 OTHERWISE GOTO STEP 11
STEP 10 - PRINT (char)(c+n)
STEP 11 - c=c+n;c=c%10,c=65+(c-1) & PRINT (char)(c)
STEP 12 - ELSE IF n<0 THEN GOTO STEP 13 OTHERWISE GOTO STEP
19
STEP 13 - n1=[Link](n)
STEP 14 - IF (c-n1) >=65 THEN GOTO STEP 15 OTHERWISE GOTO STEP
16
STEP 15 - DISPLAY (char) (c-n1)
STEP 16 - IF c>65 THEN GOTO STEP 17 OTHERWISE GOTO STEP 18
STEP 17 - c=c-65,
STEP 18 - c=n1 & PRINT (char)(90-(c-1))
STEP 19 - ELSE IF n==0
STEP 20 - DISPLAY "no change "+name
STEP 21 – END
SOURCE CODE:
import [Link].*;
class Decode
{
public void compute()throws IOException //compute() function
{BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter name:");
String name=[Link]();
[Link]("Enter number:");
int n=[Link]([Link]());
int j,i,l,c=0,y,n1;
l=[Link]();
[Link]("original string is "+name);
for(i=0;i<l;i++)
{char c1=[Link](i);
try //trying for NumberFormatException
{c=(int)c1 ;
}
catch(NumberFormatException e)
{}
if(n>0)
{if((c+n)<=90)
/*Decoding String*/
[Link]((char)(c+n));
else {c=c+n;
c=c%10;
c=65+(c-1);
[Link]((char)(c));
}}
else if(n<0)
{n1=[Link](n);
if((c-n1) >=65)
[Link]((char) (c-n1));
else {if(c>65)
c=c-65;
else c=n1;
[Link]((char)(90-(c-1)));
}}
else if (n==0)
{[Link]("no change "+name);
break;
}}}
public static void main()throws IOException
{
Decode obj=new Decode();
[Link]();
}
}
OUTPUT:
VARIABLE DESCRIPTION:
Variable Name Data Type Description
Br BufferedReader BufferedReader
object
name string Input String
n int Decode Number
i int Loop variable
j int Loop variable
l int Length of string
c int ASCII of C1
To Display the Entered String in Alphabetical Order
QUESTION:
WRITE A PROGRAM IN BLUE J TO DISPLAY THE ENTERED STRING
IN ALPHABETICAL ORDER
SOLUTION:
ALGORITHM:
STEP 1 - START
STEP 2 - str = "" , l = 0
STEP 3 - INPUT string str
STEP 4 - l =[Link]()
STEP 5 - FROM i=0 to i<l REPEAT STEP 6
STEP 6 - c[i] = [Link](i)
STEP 7 - FROM i=0 to i<l-1 REPEAT STEP 8
STEP 8 - FROM j=0 to i<l-1 REPEAT STEP 9
STEP 9 - temp =c[j], c[j] = c[j+1] , c[j+1] = temp
STEP 10 - FROM i=0 to i<l REPEAT STEP 11
STEP 11 - PRINT c[i]
STEP 12 – END
SOURCE CODE:
import [Link].*;
class Alpha
{String str;
int l;
char c[] = new char[100];
public Alpha() //Alpha() constructor
{str = "";
l =0;
}
public void readword() throws IOException //function to read input
string
{[Link]("enter word - ");
BufferedReader br =new BufferedReader(new
InputStreamReader([Link]));
str = [Link]();
l = [Link]();
}
public void arrange() //function to arrange string in ascending order
{int i,j;
char temp;
for(i=0;i<l;i++)
{c[i]= [Link](i);
}
for(i=0;i<l-1;i++) //loops for swapping of characters
{for(j=0;j<l-1-i;j++)
{if(c[j] > c[j+1])
{temp = c[j];
c[j] = c[j+1];
c[j+1] = temp;
}}}}
public void display() //function to display the rearranged string
{[Link]();
for(int i=0;i<l;i++)
{[Link](c[i]);
}}
public static void main(String args[]) throws IOException //main
function
{Alpha obj = new Alpha();
[Link]();
[Link]();
[Link]();
}}
OUTPUT:
VARIABLE DESCRIPTION
Variable Name Data Type Description
br BufferedReader BufferedReader object
str String Input string
l int Length
c char[] Character Array
i int Loop Variable
j int Loop Variable
temp char Temporary Storage
obj alpha Alpha Object
LINEAR SEARCH USING RECURSION
QUESTION:
WRITE A PROGRAM IN BLUE J TO IMPLEMENT LINEAR SEARCH
USING RECURSION
SOLUTION:
Algorithm
Step 1: start
Step 2: accept the element to be searched from the users
Step 3: call a recursive function int linsearch(int arr[],int
l,int r,int key)
Step 4: if(r<l) return -1; if(arr[l]==key) then return l;
if(arr[r]==key) return r; return linsearch(arr,l+1,r-1,key);
Step 5: use a variable pos to find the element searched
Step 6: stop
SOURCE CODE:
import [Link].*;
class search{ //class name
int linsearch(int arr[],int l,int r,int key){ //to call the recursive function
if(r<l)
return -1;
if(arr[l]==key) return l;
if(arr[r]==key)
return r;
return linsearch(arr,l+1,r-1,key);}
public static void main()throws IOException{
search obj=new search();//to create an object
BufferedReader br =new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter the number of element :");
int n=[Link]([Link]());
int arr[]=new int[n];//to input the array elements
[Link]("Enter your elemets :");
for(int i=0;i<n;i++){
arr[i]=[Link]([Link]());}
[Link]("Enter your key element :");
int key=[Link]([Link]());//to enter the searching elements
int pos=[Link](arr,1,n-1,key); if(pos==-1){//to calculate the
position
[Link]("Element not found ");} //to display if the element has
been not found
else{
[Link]("Element foud at "+pos+" index"); } }}//to display the
position of key
}
}
OUTPUT:
VARIABLE DESCRIPTION
VARIABLE NAME DATA TYPE DESCRIPTION
Arr[] Int To enter the array
Key Int To search the number
I Int To run the for loop
r Int To return 1
pos Int To get the position of key
n int To enter the elements of
array
BINARY SEARCH USING RECURSION
QUESTION:
WRITE A PROGRAM IN BLUE J TO IMPLEMENT BINARY SEARCH
USING RECURSION
SOLUTION:
ALGORITHM:
Step 1 : Find the middle element of array. using ,
middle = initial_value + end_value / 2 ;
Step 2 : If middle = element, return ‘element found’ and index.
Step 3 : if middle > element, call the function with end_value = middle - 1 .
Step 4 : if middle < element, call the function with start_value = middle + 1 .
Step 5 : exit.
SOURCE CODE:
import [Link];
public class Binary
{
public static int binarySearch(int[] ar, int item, int first, int last)
{
int mid=(first+last)/2;
while(first<=last)
{
if(ar[mid]==item) return mid;
else if(ar[mid]>item) return binarySearch
(ar,item,first,mid-1);
else return binarySearch(ar,item,mid+1,last);
}
return -1;
}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter number of elemnts in array:");
int n=[Link]();
int ar[]=new int[n];
[Link]("Enter array elements");
for(int i=0;i<n;i++) ar[i]=[Link]();
[Link]("Enter element to be searched");
int item=[Link]();
int index=binarySearch(ar, item, 0, n-1);
if(index==-1) [Link]("Item not found");
else [Link]("Item found at index "+index);
}
}
OUTPUT:
VARIABLE DESCRIPTION
VARIABLE NAME DATA TYPE DESCRIPTION
Arr[] Int To enter the array
Key Int To search the number
I Int To run the for loop
r Int To return 1
Mid Int To find the mid value
pos Int To get the position of key