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:3The 3 numbers are :234

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

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

/*** @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 :300Enter the no. of working hours in a day :6Enter the no. of hours worked in over time :1Enter the no. of hours took leave :1560

Gross Pay (in $) : 2892.0Tax (in $) : 433.8Net 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);}