Breaking

Friday 15 September 2017

Average Speed

                             Average Speed


A single line L with a set of space separated values indicating distance travelled and time taken is passed as the input. The program must calculate the average speed S (with precision upto 2 decimal places) and print S as the output.
Note: The distance and time taken will follow the format DISTANCE@TIMETAKEN. DISTANCE will be in kilometers and TIMETAKEN will be in hours.
Input Format:
The first line contains L.
Output Format:
The first line contains the average speed S.
Boundary Conditions:
Length of L will be from 3 to 100.
Example Input/Output 1:
Input:
60@2 120@3
Output:
36.00 kmph
Explanation:
Total distance = 60+120 = 180 km.
Total time taken = 2+3 = 5 hours.
Hence average speed = 180/5 = 36.00 kmph
Code:


import java.util.*;
public class Hello {

    public static void main(String[] args) {
                   Scanner sc=new Scanner(System.in);
                   String str=sc.nextLine();
                   String[] ch=str.split(" ");
                   float d=0,t=0;
                   for(String s:ch)
                   {
                       String[] data=s.split("@");
                       d+=Integer.parseInt(data[0]);
                       t+=Integer.parseInt(data[1]);
                   }
        System.out.printf("%.2f kmph",d/t);
          }
}


Please do comment If u have any Queries!

No comments:

Post a Comment

Like