Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7554811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:26:23+00:00 2026-05-30T11:26:23+00:00

I’m in a programming class where we have just switched from python to C.

  • 0

I’m in a programming class where we have just switched from python to C. I’m having a bit of trouble with it, as C doesn’t seem to perform mathematical operations with the ease that python does, or I’m missing something when it comes to math in C.

For my homework, I’m writing a program that collects how many miles to a gallon user’s car gets, how much their gas costs per gallon, and how many miles they drive each month. The program then tells them how much they can expect to pay for gas for the current month. My current code is as follows:

#include <stdio.h>

int main () {

int mpg, miles;
double gas_price;

printf("How many miles per gallon does your car get?\n");
scanf("%d", &mpg);

printf("What is the price of gasoline per gallon?\n");
scanf("%lf", &gas_price);

printf("How many miles do you drive in a month?\n");
scanf("%d", &miles);

printf("The cost of gas this month is $%.2lf\n", miles / mpg * gas_price);

printf("%d %d %d", mpg, gas_price, miles);

return 0;
}

When I run the program with the values 24 for “mpg”, 3.00 for “gas_price”, and 1000 for miles, the total comes to $123.00. This isn’t correct, and is about two dollars less than the actual price. When you take ((1000 / 24) * 3.00), you should get 125 even. I added a string to print out all the values to see what C was using for the formula in line 23, and while mpg and gas_price are correct, “miles” shows up as having a value of 1,074,266,112. I know there must be some error here, as this would throw the result off way more than 2 dollars, but I can’t help but think it’s related.

I apologize for the length of the question, but I wanted to be as specific as possible, and I’m completely stumped as to why C is reading this in so strangely.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:26:24+00:00Added an answer on May 30, 2026 at 11:26 am

    You’re doing integer division here:

    miles / mpg * gas_price
    

    Cast one of the two operands to a double first:

    (double)miles / mpg * gas_price
    

    Integer division will truncate the fractional part. That’s why your numbers are off.


    You have another error here:

    printf("%d %d %d", mpg, gas_price, miles);
    

    Your printf format specifiers don’t match the operands. It should be:

    printf("%d %f %d", mpg, gas_price, miles);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a bunch of posts stored in text files formatted in yaml/textile (from
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.