CMP 23

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

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=Integer.toString(x*2)
STEP 10 b=Integer.toString(x*3)
STEP 11 c=Integer.toString(x)
STEP 12 t=Integer.parseInt(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 java.io.*;
class Fascinating
{
int x;
void accept() throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a number");
x=Integer.parseInt(br.readLine());
}

int getNumber()
{
String a=Integer.toString(x*2), b=Integer.toString(x*3),
c=Integer.toString(x);
return Integer.parseInt(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)
{
System.out.println("The given number is fascinating number");
}
else
{
System.out.println("The given number is not a fascinating number");
}
}

public static void main() throws IOException


{
Fascinating obj = new Fascinating();
obj.accept();
obj.isFascinating();
}
}
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 java.util.Scanner;
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)
{
System.out.println(n+" IS A HAPPY NUMBER");
}
else
{
System.out.println(n+" IS NOT A HAPPY NUMBER");
}
}

public static void main()


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int a=sc.nextInt();
Happy ob1=new Happy( );
ob1.getnum(a);
ob1.ishappy();
}
}

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

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 java.io.*;
class twarray{
public static void main()throws IOException{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the number of rows for matrix 1 ");
int row1=Integer.parseInt(br.readLine()); //to enter the first row
System.out.println("Enter the number of columns matrix 1");
int col1=Integer.parseInt(br.readLine());//to enter the first column
int mat1[][]=new int[row1][col1];//to input the first matrix
System.out.println("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]=Integer.parseInt(br.readLine());}}
for(int i=0;i<row1;i++){
for(int j=0;j<col1;j++){
System.out.print(mat1[i][j]+" ");}//to display the elements of first
matrix
System.out.println();}
System.out.println("Enter the number of rows for matrix 2 ");
int row2=Integer.parseInt(br.readLine());
System.out.println("Enter the number of columns matrix 2");
int col2=Integer.parseInt(br.readLine());//to enter the second column
int mat2[][]=new int[row2][col2];//to input the second matrix
System.out.println("Enter the elments for Second matrix");
for(int i=0;i<row2;i++){
for(int j=0;j<row2;j++){ mat2[i][j]=Integer.parseInt(br.readLine());}}
for(int i=0;i<row2;i++){
for(int j=0;j<col2;j++){
System.out.print(mat2[i][j]+" ");}
System.out.println();}
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
System.out.println("Result elments are :");//to calculate the result elements
for(int i=0;i<row2;i++){
for(int j=0;j<col2;j++){
System.out.print(mat3[i][j]+" ");}//to display the difference
System.out.println();}
else{
System.out.println("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
5.3.1.1: b[i][j] = a[j][i]
5.3.1.2: 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 java.util.*;
class transpose{
public static void main(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows for matrix 1 ");
int row1=sc.nextInt();//to accept the first row
System.out.println("Enter the number of columns matrix 1");
int col1=sc.nextInt();//to accept the first column
int mat1[][]=new int[row1][col1];//to accept the first matrix
System.out.println("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]=sc.nextInt();}}
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];}}
System.out.println("Before");//to display the matrix before reverse
for(int i=0;i<row1;i++){
for(int j=0;j<col1;j++){
System.out.print(mat1[i][j]+" ");} } System.out.println("After");//to
display the matrix after reverse
for(int i=0;i<col1;i++){
for(int j=0;j<row1;j++){
System.out.print(mat2[i][j]+" ");
System.out.println();}}}
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 java.util.*;

class MatrixImage

int a[ ][ ];

int i, j, m;

Scanner ob=new Scanner(System.in);

public void show( )

System.out.println("Enter size of an Array from 2 to 20 ");

m=ob.nextInt( );

if(m>1 && m<=20)

a=new int[m][m];

for(i=0;i<m;i++)

for(j=0;j<m;j++)

System.out.print("Enter value for a[i][j] ");

a[i][j]=ob.nextInt( );

}
}

System.out.println("Original Matrix is ");

System.out.println( );

for(i=0;i<m;i++)

for(j=0;j<m;j++)

System.out.print(a[i][j]+" ");

System.out.println( );

System.out.println( );

System.out.println("Image Matrix is ");

System.out.println( );

for(i=0;i<m;i++)

for(j=m-1;j>=0;j--)

System.out.print(a[i][j]+" ");

System.out.println( );

else System.out.println("SIZE OUT OFF RANGE");

public static void main(String arg[])


{

MatrixImage obj = new MatrixImage( );

obj.show();

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


Count the Number of Vowels and Consonants of a String

QUESTION:

WRITE A PROGRAM IN BLUE J TO COUNT NUMBER OF VOWELS


AND CONSONANTS

SOLUTION:

ALGORITHM:

STEP 1 - START
STEP 2 - a = "Computer Applications"
STEP 3 - z = a.length()
STEP 4 - x= 0 , b= 0
STEP 5 - FROM y =0 to y<z REPEAT STEP 6
STEP 6 - IF
(a.charAt(y)=='a'||a.charAt(y)=='e'||a.charAt(y)=='i'||a.charAt(
y)=='o'||a.charAt(y)=='u') THEN x =x +1 OTHERWISE b =
b+1
STEP 7 - PRINT x
STEP 8 - PRINT b
STEP 9 – END
Source Code:

import java.io.*;
class Vowels
{public static void main(String args[])throws IOException
//main function
{BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a string");
String a= br.readLine(); //Accepting string
int z=a.length(),y,x=0,b=0;
for(y=0;y<z;y++) //loop for counting number of vowels
{if(a.charAt(y)=='a'||a.charAt(y)=='e'||a.charAt(y)=='i'||a.char
At(y)=='o'||a.charAt(y)=='u')
x++;
else b++;
}
System.out.println("Number of vowels in string ="+x);
//displaying result
System.out.println("Number of consonants in string ="+b);
}}
OUTPUT:

VARIABLE DESCRIPTION:
Variable Name Data Type Description
br bufferedReader BufferedReader object
a String Input String
z Int Length of the string
y Int Loop variable
b Int No of consonants
x Int No of vowels
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=name.length()
STEP 4 - PRINT original string is "+name
STEP 5 - IF i=0 THEN GOTO STEP 6
STEP 6 - char c1=name.charAt(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=Math.abs(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 java.io.*;
class Decode
{
public void compute()throws IOException //compute() function
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name:");
String name=br.readLine();
System.out.println("Enter number:");
int n=Integer.parseInt(br.readLine());
int j,i,l,c=0,y,n1;
l=name.length();
System.out.println("original string is "+name);
for(i=0;i<l;i++)
{char c1=name.charAt(i);
try //trying for NumberFormatException
{c=(int)c1 ;
}
catch(NumberFormatException e)
{}
if(n>0)
{if((c+n)<=90)
/*Decoding String*/
System.out.print((char)(c+n));
else {c=c+n;
c=c%10;
c=65+(c-1);
System.out.print((char)(c));
}}
else if(n<0)
{n1=Math.abs(n);
if((c-n1) >=65)
System.out.print((char) (c-n1));
else {if(c>65)
c=c-65;
else c=n1;
System.out.print((char)(90-(c-1)));
}}
else if (n==0)
{System.out.println("no change "+name);
break;
}}}
public static void main()throws IOException
{
Decode obj=new Decode();
obj.compute();
}

}
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 =str.length()
STEP 5 - FROM i=0 to i<l REPEAT STEP 6
STEP 6 - c[i] = str.charAt(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 java.io.*;
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
{System.out.println("enter word - ");
BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
str = br.readLine();
l = str.length();
}
public void arrange() //function to arrange string in ascending order
{int i,j;
char temp;
for(i=0;i<l;i++)
{c[i]= str.charAt(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
{System.out.println();
for(int i=0;i<l;i++)
{System.out.print(c[i]);
}}
public static void main(String args[]) throws IOException //main
function
{Alpha obj = new Alpha();
obj.readword();
obj.arrange();
obj.display();
}}
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 java.io.*;
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(System.in));
System.out.println("Enter the number of element :");
int n=Integer.parseInt(br.readLine());
int arr[]=new int[n];//to input the array elements
System.out.println("Enter your elemets :");
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(br.readLine());}
System.out.println("Enter your key element :");
int key=Integer.parseInt(br.readLine());//to enter the searching elements
int pos=obj.linsearch(arr,1,n-1,key); if(pos==-1){//to calculate the
position
System.out.println("Element not found ");} //to display if the element has
been not found
else{
System.out.println("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 java.util.Scanner;
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(System.in);
System.out.println("Enter number of elemnts in array:");
int n=sc.nextInt();
int ar[]=new int[n];
System.out.println("Enter array elements");
for(int i=0;i<n;i++) ar[i]=sc.nextInt();
System.out.println("Enter element to be searched");
int item=sc.nextInt();
int index=binarySearch(ar, item, 0, n-1);
if(index==-1) System.out.println("Item not found");
else System.out.println("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

You might also like