This question already has answers here:
Reading in double values with scanf in c
(7 answers)
Closed 4 years ago.
I have a piece of code in C, which is supposed to compute the circumference.
No matter what I put in for variable Z when asked for it, it always prints 0.000000
Any ideas?
#include <stdio.h>
int main()
{
double pi = 3.1415926;
double z = 0.0;
printf("What is the radius of the circle? \n ");
scanf("%1f", &z);
double c = 2.0 * pi * z;
printf("The circumference is %1f", c);
return 0;
}
Change %1f to %lf.
Like so:
#include <stdio.h>
int main()
{
double pi = 3.1415926;
double z = 0.0;
printf("What is the radius of the circle? \n ");
scanf("%lf", &z);
double c = 2.0 * pi * z;
printf("The circumference is %lf", c);
return 0;
}
For reading into z, a double, you have to use scanf("%lf", &z) instead of "%1f".
You were very close. try this
#include <stdio.h>
int main()
{
double pi = 3.1415926;
double z = 0.0;
printf("What is the radius of the circle? \n ");
scanf("%1f", &z);
double c = 2.0 * pi * z;
printf("The circumference is %.1f", c);
return 0;
}
your logic is telling the float that it needs a whole number, but no decimals afterwards
Related
#include <stdio.h>
int a, b, h, area, perimeter;
float pi;
void rectangle() {
printf("enter rectangular base length");
scanf("%d", &a);
printf("enter rectangular base height\n");
scanf("%d", &b);
area = a * b;
perimeter = (a + b) * 2;
printf("area of a rectangle = %d\n", area);
printf("perimeter of a rectangle = %d", perimeter);
}
void circle() {
pi = 3.14;
printf("enter the length of the circle radius\n");
scanf("%d", &a);
area = pi * a * a;
perimeter = 2 * pi * a;
printf("area of the circle = %d\n", area); //I want this part to conversion from int to float
printf("perimeter of the circle = %d", perimeter);
}
int main() {
printf("Choose which way you will operate\n");
printf("circle=1\nrectangle=2\n");
scanf("%d", &h);
switch (h) {
case 1:
printf("you chose a circle\n");
circle();
break;
case 2:
printf("you chose a rectangle\n");
rectangle();
break;
}
}
As I wrote in the code, I want the part I specified to be converted from int to float. How can I do it?
I was know like that but it didn't work -> (float)area = a * b;
You would probably want to define PI as a constant. Prefer to minimize scope of variables (now local to the function that needs them). Use floating point types (float, double etc) when needed to store fractional values with more than integer precision. Make sure you use the %f to print them (optionally, specify how many digits you want to see here %.1f means 1 fractional digit):
#define PI 3.14
void circle() {
printf("enter the length of the circle radius\n");
float a;
scanf("%f", &a);
float area = PI * a * a;
float perimeter = 2 * pi * a;
printf("area of the circle = %.1f\n", area);
printf("perimeter of the circle = %.1f", perimeter);
}
I found that your code here:
area = pi * a * a;
where "pi" you defined it as a float constant, so compiler will warn at this line something like "assign float to a integer", you should know that no matter how many integers in a formula, even one float number in it, the type of final value will be automatically converted to float
means the result of:
pi * a * a
will be a float (or double) value. But you assign this value to a int variable "area", so that value then cast to int.So you should declare area as:
float area; // or double area
By your comment in this line:
printf("area of the circle = %d\n", area); //I want this part to conversion from int to float
I guess you want to print a number with decimal part.
You can try this:
printf("area of the circle = %.3f\n", area);
".3" after "%" means print 3 decimal digits, or to say the precision is 3; "f" after ".3" means print argument 1 as a float number.
Reference Image
The output always comes: 6.35 (I think it's a garbage value)
the code is
#include<stdio.h>
#include<math.h>
float formula(float,float);
int main()
{
float l=0,a=0;
printf("enter the length of the pendulum(l)\n");
scanf("%f",&l);
printf("Enter the angle of displacemnt(a)\n");
scanf("%f",&a);
printf("the length is %0.2f\n",l);
printf("the angle of displacemnt is %0.2f\n",a);
printf("the period of pendulum is %0.2f",formula(l,a));
return 0;
}
float formula(float l, float a)
{
float P=0,ran=0,g = 9.8;
ran = (l/g) * (1 + ((1/4)*(pow((float)(sin((double)a/2)),2))));
P = 2 * M_PI * ((float)sqrt((double)ran));
return P;
}
I don't know what is happening 😐
You can simplify this implementation by using doubles or floats exclusively. The 1/4 integer division yields 0. Here's an example:
#include <stdio.h>
#include <math.h>
float formula(float l, float a)
{
float ran = l / 9.8f * (1.0f + powf(sinf(a/2.0f), 2.0f) / 4.0f);
return 2.0f * (float) M_PI * sqrtf(ran);
}
int main(void)
{
float l;
float a;
printf("Enter the length of the pendulum(l): ");
scanf("%f", &l);
printf("Enter the angle of displacemnt(a): ");
scanf("%f", &a);
printf("The length is %0.2f\n", l);
printf("The angle of displacemnt is %0.2f\n", a);
printf("The period of pendulum is %0.2f\n", formula(l,a));
return 0;
}
I'm new to C language and coding and I encountered a question asking me to change the function header of:
float RealRoot_1(float a, float b, float c);
float RealRoot_2(float a,float b,float c);
to become:
void RealRoot_1(void);
void RealRoot_2(void);
I was told that it has something to do with Global Variables but I still couldn't figure it out after trying quite some time. Can anyone please explain on how to do it? Thanks a lot.
The source file is as below:
#include<stdio.h>
#include<math.h>
int main()
{
float RealRoot_1(float a, float b, float c); // Prototype declaration
float RealRoot_2(float a, float b, float c);
// Defining Input Variables
float x, y, z;
// Defining Output Variables
float Root_1, Root_2;
printf("Please enter the factor of X^2: ");
scanf("%f",&x);
printf("Please enter the factor of X: ");
scanf("%f",&y);
printf("Please enter the free factor: ");
scanf("%f",&z);
Root_1 = RealRoot_1(x,y,z);
Root_2 = RealRoot_2(x,y,z);
printf("the First Root is: %f \n", Root_1);
printf("the Second Root is: %f \n", Root_2);
system("pause");
}
float RealRoot_1(float a, float b, float c)
{
float x;
x = (-1*b + sqrt(pow(b,2) - 4 * a * c)) / (2 * a);
return x;
}
float RealRoot_2(float a, float b, float c)
{
float x;
x = (-1*b - sqrt(pow(b,2) - 4 * a * c)) / (2 * a);
return x;
}
This can be done by using global variables. You need to ensure that the variable names used in the function are the same as the ones used in the main code.
#include<stdio.h>
#include<math.h>
void RealRoot_1(void); // Prototype declaration
void RealRoot_2(void);
float x, y, z;
float Root_1, Root_2;
int main()
{
// Defining Output Variables
printf("Please enter the factor of X^2: ");
scanf("%f",&x);
printf("Please enter the factor of X: ");
scanf("%f",&y);
printf("Please enter the free factor: ");
scanf("%f",&z);
RealRoot_1();
RealRoot_2();
printf("the First Root is: %f \n", Root_1);
printf("the Second Root is: %f \n", Root_2);
system("pause");
}
void RealRoot_1(void)
{
Root_1 = (-1*y + sqrt(pow(y,2) - 4 * x * z)) / (2 * x);
}
void RealRoot_2(void)
{
Root_2 = (-1*y - sqrt(pow(y,2) - 4 * x * z)) / (2 * x);
}
Please note that this is a worse way of doing things than was given in the initial problem. In the initial exercise. You are loosing modularity and using too many globals is in general a bad idea.
You can also see Are global variables bad?
This should be self explanatory:
float RR_a, RR_b, RR_c;
float RR_d; // store result here(like a return value)
void RealRoot_1(void); // prototypes
void RealRoot_2(void);
void main(void)
{
printf("Please enter the factor of X^2: ");
scanf("%f",&RR_a);
printf("Please enter the factor of X: ");
scanf("%f",&RR_b);
printf("Please enter the free factor: ");
scanf("%f",&RR_c);
RealRoot_1();
printf("the First Root is: %f \n", RR_d);
RealRoot_2();
printf("the Second Root is: %f \n", RR_d);
system("pause");
}
void RealRoot_1(void)
{
float x;
x = (-1*RR_b + sqrt(pow(RR_b,2) - 4 * RR_a * RR_c)) / (2 * RR_a);
RR_d = x;
}
void RealRoot_2(void)
{
float x;
x = (-1*RR_b - sqrt(pow(RR_b,2) - 4 * RR_a * RR_c)) / (2 * RR_a);
RR_d = x;
}
Notice that after calling RealRoot_1 we now print the result before calling RealRoot_2. That's because the result of RealRoot_1 which is stored in RR_d is overwritten by RealRoot_2, thus it is lost.
You can circumvent this by declaring a second return variable, RR_d_2 and storing the result of RealRoot_2 in it.
We do not need duplicates for RR_a, RR_b or RR_c because their values are not modified within the functions.
This way of writing functions has limitations, which will be obvious when faced with recursion or multi-threading.
I am trying to input a decimal in the program, but it is giving me a different output. I have used %5.2f, float and double, and it still not working. I want to calculate the diameter, area and circumference of a circle, but the output is different.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
int main(int argc, char**argv)
{
double radius;
double pi = 3.14;
double diameter;
double area;
double circumference;
printf("\t\tEnter value for radius: ");
scanf_s("%5.2f", &radius);
printf("radius is %5.2f", radius);
diameter = radius*2;
area = pi*(radius*radius);
circumference = 2*(pi*radius);
printf("\n\t\tThe diameter of the circle is %d \n", diameter);
printf("\t\tArea: %d", area);
printf("\t\tCircumference: %d", circumference);
getch();
return 0;
}
please tell me my mistakes,
Also, I tried using M_PI to have a mathematical constant, but it does not work.
The correct input format specifier for a double is %lf (or %lg or %le).
scanf_s("%lf", &radius);
When you print it again, you can format it however you want.
printf("radius is %5.2f", radius);
Is this the correct way to write the normal distribution function http://en.wikipedia.org/wiki/Normal_distribution or should I be using the pow function? I am really confused so help would be greatly appreciated :)
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdlib.h>
int main()
{
double u,s, N, x1,math1, math2, math3,n, v, x;
printf("Enter Mean: ");
scanf("%lf", &u);
printf("Enter Standard Deviation: ");
scanf("%lf", &s);
printf("Enter Number Of Inputs: ");
scanf("%lf", &N);
for (v=1; v<=N; v++)
{
printf("Enter Value: ");
scanf("%lf", &x);
n=(-1/2);
printf("f(x)= ");
math1 =1/(s*sqrt(2*M_PI));
math2= (x-u)/s * (x-u)/s;
math3= M_E * exp(n);
x1 = math1 * exp(math3)*exp(math2);
printf("%lf \n", x1);
}
system("Pause");
}
It's too hard to read your code, but I can tell it's wrong. Here is a short version:
double twopi = 8.0 * atan(1.0); // preferable to using M_PI
double x = ..., sigma = ..., mu = ...;
double y = (1.0 / (sigma * sqrt(twopi))) *
exp(-(x - mu)*(x - mu) / (2.0 * sigma * sigma));
Notice how I translate the mathematical formula directly to an expression in C... this makes it easy to verify that the code is correct. It's a bit harder when you use a bunch of temporary variables math1, math2, math3...
Remember: exp() is the same thing as its counterpart in mathematics, exp. So exp(x) is ex. Once you realize this, you will see the errors in your code.