Home > other >  facing empty result printed while calculating the duration of the journey
facing empty result printed while calculating the duration of the journey

Time:04-03

Instead of putting value at the working storage, i tried the other way round by being able for user to enter any value in this set of code but the result printed out empty.

int main()
    {
        // declare and initialise working storage
        int distance;
        int speed;
        int hours = distance / speed;
        float speedInMinutes = speed / 60.0;
        float remainingMinutes = (distance % speed) / speedInMinutes;  // Minutes unit
    
        // prompt user to enter the distance and speed;
        printf("Enter the distance (KM) : ");
        scanf("%d", &distance);
        printf("Enter the speed (KM/HR) : ");
        scanf("%d", &speed);
        
        // calculate and print the duration of the journey
        printf("The duration of the journey : %d hours %.0f minutes\n", hours, remainingMinutes);
        
        return 0;
    
    }

But with this set of code that is the solution given on this exercise, i couldn't key any other value.

int main()
{
     // declare and initialise working storage
        int distance = 300;
        int speed = 80;
        int hours = distance / speed;
        float speedInMinutes = speed / 60.0;
        float remainingMinutes = (distance % speed) / speedInMinutes;  // Minutes unit`

      // calculate and print the duration of the journey
       printf("The duration of the journey : %d hours %.0f minutes\n", hours, remainingMinutes);
    
       return 0;

}`

CodePudding user response:

You are trying to calculate the answer before any of the variables are assigned their values try this:

int main()
    {
        // declare and initialise working storage
        int distance;
        int speed;
        // prompt user to enter the distance and speed;
        printf("Enter the distance (KM) : ");
        scanf("%d", &distance);
        printf("Enter the speed (KM/HR) : ");
        scanf("%d", &speed);
        int hours = distance / speed;
        float speedInMinutes = speed / 60.0;
        float remainingMinutes = (distance % speed) / speedInMinutes;  // Minutes unit
    
        // calculate and print the duration of the journey
        printf("The duration of the journey : %d hours %.0f minutes\n", hours, remainingMinutes);
        
        return 0;
    
    }
  •  Tags:  
  • c
  • Related