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

ASSIGNMENT1

java basic programs

Uploaded by

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

ASSIGNMENT1

java basic programs

Uploaded by

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

#1- WAP to find whether number is even or odd using bitwise operator

* bitwise xor operator

class bitwise {
public static void main(String[] args)
{
int n=47;
if((n ^ 1) == (n+1))
System.out.println("even");
else
System.out.println("odd");
}

*bitwise and operator

class EO
{
public static void main(String[] args)
{
int n=3;
if ((n & 1) == 0)
System.out.println(n + " is even");
else
System.out.println(n + " is odd");
}
}

#2- WAP to find largest of three numbers using ternary/ conditional operator

class large
{
public static void main(String[] args)
{
int n1=5;
int n2=4;
int n3=7;
int l = (n1 > n2) ? ((n1 > n3) ? n1 : n3) : ((n2 > n3) ? n2 : n3);

System.out.println("Largest no.: " + l);


}
}

#3- WAP to reverse a number

class reverse
{
public static void main(String[] args)
{
int n = 12;
int r = 0;
while (n != 0)
{
int n1 = n % 10;
r = r * 10 + n1;
n = n/10;
}
System.out.println("Reversed no.: " + r);
}
}

#4- To find whether number is positive, negative or zero using else if


class checkelseif
{
public static void main(String[] args)
{
int n = 1;
if (n > 0)
{
System.out.println("No. is +ve");
}
else if (n < 0)
{
System.out.println("No. is -ve");
}
else
{
System.out.println("No. is 0");
}
}
}

#5- To find whether its pos even , pos odd, negative even or negative odd or zero
using nested if

class checknestedif
{
public static void main(String[] args)
{
int n = -5;
if (n > 0)
{
if (n % 2 == 0)
System.out.println("No. is +ve even");
if (n % 2 != 0)
System.out.println("No. is +ve odd");
}
if (n < 0)
{
if (n % 2 == 0)
System.out.println("No. is -ve even");
if (n % 2 != 0)
System.out.println("No. is -ve odd");
}
if (n == 0)
{
System.out.println("No. is 0");
}
}
}

#6- Simple calculator using switch

class calci
{
public static void main(String[] args)
{
int n1 = 4;
int n2 = 8;
int op = 1;
int res;
switch (op)
{
case 1:
res = n1 + n2;
break;
case 2:
res = n1 - n2;
break;
case 3:
res = n1 * n2;
break;
case 4:
if (n2 != 0)
res = n1 / n2;
else
{
System.out.println("Error!Division by 0");
return;
}
break;
default:
System.out.println("Invalid op!!");
return;
}
System.out.println("Result: " + res);
}
}

You might also like