Adding float values in c [closed] - c

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 7 years ago.
Improve this question
Why does f is 0.0 even after adding 0.1?
#include <stdio.h>
int main()
{
float f=0.0f;
f = f + 0.1f;
printf("f %f \n",&f);
return 0;
}
Sorry for that I messed up with original question
Why these two values are not ? Is this because of precision.
Sorry I have to ask this question here because I'm blocked I can't ask more questions
#include <stdio.h>
int main()
{
float f=0.0f;
int i;
for(i=0;i<10;i++)
{f = f + 0.1f; }
if(f == 1.0f)
printf("f is 1.0 \n");
else
printf("f %f is NOT 1.0\n",f);
return 0;
}

printf("f %f \n",f);
Gives correct output. See here-https://ideone.com/158Zbv
To print address printf(" %p \n",&f); will do it .
Where as your printf statement will give undefined behaviour.
About your second code -
You can re-write your if condition like this -
if(f>0.99f && f<1.01f)
So that it gives you correct output- https://ideone.com/kARx3A
EDIT
While adding in your program value of f is not exactly 1.0f but it may have some different value ,when we see its value with more decimal places it is bit different than 1.0f that's why your code always go to else part .
See here what value f has with every iteration-https://ideone.com/wJ7u1R

you are printing address of variable f by using & operator, remove this & operator.

Related

C code doesnt show anything on the console [closed]

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 8 months ago.
Improve this question
I was trying to multiply a double and an int but it shows nothing.
My code is:
#include <stdio.h>
int main()
{
double vergi = 0.01;
int fiyat = 20;
double sonuc = fiyat * vergi;
printf("%s", sonuc);
}
The %s specifier is for a String. As per the doc says, you should use the %f specifier in printf for a double result.
Wrong conversion type in printf.
You want to print variable which type is double.
This is correct - printf("%lf\n", sonuc);

Calculating n! and x^n and proceeding the calculation [closed]

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 3 years ago.
Improve this question
Write a C program to calculate (x^n)/(n!) where x is a floating point number and n is an integer greater than or equal to zero.
I coded the following:
#include<stdio.h>
#include<math.h>
void main()
{
float x,p;
int i,n,f=1;
printf("Enter the value of x,n\n");
scanf("%d %d",&x,&n);
if(n>0)
{
for(i=1;i<=n;i++)
{
f=f*i;
}
p=(float)pow(x,n)/f;
printf("The value of p is %.3f",p);
}
if(n==0)
{
p=(float)pow(x,n)/1;
printf("The value of p is %d",p);
}
getch();
}
But this is not running well. Where have I gone wrong?
PS: Edit
In your question I have recognize 3 problems.
main problem is scanf("%d %d",&x,&n); should be change into scanf("%f %d",&x,&n);
because x is `float type #dragosht has mentioned it.
printf("The value of p is %d",p); should be correct as printf("The value of p is %f",p); beacause p is also float type.
It is better to set p = 0; at the beginning because you did not assign value to p using keyboard. There for some times you will get corrupted values because of this.

Guessing Game - Can't figure out why it produces a large negative integer [closed]

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 4 years ago.
Improve this question
Cheers guys. I am having trouble with this code I produced. It functions everything fine but when it prints the number when I guessed correctly, it prints a number like -3529583 which I don't understand. Shed some light?
#include <stdio.h>
#include <ctype.h>
#include <time.h>
int main()
{
int y, iRandomNum1; // Declare the three variables
y = 0;
iRandomNum1 = 0;
srand(time(NULL)); // Randomize function
iRandomNum1 = rand() % 10; // Randomize and collect 1 to 10 Value
while (iRandomNum1 != y) {
printf("Guess a number from 1 to 10\n");
scanf("%d", &y);
}
printf("\nCorrect Guess! Congrats! The answer is %d.\n", &y);
return 0;
}
You are display the address of the variable &y instead of the variable itself in your printf just remove the & symbol and it should be ok
https://stackoverflow.com/users/2173917/sourav-ghosh had the answer before me in comment

no answer from simple subtraction code in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
i wrote code, and it gives me answers like 0.00 and 1.00, not actual math answer. Where i made mistake? (im begginer, dont scream on me :) )
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
float x;// score
// Request 1
printf("Zadanie 1(12)\n");
// Calculate the square root of the given interval
printf("Oblicz pierwiastek rownania w podanym przedziale\n");
// Give me a number a
printf("Podaj liczbe a:\n");
scanf("%d",&a);
// Give me a number b
printf("Podaj liczbe b:\n");
scanf("%d", &b);
x=&a-&b;
//answer
printf("%f", x);
system("PAUSE");
return 0;
}
x = a - b
not
x = &a - &b
Explanation: The & operator gives you the memory address of a, which you need to give to scanf, so that it can place data there. But you do math on the actual value of a.. which is just a.
because of pointer arithmetic:
x = &a - &b;
computes the distance between the addresses of a and b, which are probably close since they're auto variables of the same type declared close-by. My guess is that you could get 1 or -1 (or any other integer value but not 0 since a and b are located in 2 separate addresses) then put in a float.
(the difference of addresses of 2 consecutive integers is 1, independently of the size of the integer)
You need of course to do:
x = a - b;
You've been misled by the fact that scanf needs a pointer on a or b to be able to write a value when reading the input.

Unable to understand the meaning of errors in C language using Turbo C. I've coded this code [closed]

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 6 years ago.
Improve this question
This is what I've coded - the exchange of 2 numbers in c language using turbo c. I've tried real hard to google the solution but the explanation is not understandable as I'm a mere beginner. I'm having my internals on 20th and this is what they might ask along with 40 other programs.....
You should write printf("please enter"); instead of printf{"please enter"};
The same with scanf.
When you see Statement missing ; error, you definitely have an language error.
Functions take arguments between () and {} are used for blocks of code. For example:
if (1) {
printf("Printing\n");
printf("Printing again\n");
}
Another thing is, that we assign value to the left side of =.
a = a-b
a = a+b
Your code should be:
#include<stdio.h>
void main() {
int a,b;
printf("Enter two (2) numbers, please\n");
scanf("%d%d", &a, &b);
a = a+b; //assign a to the sum of current a and b
b = a-b;
a = a-b;
printf("The numbers a, b after some calculations are: a=%d and b=%d\n", a, b);
}
Use parenthesis () instead of braces {} in your functions printf() , scanf().
printf("please enter 2 nos");
scanf("%d %d" &a,&b);
printf("our exchanged numbers are a = %d" and b=%d" a,b);
And you can't assign value to a expression, it will give lvalue error. So to remove this error change,
a-b = a change it to a=a-b and change all the expressions.

Resources