You are on page 1of 5

1. Write a program to find the difference between sum of the squares and the square of the sums of n numbers?

Source Code: import java.util.Scanner; import java.io.IOException; public class prog1 { ... public static void main(String[] args)throws IOException{ int sum1=0,sum2=0; System.out.print("enter the count of numbers for which u need to find the diff....."); Scanner kb=new Scanner(System.in); int s=kb.nextInt(); int[] x=new int[s]; System.out.print("enter the numbers "); try{for(int i=0;i<x.length;i++) { x[i]=kb.nextInt(); } }catch(Exception ex){ System.out.println("Enter only integer values"); System.exit(0); } for(int i=0;i<x.length;i++) { int p=x[i]*x[i]; sum1+=p; sum2+=x[i]; } int diff=sum1-(sum2*sum2); System.out.println("Difference is "+Math.abs(diff)); } } 2. Develop a program that accepts the area of a square and will calculate its perimeter. Source Code: import java.util.Scanner; public class prog2 { public static void main(String[] args){ Scanner kb=new Scanner(System.in); System.out.println("Enter the area of square "); double x=kb.nextDouble(); System.out.printf("Side of Square:%.2g units",Math.sqrt(x));

System.out.printf(" perimeter of square:%.2f units",4*(Math.sqrt(x))); } } 3. Develop the program calculateCylinderVolume., which accepts radius of a cylinder's base disk and its height and computes the volume of the cylinder. Source Code: import java.util.Scanner; public class prog3 { public static void main(String[] args) { Scanner kb=new Scanner(System.in); System.out.print("Enter the base radius of cylinder:"); double r=kb.nextDouble(); System.out.print("Enter the height of the cylinder:"); double h=kb.nextDouble(); System.out.printf("Volume of the cylinder:%.2f units",3.14*r*r*h); } } 4. Utopias tax accountants always use programs that compute income taxes even though 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 number of hours worked. Assume an hourly rate of $12.

Source Code: import java.util.Scanner; public class prog4 { public static void main(String[] args) { Scanner kb=new Scanner(System.in); double hourlyrate=12,taxrate=0.15; System.out.print("Enter no. of hours worked:"); double w=kb.nextDouble(); double gross=w*hourlyrate; double tax=gross*taxrate; System.out.printf("Netpay:$ %.2f",gross-tax); } } 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 calculateTotalProfit that consumes the number of attendees (of a show) and calculates how much income the show earns. Source Code: import java.util.Scanner; public class prog5 {

public static void main(String[] args) { Scanner kb=new Scanner(System.in); System.out.print("Enter no. of attendees of a show:"); int attendees=kb.nextInt(); double attendeescost=0.5*attendees; double attendeesincome=5*attendees; double profit=attendeesincome-attendeescost-20; System.out.printf("Profit for single show:$ %.2f",profit); } } 6. Develop the program calculateCylinderArea, which accepts radius of the cylinder's base disk and its height and computes surface area of the cylinder. Source Code: import java.util.Scanner; public class prog6 { public static void main(String[] args) { Scanner kb=new Scanner(System.in); System.out.print("Enter radius of the cylinder: "); double r=kb.nextDouble(); System.out.print("Enter height of the Cylinder: "); double h=kb.nextDouble(); System.out.printf("Surface area of cylinder:%.2f units",2*3.14*r*(r+h)); } } 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 length, and the thickness of its wall. Source Code: import java.util.Scanner; public class prog7 { public static void main(String[] args) { Scanner kb=new Scanner(System.in); System.out.print("Enter the radius of pipe: "); double r=kb.nextDouble(); System.out.println("Enter height of pipe: "); double h=kb.nextDouble(); System.out.println("enter thickness of pipe: "); double t=kb.nextDouble(); System.out.printf("Surface area of Open Pipe:%.2f units",2*3.14*(r+t)*h); } }

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 of g* t in t time units and a height of 1/2 * v * t where v is the speed at t. Source Code: import java.util.Scanner; public class prog8 { public static void main(String[] args) { double g=9.81; Scanner kb=new Scanner(System.in); System.out.print("Enter the time of rocket launch: "); Double t=kb.nextDouble(); System.out.printf("Speed of Rocket:%.3f units",g*t); System.out.printf(" Distance traversed by the Rocket:%.3f units",0.5*g*t*t); } } 9. Develop a program that computes the distance a boat travels across a river, given 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. Source Code: import java.util.Scanner; public class prog9 { public static void main(String[] args) { Scanner kb=new Scanner(System.in); System.out.print("enter Width of River : "); double w=kb.nextDouble(); System.out.println("Enter boat's speed perpendicular to the river :"); double bs=kb.nextDouble(); System.out.println("Enter River's Speed :"); double rs=kb.nextDouble(); double t=w/bs; double b=t*rs; double distance=Math.sqrt((w*w)+(b*b)); System.out.printf("Distance travelled by Boat:%.2f units",distance); } } 10. Develop a program that accepts an initial amount of money (called the principal), 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 expressed as a decimal, and the number of years. Source Code: import java.util.Scanner;

public class prog10 { public static void main(String[] args){ Scanner kb=new Scanner(System.in); System.out.println("Enter principal amount: "); double p=kb.nextDouble(); System.out.println("Enter rate of interest in %: "); double r=kb.nextDouble(); System.out.println("Enter the duration in months: "); double t=kb.nextDouble(); double interest=p*(t/12)*(r/100); System.out.printf("Total Interest:%.2f units",interest); } }

You might also like