Anda di halaman 1dari 7

1.

Write a program to find the difference between sum of the squares and the squ are of the sums of n numbers ? Program: import java.io.*; import java.util.Scanner; class Difference { public static void main(String args[]) throwsIOException { Scanner s=new Scanner(System.in); System.out.println("Enter the value of n: "); int n=s.nextInt(); int a[]=new int[n]; int i, sqr, diff, sum=0, add=0; System.out.print("The "+n); System.out.println(" numbers are : "); for(i=0;i<n;i++) { a[i]=s.nextInt(); sum+=(a[i]*a[i]); add+=a[i]; } sqr=add*add; diff=sqr-sum; System.out.println(""); System.out.print("Sum of Squares of given "+n); System.out.println(" numbers is : "+sum); System.out.print("Squares of Sum of given "+n); System.out.println(" numbers is : "+sqr); System.out.println(""); 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 put: Enter the value of n: 4 The 4 numbers are : 2 3 4 5 Sum of Squares of given 4 numbers is : 54 Squares of Sum of given 4 numbers is : 196 Difference between sum of the squares and the square of the sum of given 4 numbers is : 142 ----------------------------------------------------------------------------------------------------2. Develop a program that accepts the area of a square and will calculate its pe rimeter. Program:

import java.util.Scanner; public class CalculateSquarePeri { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("Enter the Area: "); double a=s.nextDouble(); double p=4*Math.sqrt(a); System.out.println(" "); System.out.print("Perimeter of the Square : "+p); System.out.println(" "); } } Output: Enter the Area: 23 Perimeter of the square is: 19.183326093250876 ------------------------------------------------------------------------------------------------------3. Develop the program calculate CylinderVolume., which accepts radius of a cyli nder's base disk and its height and computes the volume of the cylinder. Program: import java.io.*; import java.util.Scanner; class calculateCylinderVolume { public static void main(String args[]) throwsIOException { Scanner s=new Scanner(System.in); System.out.println("Enter the radius : "); double rad=s.nextDouble(); System.out.println("Enter the height : "); double ht=s.nextDouble(); double vol=Math.PI*rad*rad*ht; System.out.println(""); System.out.println("Volume of the cylinder is : " + vol); } } Output: Enter the radius : 12 Enter the height : 13 Volume of the cylinder is : 5881.061447520093 ------------------------------------------------------------------------------------------------------4. Utopias tax accountants always use programs that compute income taxes even th ough the tax rate is a solid, never-changing 15%.

Define the program calculateTax which determines the tax on the gross pay. Define calculateNetPay that determines the net pay of an employee from the nu mber of hours worked. Assume an hourly rate of $12. Program: import java.util.Scanner; public class calculateTax { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("Enter the no. of working days in the year : "); int d=s.nextInt(); System.out.println("Enter the no. of working hours in a day : "); int h=s.nextInt(); System.out.println("Enter the no. of hours worked in over time : "); int ot=s.nextInt(); System.out.println("Enter the no. of hours took leave : "); int l=s.nextInt(); double gross=((d*h)+ot-l)*12; double tax= gross*0.15; double net=gross-tax; System.out.println(""); System.out.println("Gross Pay (in $) : "+gross); System.out.println("Tax (in $) : "+tax); System.out.println("Net Pay (in $) : "+net); } } Output: Days worked by employer in a year : 300 Enter the no. of working hours in a day : 6 Enter the no. of hours worked in over time : 1 Enter the no. of hours took leave : 1560 Gross Pay (in $) : 2892.0 Tax (in $) : 433.8 Net Pay (in $) : 2458.2

-----------------------------------------------------------------------------------------------------5. An old-style movie theater has a simple profit program. Each customer pays $5 per ticket. Every performance costs the theater $20, plus $.50 per attendee. Develop the program calculate TotalProfit that consumes the number of attende es (of a show) and calculates how much income the show earns.

Program: import java.util.Scanner; public class calculateTotalProfit { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Enter the no. of attendees of a show : "); int n=s.nextInt(); double profit = (n*5)-(20+(n*0.5)); System.out.println(""); System.out.println("Total Profit of the theater per show (in $) is : " + profit) ; } } Output: Enter the no. of attendees per show : 50 Total Profit of the theater per show (in $) is : 205.0 ------------------------------------------------------------------------------------------------------6. Develop the program calculate CylinderArea, which accepts radius of the cylin der's base disk and its height and computes surface area of the cylinder. Program: import java.util.Scanner; public class calculateCylinderArea { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("Enter the base radius : "); double rad=s.nextDouble(); System.out.println("Enter the height : "); double ht=s.nextDouble(); double area=2*Math.PI*rad*(rad+ht); System.out.println(""); System.out.println("Surface Area of the cylinder is : " + area); } } Output: Enter the base radius : 12 Enter the height : 13 Surface Area of the cylinder is : 1884.9555921538758 ------------------------------------------------------------------------------------------------------7. Develop the program calculatePipeArea. It computes the surface area of a pipe , which is an open cylinder. The program accpets three values: the pipes inner radius, its leng th, and the thickness of its

wall. Program: import java.util.Scanner; public class calculatePipeArea { publicstaticvoid main(String[] args) { Scanner s=newScanner(System.in); System.out.println("Enter the inner radius : "); double rad=s.nextDouble(); System.out.println("Enter the length : "); double len=s.nextDouble(); System.out.println("Enter the thickness : "); double thick=s.nextDouble(); double area=2*Math.PI*(rad+thick)*len; System.out.println(""); System.out.println("Surface Area of the pipe is : " + area); } } Output: Enter the inner radius : 13 Enter the length : 20 Enter the thickness : 5 Surface Area of the pipe is : 2261.946710584651

-----------------------------------------------------------------------------------------------8. Develop the program calculateHeight, which computes the height that a rocket reaches in a given amount of time.If the rocket accelerates at a constant rate g, it reaches a speed o f g t in t time units and a height of 1/2 * v * t where v is the speed at t. program: import java.util.Scanner; public class calculateHeight { public static void main(String[] args) { Scanner s=newScanner(System.in); System.out.println("Enter the time (in seconds) : "); double t=s.nextDouble(); double v=9.8*t; double height=0.5*v*t; System.out.println(""); System.out.println("Height reached (in meters) is : " + height); } } Output: Enter the time (in seconds) : 300

Height reached (in meters) is : 441000.0 ----------------------------------------------------------------------------------------------9. Develop a program that computes the distance a boat travels across a river, g iven the width of the river, the boat's speed perpendicular to the river, and the river's speed. Speed is distance/time, and the Pythagorean Theorem is c2 = a2 + b2. Program: import java.util.Scanner; public class BoatDistance{ public static void main(String[] args) { Scanner s= new Scanner(System.in); System.out.println("Enter the width of the river (in meters) : "); double rw=s.nextDouble(); System.out.println("Enter the river's speed (in meter/sec) : "); double rs=s.nextDouble(); System.out.println("Enter the boat's speed (in meter/sec) : "); double bs=s.nextDouble(); double time=rw/bs; //time takes to travel from shore to shore straight by the bo at double w2=time*rs; //distance due to down stream double bd=Math.sqrt((rw*rw)+(w2*w2)); System.out.println(""); System.out.println("The distance travelled by boat (in meters) is : "+bd); } } Output: Enter the width of the river (in meters) : 15 Enter the river's speed (in meter/sec) : 200 Enter the boat's speed (in meter/sec) : 250 The distance travelled by boat (in meters) is : 19.209372712298546 -------------------------------------------------------------------------------------------------10. Develop a program that accepts an initial amount of money (called the princi pal), a simple annual interest rate, and a number of months will compute the balance at the end of that time. Assume that no additional deposits or withdrawals are made and that a month is 1/ 12 of a year. Total interest is the product of the principal, the annual interest rate express ed as a decimal, and the number of years. Program: import java.util.Scanner; public class calculateBalance { public static void main(String[] args) {

Scanner s=new Scanner(System.in); System.out.println("Enter the principal amount : "); double p=s.nextDouble(); System.out.println("Enter the annual interest rate : "); double r=s.nextDouble(); System.out.println("Enter the no. of years in months : "); double m=s.nextDouble(); double si=(p*(m/12)*r)/100; double bal=p+si; System.out.println(""); System.out.print("Balance after " +(int)m); System.out.println(" month(s) is : "+bal); } } Output: Enter the principal amount : 15000 Enter the annual interest rate : 12 Enter the no. of months : 24 Balance after 24 month(s) is : 18600.0

Anda mungkin juga menyukai