I am making a triangle area calculator, but it only show 0 instead of answer, It must be the formula 1/2 * ab sin c , can someone tell me what should I change to make it work.
#include <stdio.h>
#include <math.h>
float main ()
{
float a,b,c,pi ;
printf("Enter a : ");
scanf("%f",&a);
printf("Enter b : ");
scanf("%f",&b);
printf("Enter c :");
scanf("%f",&c);
printf("%f\n",(a*(1/2)*b*(c*pi)/180));
return 0;
}
The sub-expression (1/2) is always equal to 0 due to the integer arithmetic. You need to write for example (1.0f/2).
Apart from this the variable pi is not initialized
float a,b,c,pi ;
Also according to the C Standard the function main without parameters shall be declared like
int main( void )
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
#include <stdio.h>
#define s scanf
#define p printf
void main (){
int P,I,A;
float T,R;
clrscr();
p("PRINCIPAL: ");
s("%i",&P);
p("RATE: ");
s("%f",&R);
p("TERM: ");
s("%f",&T);
R = R/100;
I = P*R*T;
A = P+I;
p("I: %i\nMA: %i",I,A);
getch();
}
This really bugs me, if i put PRINCIPAL: 1200 RATE: 3 TERM:1 I: 35 MA: 1235 but if you compute in manually the answer should be I: 36 MA: 1236 the number is decreasing by 1. Why is it happening? why does the answer differ from computer and manual computing?
You try to typecast float to int that causes some data loss. Just like we can not store the big object in the small bag.
#include <stdio.h>
#define s scanf
#define p printf
int main (){
int P;
float T,R,I,A;
p("PRINCIPAL: ");
s("%i",&P);
p("RATE: ");
s("%f",&R);
p("TERM: ");
s("%f",&T);
R = R/100;
I = P*R*T;
A = P+I;
p("\nI: %f\nMA: %f",I,A);
return 0;
}
Your problem is with typecasting, please research on this subject a look at this, it's a little bit hude to explain in a simple text here.
But I can tell you, you are problably losing information when casting from float to int, because this two primitive types in C have diferent max length.
You can see changin the int variables to float and running your own program again, like this:
#include <stdio.h>
#define s scanf
#define p printf
void main (){
float P,I,A;
float T,R;
p("PRINCIPAL: ");
s("%f",&P);
p("RATE: ");
s("%f",&R);
p("TERM: ");
s("%f",&T);
R = R/100;
I = P*R*T;
A = P+I;
p("I: %f\nMA: %f",I,A);
getch();
}
This will produce your desired output, just in float.
Your problem is in the conversion of float to int. If you do your program with everything typed as float, you get the expected results:
#include <stdio.h>
#define s scanf
#define p printf
int main (){
float P,I,A;
float T,R;
p("PRINCIPAL: ");
s("%f",&P);
p("RATE: ");
s("%f",&R);
p("TERM: ");
s("%f",&T);
R = R/100;
I = P*R*T;
A = P+I;
p("I: %f\nMA: %f",I,A);
return 0;
}
outputs:
PRINCIPAL: 1200
RATE: 3
TERM: 1
I: 36.000000
MA: 1236.000000
However, when you convert your float values to int, you just take the integer part; everything to the right of the decimal point gets deleted. So, even though it's printing as 36.000000 when I do it, it's possible that the value of I may be coming out to something like 35.9999999, due to imprecision in floating-point math, and simply displaying as 36.000000 due to rounding in the display process. In this case, you'll just get the 35, and lose everything else.
To solve your problem, either leave everything as a float, or convert your floats to ints by rounding them—for example, by using lroundf in math.h—instead of just casting them.
I am currently attempting to learn C, and have made this program to calculate the area of a regular hexagon:
#include <stdio.h>
#include <math.h>
void main(){
int a;
float ans;
scanf("%d", &a); // get length of side
ans = ((pow(a, (1/3)))/2)*(a*a);
printf("%f", ans);
}
However, it outputs seemingly random numbers.
Firstly your code doesn't compile (Missing semicolon) and also you should use int main() instead of void main().
Secondly your formula also wrong, the area of a regular hexagon of side length a is calculated as ((3√3)/2)*a².
Thirdly Expression like 1/3 always yield zero as both are integer, to get expected behavior make one of them float/double. like 1.0/3 or (float)1/3 etc.
#include <stdio.h>
#include <math.h>
int main()
{
int a;
float ans;
scanf("%d", &a); // get length of side
ans = (3*sqrt(3)/2.0)*a*a;
printf("%f", ans);
}
#include <stdio.h>
#define PI 3.14159
int Circle (int);
int Rectangle (int, int);
int main()
{
int a;
int b;
int c;
int d;
int area;
int AreaOfCircle;
int AreaOfRectangle;
int area1;
printf("Program to calculate area\n");
printf("1 - Circle\n");
printf("2 - Rectangle\n");
printf("\n");
printf("What option = \n");
scanf("%d", &a);
if(a=1)
{
area=Circle(b);
printf("Area= %d\n", area);
}
else if(a=2)
{
area1=Rectangle(c,d);
printf("Area= %d\n", area1);
}
return 0;
}
int Circle (int b)
{
int area;
printf("radius= \n");
scanf("%d", &b);
area=PI*b*b;
return area;
}
int Rectangle(int c, int d)
{
int area1;
printf("length= \n");
scanf("%d",&c);
printf("width= \n");
scanf("%d",&d);
area1=c*d;
return area1;
}
//I want to ask if my coding is ok .. but as I run it the output only ask for radius which is the calling function for circle .. but if i want to call rectangle the output also shows calculation for circle .. can someone help me to spot the mistake .. by the way this is my first coding about calling function and I just started learning coding c last month .. T-T
With C you use == to evaluate (e.g. if (x == 1)). "=" is assignment, so you'll always hit the first block.
Also, you're accepting parameters which you're then modifying, which is not good practice. Consider declaring your variables at usage time also, the "everything at the top of the block" paradigm is very dated.
This question is not about functional programming, this is an example of imperative programming.
Also, your input being poured directly into an integer is not bounds checked, consider a switch/case so you can add a default of "invalid input" and extend to different shapes in the future.
Yes bro just make if(a==1) and else if(a==1).
You've used the assignment = operator instead of the comparison == operator.
A statement like
if(a=1)
will assign a value of 1 to a and check then check for the non-zero value of a [which always evaluates to TRUE].
Instead, what you want is
if (a == 1)
which evaluates to TRUE if a contains 1. Same for other comparison(s) also.
Note: In your int Circle (int b) case you're storing the result to an int, which will truncate the result of a double/float multiplication. To get the exact value, make the area as float or double and use %f/ %lf format specifier.
Next, as per the logical part, you don't need to pass b, c, d as parameters to the called functions. Simply a local variable in the functions would do the job.
Program only displaying the 32 for when I have it print "NewTemp"
NewTemp = 32 + input * 180/100; this part seems like the main problem
#include <stdio.h>
float celsius(float input) {
float NewTemp;
**NewTemp = 32 + input * 180/100;
printf("Please enter the temperature value to convert to fahrenheit\n");
scanf("%f", &input);
printf("The temperature in celsius is: %f\n", NewTemp);
return NewTemp;
}
int main(void){
float CelToFahren, input;
CelToFahren = celsius(input);
}
You do the math before you read the input. You need to do it the other way around.
Also, there's no reason to pass a meaningless and uninitialized value to the celsius function.
Lastly, 180/100 is 1 remainder 80 because when you divide two integers, you get integer division. You can use 180.0/100.0.
Basically, you need to learn C.
This was the instructions: Write a program that starts out asking the user for the mean u and
standard deviation s for the normal distribution
(see the wiki article http://en.wikipedia.org/wiki/Normal_distribution
The program then asks for an N, and then asks for N values x. For each x it
writes out f(x) to the screen.
Note that the program asks the user for u, s, and N just once. After that it
asks for N values for x, one by one. After each value x it writes out the
corresponding value of the function.
Of course, use double precision for this and use the square root and
exponential function from the standard math library.
This is my code so far but I can't get the N to work.
#include <stdio.h>
#define PI 3.141592653589793238462643383
#define E 2.7182818284590452353602874713526624977572470937
#include <math.h>
#include <stdlib.h>
int main()
{
double u,s,N,x,math1, math2, math3,n,;
printf("Enter Mean: \n");
scanf("%d", &u);
printf("Enter Deviation: \n");
scanf("%d", &s);
n=1/2;
math1 =1/(u*sqrt(2*PI));
math2= (x-u)/s * (x-u)/s;
math3= E * exp(n);
x = math1 * exp(math3)*exp(math2);
printf("%d \n", x);
system("Pause");
}
n=1/2;
This will be equal to 0, since 1 is an integer, 2 is an integer and 1 divided by 2 is 0 in integer math.
Try 1.0/2.0
Make sure every other division has a double on one or both sides, otherwise it will be done as integer math