The program I am writing displays the velocity and acceleration when the user inputs time.

The code I have thusfar is:

#include <stdio.h>
#include <math.h>
int main (void)
{
int time,velocity, acceleration;
printf ("Please input time in seconds: \n");
scanf_s("%d" , &time);
velocity = 0.00001* pow(time,3)-.00488* pow (time,2)+.75795*time+181.3566;
acceleration = 3-0.000062*pow(velocity,2);
printf ("Velocity: ""%d\n",velocity);
printf ("Acceleration: ""%d\n",acceleration);
return 0;
 
}

The answer displays, but I can't get it to display the decimals. It only shows whole numbers.

I tried replacing int with float, but then it shows a completely wrong answer. I also changed the %d to %f and still shows the problem.

I believe I am declaring wrong.

I also tried %5.3f......but the answer comes out completely wrong.

Can anyone help me, I tried...hence the code I am posting ;)

:D

Replace all integers with float and change all "%d" to "%f", even in the scanf statement. Also post the results you get when you use ints and when you use floats.

I replaced everything even the scanf statement as you said and it worked like a charm. Thank you ~s.o.s~ :*

Now, something extra...how do I restrict alphabets?

isalpha?
isnumerical?

Thank you again, for the help, I am so happy when a program works. :)

#include<stdio.h>
#include<math.h>
int main (void)
{
int time,velocity, acceleration;
printf ("Please input time in seconds: \n");
scanf_s("%d" , &time);
velocity = 0.00001* pow(time,3)-.00488* pow (time,2)+.75795*time+181.3566;
acceleration = 3-0.000062*pow(velocity,2);
printf ("Velocity: ""%d\n",velocity);
printf ("Acceleration: ""%d\n",acceleration);
return 0;
}

The answer displays, but I can't get it to display the decimals. It only shows whole numbers.

I tried replacing int with float, but then it shows a completely wrong answer. I also changed the %d to %f and still shows the problem.

I believe I am declaring wrong.

I also tried %5.3f......but the answer comes out completely wrong.

Can anyone help me, I tried...hence the code I am posting ;)

:D

Edited 3 Years Ago by pyTony: fixed formating

This question has already been answered. Start a new discussion instead.