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

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);

Related

Why this programme doesn't print the value 2 .. My desire output is 2.. where is the error here [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Is floating-point == ever OK?
(14 answers)
Closed 11 months ago.
sample code is here, desire output is 2 ::
#include <stdio.h>
int main()
{
double i, a, b;
int j;
for (i = 0; i <= 3; i = i + .20)
{
if (i == 2)
{
printf("I=%lf\n", i);
}
}
}
When I use
#include <stdio.h>
int main()
{
double i, a, b;
int j;
for (i = 0; i <= 3; i = i + .25)
{
if (i == 2)
{
printf("I=%lf\n", i);
}
}
}
it works; but in the first case, it is not working. WHY ??
The short answer is that the use of a floating control variable for a for loop is unwise... comparing a floating value for equality is even less so.
Due to the storage of floating point numbers as a mantissa and an exponent, your 0.20000000 may well be 0.199999999...9 or 020000000...01 thus the comparison fails.
Typically, 0.25 and 2.000 will store exactly, as they are powers of 2. Hence a step of 0.25 works as anticipated.
MISRA C:2012 has Rule 14.1 to protect against using float or doubles as loop counters... and previously had a Rule to protect against testing float/double for equality -perhaps we should reinstate that Rule.

why does this program output 37? [duplicate]

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.

average from three numbers with different weight [duplicate]

This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 7 years ago.
Why does output equal zero in this code?
Number 1 with weight of 2, number two with weight of 3 and number three with weight of 5. I can not understand why output = 0.
#include <stdio.h>
int main ()
{
float A ,B, C ,MEDIA=0 ;
scanf("%f%f%f",&A ,&B,&C);
MEDIA+=1/2*A + 1/3*B + 1/5*C;
printf("MEDIA = %.1f", MEDIA );
return 0;
}
MEDIA+=1/2*A + 1/3*B + 1/5*C;
Because 1/2, 1/3 and 1/5 will be evaluated as 0. Since they are integers.
Either write
1.0/2, 1.0/3 and 1.0/5 instead. So the compiler will know to treat the result as float.
Or
MEDIA+=A/2 + B/3 + C/5;
P.S.
Maybe I am wrong but if I understood correctly what you wrote at the description then I think your calculation of the weighted average is incorrect. It should be something like
(A*2 + B*3 + C*5)/10

Trouble with float on C [duplicate]

This question already has answers here:
C program to convert Fahrenheit to Celsius always prints zero
(6 answers)
Closed 8 years ago.
I have this little program in C that calculates the square root x of a positive integer N using a recursive function (implemented using a while loop). If I calculate x using this:
x = (1/2)*(x + N/x) //x0 = 1.0
Then x keeps growing to inf and then nan. However if I use this:
x = (x + N/x)/2 //x0 = 1.0
It works fine, why? Thanks.
1/2 does integer division, its result is 0, change either or both operand to double, e.g:
1.0/2

Percentage not showing in c++ with Two Dimensional Array and Loop [duplicate]

This question already has answers here:
Division result is always zero [duplicate]
(4 answers)
Closed 8 years ago.
This is my code, please help me out, percentage showing 0.00 instead of what i want.
I want to calculate percentage, as you will know this by code below...
#include<stdio.h>
#include<conio.h>
int main()
{
int marks[2][3]={80,70,50,90,50,60};
int total[2]={0,0};
float per[2]={0.0f,0.0f};
for (int x=0;x<2;x++)
{
for(int y=0;y<3;y++)
{
printf("[%d][%d]=%d\t",x,y,marks[x][y]);
total[x]=total[x]+marks[x][y];
per[x]=total[x]/300*100;
}
printf("total [%d]=%d",x,total[x]);
printf("\n\npercentage [%d]=%2.2f \n",x,per[x]);
putchar('\n');
}
getch();
return 0;
}
In the expression
total[x]/300*100
all involved values are integers, so the result is truncated before the assignment to the floating point array entry.
Change to e.g.
total[x]/300.0f*100.0f
Replace per[x]=total[x]/300*100; with per[x]=total[x] * 1.0f / 300 * 100;
You need to convert int to double / float before division to make sure you don't loose the precision because of truncation of integer division.
per[x]=total[x]/300*100; /* Assuming total[x] = 280 */
per[x]=280/300*100;
per[x]=(280/300)*100; /* Associativity is left-to-right */
per[x]=0*100;
per[x]=0;
You may also want to read integer division in C and operator associativity

Resources