0% found this document useful (0 votes)
15 views4 pages

Exp 03 PDF

The document contains multiple Java programs demonstrating the use of conditional statements, loops, and logical operators. It includes examples for checking if a number is even or odd, using switch-case with characters, and displaying numbers from 1 to 20 using different loop constructs. Additionally, it showcases the application of logical operators within a do-while loop.

Uploaded by

malinikhil2007
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)
15 views4 pages

Exp 03 PDF

The document contains multiple Java programs demonstrating the use of conditional statements, loops, and logical operators. It includes examples for checking if a number is even or odd, using switch-case with characters, and displaying numbers from 1 to 20 using different loop constructs. Additionally, it showcases the application of logical operators within a do-while loop.

Uploaded by

malinikhil2007
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/ 4

1. Write a program to check multiple conditions using if statements along with logical operators.

public class if_statement


{
public static void main(String[] args)
{
int num=10; if(num
%2==0 && num>0)
{
System.out.print("The number is even and positive");
}
else if(num%2==0 && num<0)
{
System.out.print("The number is even and negative");
}
else if(num%2==0 || num>0)
{
System.out.print("The number is either even or positive");
}
else if(num%2!=0 || num<0)
{
System.out.print("The number is either even or negative");
}
else
{
System.out.print("The number is zero");
}
}
}
OUTPUT :

2. Write a program to check no is even or odd.


public class Even_odd
{
public static void main(String[] args)
{
int num=10; if(num
%2==0)
{
System.out.print("The number is even");
}
else
{
System.out.print("The number is odd");
}
}
}
OUTPUT :

3.Write a program to check switch-case using character datatype.


public class Switch_case
{
public static void main(String[] args)
{
char grade='A';
switch(grade)
{
case 'A':
System.out.print("Excellent");
break;

case 'B':
System.out.print("Good");
break;

case 'C':
System.out.print("Average");
break;

case 'D':
System.out.print("Below average");
break;

default:
System.out.print("Fail");
break;
}
}
}
OUTPUT :

4. Write a program to display 1 to 20 numbers using for, while and do-while loop.
● Using for loop
public class For_loop
{
public static void main(String[] args)
{
int i;
for(i=1; i<=20; i++)
{
System.out.print(" "+i);
}
}
}
OUTPUT :

● Using while loop


public class While_loop
{
public static void main(String[] args)
{
int i=1;
while(i<=20)
{
System.out.print(" "+i);
i++;
}
}
}
OUTPUT :

● Using do-while loop


public class do_while_loop
{
public static void main(String[] args)
{
int i=1;
do
{
System.out.print(" "+i);
i++;
}while(i<=20);
}
}
OUTPUT :
5. Develop a program to use logical operators in do-while loop.
public class do_while
{
public static void main(String[] args)
{
int i=1;
do
{
if(i%2==0 && i%3==0)
{
System.out.println(i+" is divisible by 2 and 3");
}
else if(i%2==0 || i%3==0)
{
System.out.println(i+" is divisible by 2 or 3");
}
else
{
System.out.println(i+" not divisible by 2 or 3");
}
i++;
}
while(i<=5);
}
}
OUTPUT :

You might also like