Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
203 views18 pages

Java Code Examples PDF Part 1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

Java Code Examples

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

Example 26: www.code4examples.com


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.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}

/* https://www.code4example.com */

Example 2: This program inputs from user and then adds that given two numbers.
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
int number1, number2, sum;
Scanner scan = new Scanner(System.in);

System.out.print("Enter the First Number: ");


number1 = scan.nextInt();
System.out.print("Enter the Second Number: ");
number2 = scan.nextInt();

sum = number1 + number2;


System.out.println("\nResult = " +sum);
}
}

/* 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;

public class Main {


public static void main(String[] args) {
int a, b, temp;
Scanner s = new Scanner(System.in);

System.out.print("Enter the First Number: ");


a = s.nextInt();
System.out.print("Enter the Second Number: ");
b = s.nextInt();

temp = a;
a = b;
b = temp;

System.out.println("\na = " +a);


System.out.println("b = " +b);
}
}

/* https://www.code4example.com */

Example 4: Write a program in Java to find and print area of a square.


import java.util.Scanner;

public class Main {


public static void main(String[] args) {
float side, area;
Scanner s = new Scanner(System.in);

System.out.print("Enter the Side Length of Square: ");


side = s.nextFloat();

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;

public class Main {


public static void main(String[] args) {
float s, perimeter;
Scanner scan = new Scanner(System.in);

System.out.print("Enter the Side Length of Square: ");


s = scan.nextFloat();

perimeter = 4*s;
System.out.println("\nPerimeter = " +perimeter);
}
}

/* https://www.code4example.com */

Example 6: Write a program in Java that calculates area of rectangle.


import java.util.Scanner;

public class Main {


public static void main(String[] args) {
float len, bre, area;
Scanner s = new Scanner(System.in);

System.out.print("Enter the Length of Rectangle: ");


len = s.nextFloat();
System.out.print("Enter the Breadth of Rectangle: ");
bre = s.nextFloat();

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;

public class Main {


public static void main(String[] args) {
float length, breadth, perimeter;
Scanner s = new Scanner(System.in);

System.out.print("Enter the Length of Rectangle: ");


length = s.nextFloat();
System.out.print("Enter the Breadth of Rectangle: ");
breadth = s.nextFloat();

perimeter = (2*length) + (2*breadth);


System.out.println("\nPerimeter = " +perimeter);
}
}

/* 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;

public class Main {


public static void main(String[] args) {
Scanner s = new Scanner(System.in);

System.out.print("Enter the Length and Breadth of Rectangle: ");


float a = s.nextFloat();
float b = s.nextFloat();

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;

public class Main {


public static void main(String[] args) {
float r, area;
Scanner s = new Scanner(System.in);

System.out.print("Enter the Radius of Circle: ");


r = s.nextFloat();

area = (float)(3.14*r*r);
System.out.println("\nArea = " +area);
}
}

/* https://www.code4example.com */

Example 10: Write a Java program to calculate circumference of a circle.


import java.util.Scanner;

public class Main {


public static void main(String[] args) {
float r, circum;
Scanner s = new Scanner(System.in);

System.out.print("Enter the Radius of Circle: ");


r = s.nextFloat();

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;

public class Main {


public static void main(String[] args) {
float r, area, circum;
Scanner s = new Scanner(System.in);

System.out.print("Enter the Radius of Circle: ");


r = s.nextFloat();

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;

public class Main {


public static void main(String[] args) {
int num;
Scanner scan = new Scanner(System.in);

System.out.print("Enter a Number: ");


num = scan.nextInt();

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;

public class Main {


public static void main(String[] args) {
int a, b, c, large;
Scanner scan = new Scanner(System.in);

System.out.print("Enter the First Number: ");


a = scan.nextInt();
System.out.print("Enter the Second Number: ");
b = scan.nextInt();
System.out.print("Enter the Third Number: ");
c = scan.nextInt();

if(a>b && a>c)


large = a;
else if(b>a && b>c)
large = b;
else
large = c;

System.out.println("\nLargest = " +large);


}
}

/* 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;

public class Main {


public static void main(String[] args) {
int year;
Scanner scan = new Scanner(System.in);

System.out.print("Enter the Year: ");


year = scan.nextInt();

if(year%4==0 && year%100!=0)


System.out.println("\nIt is a Leap Year.");
else if(year%400==0)
System.out.println("\nIt is a Leap Year.");
else
System.out.println("\nIt is not a Leap Year.");
}
}

/* https://www.code4example.com */
Example 15: Write a simple calculator program in Java that performs four basic arithmetic
operations.
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
float a, b, res;
int choice;
Scanner scan = new Scanner(System.in);

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 && choice<=4)


{
System.out.print("\nEnter any Two Number: ");
a = scan.nextFloat();
b = scan.nextFloat();

if(choice==1)
res = a+b;
else if(choice==2)
res = a-b;
else if(choice==3)
res = a*b;
else
res = a/b;

System.out.println("\nResult = " +res);


}
else
System.out.println("\nInvalid Choice!");
}
}

/* 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

>800 and <=1500 10%

>1500 and <=2500 15%

>2500 and <=5000 20%

>5000 30%

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
float totalCost, costToPaid, discount;
Scanner scan = new Scanner(System.in);

System.out.print("Enter the Total Amount of Shopping: ");


totalCost = scan.nextFloat();

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;

public class Main {


public static void main(String[] args) {
int n, i, num, sum=0;
Scanner scan = new Scanner(System.in);

System.out.print("Enter the Value of n: ");


n = scan.nextInt();
System.out.print("Enter " +n+ " Numbers: ");
for(i=0; i<n; i++)
{
num = scan.nextInt();
sum = sum + num;
}

System.out.println("\nSum = " +sum);


}
}
/* https://www.code4example.com */
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.

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
int num, i, fact=1;
Scanner s = new Scanner(System.in);

System.out.print("Enter a Number: ");


num = s.nextInt();

for(i=num; i>=1; i--)


{
fact = fact*i;
}

System.out.println("\nFactorial Result = " +fact);


}
}
/* https://www.code4example.com */

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;

public class Main {


public static void main(String[] args) {
int num, rem, rev;
Scanner s = new Scanner(System.in);

System.out.print("Enter a Number: ");


num = s.nextInt();

for(rev=0; num!=0; num=num/10)


{
rem = num%10;
rev = (rev*10) + rem;
}

System.out.println("\nReverse = " +rev);


}
}
/* https://www.code4example.com */
Example 21: Write a Java program to check prime number using for loop.
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
int num, i, count=0;
Scanner s = new Scanner(System.in);

System.out.print("Enter a Number: ");


num = s.nextInt();

for(i=2; i<num; i++)


{
if(num%i == 0)
{
count++;
break;
}
}

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;

public class Main {


public static void main(String[] args) {
int num, i=2, count=0;
Scanner s = new Scanner(System.in);

System.out.print("Enter a Number: ");


num = s.nextInt();

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;

public class Main {


public static void main(String[] args) {
int num, rem, rev=0;
Scanner s = new Scanner(System.in);

System.out.print("Enter a Number: ");


num = s.nextInt();

while(num!=0)
{
rem = num%10;
rev = (rev*10) + rem;
num = num/10;
}

System.out.println("\nReverse = " +rev);


}
}
/* https://www.code4example.com */

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;

public class Main {


public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

System.out.print("Enter the Value of n: ");


int n = scan.nextInt();
int[] arr = new int[n];
System.out.print("Enter " +n+ " Numbers: ");
for(int i=0; i<n; i++)
arr[i] = scan.nextInt();

int sum = 0;
for(int i=0; i<n; i++)
sum += arr[i];

System.out.println("\nSum = " +sum);


}
}
/* https://www.code4example.com */

You might also like