Code For Alls..!

get the code!!

Friday 15 September 2017

Average Speed

                                                                          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
Java Program:
import java.util.*;
public class Hello {

    public static void main(String[] args) {
//Your Code Here
Scanner sc=new Scanner(System.in);
String st;
int d=0,s=0;
st=sc.nextLine();
String[] str=st.split(" ");
for(String a:str){
int j=a.indexOf('@');
   d=d+Integer.parseInt(a.substring(0,j)); 
   s=s+Integer.parseInt(a.substring(j+1));
}
System.out.printf("%.2f kmph",((float)d/(float)s));
}
}

No comments:

Post a Comment