[go: up one dir, main page]

0% found this document useful (0 votes)
11 views5 pages

Simple Programs

The document contains Java code examples demonstrating various operators and control flow statements, including arithmetic, relational, logical, and assignment operators. It also illustrates conditional statements such as if, if-else, nested if, and switch statements. Each code snippet is designed to showcase the functionality of these programming constructs.

Uploaded by

tmpriya310
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)
11 views5 pages

Simple Programs

The document contains Java code examples demonstrating various operators and control flow statements, including arithmetic, relational, logical, and assignment operators. It also illustrates conditional statements such as if, if-else, nested if, and switch statements. Each code snippet is designed to showcase the functionality of these programming constructs.

Uploaded by

tmpriya310
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/ 5

Arithmetic Operators

public class pg1


{
public static void main(String[] args)
{
int a = 10;
int b = 5;

System.out.println("a + b = " + (a + b));


System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
}
}
Relational Operators
public class pg2
{
public static void main(String[] args)
{
int a = 10;
int b = 5;

System.out.println("a == b: " + (a == b));


System.out.println("a != b: " + (a != b));
System.out.println("a > b: " + (a > b));
System.out.println("a < b: " + (a < b));
System.out.println("a >= b: " + (a >= b));
System.out.println("a <= b: " + (a <= b));
}
}
Logical Operators

public class pg3


{
public static void main(String[] args)
{
boolean x = true;
boolean y = false;

System.out.println("x && y: " + (x && y));


System.out.println("x || y: " + (x || y));
System.out.println("!x: " + (!x));
}
}
Assignment Operators
public class pg4
{
public static void main(String[] args)
{
int a = 10;
int b = 5;
a += b; // equivalent to a = a + b
System.out.println("a += b: " + a);

a -= b; // equivalent to a = a - b
System.out.println("a -= b: " + a);

}
}
If Statement
public class IfStatement
{
public static void main(String[] args) {
int number = 10;

if (number > 0) {
System.out.println("The number is positive.");
}
}
}

if...else Statement

public class IfElseStatement


{
public static void main(String[] args)
{
int number = -5;

if (number > 0)
{
System.out.println("The number is positive.");
}
else
{
System.out.println("The number is negative.");
}
}
}
Nested If Statement
public class NestedIfStatement
{
public static void main(String[] args)
{
int number = 0;

if (number > 0)
{
System.out.println("The number is positive.");
}
else
{
if (number < 0)
{
System.out.println("The number is negative.");
}
else
{
System.out.println("The number is zero.");
}
}
}
}

Switch Statement
public class SwitchStatement
{
public static void main(String[] args)
{
int day = 3;
String dayName;
switch (day)
{
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day";
break;
}
System.out.println("The day is: " + dayName);
}
}

You might also like