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

JPR Manual Answers Experiment 3

The document contains multiple Java programming examples demonstrating the use of conditional statements (if, if-else, switch-case) and loops (for, while, do-while). Each example includes a program statement, code, and output. The programs cover checking conditions, determining even or odd numbers, and displaying numbers from 1 to 20 using different looping constructs.

Uploaded by

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

JPR Manual Answers Experiment 3

The document contains multiple Java programming examples demonstrating the use of conditional statements (if, if-else, switch-case) and loops (for, while, do-while). Each example includes a program statement, code, and output. The programs cover checking conditions, determining even or odd numbers, and displaying numbers from 1 to 20 using different looping constructs.

Uploaded by

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

Experiment No:-3

1. Program Statement: Write a program to check multiple conditions using if statement


along with logical operators.
Code:
class If
{
public static void main(String args[])
{
int per=78;
if(per>=85)
{
System.out.println("Pass with first class");
}
else if(per>=60 && per<85)
{
System.out.println("Pass with second class");
}
else if(per>=40 && per<60)
{
System.out.println("Pass class");
}
else
{
System.out.println("Fail");
}
}
}
Output:
2. Program Statement: Write a program to check no is even or odd.
Code:
class IfElse
{
public static void main(String args[])
{
int a=22;
if(a%2==0)
{
System.out.println("Entered number is even");;
}
else
{
System.out.println("Entered number is odd");
}
}
}
Output:

 Simple If Statement:
Code:
class If
{
public static void main(String args[])
{
int i=10;
if(i<15)
{
System.out.println("Value of i is smaller than 15!");
}
}
}
Output:

 If-Else-if Statement:
Code:
class IfElseif
{
public static void main(String args[])
{
int i=10;
if(i<15)
{
System.out.println("Value of i is smaller than 15!");
}
else if(i>15)
{
System.out.println("Value of i is greater than 15!");
}
else
{
System.out.println("Value of i is equal to 15");
}
}

}
Output:

 Nested –if Statement:-


Code:
class Nested
{
public static void main(String args[])
{
int i=10;
if(i<15)
{
if(i<12)
{
System.out.println("Value of i is smaller than 15 and 12!");
}
}
else
{
System.out.println("Value of i is equal to 15!");
}
}
}
Output:
3. Program Statement: write a program to check switch-case using character datatype.
Code:
class Switch
{
public static void main(String args[])
{
int day=4;
switch(day)
{
case 1:System.out.println("Sunday");
break;
case 2:System.out.println("Monday");
break;
case 3:System.out.println("Tuesday");
break;
case 4:System.out.println("Wednesday");
break;
case 5:System.out.println("Thursday");
break;
case 6:System.out.println("Friday");
break;
case 7:System.out.println("Saturday");
break;
default:System.out.println("Wrong choice!");
break;
}
}
}
Output:

4. Program Statement: Write a program to display 1 to 20 numbers using for, while and
do-while loop.
1.For loop:-
Code:
class For
{
public static void main(String args[])
{
for(int i=1;i<=20;i++)
{
System.out.println(i);
}
}
}
Output:
2. While loop:-
Code:
class While
{
public static void main(String args[])
{
int i=1;
while(i<=20)
{
System.out.println(i);
i++;
}
}
}
Output:

3. Do-While loop:-
Code:
class DoWhile
{
public static void main(String args[])
{
int i=1;
do
{
System.out.println(i);
i++;
}while(i<=20);
}
}
Output:

5. Program Statement: Develop a program to use logical operators in do-while loop.


Code:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int num;
do
{
System.out.print("Enter a number between 1 and 10: ");
num = scanner.nextInt();
if (num >= 1 && num <= 10)
{
System.out.println("You entered a valid number: " + num);
break;
}
else
{
System.out.println("Invalid number");
}
} while (num < 1 || num > 10);
scanner.close();
}
}
Output:

You might also like