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.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 12 months ago.
Improve this question
#include <stdio.h>
#include <math.h>
double areaTriangle(double sideA,double sideB,double sideC);
double periTriangle(double sideA,double sideB,double sideC);
int main (void){
double side1A,side1B,side1C;
double side2A,side2B,side2C;
double area1, area2;
printf("Enter the side for the first triangle: ");
scanf("%lf %lf %lf",&side1A,&side1B,&side1C);
printf("\nEnter the sides for the second triangle: ");
scanf("%lf %lf %lf",&side2A,&side2B,&side2C);
area1=areaTriangle(side1A,side1B,side1C);
area2=areaTriangle(side2A,side2B,side2C);
printf("Area 1 is %lf and Area 2 is %f",area1,area2);
return 0;
}
double areaTriangle(double sideA,double sideB,double sideC){
double area=0;
periTriangle(sideA,sideB,sideC);
sqrt(periTriangle*(periTriangle-sideA)*(periTriangle-sideB)*(periTriangle-sideC);
}
double periTriangle(double sideA,double sideB,double sideC){
periTriangle=(sideA+sideB+sideC)/3;
return periTriangle;
}
So when I type in any values for any of the triangles I get zero no what I do. For example, I input 10 for all sides in both triangles and all I get is: Area 1 is 0.000000 and Area 2 is 0.000000. Why tho?
periTriangle(sideA,sideB,sideC);
does not save the return value;
It could be double peri = periTriangle(sideA,sideB,sideC);
(and below that use peri where you used periTriangle)
Also, the parens don't match where you call sqrt.
Also, I think you should divide by 2, not 3 in periTriangle, no?
Heron's Formula for Area of a Triangle with three sides known.
This question already has answers here:
Why printf() isn't outputting this integer as float number? [duplicate]
(8 answers)
Closed 4 years ago.
Why does the following code output 0 instead of 40?
#include <stdio.h>
int main()
{
int volume;
int length = 5;
int width = 8;
volume = length * width;
printf("%f", volume);
return 0;
}
The variable "volume" is Integer , you need in the printf function to change from %f that is for float variable, to %d that is for print Integer variable.
Declaration of a variable is int volume; and you are printing it by format specifier %f which belongs to float type of variable. Type casting requires (float)volume. In C programming it happens a lot because compiler dependency comes in picture.
This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 6 years ago.
I'm come from Java, i want to improve my skill in coding and knowledge of how it's work in deep and i figure that the best language for this is C the mother of all. I'm very excited about how it work c but now please raise me a doubt. Why in C first code don't work and the second yes?
P.s.: I'll skip few steps to speed the code and focus on problem. I'm study C99.
int a,b,c;
int sum = a+b+c;
print scanf ecc...
printf("%d", sum);
The result it will be -1234567 ecc..
And using this code it will work wonderful, this is the mean of a imperative programming?
int a,b,c;
int sum;
print scanf ecc...
sum = a+b+c;
printf("%d", sum);
Sorry for bad english is not my first language, i will improve also that :°D
When you use the first part of the code i.e.
int a,b,c;
int sum = a+b+c;
print scanf ecc...
printf("%d", sum);
it will first add the a ,b , c
and then will produce result with garbage value
while in second case
int a,b,c;
int sum;
print scanf ecc...
sum = a+b+c;
printf("%d", sum);
it will read the values by using the scanf and then add those values so will not take the garbage value and produce a wonderful result
Local variables are not initialized in C, their values are indeterminate. Using an uninitialized local variable leads to undefined behavior.
C is also, exactly like Java, sequential in the absence of loops or gotos. Statements are executed from top to bottom so calling scanf to initialize a variable after you used it will not work. The previous operation will not be redone.
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.
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);
}