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

p3.java

The document contains Java programming exercises demonstrating various control flow statements including if, if-else, nested if, if-else ladder, switch case, for loop, while loop, and do-while loop. Each section includes code snippets and their expected outputs. The exercises are part of a practical assignment for a Java programming class.

Uploaded by

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

p3.java

The document contains Java programming exercises demonstrating various control flow statements including if, if-else, nested if, if-else ladder, switch case, for loop, while loop, and do-while loop. Each section includes code snippets and their expected outputs. The exercises are part of a practical assignment for a Java programming class.

Uploaded by

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

Name:-Sangharsh parvind Sawate

Class:Co4k(B)

Roll no:-64

Sub:-java programming(314317)

Practical no:-3
*if statement
Code:-
public class is
{
public static void main(String[] args)
{
int x = 10;
if (x < 20)
{
System.out.println("x is less than 20.");
}
}
}
Output:-
*If else statement
Code:-
public class st
{
public static void main(String[] args)
{
int number = 10;
if (number % 2 == 0)
{
System.out.println(number + " is even.");
}
else
{
System.out.println(number + " is odd.");
}
}
}
Output:-
*Nested if statement
public class nested
{
public static void main(String[] args)
{
int age = 20;
String citizenship = "Indian";
if (age >= 18)
{
if (citizenship.equals("Indian"))
{
System.out.println("You are eligible to vote in India.");
}
else
{
System.out.println("You are not eligible to vote in India.");
}
}
else
{
System.out.println("You are not eligible to vote due to age restrictions.");
}
}
}
Output:-
* if else ladder
Code:-
public class ifelselader
{
public static void main(String args[])
{
int x = 30;
if (x == 10)
{
System.out.println("Value of x is 10");
}
else if (x == 20)
{
System.out.println("Value of x is 20");
}
else if (x == 30)
{
System.out.println("Value of x is 30");
} else
{
System.out.println("This is else statement");
}
}
}
Output:-
*Switch case statement
Code:-
public class switchs
{

public static void main(String args[])


{
int month = 8;
switch (month)
{
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Invalid month");
break;
}
}
}
Output:-
*For loop
Code:-
public class forloop
{
public static void main(String args[])
{
int i;
for (i=1;i<=10;i++)
{
System.out.println(i);
}
}
}
Output:-
*While loop
Code:-
public class whiles
{
public static void main (String args[])
{
int x=20;
while (x<30)
{
System.out.println ("value of x = "+x);
x++;
System.out.print("\n");
}
}
}Output:-
*Do While loop
Code:-
public class whileloop
{
public static void main(String args[])
{
int x=30;
do
{
System.out.println("Value of x: "+x);
x++;
System.out.println("\n");
}
while(x<30);
}
}
Output:-

You might also like