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);
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.
My code shows me a wrong output by showing the area to be zero:
#include <stdio.h>
double calc(float rad);
void main(void) {
float rad;
printf("Enter the circle radius: \n");
scanf("%f",&rad);
printf("You entered: %f\n",rad);
printf("Area is %d\n",calc(rad));
if(calc(rad)>1000){
printf("Area is > 1000");
}
else{
printf("Area is < 1000");
}
}
double calc(float rad){
double area=3.14*rad*rad;
return area;
}
Output:
Enter the circle radius : 20
you entered 20.000000
area is 0
area > 1000
Desired output:
Enter the circle radius : 20
you entered 20.000000
area is 1256.000000
area > 1000
The erroneous output is as a result of using %d instead of %lf for double or %f for float in your printf statement. Turning on compiler warning -Wformat will highlight this during compilation
#include <stdio.h>
double calc(float rad);
void main(void) {
float rad;
printf("Enter the circle radius: \n");
scanf("%f",&rad);
printf("You entered: %d\n",rad);
printf("Area is %f\n",calc(rad));
if(calc(rad)>1000){
printf("Area is > 1000");
}
else{
printf("Area is < 1000");
}
}
double calc(float rad){
double area=3.14*rad*rad;
return area;
}
This is the correct code.
if you want more information :
https://codeforwin.org/2015/05/list-of-all-format-specifiers-in-c-programming.html
https://www.cplusplus.com/reference/cstdio/printf/
https://aticleworld.com/format-specifiers-in-c/
Please go to the last link first.I am sorry for any bad english.
i am not getting the required result, instead i am getting garbage value
please help me finding mistake in code
CODE:
#include <stdio.h>
int main(){
int radius;
float pi=3.14;
printf("Type radius of circle \n");
scanf("%d", &radius);
printf("Area of circle is %f \n", pi*radius*radius );
return 0;}
The question is that I have to prompt the user to enter the base and height of a triangle as a float, pass it too a function where the function will get the area of the triangle, return it to main. the problem is that the output of the area is 0.000000.
its also giving me a warning
Severity Code Description Project File Line Suppression State
Warning C4477 'printf' : format string '%f' requires an argument of type 'double', but variadic argument 1 has type 'float (__cdecl *)(float,float)' line 38.
What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
float area(float base,float height);
int main()
{
float height;
printf("Enter an height: ");
scanf_s("%f", &height);
printf("Number = %f", height);
float base;
printf("Enter an base: ");
scanf_s("%f", &base);
printf("Number = %f", base);
area(height, base);
printf("area of triangle : %f\n", area);
return 0;
}
float area(float base, float height)
{
float half = .5;
float area = half * base * height;
return area;
}
Your primary issue is that you're passing a function (area), instead of the result of function invocation (area(height, base)). You need to store the result to a variable, then print that variable.
float computedArea = area(height, base);
printf("area of triangle : %f\n", computedArea);
Or you can just call the function, in-place, which work in this case, because it doesn't make the line too long:
printf("area of triangle : %f\n", area(height, base));
Here's how I would write this code:
#include <stdio.h>
#include <stdlib.h>
double area(double base,double height);
int main() {
printf("Enter the height: ");
double height;
scanf("%lf", &height);
printf("Height: %f\n", height);
printf("Enter the base: ");
double base;
scanf("%lf", &base);
printf("Base: %f\n", base);
double computedArea = area(height, base);
printf("Triangle Area: %f\n", computedArea);
return 0;
}
double area(double base, double height) {
return (base * height) / 2.0;
}
Change
area(height, base); // invoking a function without capturing its output
printf("area of triangle : %f\n", area); // area refers to the memory location where the function resides.
to
printf("area of triangle : %.2f\n", area(height, base));
// Directly passing the area output to printf. The '.2' specifies the precision you want
First, I've given a task to implement this with structs only.
I need to check whether a point is inside/outside a circle.
Input: Coordinates of the point, center of circle, radius.
Output: Is the point inside/outside the circle.
Well, I need to use the distance formula d = sqrt( (x_1-x_2)^2 + (y_1 - y_2)^2 ) and then check if it is bigger/smaller/equals to the radius.
I know the logic, but I fail with struct's syntax. can you guys help me?
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct {
float x;
float y;
}Point;
typedef struct {
Point center;
float radius;
}Circle;
int main()
{
Point Coordinates;
Coordinates.x = 0; //Is this initialization necessary?
Coordinates.y = 0; //Is this initialization necessary?
Circle insideCircle;
float distance;
printf("Please enter the coordinates of your point: ");
scanf("%f %f", Coordinates.x, Coordinates.y); //after input, throws error.
printf("Please enter your center coordinate and your radius: ");
scanf("%f %f", insideCircle.radius, insideCircle.center.x, insideCircle.center.y);
printf("%f %f %f %f %f", Coordinates.x, Coordinates.y, insideCircle.radius, insideCircle.center.x, insideCircle.center.y);
//More code for checking if distance > or < or = to radius to be added.
getch();
}
For scanf(), you need to pass the address of the variables as the argument to the supplied conversion specifiers, like
scanf("%f %f", &(Coordinates.x), &(Coordinates.y));
^ ^
and for the other usage(s).
That said, it is essential that you check the return value of scanf() call to ensure the success.
/*i think this piece of code will help you*/
#include <stdio.h>
#include <stdlib.h>
typedef struct {
float x;
float y;
}Point;
typedef struct {
Point center;
float radius;
}Circle;
int main()
{
Point Coordinates;
Coordinates.x = 0;
Coordinates.y = 0;
Circle insideCircle;
float distance;
printf("Please enter the coordinates of your point: ");
scanf("%f %f", &(Coordinates.x), &(Coordinates.y) ); //scanf requires &
printf("Please enter your center coordinate and your radius: ");
scanf("%f %f %f", &(insideCircle.radius), &(insideCircle.center.x), &(insideCircle.center.y) );//scanf requires &
printf("%f %f %f %f %f", Coordinates.x, Coordinates.y, insideCircle.radius, insideCircle.center.x, insideCircle.center.y);
//More code for checking if distance > or < or = to radius to be added.
}