why does this program output 37? [duplicate] - c

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 1 year ago.
#include <stdio.h>
int main()
{
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32);
return 0;
}

Change your statement to (9.0/5.0)*c + 32 as 9 and 5 are integers, their division returns integer that is 1. So write them in float variable format.

Related

Float prints Int in C [duplicate]

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
What is the behavior of integer division?
(6 answers)
Closed 2 years ago.
I am beginner at programming and I am just writing a very simple program to return the solution of 23/5. My code is below:
float res = 23/5;
printf("%.3f", res);
The expected answer is 4.6, however, the code outputs: 4.000.
You are setting its value to an int
float res = 23.0f/5.0f;
printf("%.3f", res);

How does one make a floating point number stop rounding to an integer? [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
How to divide integers and get a float in C
(2 answers)
Closed 2 years ago.
I'm trying to write a basic programme, which gets a user's input, divides by seven, and then returns the result.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int s = get_int("Input: ");
float f = (s / 7);
printf("%f\n", f);
}
Eg. if I input "8", I would expect 1.142857. But I instead simply get "1.000000". Why is this?
#include <stdio.h>
int main()
{
int s=4;
float f = ((float)s / 7);
printf("%f\n", f);
getch();
}
You just have to typecast the int to float. int/float division give you the floor of given integer whereas float/float division gives you float

Dev-C++ 536870912 float variable output [duplicate]

This question already has answers here:
C: printf a float value
(7 answers)
Closed 3 years ago.
I just made this code:
#include <stdio.h>
#include <stdlib.h>
#define PI 3.1416
int main (){
float x;
x = PI;
printf("x equals: %i.\n",x);
system("pause");
return 0;
}
and the 'Pi' number is 536870912. Can anybody tell me what is wrong?
You're using the %i format specifier for printf. This is for signed integers. Instead use %f as x is a float.

What's the case of float division returning inf and finite value? [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
integer division vs regular division
(2 answers)
why is if(1/10) and if(0.1) have different values? [duplicate]
(2 answers)
Integer division returns 0 in C
(2 answers)
Closed 3 years ago.
I have written this snippet of code and I cannot understand why this float division returns inf when args[2] = 200 and 5.0 when args[2] = 2000. Is this caused because I am exceeding some decimal position boundary?
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int nargs, char **args) {
float t = atoi(args[1]);
float dt = atoi(args[2])/1000;
float nsamples = (t/dt);
printf("%f\n", nsamples);
return(0);
}
You're just getting tripped up by integer division (200 / 1000 = 0).
Change float dt = atoi(args[2])/1000; -> float dt = atoi(args[2])/1000.0f;

Why is the output always zero(0.0000)? [duplicate]

This question already has answers here:
Division result is always zero [duplicate]
(4 answers)
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 4 years ago.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int x,i;
double abc,sum=0;
printf("Enter a value for x:");
scanf("%d",&x);
for(i=0;i<=6;i++)
{
abc=pow(1/2,i)*pow((x-1)/x,i+1);
sum=sum+abc;
}
printf("Sum is %f\n",sum);
}
As what i have checked there is no overflow of values in data type either or is it something else?
The issue is in this line of code:
abc=pow(1/2, i) * pow((x-1) / x, i + 1);
1/2 is always zero, and (x - 1)/x is also zero when x > 0. You can use 0.5 or 1.0 / 2.0 if you'd like to use a decimal value. Also, be careful about dividing by zero.
The resulting code would look like this:
abc=pow(0.5, i) * pow((x - 1.0)/x, i + 1.0);

Resources