I want to write down to 1 decimal place in C language.

Asked 4 months ago, Updated 4 months ago, 7 views

It's a code that calculates the total and average points by entering the scores for each subject, but I don't know how to write the average point up to one decimal place.What should I do?

#include<stdio.h>
int main(void){
  double a, b, c, Sum, Ave;
  printf("Input your scores:\n";
  printf("Math:");
  scanf("%lf", & a);
  printf("English:");
  scanf("%lf", &b);
  printf("Science:");
  scanf("%lf", &c);

  Sum = a + b + c;
  Ave = Sum/3;
  if(Sum<=179)
    printf("Your grade is F, Average is%.lf.\n", Ave);
  if(Sum>=180&Sum<=209)
    printf("Your grade is C, Average is %.lf.\n", Ave);
  if(Sum>=210&Sum<=239)
    printf("Your grade is B, Average is %.lf.\n", Ave);
  if(Sum>=240&Sum<=269)
    printf("Your grade is A, Average is %.lf.\n", Ave);
  if(Sum>=270)
    printf("Your grade is A++, Average is%.lf.\n", Ave);
  return 0;
}

c

2022-09-30 14:20

1 Answers

%lf instead of %lf will match the display up to 1 decimal place.

printf("Your grade is F, Average is %.1lf.\n", Ave);


2022-09-30 14:20

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.