Write a Program to Add Two Integers
class Main {
public static void main(String[] args) {
int first = 10;
int second = 20;
// add two numbers
int sum = first + second;
[Link](first + " + " + second + " = " + sum);
}
}
Write program to Swap two numbers using temporary
variable
public class SwapNumbers {
public static void main(String[] args) {
float first = 1.20f, second = 2.45f;
[Link]("--Before swap--");
[Link]("First number = " + first);
[Link]("Second number = " + second);
// Value of first is assigned to temporary
float temporary = first;
// Value of second is assigned to first
first = second;
// Value of temporary (which contains the initial value of first) is assigned
to second
second = temporary;
[Link]("--After swap--");
[Link]("First number = " + first);
[Link]("Second number = " + second);
}}
Check whether a number is even or odd using
if...else statement
import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner reader = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if(num % 2 == 0)
[Link](num + " is even");
else
[Link](num + " is odd");
}
}
Check whether a number is even or odd using ternary
operator
import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner reader = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
String evenOdd = (num % 2 == 0) ? "even" : "odd";
[Link](num + " is " + evenOdd);
}
}
Java Program to create a Simple Calculator using else if
public class Example {
private static Scanner sc;
public static void main(String[] xrgs) {
sc = new Scanner([Link]);
double x, y, result = 0;
[Link]("Enter Two Numbers = ");
x = [Link]();
y = [Link]();
[Link]("Enter anyy opt from +, - , /, *, % = ");
char opt = [Link]().charAt(0);
if (opt == '+') {
result = add(x, y);
} else if (opt == '-') {
result = sub(x, y);
} else if (opt == '*') {
result = mul(x, y);
} else if (opt == '/') {
result = div(x, y);
} else if (opt == '%') {
result = mod(x, y);
} else
{
[Link]("You have entered incorrect option");
}
[Link]("%.2f %c %.2f = %.2f", x, opt, y, result);
}
static double add(double x, double y) {
return x + y;
}
static double sub(double x, double y) {
return x - y;
}
static double mul(double x, double y) {
return x * y;
}
static double div(double x, double y) {
return x / y;
}
static double mod(double x, double y) {
return x % y;
}
}
Java Program to find Arithmetic Sum using
Method Overloading
public class SumMethodOverloading {
int add(int a, int b)
{
int sum = a + b;
return sum;
}
int add(int a, int b, int c)
{
return a + b + c;
}
int add(int a, int b, int c, int d)
{
return a + b + c + d;
}
int add(int a, int b, int c, int d, int e)
{
return a + b + c + d + e;
}
public static void main(String[] args) {
SumMethodOverloading obj = new SumMethodOverloading();
int sum = [Link](10, 20);
[Link]("The Sum of Two Numbers = " + sum);
[Link]("The Sum of Three Numbers = " + [Link](10, 20, 40));
[Link]("The Sum of Four Numbers = " + [Link](15, 25, 49, 66));
[Link]("The Sum of Five Numbers = " + [Link](11, 19, 16, 72,
66));