Вы находитесь на странице: 1из 4

1.

Write a program to find the difference between sum of the squares and the squ
are of the sums of n numbers?
import java.util.*;
class FindDifference
{
public static void main(String[] args)
{
int sum=0,sum1=0,sum2=0;
Scanner input=new Scanner(System.in);
System.out.println("Enter value of n: ");
int n=input.nextInt();
for(int i=1;i<=n;i++){
int sq=i*i;
sum1+=sq;
}
//System.out.println(sum1);
for(int i=1;i<=n;i++){
sum+=i;
}
sum2=sum*sum;
//System.out.println(sum2);
int diff=0;
if(sum1>sum2){
diff=sum1-sum2;
}
else{
diff=sum2-sum1;
}
System.out.println(diff);
}

2. Develop a program that accepts the area of a square and will calculate its pe
rimeter.
import java.util.*;
import java.text.*;
class PerimeterOfSquare{
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat("##.00");
Scanner input=new Scanner(System.in);
System.out.println("Enter Area of Square:");
double area=input.nextDouble();
double side=Math.sqrt(area);
System.out.println("Side of Square is: "+df.format(side));
double perimeter=4*side;
System.out.println("Perimeter of Square is: "+df.format(perimeter));
}
}
3. Develop the program calculate Cylinder Volume., which accepts radius of a cyl
inder's base disk and its height and computes the volume of the cylinder.
import java.util.*;
class CalculateCyliderVolume
{
public static void main(String[] args)
{
double PI=3.14;
Scanner input=new Scanner(System.in);
System.out.print("Enter Radius: ");
double r=input.nextDouble();
System.out.print("Enter Height: ");
double h=input.nextDouble();
double volume=PI*r*r*h;
System.out.println("Volume of Cylinder: "+volume);
}
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 calculateTa
x which determines the tax on the gross pay. Define calculateNetPay that determi
nes the net pay of an employee from the number of hours worked. Assume an hourly
rate of $12.
import java.util.*;
import java.text.*;
public class CalculateNetPay
{
public static void main(String[]args){
double taxRate=0.15;
double hourlyRate=12;
DecimalFormat df=new DecimalFormat("$##.00");
Scanner input=new Scanner(System.in);
System.out.print("Enter Number of Hours Worked: ");
double hrs=input.nextDouble();
double gp=hrs*hourlyRate;
double tax=gp*taxRate;
double netpay=gp-tax;
System.out.println("Net Pay is: "+df.format(netpay));
}
}
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. De
velop the program calculateTotalProfit that consumes the number of attendees (of
a show) and calculates how much income the show earns.
import java.util.*;
import java.text.*;
class CalculateTotalProfit{
public static void main(String[] args)
{
DecimalFormat df=new DecimalFormat("$##.00");
double theaterCost=20;
Scanner input=new Scanner(System.in);
System.out.println("Enter Number of Customers: ");
double cus=input.nextDouble();
double earnFromTickets=5*cus;
double attendeesCost=cus*0.50;
double cost=attendeesCost+theaterCost;
double profit=earnFromTickets-cost;
System.out.println("Total Profit: "+df.format(profit));
}
}
6. Develop the program calculateCylinderArea, which accepts radius of the cylind
er's base disk and its height and computes surface area of the cylinder.

import java.util.*;
import java.text.*;
class CalculateCylinderArea
{
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat("##.00");
double pi=3.14;
Scanner input=new Scanner(System.in);
System.out.print("Enter Radius: ");
double r=input.nextDouble();
System.out.print("Enter Height: ");
double h=input.nextDouble();
double v1=2*pi*r*r;
double v2=2*pi*r*h;
double surfaceArea=v1+v2;
System.out.println("Surface Area of Cylinder: "+df.format(surfaceArea));
}
}
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 r
adius, its length, and the thickness of its wall.
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.
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 th
e river's speed. Speed is distance/time, and the Pythagorean Theorem is c2 = a2
+ b2.
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 bal
ance 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 th
e principal, the annual interest rate expressed as a decimal, and the number of
years.?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class TotalInterest extends JFrame
{
TotalInterest(){
JLabel lab1=new JLabel("Principal");
final JTextField text1=new JTextField(20);
JLabel lab2=new JLabel("Rate of Interest");
final JTextField text2= new JTextField(20);
JLabel lab3=new JLabel("Number of Months");
final JTextField text3=new JTextField(20);
JButton b=new JButton("Get");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
double p=Double.parseDouble(text1.getText());
double r=Double.parseDouble(text2.getText())/100;
double t=Double.parseDouble(text3.getText())/12;
double interest=p*r*t;
JOptionPane.showMessageDialog(null, "Total Interest is: "+interest);
}
});
setLayout(null);
lab1.setBounds(10,10,100,20);
text1.setBounds(150,10,200,20);
lab2.setBounds(10,40,100,20);
text2.setBounds(150,40,200,20);
lab3.setBounds(10,70,120,20);
text3.setBounds(150,70,200,20);
b.setBounds(150,110,70,20);
add(lab1);
add(text1);
add(lab2);
add(text2);
add(lab3);
add(text3);
add(b);
setVisible(true);
setSize(400,200);
}
public static void main(String[] args)
{
new TotalInterest();
}
}

Вам также может понравиться