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;
System.out.println(first + " + " + second + " = " + sum);
}
}
Example 2: Program to check if the number
entered by user is even or odd
import java.util.Scanner;
public class JavaExample
{
public static void main(String args[])
{
int num;
System.out.print("Enter an Integer number: ");
//The input provided by user is stored in num
Scanner input = new Scanner(System.in);
num = input.nextInt();
// If number is divisible by 2 then it's an even number
//else it is an odd number
if ( num % 2 == 0 )
System.out.println(num+" is an even number.");
else
System.out.println(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++)
{
System.out.print("* ");
}
//This is just to print the stars in new line after each row
System.out.println();
}
}
}