This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
This program is supposed to input a number and its square value, then tell me if right or wrong. I have some problems but I can't figure out what they are.
#include <stdio.h>
#include <math.h>
int main()
{
float P;
float q;
float r;
printf("Enter the value of p\n");
scanf("%f",p);
q= p*p;
printf("Enter the square value of %f \n",p);
scanf("%f",r);
if (r = q){
printf("You are right\n");
}
else{
printf("you are wrong\n");
}
return 0;
}
so tell me my mistakes
Please compile the program with flags -Werror -Wall -Wextra although the first mistake is always a compilation error (typo): replace float P; with float p; because C is case-sensitive.
Then you need to pass the address of a variable to scanf, these two lines
scanf("%f",r);
...
scanf("%f",p);
should be
scanf("%f",&r);
...
scanf("%f",&p);
Lastly, there is a syntax error where you test for equality with
if (r = q)
but this changes r and tests if it is non-0. With an integer type you should use
if (r == q)
but with floating point types, equality tests don't work well, please see why in this question.
Related
This question already has answers here:
Non-static variable initialization
(7 answers)
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed last month.
I have typed out a simple program counting the number of positive divisors of a number in C, which works in CodeBlocks GCC compiler but not in the online compiler provided by the school, where it prints out some numbers close to 22k (22019 21873) etc after some inputs.
For example, for input 10 the program prints out 4 which the number of divisors, but in the online compiler it prints out 22019. I noticed when i changed the order of some conditions in the if statements from if (n<0) to if (0>n) the numbers also changed (the 22019 became 22xxx) to numbers different from the first time. Here's the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c, n, res = 0;
int i = 1;
scanf("%d", &n);
if (0>n)
printf("Wrong Input");
else {
while (n>=i)
{
res = n%i;
if (res == 0) {
c++;
i++;
}
else {
i++;
}
}
printf("%d", c);
}
return 0;
}
This question already has answers here:
Reading in double values with scanf in c
(7 answers)
Closed 5 years ago.
I am trying to get this code to run, i have tried these 2 ways but to no avail. The aim is to receive an input using scanf and print out the statement using printf. Error message i get is the Compiler just hangs.
Also, i am not to use any math functions such as square root or power
Test Cases and Expected answers
Input1: 1.41421356237
Output1: Area of the circle is 3.141590
Input2: 5.65
Output2: Area of the circle is 50.143703
Trial 1
#include <stdio.h>
double function(double a){
double area;
area = 3.14159*((a/2)*(a/2)*2);
return area;
}
int main(void){
double a;
scanf("%f",a);
double result = function(a);
printf("Area of the circle is %f\n",result);
return 0;
}
Trial 2
#include <stdio.h>
int main(void){
scanf("%f",a);
area = 3.14159*((a/2)*(a/2)*2);
printf("Area of the circle is %f\n",area);
return 0;
}
would appreciate any help, not sure why the function is not working. Thank you for your time.
In your first trial you need to change your scanf("%f",a); line to:
scanf("%lf",&a);
because correct type specifier for double is %lf which stands for "long float". Also you need to send &a as argument because scanf expects an address and not a value. Same thing with second trial.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
#include <stdio.h>
#include <math.h>
main()
{
float i;
float x,N,sum;
printf("enter x and N respectively");
scanf ("%f %f", &x, &N);
sum = 0;
for (i=1;i<=N;i++){
sum = sum + ((pow(x,i))/(fact(i)));
}
printf ("%f", sum);
}
int fact(int n){
int i,temp;
temp = 1;
for (i=1;i<=n;i++){
temp = temp*i;
return temp;
}
}
this is to print the summation of the terms accordingly. I tried defining fact inside main but there was some control flow warning and I tried the same outside this time, yet wrong answer. Any help?
You cannot (you are forbidden by the C99 or C11 standard) define a function (like fact) inside another one (like main).
However, some C compilers, in particular GCC accept as an extension to have nested functions.
(I don't recommend using that extension, in particular if you are newbie in C)
Of course you'll better declare
int fact(int n);
before your main and leave its definition after.
Your code is wrong (in particular, better define main as int main(int argc, char**argv) then learn perhaps about getopt and use it). Compile it with all warnings & debug info (e.g. gcc -Wall -Wextra -g homework.c -o binaryprog...) then use a debugger (e.g. gdb ./binaryprog)
You need to add int fact(int n); before main() function to tell the compiler that a function called fact exists or you can add the whole function definition before.
You have to declare fact() above of main. There are 2 ways to do it.
First way: Add int fact(int n); above of main()
Second way: Copy the whole function above main(). So it looks like:
#include <stdio.h>
#include <math.h>
int fact(int n)
{
int i,temp;
temp = 1;
for (i=1;i<=n;i++){
temp = temp*i;
return temp;
}
}
main()
{
float i;
float x,N,sum;
printf("enter x and N respectively");
scanf ("%f %f", &x, &N);
sum = 0;
for (i=1;i<=N;i++){
sum = sum + ((pow(x,i))/(fact(i)));
}
printf ("%f", sum);
}
This question already has answers here:
Floating point comparison [duplicate]
(5 answers)
Closed 8 years ago.
1)#include<stdio.h>
int main()
{
float x=0.5;
if(x>0.5)
printf("\ngreater");
else
printf("\nlesser ");
return 0;
}
output->lesser
2)#include<stdio.h>
int main()
{
float x=0.1;
if(x>0.1)
printf("\ngreater ");
else
printf("\nlesser ");
return 0;
}
output->greater
Why in the first case output is "lesser" while in the second one output is "greater"? What is the difference?
EDIT: I understood that 0.1 is not equal, but then why 0.5 is showing as equal?
I almost sure it's because you are comparing float and double.
There's an answer to why it is greater answered here : 0.1 float greater than double
This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 5 years ago.
Please do flog me for this really basic question, but I still can not get why this happen.
I read that for printing several value behind coma, I should use %.f in C.
So I have this problem, counting 90/100. I expect to print 0.9
#include <stdio.h>
#include <math.h>
int main()
{
double c=0;
c = 90/100;
printf("%.1f\n", c);
}
And it shows me 0.0 ..(err..) . tried to change it into (printf("%f\n",c)) return me with 0.00000.. (err..)
Can anyone help me with this? (sorry, really new in programming..)
Thank you
The problem is that you are doing integer division. 90/100 = 0 in integer terms.
If you want to get 0.9, do : 90.0/100.0
The problem is at
c = 90/100;
Although, it will be assigned to a double data type, but the computation itself is all integer and that's why the value is 0.0.
Try,
c = 90.0/100;
it is integer division, do:
c = 90.0/100;
c = (float)90/100;
you need to make atleast one operant a double to evaluate the whole equation as double
You are dividing two integers, thus the result is an integer too. Try the following
#include <stdio.h>
#include <math.h>
int main()
{
double c=0;
c = 90.0/100;
printf("%.1f\n", c);
}