Use double
(or float
) for all the values. Read the numbers with %lf
(or %f
). If the user omits a decimal point, that's OK; the floating point formats do not insist on the decimal point.
Difference between inputing whole numbers and decimal numbers in C
Question
I was writing a program that follows a formula, Volume = (((h*a)-(e*f))*ht), so I need values for h, a, e, f, and ht. The way I wrote this program is to take only whole numbers, but I need the program to take both decimal numbers (2 decimal places) and whole Numbers(if necessary).
#include <stdio.h>
int main (void)
{
int d;
printf("Enter value for h: \n");
scanf("%d", &d);
printf("You entered %d\n", d);
int c;
printf("Enter value for c: \n");
scanf("%d", &c);
printf("You entered %d\n", c);
int e;
printf("Enter value for e: \n");
scanf("%d", &e);
printf("You entered %d\n", e);
int f;
printf("Enter value for f: \n");
scanf("%d", &f);
printf("You entered %d\n", f);
int h;
printf("Enter value for ht: \n");
scanf(%d", &h);
printf("You entered %d\n", h);
return 0;
}
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow