Program to calculate speed in km/hr

Problem statement:-  Program to calculate speed in km/hr.

Formula:-

speed=d/t

where d= distance 
           t=time

Data requirement:-

  Input Data:- d,t

  Output Data:- speed

Program in C

Here is the source code of the C Program to calculate speed in km/hr.

Code:

#include<stdio.h>
#include<math.h>
int main()
{
    printf("Enter the Distance in Kms:");
    double d;
    scanf("%lf",&d);
    printf("Enter the Time in Hrs:");
    double t;
    scanf("%lf",&t);
    double speed=d/t;
    printf("Speed is %0.2lf(Km/Hr)\n",speed);
}

Input/Output:
Enter the Distance in Kms:100
Enter the Time in Hrs:2
Speed is 50.00(Km/Hr)

Program in C++

Here is the source code of the C++ Program to calculate speed in km/hr.

Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    cout<<"Enter the Distance in Kms:";
    double d;
    cin>>d;
    cout<<"Enter the Time in Hrs:";
    double t;
    cin>>t;
    double speed=d/t;
    cout<<"Speed is "<<speed<<" (Km/Hr)";
}

Input/Output:
Enter the Distance in Kms:200
Enter the Time in Hrs:6
Speed is 33.3333 (Km/Hr)

Program in Java
  
Here is the source code of the Java Program to calculate speed in km/hr.

Code:

import java.util.Scanner;
public class Speed {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
System.out.println("Enter the Distance in Kms:");
    double d=cs.nextDouble();
    System.out.println("Enter the Time in Hrs:");
    double t=cs.nextDouble();
    double speed=d/t;
    System.out.println("Speed is "+speed+" (Km/Hr)");
     cs.close();
}
}

Input/Output:
Enter the Distance in Kms:
1000
Enter the Time in Hrs:
10
Speed is 100.0 (Km/Hr)

Program in Python
  
Here is the source code of the Python Program to calculate speed in km/hr.

Code:

d=float(input("Enter the Distance in Kms:"))
t=float(input("Enter the Time in Hrs:"))
speed=d/t
print("Speed is ",speed," (Km/Hr)")

Input/Output:

Post a Comment

0 Comments