Tuesday 22 November 2011

TCS ASPIRE JAVA ASSIGNMENT - ANSWERS


1.
Program:

import java.io.*;
importjava.util.Scanner;
class Difference
{
publicstaticvoid main(String args[]) throwsIOException
{
Scanner s=newScanner(System.in);
System.out.println("Enter the value of n: ");
int n=s.nextInt();
int a[]=newint[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:
3
The 3 numbers are :
2
3
4



Sum of Squares of given 4 numbers is : 29
Squares of Sum of given 4 numbers is : 81

Difference between sum of the squares and the square of the sum of given 4
numbers is : 52










2.
Program:

importjava.util.Scanner;


publicclassCalculateSquarePeri
{

/**
* @paramargs
*/
publicstaticvoid main(String[] args)
{

Scanner s=newScanner(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("");
}


}


Output:

Enter the area:
23
Perimeter of the square is: 19.183326093250876











3.
Program:

import java.io.*;
importjava.util.Scanner;
classcalculateCylinderVolume
{
publicstaticvoid main(String args[]) throwsIOException
{
Scanner s=newScanner(System.in);
System.out.println("Enter the radius : ");
double rad=s.nextDouble();
System.out.println("Enter the height : ");
doubleht=s.nextDouble();
doublevol=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.
Program:

importjava.util.Scanner;


publicclasscalculateTax {

/**
* @paramargs
*/
publicstaticvoid main(String[] args)
{
Scanner s=newScanner(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 : ");
intot=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.


Program:
importjava.util.Scanner;


publicclasscalculateTotalProfit
{

/**
* @paramargs
*/
publicstaticvoid main(String[] args)
{
Scanner s = newScanner(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.
Program:

importjava.util.Scanner;


publicclasscalculateCylinderArea
{

/**
* @paramargs
*/
publicstaticvoid main(String[] args)
{
Scanner s=newScanner(System.in);
System.out.println("Enter the base radius : ");
double rad=s.nextDouble();
System.out.println("Enter the height : ");
doubleht=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.

Program:

importjava.util.Scanner;


publicclasscalculatePipeArea
{

/**
* @paramargs
*/
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 : ");
doublelen=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.
importjava.util.Scanner;


publicclasscalculateHeight {

/**
* @paramargs
*/
publicstaticvoid 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.
importjava.util.Scanner;


publicclassBoatDistance
{

/**
* @paramargs
*/
publicstaticvoid main(String[] args)
{
Scanner s= newScanner(System.in);
System.out.println("Enter the width of the river (in meters) : ");
doublerw=s.nextDouble();
System.out.println("Enter the river's speed (in meter/sec) : ");
doublers=s.nextDouble();
System.out.println("Enter the boat's speed (in meter/sec) : ");
doublebs=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
doublebd=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.
Program:

importjava.util.Scanner;


publicclasscalculateBalance {

/**
* @paramargs
*/
publicstaticvoid main(String[] args)
{
Scanner s=newScanner(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();
doublesi=(p*(m/12)*r)/100;
doublebal=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






You may also like this :

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...