Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Here is my code.
float total,rate;
rate = score / 25;
printf("Total: %f", rate);
But this doesn't work; it always outputs 0.000. Can you help?
I'm going out on a limb here and say that you have score declared as an int. int divided by int will always result in an int. You can fix this by either:
declare score as float
cast it as (float)score
multiply it with a float rate = score * 1.0f / 25
change 25 to 25.0f
Try this:
rate = score / 25.0;
printf("Total: %f", rate);
Or:
rate = (float) score / 25;
printf("Total: %f", rate);
An int divided by an int will always be an int, with everything "after the decimal" truncated.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Here is the code. The function getDiscountedPrice will give me an amount of the money I am getting as a cashback discount. Suppose, I have bought a Mobile, it costs 2000 USD, the amount of discount is 15%. So I will get (15/100*2000) = 300 USD discount so that the mobile will cost me 1700 USD.
So, I want the total price value and the percentage to take from the user and use it inside my function. So, what can I do?
#include<stdio.h>
float getDiscountedPrice(float totalPrice, float discountedPercent){
float percentageOf = discountedPercent/100*totalPrice;
return totalPrice - percentageOf;
}
int main(){
float totalPrice, discountedPercent, sum;
printf("Total price of the product? - ");
scanf("%d", &totalPrice);
printf("The amount of discount percentage? - ");
scanf("%d", &discountedPercent);
sum = getDiscountedPrice(totalPrice,discountedPercent);
printf("Your discounted price is Rs%f", sum);
}
You are using %d as the format specifier when the data-type is float. Just change it to %f and your program should work fine.
Corrected code -
#include<stdio.h>
float getDiscountedPrice(float totalPrice, float discountedPercent){
float percentageOf = (discountedPercent/100)*totalPrice;
return totalPrice - percentageOf;
}
int main(){
float totalPrice, discountedPercent, sum;
printf("Total price of the product? - ");
scanf("%f", &totalPrice);
printf("The amount of discount percentage? - ");
scanf("%f", &discountedPercent);
sum = getDiscountedPrice(totalPrice,discountedPercent);
printf("Your discounted price is Rs %f", sum);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The program runs fine but keeps giving me an answer of 0.00. I have floated the numbers and answer and it asks for the first and second number but I cannot see where I have gone wrong.
#include <stdio.h>
#include <math.h>
int main()
{
float sub;
float num1 = 18.73;
float num2 = 20.00;
printf("Please enter the total of the meal: \n");
scanf("%f", &num1);
printf("Please enter the amount of money you have: \n");
scanf("%f", &num2);
sub = num2 - num1;
printf("\nYour change is: %.2f\n", &sub);
return 0;
}
You're printing the address of sub:
Do this:
printf("\nYour change is: %.2f\n", sub);
instead of:
printf("\nYour change is: %.2f\n", &sub);
In your code I have found 2 mistakes
Don't assign greater than 0 numbers to variables when you use scanf
float num1 = 0; //use this
float num2 = 0;
When you output a number don't use & in printf. That is the reason you were given 0.
printf("\nYour change is: %.2f\n", sub);
Finally, you don't need to use #include <math.h> in these king of programmes
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Ok so basically, I am trying to make a program that will take a KPH of 185 and convert it to MPH all the way to 0 like so. (using prototypes)
Kilometers per hour converted to miles per hour:
Kph Mph
185 115
180 112
175 109
... ...
10 6
5 3
0 0
Unfortunately my conversion is a bit off, can someone heed some information on why that may be?
#include <stdio.h>
// Prototypes
double mph2kph(double); // convert Miles to KM
double kph2mph(double); // convert KM to Miles
int main()
{
int loop = 1;
double kph = 185; // kilometers per hour
double mph = 115; // miles per hour for computation
printf("Kilometers per hour converted to miles per hour: \n");
printf("Kph Mph\n"); // Display Header
while (loop == 1){
printf("%.2d %.2d \n", kph, kph2mph(kph));
break;
}
loop = 0;
}
//Other Functions:
double mph2kph(double x){
return x*1.61;
}
double kph2mph(double x){
return x*1.61;
}
Output =
Kilometers per hour converted to miles per hour:
Kph Mph
40325120 38090656
You are using %d to show your final result, which is used for int variables. In your case, as you are using double variables, you should go for %f or %lf.
printf("%.2lf %.2lf \n", kph, kph2mph(kph));
Also, your kilometers per hour to miles per hour conversion function is wrong. You should divide and not multiply.
double kph2mph(double x){
return x/1.61;
}
Testing your code with those corrections leads to correct results:
Kilometers per hour converted to miles per hour:
Kph Mph
185.00 114.91
I have made some changes to your program to give your desired output. Some errors in your program are already identified by some other users. Compare this with yours and try to learn. Best of luck!
#include <stdio.h>
// Prototypes
double mph2kph(double); // convert Miles to KM
double kph2mph(double); // convert KM to Miles
int main()
{
int loop = 1;
int kph = 185; // kilometers per hour
double mph = 115; // miles per hour for computation
printf("Kilometers per hour converted to miles per hour: \n");
printf("Kph Mph\n"); // Display Header
while (kph != -5){
printf("%d %.2lf \n", kph, kph2mph(kph));
getchar();
kph = kph - 5;
}
}
//Other Functions:
double mph2kph(double x){
return x*1.61;
}
double kph2mph(double x){
return x/1.61;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I was suppose to get distance and speed from user and to return the time.
Here's the code I did:
int main()
{
int distance, speed;
scanf("%d,%d", &distance, &speed);
printf("%d\n", distance / speed);
printf("%d hours and %d minutes", (distance/speed), (distance / speed)%60);
}
For the values:
10 10
I receive 0 as output.
The problem here is, you did not check the return value of scanf() to ensure it's success.
By property, the format string supplied with scanf() should exactly match with the input, otherwise, due to matching failure, the receiving arguments won't get the expected value.
With a format string like
scanf("%d,%d", &distance, &speed);
an input
10 10
is not proper, you need to enter like
10,10
to match the , in the format string.
Otherwise, you can remove the , from the format string also, and provide the input in space-delimited format.
[Edit]:
To enforce a floating point division, please chnage your statement to
printf("%f\n", ( (float)distance / speed ) );
printf("%f hours and %d minutes", ( (float)distance / speed ), (distance / speed)%60);
You need to enter 10,10 because that's what you require with your scanf().
Then of course your calculation are incorrect. You'll get 1 and 1 for both hours and minutes.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Why does this code give a completely wrong answer if I use float or double but not int?
//C How to Program Exercises 2.33
#include <stdio.h>
#include <conio.h>
double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon);
int main(void){
double a,b,c,d,e;
printf ("Please enter the number of miles daily\n");
scanf("%lf",&a);
printf ("Please enter the cost per gallon\n");
scanf("%lf",&b);
printf ("Please enter the average miles per gallon\n");
scanf("%lf",&c);
printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
getch();
return 0;
}
double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon){
double overall_cost;
overall_cost= (miles/averageMilesPerGallon)+ costPerGallon;
return overall_cost;
}
Change
printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
^
to
printf ("The number of miles per gallon is:%f",dailyDrivingCost(a,b,c));
d conversion specifier is used to print an int, to print a double (or a float) you need the f conversion specifier.