You are on page 1of 44

PROGRAM -1

Write a program to find the side of a cube


//A program to find the side of a cube//
import java.util.*;

class Side

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("enter volume of a cube");

int v=sc.nextInt();

double a =Math.cbrt(v);

System.out.println("Volume of a cube ="+v);

System.out.println("Side of a cube ="+a);

Variable description

Variable Data type Description


v int To enter volume
a double To enter side

Output:
enter volume of a cube
25

Volume of a cube =25

Side of a cube =2.924017738212866


Program-2

Write a program to print “Hello Java!!!”

import java.util.*;

class HelloJava

public static void main(String args[])

System.out.println("Hello Java!!!");

Variable description

Variable Data type Description


- - -

Output:

Hello Java!!!
Program-3

Write a program to print the details of a class biodata

import java .util.*;

class Biodata

public static void main(String args[])

System.out.println("Name:");

System.out.println("Father's name:");

System.out.println("Date of birth:");

System.out.println("Blood group:");

System.out.println("Aadhar No.:");

System.out.println("State:");

Output:
Name:

Father's name:

Date of birth:

Blood group:
Aadhar No.:

State:

Variable description

Variable Data type Description


- - -
Program-4

Write a program to display months in a year

//To display months in a year//

import java.util.*;

class months

public static void main (String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number:");

int m=sc.nextInt();

switch(m)

case 1:

System.out.println("January");

break;

case 2:

System.out.println("February");

break;
case 3:

System.out.println("March");

break;

case 4:

System.out.println("April");

break;

case 5:

System.out.println("May");

break;

case 6:

System.out.println("June");

break;

case 7:

System.out.println("July");

break;

case 8:

System.out.println("August");

break;

case 9:

System.out.println("September");
break;

case 10:

System.out.println("October");

break;

case 11:

System.out.println("November");

break;

case 12:

System.out.println("December");

break;

default:

System.out.println("Invalid");

break;

}
Output:

Enter a number:

January

February

March

April

May

June

July

August

September
10

October

11

November

12

December

Variable description

Variable Data type Description


m int To receive number
Program-5

Write a program to calculate the potential energy

// to find potential energy

import java.util.*;

class Energy

public static void main(String args[])

Scanner sc=new Scanner(System.in);

double m,h,v;

double g=9.81;

System.out.println("Enter mass:");

m=sc.nextDouble();

System.out.println("Enter velocity:");

v=sc.nextDouble();

System.out.println("Enter height:");

h=sc.nextDouble();

double pe=m*g*h;

double ke=1.0/2.0*m*Math.pow(v,2);

double me=pe+ke;
System.out.println("Potential energy : "+pe);

System.out.println("Kinetic energy : "+ke);

System.out.println("Mechanical energy : "+me);

Output:

Enter mass:

28

Enter velocity:

Enter height:

37

Potential energy : 10163.16

Kinetic energy : 350.0

Mechanical energy : 10513.16


Variable description

Variable Data type Description


m Double To store mass
h Double To store height
v Double To store velocity
pe Double To store the values of
m*h*v
ke Double To find
1.0/2.0*m*Math.pow(v,2);
me Double To add pe+ke
Program-6

Write a program to display the following pattern:

*****

****

***

//To display the pattern

import java.util.*;

class pattern

public static void main (String args[])

System.out.println("*****");

System.out.println("****");

System.out.println("***");

}
Output:
*****

****

Variable description

Variable Data type Description


- - -
Program-7

Write a program to display the given format:

*******************

TamilNadu Electricity Board

___________________

Consumer no: Consumer name:

Units consumed: Charge/unit:

Amount to be paid

*******************

*******************

TamilNadu Electricity Board

___________________

Consumer no: Consumer name:

Units consumed: Charge/unit:

Amount to be paid

*******************
//to display the given format

import java.util.*;

class Program_1

public static void main(String args[])

System.out.println("*******************");

System.out.println("TamilNadu Electricity Board");

System.out.println("___________________");

System.out.println("Consumer no: \t Consumer name:");

System.out.println("Units consumed: \t Charge/unit:");

System.out.println("Amount to be paid");

System.out.println("*******************");

}
Output:

*******************

TamilNadu Electricity Board

___________________

Consumer no: Consumer name:

Units consumed: Charge/unit:

Amount to be paid

*******************

*******************

TamilNadu Electricity Board

___________________

Consumer no: Consumer name:

Units consumed: Charge/unit:

Amount to be paid

*******************

Variable description

Variable Data type Description


- - -
Program-8

Write a program to calculate the log,abs,sqrt,cbrt,random number

//To calculate the logarthim,Absolute value,Square root,Cube


root,Random number

import java.util.Scanner;

class calculation

public static void main(String args[])

Scanner in = new Scanner(System.in);

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

double n = in.nextDouble();

System.out.println("Logarithm = " + Math.log(n));

System.out.println("Absolute value = " + Math.abs(n));

System.out.println("Square root = " + Math.sqrt(n));

System.out.println("Cube root= " + Math.cbrt(n));

System.out.println("Random number = " + Math.random());

Output:
Enter Number: 5

Logarithm = 1.6094379124341003

Absolute value = 5.0

Square root = 2.23606797749979

Cube root= 1.709975946676697

Random number = 0.773766324020185

Variable description

Variable Datatype Description


n Double To store a number
Program-9

Write a program to calculate the square root

//To find square root

import java.util.Scanner;

class FindSquareroot

public static void main(String[] args)

int min=10,max=20;

int range = max-min+1;

int num1=(int)(range*Math.random() + min);

int num2=(int)(range*Math.random() + min);

System.out.println("First number is " + num1);

System.out.println("Second number is " + num2);

}
Output

First number is 11

Second number is 19

Variable description

Variable Data type Description


min int To generate lowest
value in the column
max int To generate highest
lowest value in the
column
range int TO subtract max and
min and add 1
Program – 10

Write a program to find the velocity

//To find velocity

import java.util.*;

class Velocity

public static void main(String[] args)

Scanner in = new Scanner(System.in);

System.out.print("Enter initial velocity: ");

double u = in.nextInt();

System.out.print("Enter acceleration: ");

double a = in.nextInt();

System.out.print("Enter distance: ");

double s = in.nextInt();

double v=0;

v=Math.pow(u,2)+2*a*s;

System.out.println("Final Velocity "+Math.pow(v,2));

}
Output:

Enter initial velocity: 28

Enter acceleration: 14

Enter distance: 30

Final Velocity 2637376.0

Variable description

Variable Datatype Description


u double To store initial velocity
a double To store acceleration
s double To store distance
v double To calculate velocity
Program – 11

Write a program to calculate slope

// to calculate the slope of a line

import java.util.*;

class slope

public static void main (String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter x1 and y1, x2 and y2:");

int x1= sc.nextInt();

int y1= sc.nextInt();

int x2= sc.nextInt();

int y2= sc.nextInt();

double m =(y2-y1)/(x2-x1);

System.out.println("Slope=" +m);

}
}

Output:

Enter x1 and y1, x2 and y2:

15

11

27

28

Slope=1.0

Variable description

Variable Datatype Description


x1 int To receive x1
y1 int To receive y1
x2 int To receive x2
y2 int To receive y2
Program 12

A dealer allows his customer two successive discounts of 20% and


10%. If the article costs Rs.7,200, calculate and display the selling price
and the total discount given by the dealer.

//Program to calculate discounts

class discount

public static void main(String args[])

int cp=7200;

double d1,d2,dis,amt;

d1= cp*20.0/100.0;

d2= (cp-d1)*10.0/100.0;

dis= d1+d2;

amt= cp-dis;

System.out.println("Total discount ="+dis);

System.out.println("Amount to pay ="+amt);

}
Output:

Total discount =2016.0

Amount to pay =5184.0

Variable description:

Variable Datatype Description


cp int To store cost price
b1 double To store cp*20.0/100.0
b2 double To store cp-d1*10.0/100.0
dis double To store d1+d2
amt double To store dp-dis
Program-13

Write a program to calculate the following expression

// A program to find the value of an expression

import java.util.*;

class Expression

public static void main (String args[])

Scanner sc= new Scanner (System.in);

int a,b,c;

double d;

System.out.println("Enter values of a,b,c:");

a= sc.nextInt();

b= sc.nextInt();

c= sc.nextInt();

d= 1/Math.pow(a,2)+ 1/Math.pow(b,3)+ 1/Math.pow(c,4);

System.out.println("The value of the given expression is:" +d)

}
Output:

Enter values of a,b,c:

11

15

15

The value of the given expression is:0.008580512192633404

Variable description:

Variable Datatype Description


a int To store first value
b Int To store second value
c int To store third value
d Double To store the final value
Program- 14

Write a program to swap numbers

// a program to display the numbers after swapping

import java.util.*;

class Swap

public static void main (String args[])

Scanner sc= new Scanner (System.in);

int a,b,c = 0;

System.out.println("Enter your number:");

a= sc.nextInt();

b= sc.nextInt();

c=a;

a=b;

b=c;

System.out.println("The value of a =" +a);

System.out.println("The value of b =" +b);

}
Output:

Enter your number:

11

15

The value of a =15

The value of b =11

Variable description:

variable Datatype Description


a int To store a number
b Int To store a number
c int To store final value
Program- 15

Write a program to find the second smallest number

// a program to display the second smallest number

import java.util.*;

class Second_smallest

public static void main (String args[])

Scanner sc= new Scanner(System.in);

int a,b,c;

System.out.println ("Enter three numbers");

a= sc.nextInt();

b= sc.nextInt();

c= sc.nextInt();

if (a<b)

if (b<c)

System.out.println("The second smallest number:" +b);

else
System.out.println("The second smallest number:" +c);

if (b<c)

if (c<a)

System.out.println("The second smallest number:" +c);

else

System.out.println("The second smallest number:" +a);

if (c<a)

if (a<b)

System.out.println("The second smallest number:" +a);

else

System.out.println ("The second smallest number:" +b);

}
Output:

Enter three numbers

27

11

15

The second smallest number:15

Variable description

You might also like