[go: up one dir, main page]

0% found this document useful (0 votes)
21 views43 pages

COMPUTER PROJECT (AutoRecovered)

The document outlines various programming tasks, including converting decimal numbers to binary, checking if a number is evil or a mystery number, processing matrices, and determining if a matrix is symmetric. Each task is accompanied by an algorithm and sample code in Java, detailing the steps and variable descriptions. The document serves as a guide for implementing these algorithms in programming.

Uploaded by

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

COMPUTER PROJECT (AutoRecovered)

The document outlines various programming tasks, including converting decimal numbers to binary, checking if a number is evil or a mystery number, processing matrices, and determining if a matrix is symmetric. Each task is accompanied by an algorithm and sample code in Java, detailing the steps and variable descriptions. The document serves as a guide for implementing these algorithms in programming.

Uploaded by

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

PROGRAM – 1

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

DATAYPE VARIABLE PURPOSE


int d To store the remainder
int p To store the binary equivalent
String s To store the remainder after
dividing by 2 and concatenating it
to get desired result.
PROGRAM – 2
Question 2 : Write a program to check whether a no. is evil no. or not.
ALGORITHM
STEP 1 : Start
STEP 2 : Take input of a no. in variable x
STEP 3 : Initialize variables d=0,p=0,l=0,k=0,String s=” ”
STEP 4 : Repeat step 5 while (x!=0)
STEP 5 : Calculate d=x%2 ,x/=2 , s=d+s.
STEP 6 : Conversion from string to integer of value stored in s and store in variable p
STEP 7 :Repeat step 8 to step 11 while (p!=0)
STEP 8 : Calculate l=p%10
STEP 9 : Go to step 10 if (l=1) else go to step11
STEP 10 : increment the value of k
STEP 11 :calculate p/=10
STEP 12 : Go to step 13 if (k%2=0) else go to step 14
STEP 13 : return true
STEP 14 : return false
STEP 15 : create the main function
STEP 16 : create a scanner object to take input from user
STEP 17 : create an instance of the class evil
STEP 18 : display enter a no.
STEP 19: store the input in the variable n
STEP 20 : if (n>0 and check(n)=true) go to step 21 else step 22
STEP 21 : display the no is evil
STEP 22 : display the no. is not evil
STEP 23 : Stop
CODE
import java.util.*;
class evil
{boolean check(int x)
{int d=0,p=0,l=0,k=0;String s="";
while(x != 0) // for binary conversion
{ d=x%2;
x/=2;
s=d+s;
}
p=Integer.parseInt(s); // conversion from string to integer
while(p!=0) // for counting the no. of 1 in the binary equivalent of given no.
{ l=p%10;
if(l==1)
k++;
p/=10;
}
if(k%2==0)
return true;
else
return false;
}
void main()
{Scanner sc=new Scanner(System.in);
evil ob=new evil();
System.out.println("enter a no.");
int n=sc.nextInt();
if(n>0 && ob.check(n)==true)
System.out.println("no. is evil");
else
System.out.println("no. is not evil");
}}
OUTPUT
VARIABLE DESCRIPTION

datatype variable purpose


int d To store remainder after dividing
with 2
int p To store the binary form of the
given no.
int k Counter variable
int l To extract last digit of the no.
String s To store the binary form by
concatenation
int n To store given input
PROGRAM – 3
Question 3 : Take input in a 4*3 matrix and print the square of even elements and cube of odd elements.
ALGORITHM
STEP 1 : Start
STEP 2 : Take input of no. in variable x
STEP 3 : if(x%2=0)go to step 4 else step 5
STEP 4 :return x*x
STEP 5 : return x*x*x
STEP 6 : create main function
STEP 7 : create scanner object in class
STEP 8 : create an instance of the class Matrix
STEP 9 : create a double dimensional array of order 4*3
STEP11 : Initialize i=0,j=0
STEP 11 : Repeat step 11 while(i<4)
STEP 12 : Repeat step 13 while (j<3)
STEP 13 : Take input in variable a[][] at i’th row and j'th column
STEP 14 : display the original matrix
STEP 15:Repeat step 16 to step 18 while(i<4)
STEP 16 : Repeat step 17 while(j<3)
STEP 17 : display check(a[i][j])
STEP 18 : go to next line.
STEP 19 : Stop
CODE
import java.util.*;
class Matrix
{
int check (int x)
{ if (x%2==0) // to check whether no. is even or odd
return (x*x);// to find square of no. if even
else
return (x*x*x);// to find cube of no. if odd
}
void main()
{Scanner sc=new Scanner(System.in);
Matrix ob=new Matrix();
System.out.println("enter elements for matrix");
int a[][]=new int[4][3];
for(int i=0;i<4;i++)
for(int j=0;j<3;j++)
a[i][j]=sc.nextInt();
System.out.println("original matrix");
for(int i=0;i<4;i++) // to display original matrix
{for(int j=0;j<3;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
System.out.println("new matrix");
for(int i=0;i<4;i++)
{for(int j=0;j<3;j++)
System.out.print(ob.check(a[i][j])+" ");
System.out.println();
}}}
OUTPUT
VARIABLE DESCRIPTION

datatype variable Purpose


int m To store array elements
Int i Loop variable
int j Loop variable
PROGRAM – 4
Question 4 : Take input of a number and check whether a no. is a mystery no. or not
ALGORITHM

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

datatype variable purpose


int x To store the given input
int d To store last digit
int i Loop variable
int rev To store reverse of the no.
int k Counter variable
int n To store the input given by user
PROGRAM – 5
Question – 5 : Take input from the keyboard where you are pressing print the Unicode value of the key pressed
ALGORITHM
CODE
VARIABLE DESCRIPTION
PROGRAM – 6
Question 6 : Take the input of a square matrix where the order is greater than 4 and less than 10 and check whether
it is symmetric.
ALGORITHM

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();

for(int i=0;i<a.length;i++) //to display matrix


{for(int j=0;j<a.length;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}

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

Datatype Variable Purpose


int i Loop variable
int j Loop variable
int b 2 d array for storing matrix
int k Counter variable
int a 2 d array for storing matrix
int n For storing order of matrix
PROGRAM- 8
Question 8 : Write a program to check whether a no. is a smith no. or not.
ALGORITHM
CODE
OUTPUT
VARIABLE DESCRIPTION

You might also like