This question already has answers here:
C: converting Farenheit to Celsius
(3 answers)
Closed 6 years ago.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int celsius = 0, kelvin = 0;
float farenheit;
printf("Enter the Boiling of water in Celcius \n");
scanf("%d", &celsius);
kelvin = 273 + celsius;
farenheit = celsius * (9 / 5) + 32;
printf("The Boiling point of water in Kelvin is %d \n and in farenheit is %f \n \n", kelvin, farenheit);
printf("Enter the Freezing point of water in Celcius \n");
scanf("%d", &celsius);
kelvin = 273 + celsius;
farenheit = celsius * (9 / 5) + 32;
printf("The freezing point of water in Kelvin is %d \n and in farenheit is %f \n \n", kelvin, farenheit);
return 0;
}
In eclipse, I type this code and I'm expecting it to ask me on console "Enter the Boiling of water in Celsius" and wait till i type in 100 and again do the same for freezing point. But after I build(ctrl+B), it does nothing. I had to enter some number(Here i did '100')then press 'enter' and again some number (here its '1') then press 'enter'. It displayed me the calculations for '100' degree Celsius.And it didn't even ask me to enter the freezing point either. I typed '1' and pressed enter.
Whats wrong with my code.?
Here's my output.
100
1
Enter the Boiling of water in Celcius
The Boiling point of water in Kelvin is 373 and in farenheit is 132.000000
Enter the Freezing point of water in Celcius
The freezing point of water in Kelvin is 274 and in farenheit is 33.000000
int celsius;
scanf("%d", &celsius);
int kelvin = 273 + celsius;
float farenheit = 1.8 * celsius + 32; // To make it a float
Input
100
0
Output
212.000000 373
32.000000 273
See http://ideone.com/6ikg23 demo.
the 9/5 is an integer divide.
an integer divide drops any fraction.
So the result is 1.
To fix the problem, use float rather than int
9.0f/5.0f
First, the code you provided is missing a brace at the end and won't calculate the proper Fahrenheit conversion due to integer truncation; however, the first one can be chalked up to a copy-paste error, and the second is a common mistake among programmers (new and old).
You code compiled and ran perfectly fine for me, and after fixing the Fahrenheit error (hint: use a decimal point, (9./5), to calculate it in floating-point) it returned the correct values for my inputs.
Unfortunately, I'm not familiar with Eclipse, so I can't be much help there. But it's definitely a tooling issue, not a problem with your code.
Related
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 5 months ago.
Improve this question
Edited: fixed some typos, also add more context
So I tried to put this code:
#include <stdio.h>
int main() {
float ps, ls, ms, es;
printf("Enter the project score: ");
scanf("%d", &ps);
printf("Enter the long exam score: ");
scanf("%d", &ls);
printf("Enter the midterm exam score: ");
scanf("%d", &ms);
90 = (ps * 0.15) + (ls * 0.2) + (ms * 0.25) * (es * 0.4);
printf("Final exam score needed: %d", es);
return 0;
}
As I want this equation 90=85(.15)+88(.2)+92(.25)+x(.4)
but it states that "lvalue required as left operand of assignment"
As others point out you need to use %f to read in float.
But here's an example that solves your equation:
Given the scores of the Project, Long Exam and Mid-term what score in the Final Exam is required for a weighted average of 90.
C is an 'imperative' procedural programming language.
You need to specify how to derive the es value.
It can't be expected to solve your equation.
The = means "assign to the variable on the left the value on the right".
It's not a statement of equality or even a logical test. It's an act of assigning a value into a variable.
I'm labouring the point because a 'breakthrough' understanding in programming is that equals "=" doesn't always mean what it does in Maths. In fact rarely does!
If you try 90, 90 and 90 below it will say 90 (which makes sense).
If you try 80, 85 and 90 you need a final score of 93.125.
This code does a back check to show the value calculated gives the right weighted score.
#include <stdio.h>
int main() {
float ps, ls, ms, es;
printf("Enter the project score: ");
scanf("%f", &ps);
printf("Enter the long exam score: ");
scanf("%f", &ls);
printf("Enter the midterm exam score: ");
scanf("%f", &ms);
printf("\nps=%f ls=%f ms=%f\n",ps,ls,ms);
es=(90-(ps * 0.15) - (ls * 0.2) - (ms * 0.25) )/0.4;
printf("Final exam score needed: %f\n", es);
float check=ps*0.15+ls*0.2+ms*0.25+es*0.4;
printf("Giving final score of %f\n",check);
return 0;
}
Typical Output:
Enter the project score: Enter the long exam score: Enter the midterm exam score:
ps=80.000000 ls=85.000000 ms=95.000000
Final exam score needed: 93.125000
Giving final score of 90.000000
For starters you declared variables as having the type float
float ps, ls, ms, es;
So to enter values to the variables you have to use the conversion specifier f instead of d like
printf("Enter the project score: ");
scanf("%f", &ps);
This statement
90 = (ps * 0.15) + (ls * 0.2) + (ms * 0.25) * (es * 0.4);
does not make a sense. You may not change an integer constant. Also it is unclear why there is used the magic number 90.
As in the next statement you are trying to output the value of the variable es again using the invalid conversion specifier d instead of f then it seems you mean
es = (ps * 0.15) + (ls * 0.2) + (ms * 0.25) * (es * 0.4);
printf("Final exam score needed: %f\n", es);
However pay into account that the variable es was not initialized. So the program will have undefined behavior.
Maybe you mean something like (though it is difficult to say what actually you mean)
es = (ps * 0.15) + (ls * 0.2) + (ms * 0.25);
es *= 0.4;
You have
90=85(.15)+88(.2)+92(.25)+x(.4)
This should be
// solve equation for x
float x = (90 - (85*(.15)+88*(.2)+92*(.25))) / .4;
printf("Final exam score needed: %f\n", x);
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 2 years ago.
Improve this question
I ran into a problem while calculating density of a box.
The output always comes out to be 0.00.
I tried type-casting but nothing worked.
Please help, I will be very grateful.
Here is the code...
#include <stdio.h>
void main()
{
int l, b, h;
float wt, vol;
float den = 0.0;
// Taking Length
printf("Enter Length ");
scanf_s("%d", &l);
//Taking Breadth
printf("\nEnter Breadth ");
scanf_s("%d", &b);
//Takhing Height
printf("\nEnter Heigh ");
scanf_s("%d", &h);
//Calculating Volume of the Box
vol = (l * b * h);
//Printing Volume
printf("\nVolume Of The Box Is %.2f\n\n", vol);
//Now taking mass of the box
printf("Enter Weight ");
scanf_s("%f", &wt);
//Calculating Density
den = wt /(float) vol;
//Printing Density
printf("\nDensity of the Box Is %.2f\n\n", &den);
}
The output of the program is as follows :
Enter Length 10
Enter Breadth 10
Enter Height 10
Volume Of The Box Is 1000.00
Enter Weight 5000
Density of the Box Is 0.00
Press any key to continue. . .
Note
Density = Mass(Weight)/Volume
Thanks :)
At least: wrong format specifier.
// double address
printf("\nDensity of the Box Is %.2f\n\n", &den);
Save time, enable all warnings. A good well enabled compiler will warn about this mismatch.
Recall &v prints the address of the variable v in memory, we use this in scanf (if you're a beginner), so change your code to:
printf("\nDensity of the Box Is %.2f\n\n", den);
This question already has answers here:
C: converting Farenheit to Celsius
(3 answers)
Closed 8 years ago.
This is the code. S (arithmetic mean) is done right, I have valid result. But for other calculations I'm getting this error: Floating point exception (core dumped).
Does anyone know what is wrong with my code?
#include <stdio.h>
#include <math.h>
main () {
int a,b,c;
double s,h,g,k;
printf ("unesite 3 cela broja\n");
scanf ("%d%d%d", &a, &b, &c);
s=(a+b+c)/3;
printf ("aritmeticka s.v. je: %.2lf\n", s);
/* this should be formula for medium value of harmonic number */
h=3/((1/a)+(1/b)+(1/c));
printf ("harmonijska s.v. je: %.2lf\n", h);
/* this should be http://upload.wikimedia.org/math/a/f/f/aff7a590d055d563ceea52fd66fe7ee2.png */
k=sqrt ((pow(a,2)+pow(b,2)+pow(c,2))/3);
printf ("kvadratna s.v. je: %.2lf\n", k);
/* and this http://upload.wikimedia.org/math/e/3/4/e348daea2f4f2bb60f5cb40706fcbad4.png */
g=pow(a*b*c,1/3);
printf ("geometrijska s.v. je: %.2lf\n", g);
return 0;
}
Almost all your divisions are integer divisions. And if you divide an integer with a larger value then the result will be truncated to zero. Division by zero is not good, and will abort your program.
Change those divisions to floating point divisions.
This question already has answers here:
C Temperature Conversion Program Keeps Outputting 0 For Fahrenheit to Celsius [duplicate]
(2 answers)
Closed 8 years ago.
I have following C program, that takes a temperature in Fahrenheit and convert in to Celsius value. But, every time I input a value it always gives me0.00 as output. I'm not understanding where is the problem.
#include <stdio.h>
int main()
{
float cel_out, fht_in;
printf("Enter a temperature in farenheit: ");
scanf("%f", &fht_in);
cel_out = (fht_in - 32) * (5/9);
printf("Temperature in celcious: %.2f", cel_out);
return 0;
}
In this euqation
cel_out = (fht_in - 32) * (5/9);
When you use 5/9 it is integer data type which results 0. so
cel_out = (fht_in - 32) * 0;
Results 0 only!
You need to use-
cel_out = (fht_in - 32) * (5.0f/9.0f);
here it is treated as float values and gives the actual result of 5/9.
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 8 years ago.
Improve this question
Question from book:
Write a program that does temperature conversion from Fahrenheit to Celsius, your program should :
prompt the user for which type of conversion they want to do.
prompt the user for the temperature they want to convert.
I am getting incorrect output.I'm not sure where i'm going wrong.I'm new to c language. Any help is greatly appreciated.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{int f, c, f_or_c;
printf("Would you like to convert Fahrenheit (1) or Celsius (2)?\n");
scanf("%d", &f_or_c);
if(f_or_c==1)
{
printf("Enter the temperature in Fahrenheit to convert?\n");
scanf("%d", &c);
f = 1.8*c + 32.0;
printf("Celsius of %d is %d degrees.\n");
}
if(f_or_c==2)
{
printf("Enter the temperature in Celsius to convert?\n");
scanf("%d", &f);
c = (f-32)*5/9;
printf("Fahrenheit of %d is %d degrees.\n");
}
return 0;
}
My guess is you just aren't printing the values out, but everything else looks pretty good.
printf("Fahrenheit of %d is %d degrees.\n");
You're not printing any variables.
This might work for you
printf("Fahrenheit of %d is %d degrees.\n", f, c);
You can take a look at general usage of printf here
http://www.cplusplus.com/reference/cstdio/printf/