codescracker


java

Java Programming Examples



« Java Tutorial Next Program »


To understand Java practically, it is important to experiment a lot with many programs written in Java. So, we provided a lot of programs written in Java for you. Here are the list of more than 100 examples in Java programming.

Now let's take a look at these Java programs starting from the simplest Java program written here with output to shutdown your computer.

Java Program 1

Following is the simplest Java Program which will print "Hello Compiler !, I am Java..!!" on the screen :

/* Java Program Example - Simplest Java Program */
		
public class JavaProgram
{
    public static void main(String args[])
    {
       System.out.println("Hello Compiler !, I am Java..!!");
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Programming Examples with output

Java Program 2

Following Java program prints number from 1 to 10 in vertical line :

/* Java Program Example - Print 1-10 */
		
public class JavaProgram
{
    public static void main(String args[])
    {
        int n=1, i;
        for(i=0; i<10; i++)
        {
            System.out.println(n);
            n++;
        }
    }
}

When the above Java program is compile and executed, it will produce the following output:

Java Programs

Java Program 3

Following Java program print 1 to 100 in two dimension:

/* Java Program Example - Print 1-100 */
		
public class JavaProgram
{
    public static void main(String args[])
    {
        int n=1, i, j;
        for(i=0; i<10; i++)
        {
            for(j=0; j<10; j++)
            {
                System.out.print(n+ "  ");
                n++;
            }
            System.out.println();
        }
    }
}

When the above Java program is compile and executed, it will produce the following output:

Java Programs list

Java Program 4

Following Java program print 1 to 45 in two dimension :

/* Java Program Example - Print 1-45 */
		
public class JavaProgram
{
    public static void main(String args[])
    {
        int n=1, i, j;
        for(i=0; i<10; i++)
        {
            for(j=0; j<i; j++)
            {
                System.out.print(n + "  ");
                n++;
            }
            System.out.println();
        }
    }
}

When the above Java program is compile and executed, it will produce the following output:

Java Programming examples with output

Java Program 5

/* Java Programming Example */

import java.util.Scanner;

public class JavaProgramsExamples
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int num;
        System.out.println("Enter 10: ");
        num = scan.nextInt();
        if(num == 10)
        {
            System.out.println("Good");
        }
        else
        {
            System.out.println("Bad");
        }
    }
}

Here is the output produced by the above Java program:

java examples

Java Program 6

/* Java Programming Example */

import java.util.Scanner;

public class JavaProgramsExamples
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int num1, num2, num3;
        int sum=0, avg=0;
        System.out.print("Enter any three numbers: ");
        num1 = scan.nextInt();
        num2 = scan.nextInt();
        num3 = scan.nextInt();
        System.out.println("You entered "+num1+", "+num2+", and "+num3);
        System.out.println("Summing up all these numbers together...");
        sum = num1+num2+num3;
        System.out.println("Sum = " +sum);
        System.out.println("Finding average of all these three numbers...");
        avg = sum/3;
        System.out.println("Average = " +avg);
    }
}

Here is the sample run of the above Java Program:

java examples list

Programming Examples in Other Languages

You may also like to learn and practice programming examples in other popular programming languages:


« Java Tutorial Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test