You are on page 1of 13

JAVA ASSIGNMENT 1 1.

Write a program to find the difference between sum of the squares and the square of the sums of n numbers? Program: import java.io.*; import java.util.*; class DiffSumSqu { public static void main(String args[]) throws IOException { Scanner s=new Scanner(System.in); System.out.println("Enter the value of n: "); int n=s.nextInt(); int SumSqua=((n^3)/3)+((n^2)/2)+(n/6); int sum = 0; for(int i = n;i>=0;i--) { sum = sum + i; } int SquaSum = sum * sum; diff = SumSqua-SquaSum; System.out.println("Difference between sum of the squares and the square of the sum of given "+n); System.out.print(" numbers is : "+diff); System.out.println(""); }

out. System. Program: import java.in).out.} Output: Enter the value of n: 5 Difference between sum of the squares and the square of the sum of given 5 numbers is : 3328 2) Develop a program that accepts the area of a square and will calculate its perimeter. class AreaPeri { public static void main(String args[]) throws IOException { Scanner s=new Scanner(System.sqrt(Area).*. } } . System. System.out.nextDouble().util.io.println("Area of Square : ").print("so the Perimeter of the Square : "+p). double Area=s.*.println(""). import java. double Perimeter=4*Math.

println(""). System.println("Enter the radius : "). } .util.nextDouble().out.out.nextDouble().println("Volume of the cylinder is : " + vol). double vol=Math. class CalculateCylinderVolume { public static void main(String args[]) throws IOException { Scanner s=new Scanner(System.*. System. import java. System. System. which accepts radius of a cylinder's base disk and its height and computes the volume of the cylinder.out.println("Enter the height : ").in).out.io.PI*rad*rad*ht.. double rad=s.*. double ht=s.Output: Area of Square : 16 so the Perimeter of the Square : 16. Program: import java.0 3)Develop the program calculateCylinderVolume.

out. System. of working hours in a day : ").*. Define the program calculateTax which determines the tax on the gross pay.println("Enter the no. class Utopias { public static void main(String args[]) throws IOException { Scanner s=new Scanner(System. System.69911184307752 4)Utopias tax accountants always use programs that compute income taxes even though the tax rate is a solid.out. of hours worked in over time : ").out. System. import java.util. Program: import java.*.} Output : Enter the radius : 2 Enter the height : 3 Volume of the cylinder is : 37. Assume an hourly rate of $12.io.nextInt(). int days=s.println("Days worked by employer in a year : "). .nextInt(). int hours=s. never-changing 15%.println("Enter the no. Define calculateNetPay that determines the net pay of an employee from the number of hours worked.in).

println("").15.println("Enter the no. of hours worked in over time : 50 Enter the no.out. of hours took leave : 10 Gross Pay (in $) : 28800. of working hours in a day : 8 Enter the no. int l=s.0 Net Pay (in $) : 29280.int ot=s.nextInt().println("Tax (in $) : "+tax).out. double gross=((days*hours)+ot-l)*12. System.out. System. double net=gross-tax.0 . System.println("Gross Pay (in $) : "+gross). double tax= gross*0.println("Net Pay (in $) : "+net). System. System.out.out. of hours took leave : ").nextInt(). } } Output : Days worked by employer in a year : 300 Enter the no.

*. Each customer pays $5 per ticket.5)).nextInt(). double profit = (n*5)-(20+(n*0.Tax (in $) : 4320.0 . System. of attendees of a show : 100 Total Profit of the theater per show (in $) is : 430. System.io. import java. of attendees of a show : ").out.util. int n=s. Develop the program calculateTotalProfit that consumes the number of attendees (of a show) and calculates how much income the show earns Program: import java. class calculateTotalProfit { public static void main (String args[]) throws IOException { Scanner s = new Scanner(System.*.in).out.0 5)An old-style movie theater has a simple profit program.50 per attendee. Every performance costs the theater $20.println("Enter the no.out.println(""). plus $. } } Output : Enter the no. System.println("Total Profit of the theater per show (in $) is : " + profit).

System.*.println("Surface Area of the cylinder is : " + area). System. } } Formula : 2*pi*radius*radius+2*pi*radius+height = 2*pi*radius*(radius+height) Output : Enter the base radius : 2 .6)Develop the program calculateCylinderArea.*. double ht=s.out.out.nextDouble(). System.PI*rad*(rad+ht). class calculateCylinderArea { public static void main(String args[]) throws IOException { Scanner s=new Scanner(System.println("Enter the height : ").println("Enter the base radius : ").util.in).io.nextDouble(). Program: import java.out.out. double area=2*Math. double rad=s. which accepts radius of the cylinder's base disk and its height and computes surface area of the cylinder. import java.println(""). System.

double radius=s. .out. double area=2*Math. System.io.util.nextDouble().Enter the height : 3 Surface Area of the cylinder is : 62.in).out.println("Enter the length : "). System. System. Program: import java. double length=s. and the thickness of its wall. It computes the surface area of a pipe. class calculatePipeArea { public static void main(String args[]) throws IOException { Scanner s=new Scanner(System. The program accpets three values: the pipes inner radius. double thick=s.println("Enter the thickness : ").83185307179586 7)Develop the program calculatePipeArea. System.nextDouble(). import java.println("").out. System.out.*.println("Surface Area of the pipe is : " + area).PI*(radius+thick)*lenght.out.nextDouble().*. which is an open cylinder.println("Enter the inner radius : "). its length.

If the rocket accelerates at a constant rate g. Program: import java.*. System.util.in).out.nextDouble().} } Output : Enter the inner radius : 3 Enter the length : 5 Enter the thickness : 2 Surface Area of the pipe is : 157. class calculateHeight { public static void main(String args[]) throws IOException { Scanner s=new Scanner(System. .println("Enter the time (in seconds) : ").*.io.07963267948966 8)Develop the program calculateHeight. double t=s. import java. it reaches a speed of g • t in t time units and a height of 1/2 * v * t where v is the speed at t. which computes the height that a rocket reaches in a given amount of time.

00000000001 9)Develop a program that computes the distance a boat travels across a river.*. Speed is distance/time.5*v*t.*. } } Output : Enter the time (in seconds) : 100 Height reached (in meters) is : 49000.in). System. System. and the river's speed.println("").out. the boat's speed perpendicular to the river. import java. given the width of the river.nextDouble().println("Enter the width of the river (in meters) : ").8*t.util. double height=0.out. .out. and the Pythagorean Theorem is c2 = a2 + b2. System.double v=9. Program: import java.io.println("Height reached (in meters) is : " + height). double rw=s. class BoatDistance { public static void main(String args[]) throws IOException { Scanner s= new Scanner(System.

Total interest is the product of the principal.nextDouble().System. Program: .0 10)Develop a program that accepts an initial amount of money (called the principal).println("Enter the river's speed (in meter/sec) : "). System. System.out. //time takes to travel from shore to shore straight by the boat double w2=time*rs. a simple annual interest rate. Assume that no additional deposits or withdrawals are made and that a month is 1/12 of a year. double bs=s. } } Output : Enter the width of the river (in meters) : 80 Enter the river's speed (in meter/sec) : 3 Enter the boat's speed (in meter/sec) : 4 The distance travelled by boat (in meters) is : 100. double time=rw/bs.println("").out. System.nextDouble().out.println("Enter the boat's speed (in meter/sec) : "). and a number of months will compute the balance at the end of that time. and the number of years. double rs=s.println("The distance travelled by boat (in meters) is : "+bd).sqrt((rw*rw)+(w2*w2)).out. the annual interest rate expressed as a decimal. //distance due to down stream double bd=Math.

double r=s.in). double si=(p*(m/12)*r)/100.*.println("Enter the principal amount : "). System. System. } } Output : Enter the principal amount : 10000 . class calculateBalance { public static void main(String args[]) throws IOException { Scanner s=new Scanner(System.out.out.println("Enter the no. double p=s. import java. double bal=p+si.println(" month(s) is : "+bal).println("Enter the annual interest rate : "). System.nextDouble().*.import java. of months : "). double m=s. System.out.util.println(""). System. System.io.nextDouble().nextDouble().out.print("Balance after " +(int)m).out.out.

0 .5 Enter the no.Enter the annual interest rate : 6. of months : 6 Balance after 6 month(s) is : 10325.