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

JAVA Assignment solutions

1)
import java.io.*;
import java.util.Scanner;
class SquarenSum
{
         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 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("");
           }
}

Output:
Enter the value of n:
5
The 5 numbers are :
3 7 11 21 26

Sum of Squares of given 5 numbers is : 1296


Squares of Sum of given 5 numbers is : 4624
Difference between sum of the squares and the square of the sum of
given 5 numbers is : 3328

2)

import java.io.*;
import java.util.Scanner;
class PeriSquare
{
         public static void main(String args[]) throws IOException
         {
                   Scanner s=new Scanner(System.in);
                   System.out.println("Area of Square : ");
                   double a=s.nextDouble();
                   double p=4*Math.sqrt(a);
                   System.out.println("");
                   System.out.print("Perimeter of the Square : "+p);
                   System.out.println("");
         }
}

Formula : Perimeter = 4*sqrt(Area)


Output:
Area of Square : 16
Perimeter of the Square : 16.0
3)

import java.io.*;

import java.util.Scanner;

class calculateCylinderVolume

         public static void main(String args[]) throws IOException

         {

                   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);

         }

Formula : Volume of Cylinder = pi*radius*radius*height


Output :

  Enter the radius :

Enter the height :

Volume of the cylinder is : 37.69911184307752

4)

import java.io.*;
import java.util.Scanner;
class calculateTax{
        public static void main(String args[]) throws IOException
    {
                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*12;
                 double net=((d*h)+ot-l)*12;
                 double tax= gross*0.15;
                 System.out.println("");
                 System.out.println("Gross Pay (in $) : "+gross);
                 System.out.println("Net Pay (in $) : "+net);
                 System.out.println("Tax (in $) : "+tax);
       }
}

Output :

Enter the no. of working days in the year :

300

Enter the no. of working hours in a day :

Enter the no. of hours worked in over time :

50

Enter the no. of hours took leave :

10

Gross Pay (in $) : 28800.0

Net Pay (in $) : 29280.0

Tax (in $) : 4320.0

5)

import java.io.*;

import java.util.Scanner;

class calculateTotalProfit
{

        public static void main (String args[]) throws IOException

       {

                  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 of a show :

100

Total Profit of the theater per show (in $) is : 430.0

6)

import java.io.*;
import java.util.Scanner;

class calculateCylinderArea

         public static void main(String args[]) throws IOException

         {

                   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);

    }

Formula : 2*pi*radius*radius+2*pi*radius+height = 2*pi*radius*(radius+height)

Output :

Enter the base radius :

Enter the height :


3

Surface Area of the cylinder is : 62.83185307179586

7)

import java.io.*;

import java.util.Scanner;

class calculatePipeArea

        public static void main(String args[]) throws IOException

    {

                  Scanner s=new Scanner(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);

    }

Formula : 2*pi*(inner_radius+thickness)*length

Output :

Enter the inner radius :

Enter the length :

Enter the thickness :

Surface Area of the pipe is : 157.07963267948966

8)

import java.io.*;
import java.util.Scanner;

class calculateHeight

         public static void main(String args[]) throws IOException

         {

                   Scanner s=new Scanner(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) :

100

Height reached (in meters) is : 49000.00000000001


9)

import java.io.*;

import java.util.Scanner;

class BoatDistance

         public static void main(String args[]) throws IOException

         {

                  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 boat                   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) :

80

Enter the river's speed (in meter/sec) :

Enter the boat's speed (in meter/sec) :

The distance travelled by boat (in meters) is : 100.0

10)

import java.io.*;

import java.util.Scanner;

class calculateBalance

{
         public static void main(String args[]) throws IOException

         {

                   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 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 :

10000

Enter the annual interest rate :


6.5

Enter the no. of months :

Balance after 6 month(s) is : 10325.0

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