C Program to Calculate the Speed

How to Calculate the Speed in C Language

Example of how you can calculate the speed in C language, using the value of distance and time with the specific conditions.

How this Program Work?

First, we'll take input the value of distance and time from the user.

After that, we will be applying condition if time and distance are positive then calculate the value of speed.
And, if time or distance is negative then terminate the program.

C Program to Calculate the Speed

#include<stdio.h>

void main(){

//declaration of variable
float d,t, speed;

/*
we're taking
d variable for distance
t variable for time
*/

//getting the value of d from user
printf("Enter the Value Of Distance");
scanf("%f",&d);

//getting the value of t from user
printf("Enter the Value Of Time 'T' ");
scanf("%f",&t);

if(t>0 && d>0) //applying condition since time and distance can't be negative
{
// formula to find speed
speed= d/t;
printf("With distance %f and time %f, the value of speed is %f km", d, t, speed);
}
else
{
printf("Distance or Time can't be negative");
}
}

Related :



Comments

Popular posts from this blog

Pseudocode to Check, Number is Odd or Even

How to Create Marksheet in C