COMPUTER PROJECT (AutoRecovered)
COMPUTER PROJECT (AutoRecovered)
QUESTION 1 : Write a program to take input of a decimal number and print its equivalent binary form.
ALGORITHM
STEP 1 :Start
STEP 2 :Initialize d = 0, p = 0, and s = ""
STEP 3 : Repeat step 4 while (x!=0)
STEP 4 :Compute d = x % 2
o Update x = x / 2
o Update s = d + s (prepend the digit to the binary string)
STEP 5 :Convert the binary string s into an integer and store it in p
STEP 6 : Return p
STEP 7 : In the main method:
STEP 8 : Initialize a scanner object to read input.
STEP 9 : Prompt the user to enter a decimal number n.
STEP 10 :Create an instance of the conversion class.
STEP 11 :Call the con(n) method to convert n into binary.
STEP 12 :Print the binary equivalent.
STEP 8 : Stop
CODE
import java.util.*;
class conversion
{int con(int x)
{int d=0,p=0;String s="";
while(x != 0) // to calculate the binary equivalent
{ d=x%2;
x/=2;
s=d+s;
}
p=Integer.parseInt(s); // for conversion to integer
return p;
}
void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("enter a decimal no.");
int n=sc.nextInt();
conversion ob=new conversion();
System.out.println("The binary equivalent is "+ob.con(n));
}
}
OUTPUT
VARIABLE DESCRIPTION
STEP 1: Start
STEP 2 : Take input in variable x
STEP 3 : Create variable d and rev and initialize rev=0
STEP 4 : Repeat step 5 while(x!=0)
STEP 5 : calculate d=x%10 , rev=rev*10+d,x=x/10
STEP 6 : return rev
STEP 7 : create the main function
STEP 8 : create scanner object in class
STEP 9 : create an instance of the class mystery
STEP 10 : display enter a no. with atleast 3 digits.
STEP 11 : take input of no. and store it in n
STEP 12 : if(n>99) go to step 13 else step 14
STEP 13 : initialize i=10 , repeat steps 14 to 15 while(i<n)
STEP 14 : if(i+rev(i)=n) go to step 15
STEP 15 : assign 1 in k and write break
STEP 16 : if(k=1) go to step 17
STEP 17 : display mystery no.
STEP 18 : display not a mystery no.
STEP 19 : Stop
CODE
import java.util.*;
class mystery
{int reverse(int x)
{int d,rev=0;
while(x!=0) //to find reverse of a given no.
{ d=x%10;
rev=rev*10+d;
x/=10;
}
return rev;
}
void main()
{int k=0;
Scanner sc=new Scanner (System.in);
mystery ob=new mystery();
System.out.println("enter a no. with atleast 3 digits");
int n=sc.nextInt();
if(n>99) // condition to check if the no. is atleast three digits
{for(int i=10;i<n;i++)
if(i+ob.reverse(i)==n)
{k=1;
break;
}
if(k==1)
System.out.println(" mystery number ");
else
System.out.println(" not a mystery number ");
}
else
System.out.println("INVALID INPUT");
}}
OUTPUT
VARIABLE DESCRIPTION
STEP 1 : Start.
STEP 2 : Input the order of the matrix, n, from the user (valid range: 5 to 9).
STEP 3 : Check if n is within the valid range:
If n is not between 5 and 9, display "Invalid order of matrix" and exit.
If valid, proceed to Step 4.
STEP 4 : Initialize a 2D array a[n][n] to store matrix elements.
STEP 5 : Input the matrix elements from the user using nested loops (i for rows and j for columns).
STEP 6 : Display the matrix.
STEP 7 : Call the check method to verify if the matrix is symmetric:
Set a variable k = 0.
Loop through the matrix:
o For each element a[i][j], compare it with a[j][i].
o If a[i][j] != a[j][i], set k = 1 and break the loop.
Return true if k == 0 (symmetric), otherwise return false (not symmetric).
STEP 8: Output the result:
If check(a) returns true, print "Symmetric matrix".
If check(a) returns false, print "Not a symmetric matrix".
STEP 9 : Stop
CODE
import java.util.*;
class symmetric
{boolean check(int b[][])
{int k=0;
for(int i=0;i<b.length;i++)
for(int j=0;j<b.length;j++)
if(b[i][j]!=b[j][i]) //to check the similarity of elements by comparision
{ k=1;
break;
}
if(k==0)
return true;
else
return false;
}
void main()
{
Scanner sc=new Scanner(System.in);
symmetric ob=new symmetric();
System.out.println("enter order of matrix between 4 and 10");
int n=sc.nextInt();
if(n>4 && n<10) // to verify the order of matrix
{int a[][]=new int[n][n];
System.out.println("enter elements for matrix ");
for(int i=0;i<a.length;i++) // to take input in matrix from user
for(int j=0;j<a.length;j++)
a[i][j]=sc.nextInt();
if(ob.check(a)==true)
System.out.println("symmetric matrix");
else
System.out.println("not a symmetric matrix");
}
else
System.out.println("invalid order of matrix");
}
}
OUTPUT
VARIABLE DESCRIPTION