This post contains a list of basic java programs for beginners. These programs will provide you a good understanding of the basics that how java programs work. All coding written below is well tested. Before you start executing these codes you must need these things to be done first:
- install the JDK ( Java Development Kit )
- set path of the jdk/bin directory
List Of Programs:
1. Even Or Odd Numbers Checking (with user input)
import java.util.Scanner;
class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an integer ");
Scanner in = new Scanner(System.in); //To ask input from user
x = in.nextInt();
// remainder after dividing x by2
if ( x % 2 == 0 )
System.out.println("You entered " + "'" + x + "'" + " which is an even number.");
else
System.out.println("You entered " + "'" + x + "'" + " which is an odd number.");
}
}
class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an integer ");
Scanner in = new Scanner(System.in); //To ask input from user
x = in.nextInt();
// remainder after dividing x by2
if ( x % 2 == 0 )
System.out.println("You entered " + "'" + x + "'" + " which is an even number.");
else
System.out.println("You entered " + "'" + x + "'" + " which is an odd number.");
}
}
Outputs:
Enter an integer 2
You entered 2 which is an even number
Enter an integer 2
You entered 2 which is an even number
2. Printing Week Days (Using For Loop)
import java.text.DateFormatSymbols;
public class PrintWeekDays {
public static void main(String[] args) {
String[] weekdays = new DateFormatSymbols().getWeekdays();
// to Get day names
for(String s: weekdays){public class PrintWeekDays {
public static void main(String[] args) {
String[] weekdays = new DateFormatSymbols().getWeekdays();
// to Get day names
System.out.println(s);
} } }
Output:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
3. List Of Even Numbers (1 to any (both inclusive))
/*
find and list even numbers between 1 and any given number.
*/
public class ListEvenNumbers {
public static void main(String[] args) {
//define limit
int limit = 10;
System.out.println("Printing Even numbers between 1 and " + limit);
for(int i=1; i <= limit; i++){
// if the number is divisible by 2 then it is even
if( i % 2 == 0){
System.out.print(i + " ");
}
}
}
}
Output:
Printing Even Numbers between 1 and 102 4 6 8 10
4. Multiplication Table (By User Input)
import java.util.Scanner;
class MultiplicationTable
{
public static void main(String args[])
{
int n, c;
System.out.println("Enter an integer to print it's multiplication table:");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :-");
for ( c = 1 ; c <= 10 ; c++ )
System.out.println(n+"*"+c+" = "+(n*c));
}
}
Output:
Enter an integer to print it's multiplication table:
2
Multiplication table of 2 is :-
2*1 = 2
2*2 = 4
2*3 = 6
2*4 = 8
2*5 = 10
2*6 = 12
2*7 = 14
2*8 = 16
2*9 = 18
2*10 = 20
5. Printing Text ( Hello World )
public class MyFirstJavaProgram {
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}
Output:
Hello World
6. Rectangle Measurement (Squares Included)
import java.util.Scanner;
class RectDemo
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);//This is necessary for inputs
System.out.print("Enter the length of Rectangle: ");
double length = sc.nextDouble(); //Takes the user input as length
System.out.print("Enter the breadth of Rectangle: ");
double breadth = sc.nextDouble();
if(length==breadth){
double area = length * breadth;
System.out.println("The area of Square is: " + area);
double perimeter= 2 * (length + breadth);
System.out.println( "The perimeter of the Square is:"+perimeter) ;
}else{
double area = length * breadth;
System.out.println("The area of Rectangle is: " + area);
double perimeter= 2 * (length + breadth);
System.out.println( "The perimeter of the rectangle is:"+perimeter) ;
}
}
}
Enter the breadth of Rectangle: 3
The area of Rectangle is: 6.0
The perimeter of Rectangle is:10.0
Output:
Enter the length of Rectangle: 2Enter the breadth of Rectangle: 3
The area of Rectangle is: 6.0
The perimeter of Rectangle is:10.0