1. Program to Print Hello World.
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World!!");
}
}
@java_codings
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World!!");
}
}
@java_codings
2. Program to Accept values of Two Numbers and print their Addition.
import java.util.Scanner;
class AddNumbers
{
public static void main(String args[])
{
int x, y, z;
System.out.print("Enter two integers to calculate their sum : ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = x + y;
System.out.println("Sum of entered integers = " + z);
}
}
@java_codings
import java.util.Scanner;
class AddNumbers
{
public static void main(String args[])
{
int x, y, z;
System.out.print("Enter two integers to calculate their sum : ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = x + y;
System.out.println("Sum of entered integers = " + z);
}
}
@java_codings
3. Program to accept three Number and Print Largest among them.
class CommandLineArgs
{
public static void main(String args[])
{
int a, b, c;
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c = Integer.parseInt(args[2]);
if (a > b && a > c) {
System.out.println("Largest Number is : " + a);
} else if (b > c) {
System.out.println("Largest Number is : " + b);
} else {
System.out.println("Largest Number is : " + c);
}
}
}
@java_codings
class CommandLineArgs
{
public static void main(String args[])
{
int a, b, c;
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c = Integer.parseInt(args[2]);
if (a > b && a > c) {
System.out.println("Largest Number is : " + a);
} else if (b > c) {
System.out.println("Largest Number is : " + b);
} else {
System.out.println("Largest Number is : " + c);
}
}
}
@java_codings
4. Program to Accept value of Radius and Calculate Area of Circle.
class CalculateCircleAreaExample
{
public static void main(String[] args)
{
int radius = 3;
System.out.println("The radius of the circle is " + radius);
/*
* Area of a circle is pi * r * r where r is a radius of a circle.
*/
// NOTE : use Math.PI constant to get value of pi
double area = Math.PI * radius * radius;
System.out.println("Area of a circle is " + area);
}
}
@java_codings
class CalculateCircleAreaExample
{
public static void main(String[] args)
{
int radius = 3;
System.out.println("The radius of the circle is " + radius);
/*
* Area of a circle is pi * r * r where r is a radius of a circle.
*/
// NOTE : use Math.PI constant to get value of pi
double area = Math.PI * radius * radius;
System.out.println("Area of a circle is " + area);
}
}
@java_codings
5. Program to Accept value of length & width and Calculate Area of Rectangle.
import java.util.Scanner;
class AreaOfRectangle
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();
//Area = length*width;
double area = length * width;
System.out.println("Area of Rectangle is:" + area);
}
}
@java_codings
import java.util.Scanner;
class AreaOfRectangle
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();
//Area = length*width;
double area = length * width;
System.out.println("Area of Rectangle is:" + area);
}
}
@java_codings
6. Program to Accept value of the side of Square and Calculate Area of Square.
import java.util.*;
class AreaOfSquare
{
public static void main(String args[])
{
int side, area;
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of the sides of square");
side = sc.nextInt();
area = side * side;
System.out.println("Area of Square : " + area);
}
}
@java_codings
import java.util.*;
class AreaOfSquare
{
public static void main(String args[])
{
int side, area;
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of the sides of square");
side = sc.nextInt();
area = side * side;
System.out.println("Area of Square : " + area);
}
}
@java_codings