Java Code Examples PDF Part 1
Java Code Examples PDF Part 1
Java Code Examples PDF Part 1
Example 1: This example covers a program in Java that prints Hello, World. To print Hello,
World on the output screen in Java programming, just place the string “Hello, World”
inside System.out.println() as shown in the program given below. ................................................ 3
Example 2: This program inputs from user and then adds that given two numbers. ......................... 3
Example 3: Write a Java program to swap any two given numbers. The number must be
received by user at run-time. ............................................................................................................... 4
Example 4: Write a program in Java to find and print area of a square. .................................... 4
Example 5: Write a program in Java to find and print perimeter of a square. ........................... 5
Example 6: Write a program in Java that calculates area of rectangle. ...................................... 5
Example 7: Write a Java program to find and print perimeter of a rectangle. ........................... 6
Example 8: Created basically to print area and perimeter both, of a rectangle, whose length and
breadth will get entered by user at run-time.......................................................................................... 6
Example 9: Write a Java program to calculate area of circle. ....................................................... 7
Example 10: Write a Java program to calculate circumference of a circle. ................................. 7
Example 11: This is basically the combined version of previous two programs. That is, I’ve
combined both the program, so that in a single program, we can see the area and circumference
both at one execution of the program. ................................................................................................... 8
Example 12: Write a Java program to check odd or even number. The number must be received by
user at run-time....................................................................................................................................... 8
Example 13: Write a Java program to find the largest of three given numbers. ........................ 9
Example 14: Write a Java program that checks whether an year is a leap year or not. ......... 10
Example 15: Write a simple calculator program in Java that performs four basic arithmetic
operations. ............................................................................................................................................ 11
Example 16: Write a Java program to find the price that has to be paid after applying the discount (if
any). ....................................................................................................................................................... 12
Example 17: This program prints Hello, World 10 number of times, using for loop. .................. 14
Example 18: Write a Java program to find the sum of n numbers. The value
of n and n numbers must be received by user at run-time. .......................................................... 14
Example 19: Write a Java program to find factorial of a number. The number must be
received by user at run-time of the program. ................................................................................. 15
Example 20: Write a Java program to reverse a number using for loop. The number to
reverse, must be received by user at run-time of the program..................................................... 15
Example 21: Write a Java program to check prime number using for loop. ............................ 16
Example 22: This program prints Hello, World 10 number of times, using while loop, instead
of for. .................................................................................................................................................... 16
Example 23: Write a Java program to check prime number using while loop......................... 17
Example 24: Write a Java program to reverse a number. The number to reverse, must be
received by user at run-time of the program. ................................................................................. 18
Example 25: Write a Java program to find the sum of n numbers using array. The value of n and n
numbers must be received by user at run-time. ................................................................................... 18
/* https://www.code4example.com */
Example 2: This program inputs from user and then adds that given two numbers.
import java.util.Scanner;
/* https://www.code4example.com */
Example 3: Write a Java program to swap any two given numbers. The number must
be received by user at run-time.
import java.util.Scanner;
temp = a;
a = b;
b = temp;
/* https://www.code4example.com */
area = 4*side;
System.out.println("\nArea = " +area);
}
}
/* https://www.code4example.com */
Example 5: Write a program in Java to find and print perimeter of a square.
import java.util.Scanner;
perimeter = 4*s;
System.out.println("\nPerimeter = " +perimeter);
}
}
/* https://www.code4example.com */
area = len*bre;
System.out.println("\nArea = " +area);
}
}
/* https://www.code4example.com */
Example 7: Write a Java program to find and print perimeter of a rectangle.
import java.util.Scanner;
/* https://www.code4example.com */
Example 8: Created basically to print area and perimeter both, of a rectangle, whose
length and breadth will get entered by user at run-time.
import java.util.Scanner;
float ar = a*b;
float pr = 2*(a+b);
System.out.println("\nArea = " +ar);
System.out.println("Perimeter = " +pr);
}
}
/* https://www.code4example.com */
Example 9: Write a Java program to calculate area of circle.
import java.util.Scanner;
area = (float)(3.14*r*r);
System.out.println("\nArea = " +area);
}
}
/* https://www.code4example.com */
circum = (float)(2*3.14*r);
System.out.println("\nCircumference = " +circum);
}
}
/* https://www.code4example.com */
Example 11: This is basically the combined version of previous two programs. That is, I’ve
combined both the program, so that in a single program, we can see the area and
circumference both at one execution of the program.
import java.util.Scanner;
area = (float)(3.14*r*r);
circum = (float)(2*3.14*r);
System.out.println("\nArea = " +area);
System.out.println("Circumference = " +circum);
}
}
/* https://www.code4example.com */
Example 12: Write a Java program to check odd or even number. The number must be
received by user at run-time.
import java.util.Scanner;
if(num%2==0)
{
System.out.println("\nIt is an Even Number.");
}
else
{
System.out.println("\nIt is an Odd Number.");
}
}
}
/* https://www.code4example.com */
Example 13: Write a Java program to find the largest of three given numbers.
import java.util.Scanner;
/* https://www.code4example.com */
Example 14: Write a Java program that checks whether an year is a leap year or not.
import java.util.Scanner;
/* https://www.code4example.com */
Example 15: Write a simple calculator program in Java that performs four basic arithmetic
operations.
import java.util.Scanner;
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.print("Enter Your Choice (1-4): ");
choice = scan.nextInt();
if(choice==1)
res = a+b;
else if(choice==2)
res = a-b;
else if(choice==3)
res = a*b;
else
res = a/b;
/* https://www.code4example.com */
Example 16: Write a Java program to find the price that has to be paid after applying the
discount (if any).
Shopping Amount Discount
<=800 No discount
>5000 30%
import java.util.Scanner;
if(totalCost<=800)
{
costToPaid = totalCost;
}
else if(totalCost>800 && totalCost<=1500)
{
discount = (totalCost*10)/100;
costToPaid = totalCost - discount;
}
else if(totalCost>1500 && totalCost<=2500)
{
discount = (totalCost*15)/100;
costToPaid = totalCost - discount;
}
else if(totalCost>2500 && totalCost<=5000)
{
discount = (totalCost*20)/100;
costToPaid = totalCost - discount;
}
else
{
discount = (totalCost*30)/100;
costToPaid = totalCost - discount;
}
System.out.println("\nThe cost to be Paid is: " + costToPaid);
}
}
/* https://www.code4example.com */
Example 17: This program prints Hello, World 10 number of times, using for loop.
public class Main {
public static void main(String[] args) {
for(int i=0; i<10; i++)
System.out.println("Hello, World");
}
}
/* https://www.code4example.com */
Example 18: Write a Java program to find the sum of n numbers. The value
of n and n numbers must be received by user at run-time.
import java.util.Scanner;
import java.util.Scanner;
Example 20: Write a Java program to reverse a number using for loop. The number
to reverse, must be received by user at run-time of the program.
import java.util.Scanner;
if(count==0)
System.out.println("\nIt is a Prime Number.");
else
System.out.println("\nIt is not a Prime Number.");
}
}
/* https://www.code4example.com */
Example 22: This program prints Hello, World 10 number of times, using while loop,
instead of for.
public class Main {
public static void main(String[] args) {
int i=0;
while(i<10)
{
System.out.println("Hello, World");
i++;
}
}
}
/* https://www.code4example.com */
Example 23: Write a Java program to check prime number using while loop.
import java.util.Scanner;
while(i<num)
{
if(num%i == 0)
{
count++;
break;
}
i++;
}
if(count==0)
System.out.println("\n" +num+ " is a Prime Number.");
else
System.out.println("\n" +num+ " is not a Prime Number.");
}
}
/* https://www.code4example.com */
Example 24: Write a Java program to reverse a number. The number to reverse,
must be received by user at run-time of the program.
import java.util.Scanner;
while(num!=0)
{
rem = num%10;
rev = (rev*10) + rem;
num = num/10;
}
Example 25: Write a Java program to find the sum of n numbers using array. The value of n
and n numbers must be received by user at run-time.
import java.util.Scanner;
int sum = 0;
for(int i=0; i<n; i++)
sum += arr[i];