#include<stdio.h>
int main()
{
int n,i;
float sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("1/%d +",i);
sum=sum+1/i;
}
printf("= %f",sum);
return 0;
}
In this code I've kept the sum variable float to see the values after decimal. But the result doesn't appear to be correct. 000000 is shown after decimal. Why is this happening? What should I do to get the correct value without changing other variable's datatype? When I change other variables to float datatype,the answer remains correct.
Because both 1 and i are integers, the expression 1/i is evaluated as integer.
Therefore, if i > 1 then 1/i == 0.
You can solve this using float(1)/i or 1/float(i) or float(1)/float(i).
Related
I'm trying to build a calculator, which input a number n it can automatically output the sum of
1/2+2/2 +1/3+2/3+3/3 +...+1/n+2/n+...+n/n and for each denominator, print the result once.
The calculator will restart and ask for input after output the final result of 1/2+2/2 +1/3+2/3+3/3 +...+1/n+2/n+...+n/n .And when the the user decides to stop the program enter 0.
for input=3 ,the output is sum =1 ,sum=2 ,yet I expected it to be sum=3/2, sum=7/2
Here's the program
#include <stdio.h>
int main(){
printf("Please enter the max denominator:\n");
int i,j,n;
int sum=0;
do{
scanf("%d",&n);
for (j=2;j<=n;j++){
for(i=1;i<=j;i++){
sum+= i/j;
if (i==j){
printf("sum=%d\n",sum);
break;
}
}
}
} while (n!=0);
return 0;
}
EDIT: revised part due to advice
int i,j,n;
double sum=0.0;
do{
scanf("%d",&n);
sum=0.0;
for (j=2;j<=n;j++){
for(i=1;i<=j;i++)
sum+=(double)i/j;
printf("sum=%lf\n",sum);
}
} while (n!=0);
return 0;
}
Your program is working with integers. i, j, n, and sum are all integers. This means they cannot encode numbers like 3/2 or 7/2.
So what happens when you divide two integers, like 1/2? The answer is that the result rounds down to the nearest integer. Since 1/2 is 0.5, the nearest integer below that is 0, so the result is 0.
You need to use float, which uses floating point arithmetic. Floating point arithmetic has its own set of fun rounding issues (You may be surprised to find that "0.2" cannot be represented exactly with floating point numbers).
A more exact alternative is to write your own functions to handle rational numbers as a pair of integers -- a numerator and a denominator. It will require additional algorithms (such as a Greatest Common Divisor algorithm), but it can describe these sorts of problems exactly.
I am writing code to find the distance of a point(25,40000) from a fixed point(47,132000). The distance always printed to be 0.0000.
I have tried checking other combinations, giving smaller values of points, and printing them with %d, it works great.
But with %ld,%lf,%Lf something is not fine. Kindly help.
#include<stdio.h>
#include<math.h>
int main()
{
int x=25,y=40000;
//printf("Enter x,y");
//scanf(%d %d,&x,&y)
long double dist;
dist=sqrt((47-x)*(47-x)+(132000-y)*(132000-y));
printf(" x= %d y=%d dist=%Lf\n",x,y,dist);
return 0;
}
Integer overflow takes place in your code. The below code works -
int main()
{
long long int x=25,y=40000; //int was overflowing and causing you error
long double dist;
dist=sqrt((47-x)*(47-x)+(132000-y)*(132000-y)); //this will safely compute within range and return the desired result
printf("After distance\n");
printf(" x= %lld y=%lld dist=%Lf\n",x,y,dist);
return 0;
}
You were getting wrong output(-nan when I tried out ) because the expression value inside sqrt() was overflowing and becoming negative. (132000-y)*(132000-y) won't fit in an integer range and give negative value. Since, negative square root is not defined, sqrt() returned nan as the result. Changing the type of y to long long int will solve the error.
Hope this helps !
I am writing a simple C program that prints the sum of the harmonic series like:
1 + 1/2 + 1/3 + ...... + 1/n
My program is:
#include<stdio.h>
void main(){
int n,i;
float num=0, tmp=0;
scanf("%d", &n);
for(i=1;i<=n;i++){
tmp = 1/i;
num = num + tmp;
}
printf("%f", num);
}
As per my understanding, if n=4, the output must be: 2.03333
But the output comes as 1.00000
1/i is not a float value, it is an int of value 0 or 1, because of integer division rules.
The value is 1 for 1/1 otherwise 0. Adding all of them up results in 1.
This is a floating point value:
1.0/i
Adding them up should give the desired result.
To be precise, I should say that 1.0 is a double.
In order to work with actually a float value to match the float variable, use 1.0f. Both are floating point values in contrast to integer values.
As 1 and i are integers, dividing these two will be integer and you will get 0 for i > 1 in this case. You can cast i using (float)i and you will have 1/(float)i.
I have to write a program which calculates and displays the following expression:
P=(1/2-3/4)*(5/6-7/8)*...*[n/(n-1) - (n+2)/(n+3)]
I did this and I ran it and it shows no errors. When I run it, for every value that I enter, it shows P=0. What is wrong?
#include <stdio.h>
int main (void)
{
float s, p=1.0;
int i, n;
printf("Enter a number:");
scanf("%d", &n);
for (i=1;i<=n;++i) {
p*=((i)/(i+1)-(i+2)/(i+3));
}
printf("p=%f\n", p);
}
You're using integer arithmetic rather than floating point, so the division operations are being truncated.
Change:
p*=((i)/(i+1)-(i+2)/(i+3));
to:
p*=((float)(i)/(i+1)-(float)(i+2)/(i+3));
It is not working because i is integer: when you divide (i)/(i+1) or (i+2)/(i+3), you get a zero in both cases, because when i is positive, denominator is greater than numerator. Zero minus zero is zero, p times zero is zero, so that's what you get.
To fix this problem, declare i as float:
#include <stdio.h>
int main ()
{
float s,p=1.0, i;
int n;
printf("Put a number:");
scanf("%d",&n);
for (i=1;i<=n;++i) {
p*=((i)/(i+1)-(i+2)/(i+3));
}
printf("\n p=%f",p);
return 0;
}
You are using int variables, not float variables, thus i/(i+1) and (i+2)/(i+3) will always evaluate to 0 since it's integer division.
Just use some casts to force the evaluation using floating point arithmetics:
((float)i)/(i+1)-((float)i+2)/(i+3)
Your problem is that integer division truncates rather than returning the floating point results you appear to be looking for:
i/(i+1)
and
(i+2)/(i+3)
are always 0 in C.
I was writing this code in C when I encountered the following problem.
#include <stdio.h>
int main()
{
int i=2;
int j=3;
int k,l;
float a,b;
k=i/j*j;
l=j/i*i;
a=i/j*j;
b=j/i*i;
printf("%d %d %f %f\n",k,l,a,b);
return 0;
}
Can anyone tell me why the code is returning zero for the first and third variables (k and a)?
Are you asking why k and a show up as zero? This is because in integer division 2/3 = 0 (the fractional part is truncated).
What I think you are experiencing is integer arithmetic. You correctly suppose l and b to be 2, but incorrectly assume that k and a will be 3 because it's the same operation. But it's not, it's integer arithmetic (rather than floating-point arithmetic). So when you do i / j (please consider using some whitespace), 2 / 3 = 0.33333... which is cast to an int and thus becomes 0. Then we multiply by 3 again, and 0 * 3 = 0.
If you change i and j to be floats (or pepper your math with (float) casts), this will do what you expect.
You haven't said what you're getting or what you expect, but in this case it's probably easy to guess. When you do 'a=i/j*j', you're expecting the result to be roughly .2222 (i.e. 2/9), but instead you're getting 0.0. This is because i and j are both int's, so the multiplication and (crucially) division are done in integer math, yielding 0. You assign the result to a float, so that 0 is then converted to 0.0f.
To fix it, convert at least one operand to floating point BEFORE the division: a = (float)i/j*j);
this is due to how the c compiler treats int in divisions:
#include <stdio.h>
int main()
{
int i=2;
int j=3;
int k,l;
float a,b;
k=i/j*j; // k = (2/3)*3=0*3=0
l=j/i*i; // l = (3/2)*2=1*2=2
a=i/j*j; // same as k
b=j/i*i; // same as b
printf("%d %d %f %f/n",k,l,a,b);
return 0;
}
If you're asking why k and a are 0: i/j*j is the same as (i/j)*j. Since j is larger than i, i/j is 0 (integer division). 0*j is still 0, so the result (k) is 0. The same applies to the value of a.
it doesn’t matter if you’re variable is float or not, as long you’re using
integer / integer , you’ll get 0,
but because you’re using a float output, you get 0.0