Simple Java Programs
Simple Java Programs
Bluej includes:
OUTPUT
2. Write a program to assign 10 and 5 in two different variables.
Print the sum, difference, product, quotient and remainder.
//To do mathematical operations
class maths
{
public static void main()
{
int a=10, b=5;
int z=a+b;
int y=a-b;
int x=a*b;
int w=a/b;
int v=a%b;
System.out.println("Sum is "+z);
System.out.println("Difference is "+y);
System.out.println("Product is " +x);
System.out.println("Quotient is "+w);
System.out.println("Remainder is "+v);
}}
OUTPUT
3. Write a program to input a number and find the square root
and cube root of the number.
OUTPUT
4. Write a program to input the side of a square and find the
area and perimeter of the square.
// TO PRINT THE AREA AND PERIMETER OF A SQUARE
class square
{ s
public static void main(int s) 12
{
double x=Math.pow(s,2);
double y=4*s;
System.out.println("THE AREA OF THE SQUARE IS " +x);
System.out.println("THE PERIMETER OF THE SQUARE IS " +y);
}
}
OUTPUT
5. Write a program to input the length and breadth of a
rectangle and find the area and perimeter of the rectangle.
// TO PRINT THE AREA AND PERIMETER OF A RECTANGLE
class rectangle
{
public static void main(int l,int b)
{ l,b
int x=l*b; 24,12
int y=2*(l+b);
System.out.println("THE AREA OF THE RECTANGLE IS " +x);
System.out.println("THE PERIMETER OF THE RECTANGLE IS " +y);
}
}
OUTPUT
6. Write a program to input a number and find the square and
cube of the number.
//TO PRINT THE SQUARE AND CUBE OF A NUMBER
class sqandcb n
{ 12
public static void main(int n)
double square=Math.pow(n,2);
double cube=Math.pow(n,3);
}}
OUTPUT
7. Write a program to input two numbers and find the biggest and the
smallest number.
class smallbig
{ a,b
public static void main(int a, int b) 348,457
{
double x=Math.min(a,b);
double y=Math.max(a,b);
OUTPUT
8. Write a program to input the cost of one shirt and find the cost
of five shirts.
//To print the cost of 5 shirts
{ 798.76
double a=5*cost;
OUTPUT
9. Write a program to input three numbers and find their sum
and average.
//TO PRINT THE SUM AND AVERAGE OF THREE NUMBERS
class sumage
{ 25,32,48
int sum=a+b+c;
double average=(a+b+c)/3;
OUTPUT
10. Write a program to input appropriate values and find
√a + 𝒄𝒃 + |a|.
class expression
{ a,b,c
OUTPUT
11. Write a program to input three sides of a triangle and find its
area.
//To print the area of a triangle
a,b,c
class triangle
{ 12,14,17
public static void main(int a, int b, int c)
{
double s=(a+b+c)/2;
double x=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("The area of the triangle is "+x);
}
}
OUTPUT
12. Write a program to input the principal, rate and time and
print the simple interest.
// TO PRINT THE SIMPLE INTEREST
p,r,t
class interest
12670.0, 3.5, 2
{
double y=(p*r*t)/100;
OUTPUT
13. Write a program to print your name four times in the same line.
class name
OUTPUT
14. Write a program to input two angles of a triangle and find the
third angle.
//To print the third angle of a triangle
class angle
{ 35, 45
int C=180-(A+B);
OUTPUT
15. Write a program to input the radius of a circle and find its area
and circumference.
//To print the area and circumference of a circle
class circle
{ 12
double area=(22/7)*Math.pow(r,2);
double circum=2*(22/7)*r;
OUTPUT
16. Write a program to input three numbers and find their absolute
values.
//To find the absolute values of three numbers
class values
{ -12,46.9,
-23
int y=Math.abs(a);
double z=Math.abs(b);
int v=Math.abs(c);
OUTPUT
17. Write a program to input the side of a cube and find its volume
and surface area.
//To print the volume and surface area of a cube
class cube
{ s
double x=Math.pow(s,3); 20
double y=6*Math.pow(s,2);
OUTPUT
18. Write a program to input the length, breadth and height of a
cuboid and find its volume and surface area.
//To print the volume and surface area of a cuboid
class cuboid
{ l,b,h
double y=2*((l*b)+(b*h)+(h*l));
}}
OUTPUT
19. Write a program to evaluate the result of |-16| + |-45.67| - 8.
//To evaluate the result
class maths
int a=-16;
double b=-45.67;
int c=8;
double f=Math.abs(a)+Math.abs(b)-c;
OUTPUT
20. Write a program to input appropriate values and find
𝟑 𝒃
√𝒂 + + 𝒄 2.
𝒄
class maths_
{ 125,4,2
double f=Math.cbrt(a)+(b/c)+Math.pow(c,2);
OUTPUT