문제

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;

}
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top