You are on page 1of 5

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? import java.util.*; class Difference { public static void main(String args[]) { int n,i,res=0,sum=0,dif; System.out.println("Enter the number"); Scanner in= new Scanner(System.in); n=in.nextInt(); for(i=1;i<=n;i++) res=res+i; res=res*res; for(i=1;i<=n;i++) sum=sum+i*i; if(sum>res) dif=sum-res; else dif=res-sum; System.out.println("Result:"+dif); } } 2. Develop a program that accepts the area of a square and will calculate its perimeter. import java.util.*; class Square { public static void main(String args[]) { double a,p,s; System.out.println("Enter the area of the square"); Scanner in=new Scanner(System.in); a=in.nextInt(); s=Math.sqrt(a); p=4*s; System.out.println("The Area of the square is "+a+" and its Perimeter is "+p); } }

3. Develop the program calculate CylinderVolume., which accepts radius of a cylinder's base disk and its height and computes the volume of the cylinder. import java.util.*; class CylinderVolume { public static void main(String args[]) { double vol,r,h; Scanner in=new Scanner(System.in); System.out.println("Enter the data needed to calculate the volume of the cylender"); System.out.println("Enter the base radius of the cylinder"); r=in.nextDouble(); System.out.println("Enter the height of the cylinder"); h=in.nextDouble(); vol=Math.PI*r*r*h; System.out.println("Volume of cylinder is:"+vol); } } 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 calculate Tax 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. import java.io.*; class calculateNetPay { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter total no of hours"); int h=Integer.parseInt(br.readLine()); System.out.println("NetPay: "+(12*h-0.15*(12*h))); } }

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. import java.io.*; class calculateTotalProfit { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter total no of attendees"); int n=Integer.parseInt(br.readLine()); System.out.println("Total Income: "+((5*n)-(20+(0.5*n)))); } } 6. Develop the program calculateCylinderArea, which accepts radius of the cylinder's base disk and its height and computes surface area of the cylinder. import java.util.*; class calculateCylinderArea { public static void main(String args[]) { double area,r,h; Scanner in=new Scanner(System.in); System.out.println("Enter the required data to calculate the surface area of the Cylinder"); System.out.println("Enter the base radius of Cylinder"); r=in.nextDouble(); System.out.println("Enter the hieght of Cylinder"); h=in.nextDouble(); area=(2*Math.PI*r*r)+(2*Math.PI*h); System.out.println("The surface area of the cylinder is:"+area); } }

7. Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accepts three values: the pipes inner radius, its length, and the thickness of its wall.

import java.util.*; class calculatePipeArea { public static void main(String args[]) { double area,r,l,t; Scanner in=new Scanner(System.in); System.out.println("Enter the required data to calculate the surface area of the Pipe"); System.out.println("Enter the base radius of Pipe"); r=in.nextDouble(); System.out.println("Enter the length of Pipe"); l=in.nextDouble(); System.out.println("Enter the base thickness of Pipe"); t=in.nextDouble(); area=2*Math.PI*(r-t)*l; System.out.println("The surface area of the Pipe is:"+area); } } 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. import java.util.*; class calculateHeight { public static void main(String args[]) { double h,t; System.out.println("Enter the details to find the height of the rocket at the given time"); System.out.println("Enter the time interval at which the height reached"); Scanner in=new Scanner(System.in); t=in.nextDouble(); h=0.5*9.8*t*t; System.out.println("Height reached is:"+h); } } 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.

import java.io.*; class distance { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter boats speed"); double bs=Double.parseDouble(br.readLine()); System.out.println("Enter river speed"); double rs=Double.parseDouble(br.readLine()); System.out.println("Enter width of river"); double w=Double.parseDouble(br.readLine()); double x=rs/bs;x=1+x*x;x=1/x;x=Math.sqrt(x); System.out.println("Distance: "+(w/x)); } } 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. Balance= P*T*R/100 where T=M/12 import java.util.*; class interest { public static void main(String args[]) { double p,r,t,i,tot; System.out.println("Enter the details to calculate the interest for the given principal amount"); System.out.println("Enter the principal amount"); Scanner in=new Scanner(System.in); p=in.nextDouble(); System.out.println("Enter the period in months"); t=in.nextDouble(); System.out.println("Enter the rate of interest"); r=in.nextDouble(); i=p*t*r/(100*12); tot=i+p; System.out.println("The total interest is:"+i); System.out.println("Total amount is:"+tot); } }

You might also like