How do I convert a constant Celsius value to Fahrenheit, in C? - c

I am working with C and am tasked with defining a constant for a temperature in Celsius, and having the program convert it to Fahrenheit. To do this I would multiply by 9, then divide by 5, then add 32. I am lost. Here is my non-working code:
#include<stdio.h>
#define C 23
int F;
int main()
{
F = (5/9)*(C - 32);
scanf("%d", &F)
printf(F);
}
Any help would be greatly appreciated. I know there is lots of errors with this code, I started learning C today. I don't care if the entire thing needs to be scrapped.

The expression (5/9) performs integer division and equals 0. If you really want an integer value for the temperature in Fahrenheit, then try performing division as the very last step. Once you get the code somewhat working, test it on a few known values, including the freezing point, to make sure that the math is right. Be careful with order of operations and parentheses.
If you want sub-unit precision, you can use floating point values such as 5.0.

I have worked in C a long ago, but the problem I could see is the int type declared for F try changing it float then check the results. As an int (5/9) would give a zero and the result would always be a zero.

Did you read any basic C books before writing code? You have lots of very basic problems in your short program
Why do you read F from input while you're converting from C. You're calculating F and then read it again in scanf, so the previous result is overwritten
Why do you print F while you want the C result
The printf function is wrong. It's format is printf(<format_string>, <values_to_print>)
Integer division results in an integer. 5 and 9 are both ints so the result will also be int. You need at least one side to be float/double. 5.0/9, 5/9.0, 5./9, double(5)/9... or anything like that will work

you should read more basic c books.your code even can't compile.
I have fixed your code,do you mean that?
#include<stdio.h>
int main()
{
float F;
int C;
scanf("%d", &C);
F = C*9.0/5 + 32;
printf("%.3f\n",F);
}

Related

Power function in c language returns the cube and square of integer type 5, 124 & 24 in vVS code [duplicate]

While running the following lines of code:
int i,a;
for(i=0;i<=4;i++)
{
a=pow(10,i);
printf("%d\t",a);
}
I was surprised to see the output, it comes out to be 1 10 99 1000 9999 instead of 1 10 100 1000 10000.
What could be the possible reason?
Note
If you think it's a floating point inaccuracy that in the above for loop when i = 2, the values stored in variable a is 99.
But if you write instead
a=pow(10,2);
now the value of a comes out to be 100. How is that possible?
You have set a to be an int. pow() generates a floating point number, that in SOME cases may be just a hair less than 100 or 10000 (as we see here.)
Then you stuff that into the integer, which TRUNCATES to an integer. So you lose that fractional part. Oops. If you really needed an integer result, round may be a better way to do that operation.
Be careful even there, as for large enough powers, the error may actually be large enough to still cause a failure, giving you something you don't expect. Remember that floating point numbers only carry so much precision.
The function pow() returns a double. You're assigning it to variable a, of type int. Doing that doesn't "round off" the floating point value, it truncates it. So pow() is returning something like 99.99999... for 10^2, and then you're just throwing away the .9999... part. Better to say a = round(pow(10, i)).
This is to do with floating point inaccuracy. Although you are passing in ints they are being implicitly converted to a floating point type since the pow function is only defined for floating point parameters.
Mathematically, the integer power of an integer is an integer.
In a good quality pow() routine this specific calculation should NOT produce any round-off errors. I ran your code on Eclipse/Microsoft C and got the following output:
1 10 100 1000 10000
This test does NOT indicate if Microsoft is using floats and rounding or if they are detecting the type of your numbers and choosing the appropriate method.
So, I ran the following code:
#include <stdio.h>
#include <math.h>
main ()
{
double i,a;
for(i=0.0; i <= 4.0 ;i++)
{
a=pow(10,i);
printf("%lf\t",a);
}
}
And got the following output:
1.000000 10.000000 100.000000 1000.000000 10000.000000
No one spelt out how to actually do it correctly - instead of pow function, just have a variable that tracks the current power:
int i, a, power;
for (i = 0, a = 1; i <= 4; i++, a *= 10) {
printf("%d\t",a);
}
This continuing multiplication by ten is guaranteed to give you the correct answer, and quite OK (and much better than pow, even if it were giving the correct results) for tasks like converting decimal strings into integers.

Decimal To Binary Conversion in C using For

I am not able to convert from decimal to binary in C.Everytime I get a output which is one less than the desired output.For ex.:5 should be 101 but shows up as 100 or 4 should be 100 but shows up as 99.
#include<stdio.h>
#include<math.h>
void main() {
int a,b=0;
int n;
printf("Enter a Decimal Number\n");
scanf("%d",&n);
for(int i=0;n>0;i++) {
a=n%2;
n=n/2;
b=b+(pow(10,i)*a);
}
printf("%d",b);
}
My output is always one less than the correct answer and I dont know why.It fixes the problem if take b as 1 instead of 0 in the beginning but i dont know why.Please Help.I have just started C a few days ago.
pow is a floating-point function; it takes a double argument and returns a double value. In the C implementation you are using, pow is badly implemented. It does not always produce a correct result even when the correct result is exactly representable. Stop using it for integer arithmetic.
Rewrite the code to compute the desired power of ten using integer arithmetic.
Also, do not compute binary numerals by encoding them a decimal within a int type. It is wasteful and quickly runs into bounds of the type. Use either bits within an unsigned type or an array of char. When scanf("%d",&n); executes, it converts the input string into binary and stores that in n. So n is already binary; you do not need to decode it. Use a loop to find its highest set bit. Then use another loop to print each bit from that position down to the least significant bit.
This code seems fine. I quickly tested it on an online compiler and it seems to be working okay.
I am very sure it has to do with different versions of compilers.
compiler which I tested your code in: https://www.onlinegdb.com/online_c_compiler
Edit:
pow() function is not reliable when used with integers since the integer you pass into it as parameter is implicitly converted into data type of double and returns double as output. When you stuff this value into the integer again, it drops the decimal values. Some compilers seem to produce "correct" result with their version of pow() while some don't.
Instead, you can use a different approach to solve your decimal to binary conversion without errors in general use:
#include<stdio.h>
void main() {
int remainder,result = 0,multiplier = 1;
int input;
printf("Enter a Decimal Number\n");
scanf("%d",&input);
while(input){
remainder = input%2;
result = remainder*multiplier + result;
multiplier*=10;
input/=2;
}
printf("The binary version of the decimal value is: %d",result);
}

Confusing result while converting degree into radian in C

int x;
scanf("%d",&x);
x=(double)((x*3.14)/180);
printf("%.6lf",x);
When I run the above code and input x=60, I get -0.000000.
int x;
double a;
scanf("%d",&x);
a=(x*3.14)/180;
printf("%.6lf",a);
But when I run this above code, I get the correct answer.
I want to know where I am doing wrong. Is there problem in my type casting or use of double or any other thing? Any help will be appreciated. Thanks!
N.B. : I need to print output upto 6 digits after decimal.
There are two problems in your code:
x = (double)((x*3.14)/180);
(x*3.14)/180 is already a double therefore this line is equivalent to:
x = (x*3.14)/180;
but anyway the type of x remains int, so if x was e.g 300, the new values of x will be 300 * 3.14/180 = 5.2333 which will be trunacated to 5.
The second problem is here:
printf("%.6lf",x);
As explained before, the type of x is int, but the "%.6lf" format specifier requires a double. If the variable types don't match the format specifier, the behaviour is undefined.
The second version of your code is perfectly correct, but be aware that the user can only enter integer values.
BTW: 3.14 is a very poor approximation of PI, I'm sure you can do better.

Why does this C computation involving exponents produce the wrong answer? [duplicate]

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 9 years ago.
I am trying to implement the below formula on C
Here is my code:
int function(int x){
return pow(10, (((x-1)/(253/3))-1));
}
int main(void){
int z = function(252);
printf("z: %d\n",z);
return 0;
}
it outputs 10. However a calculator outputs 94.6.
could anyone explain me what I am doing wrong?
Note that in this line
(((x-1)/(253/3))-1))
You are dividing the integer value x - 1 by an integer value 253 / 3. This will truncate the value to an int, meaning that you'll be raising an integer power to an integer power.
To fix this, try changing this expression to
(((x-1)/(253.0 / 3.0))-1))
This now will use doubles in the expression, giving you the value you want.
Hope this helps!
Adding to that, integers do not give you the decimal part of numbers, so a number like 3.5 will get cut down to 3 using integer. To fix this double is the way to go. In other words 3 is different than 3.0

Inconsistent results while printing float as integer [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
print the float value in integer in C language
I am trying out a rather simple code like this:
float a = 1.5;
printf("%d",a);
It prints out 0. However, for other values, like 1.4,1.21, etc, it is printing out a garbage value. Not only for 1.5, for 1.25, 1.5, 1.75, 1.3125 (in other words, decimal numbers which can be perfectly converted into binary form), it is printing 0. What is the reason behind this? I found a similar post here, and the first answer looks like an awesome answer, but I couldn't discern it. Can any body explain why is this happening? What has endian-ness got to do with t?
you're not casting the float, printf is just interpreting it as an integer which is why you're getting seemingly garbage values.
Edit:
Check this example C code, which shows how a double is stored in memory:
int main()
{
double a = 1.5;
unsigned char *p = &a;
int i;
for (i=0; i<sizeof(double); i++) {
printf("%.2x", *(p+i));
}
printf("\n");
return 0;
}
If you run that with 1.5 it prints
000000000000f83f
If you try it with 1.41 it prints
b81e85eb51b8f63f
So when printf interprets 1.5 as an int, it prints zero because the 4 LSBs are zeros and some other value when trying with 1.41.
That being said, it is an undefined behaviour and you should avoid it plus you won't always get the same result it depends on the machine and how the arguments are passed.
Note: the bytes are reversed because this is compiled on a little indian machine which means the least significant byte comes first.
You don't take care about argument promotions. Because printf is a variadic function, the arguments are promoted:
C11 (n1570), ยง 6.5.2.2 Function calls
arguments that have type float are promoted to double.
So printf tries to interpret your double variable as an integer type. It leads to an undefined behavior. Just add a cast:
double a = 1.5;
printf("%d", (int)a);
Mismatch of arguments in printf is undefined beahivour
either typecast a or use %f
use this way
printf("%d",(int)a);
or
printf("%f",a);
d stands for : decimal. so, nevertheless a is float/double/integer/char,.... when you use : "%d", C will print that number by decimal. So, if a is integer type (integer, long), no problem. If a is char : because char is a type of integer, so, C will print value of char in ASCII.
But, the problem appears, when a is float type (float/double), just because if a is float type, C will have special way to read this, but not by decimal way. So, you will have strange result.
Why has this strange result ?
I just give a short explanation : in computer, real number is presented by two part: exponent and a mantissa. If you say : this is a real number, C will know which is exponent, which is mantissa. But, because you say : hey, this is integer. no difference between exponent part and mantissa part -> strange result.
If you want understand exactly, how can know which integer will it print (and of course, you can guess that). You can visit this link : represent FLOAT number in memory in C
If you don't want to have this trange result, you can cast int to float, so, it will print the integer part of float number.
float a = 1.5;
printf("%d",(int)a);
Hope this help :)

Resources