简体   繁体   中英

Is there something wrong with my program? I only seems to get 0.0000 as my answers

#include <stdio.h>
void main (void)    
{
    int days, seconds;
    
    printf("Enter the no. of days: ");
    scanf("%d", &days);
    
    seconds = days*36400;
    
    if (days<0)
    {
        printf("Invalid input!");
    }
    else 
    {
        printf("It is equal to %.5f seconds", seconds);
    }
}

You declared seconds as an integer, so you should change %.5f to %d .

#include <stdio.h>
void main (void)
{
  int days, seconds;

  printf("Enter the no. of days: ");
  scanf("%d", &days);
  
  seconds = days*36400;
 
  if (days<0)
    {
      printf("Invalid input!");
    }
  else 
    {
      printf("It is equal to %d seconds", seconds);
    }
  
}

There are couple of mistakes in your code

  1. Before converting days into second you need to check for valid and invalid input.
  2. You have taken variables in int and printing them as floating value.

Yo can use following code to get the output you want

#include <stdio.h>

int main(void) {
    int days, seconds;

    printf("Enter the no. of days: ");
    scanf("%d", &days);
    if(days<=0){
        printf("Invalid input");
    }else{
        seconds = days * 86400;
        printf("It is equal to: %.5d Seconds", seconds);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM