Example1: Program to Add Two Integers
Public 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);
}
}
Example 2: Program to check if the number
entered by user is even or odd
import [Link];
public class JavaExample
{
public static void main(String args[])
{
int num;
[Link]("Enter an Integer number: ");
//The input provided by user is stored in num
Scanner input = new Scanner([Link]);
num = [Link]();
// If number is divisible by 2 then it's an even number
//else it is an odd number
if ( num % 2 == 0 )
[Link](num+" is an even number.");
else
[Link](num+" is an odd number.");
}
}
Example 3: Program to print Right Triangle Star
Pattern
public class JavaExample
{
public static void main(String args[])
{
int row, column, numberOfRows=8;
for(row=0; row<numberOfRows; row++)
{
for(column=0; column<=row; column++)
{
[Link]("* ");
}
//This is just to print the stars in new line after each row
[Link]();
}
}
}