Professional Documents
Culture Documents
CMP 23
CMP 23
CMP 23
SCIENCE
PROJECT
FACINATING NUMBER
QUESTION:
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");
}
}
Output:
VARIABLE DESCRIPTION:
HAPPY NUMBER
QUESTION:
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");
}
}
OUTPUT:
VARIABLE DESCRIPTION:
MATRIX SUBTRACTION
QUESTION:
ALGORITHM:
Step 1: start
Step 2: accept two matrix
Step 4: mat3[i][j]=mat1[i][j]-mat2[i][j];
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
QUESTION:
SOLUTION:
ALGORITHM:
Step 1: Start
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;
m=ob.nextInt( );
a=new int[m][m];
for(i=0;i<m;i++)
for(j=0;j<m;j++)
a[i][j]=ob.nextInt( );
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( );
for(i=0;i<m;i++)
for(j=m-1;j>=0;j--)
System.out.print(a[i][j]+" ");
System.out.println( );
obj.show();
}
OUTPUT:
VARIABLE DESCRIPTION:
QUESTION:
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.charAt(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
QUESTION:
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:
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
QUESTION:
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
QUESTION:
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