0% found this document useful (0 votes)
25 views

JAVA Assignment 4

The document contains 5 questions regarding bitwise operators, if-else statements, and triangle classification in Java. Each question includes sample code to demonstrate the concept. The code examples take user input, perform bitwise operations or conditional checks, and output the results.

Uploaded by

monalisamona152
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

JAVA Assignment 4

The document contains 5 questions regarding bitwise operators, if-else statements, and triangle classification in Java. Each question includes sample code to demonstrate the concept. The code examples take user input, perform bitwise operations or conditional checks, and output the results.

Uploaded by

monalisamona152
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Assignment – 4

(Bitwise Operators, if-else)


25-01-2024
Q1. Define two numbers v and n, where v is the original number and n is the
shifting value. Then shift the value of v to left (<<) and right (>>) up to n
bits
and print the new values. Also use the >>> operator for right shift and
observe the difference between >> and >>>.
import java.util.*;
class A4Q1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
System.out.print("Enter v: ");
int v = sc.nextInt();
int ls= v << n;
int rs= v >> n;
//int lls= v <<< n;
int rrs= v >>> n;
System.out.println(v+" << "+n+" = "+ls);
System.out.println(v+" >> "+n+" = "+rs);
//System.out.println(v+" <<< "+n+" = "+lls);
System.out.println(v+" >>> "+n+" = "+rrs);
}

Name-Monalisa Das
Roll no.-2370247
CMD
To Compile : javac A4Q1.java
To Run : java A4Q1
Output : Enter n: 12
Enter v: 13
13 << 12 = 53248
13 >> 12 = 0
13 >>> 12 = 0

Name-Monalisa Das
Roll no.-2370247
Q2. Define two numbers and swap them without using third variable.
a. Use the bitwise ^ operator and show the numbers after swapping.
b. Restore the numbers by using (+, -) operator and show the numbers.
CODE
import java.util.*;
class A4Q2{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int a = sc.nextInt();
System.out.print("Enter number: ");
int b = sc.nextInt();
a=a^b;
b=a^b;
a=a^b;
System.out.println("After swapinging");
System.out.println(a);
System.out.println(b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("After recovering");
System.out.println(a);
System.out.println(b);
}
}

Name-Monalisa Das
Roll no.-2370247
CMD
To Compile : javac A4Q2.java
To Run : java A4Q2
Output : Enter number: 13
Enter number: 12
After swapinging
12
13
After recovering
13
12

Name-Monalisa Das
Roll no.-2370247
Q3. WAP in JAVA to input an amount in Rs through command line argument
and find the minimum number of 2000, 500, 200, 100, 50, 20, 10, 5, 2 and
1 Rs denominations will be needed to have that amount.
Ex: Rs. 3868 => (2000*1) + (500*3) + (200*1) + (100*1) + (50*1) + (10*1) +
(5*1) + (2*1) + (1*1)
CODE
import java.util.*;
class A4Q3{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
int n =num;
if(n/2000>0){
System.out.println("2000 = "+(n/2000));
n=n%2000;
}
if(n/500>0){
System.out.println("500 = "+(n/500));
n=n%500;
}
if(n/200>0){
System.out.println("200 = "+(n/200));
n=n%200;
}
if(n/100>0){

Name-Monalisa Das
Roll no.-2370247
System.out.println("100 = "+(n/100));
n=n%100;
}
if(n/50>0){
System.out.println("50 = "+(n/50));
n=n%50;
}
if(n/20>0){
System.out.println("20 = "+(n/20));
n=n%20;
}
if(n/10>0){
System.out.println("10 = "+(n/10));
n=n%10;
}
if(n/5>0)
{
System.out.println("5 = "+(n/5));
n=n%5;
}
if(n/2>0){
System.out.println("2 = "+(n/2));
n=n%2;
}
if(n/1>0){
System.out.println("1 = "+(n/1));

Name-Monalisa Das
Roll no.-2370247
n=n%1;
}
}
}
CMD
To Compile : javac A4Q3.java
To Run : java A4Q3
Output : Enter number: 15
10 = 1
5=1

Name-Monalisa Das
Roll no.-2370247
Q4. Test the nature of the root of a quadratic equation ax2 +bx+c=0. The nature
can be tested from the discriminant d=b2 -4ac. The result can be displayed
from the Table-2 after finding d from the values given in Table-1.
CODE
import java.util.*;
class A4Q4{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt();
System.out.print("Enter b: ");
int b = sc.nextInt();
System.out.print("Enter c: ");
int c = sc.nextInt();
int d = b*b-(4*a*c);
if(d>0){
System.out.print("Rational and squared roots");
//System.out.print("Real and equal roots");
}
else if(d<0){
System.out.print("Roots are imaginary");
}
else{
System.out.print("Roots are equal so only one root");
}
}
}

Name-Monalisa Das
Roll no.-2370247
CMD
To Compile : javac A4Q4.java
To Run : java A4Q4
Output : Enter a: 14
Enter b: 25
Enter c: 23
Roots are imaginary

Name-Monalisa Das
Roll no.-2370247
Q5. Input 3 sides of a triangle and find out which type of triangle is this.
CODE
import java.util.*;
class A4Q5{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter side a: ");
int a = sc.nextInt();
System.out.print("Enter side b: ");
int b = sc.nextInt();
System.out.print("Enter side c: ");
int c = sc.nextInt();
if(a>0&&b>0&&c>0)
{
if(a==b && b==c){
System.out.print("Equialteral triangle");
}else if(a==b || b==c || c==a){
System.out.print("Isoceles triangle");
}
else if((a*a==(b*b + c*c)) || (b*b==(a*a + c*c)) || (c*c==(b*b + a*a))){
System.out.print("Right angled triangle");
}else{
System.out.print("Scalene triangle");
}
}
}

Name-Monalisa Das
Roll no.-2370247
}
CMD
To Compile : javac A4Q5.java
To Run : java A4Q5
Output : Enter side a: 12
Enter side b: 13
Enter side c: 14
Scalene triangle

Name-Monalisa Das
Roll no.-2370247

You might also like