I have set the correct directory paths for Turbo C. But yet it gives the output as 0.000000
Following is the program:
#include <conio.h>
#include <math.h>
#include <stdio.h>
void main() {
int n;
float r, si, ci, p;
clrscr();
printf("enter principle amount\n");
scanf("%f", &p);
printf("enter rate of interest\n");
scanf("%d", &r);
printf("enter number of years\n");
scanf("%f", &n);
si = p * n * r / 100;
ci = p * (pow((1 + (r / 100)), n) - 1);
printf("simple interest=%f\n", si);
printf("compound interest=%f", ci);
getch();
}
It is supposed to give numbers instead of 0.000000
Any help?
Change:
scanf("%f",&n);
to:
scanf("%d",&n);
since n is an integer, not a float, as suggested in the comments already.
For r, which is of type float, you should use scanf("%f",&r);.
PS: Consider using a modern compiler, such as GCC.
Related
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;
}
Can somebody tell me what is wrong with my code?
#include <stdio.h>
#include <math.h>
int main(void) {
int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;
printf("Enter two integers: ");
scanf("%d%d",&n1,&n2);
for(;nSet<2;nSet++){
int sum=n1+n2;
float prod=n1*n2;
float hm=nSet/(1/n1+1/n2);
float gm=sqrt(n1+n2);
float avg=(n1+n2)/2;
}
printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);
return 0;
}
originally by initializing in for loop i got 0 as every answer but atleast that printed something. I have to use loops to find the answers and i dont see why for loop would'nt work.
In C if you declare a float and assign a value like 1/2 the result will be 0 because de expression 1/2 is evaluated. If you want to have a result as float put 1.0f/2 or 1/2.0f and will work. In case of having 2 variables and want a float you can do this: (float)n1/n2 or n1/((float)n2) if n1 and n2 are int. Another observation is in the for loop. If you declare your variables again in for they are local in the loop and outside the loop they don't exists.
These code will work:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;
printf("Enter two integers. \n");
printf("n1 = ");scanf("%d",&n1);
printf("n2 = ");scanf("%d",&n2);
for(;nSet<2;nSet++)
{
sum=n1+n2;
prod=n1*n2;
hm=nSet/(1.0f/n1+1.0f/n2);
gm=sqrt(n1+n2);
avg=(n1+n2)/2.0f;
}
printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);
return 0;
}
You are declaring new var in for scope. When the program exit from the loop all of the var created in for scope will be destroyed.
Your code corrected :
#include <stdio.h>
#include <math.h>
int main(void) {
int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;
printf("Enter two integers: ");
scanf("%d%d",&n1,&n2);
for(;nSet<2;nSet++){
sum=n1+n2;
prod=n1*n2;
hm=nSet/(1/n1+1/n2);
gm=sqrt(n1+n2);
avg=(n1+n2)/2;
}
printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);
return 0;
}
It's work much better now :)
Enter two integers: 10
1
Sum: 11
Product: 10.00
Average: 5.00
Geometric mean: 3.32
Harmonic mean: 1.00
I'm trying to write a code that calculates monthly pay for a project.
This is the formula I was given:
(Rate + Rate/((1+Rate)^Months)-1) * Principle
Rate according to this formula is Rate/1200 so as an example if the rate is 7% it would be 7/1200 which is 0.00583333333. I'm trying to get the exact number 0.00583333333 into my program but then I get the error "illegal use of floating point".
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
float r;
int m, y;
int p;
//int mp;
printf("Enter Rate: ");
scanf("%d", &r);
r = r%1200;
printf("Enter number of years: ");
scanf("%d", &y);
m = y*12;
printf("%.10lf\n",r);
printf("%d",m);
return 0;
}
How do I get 0.00583333333 to be a part of my calculation in the program?
try to change scanf("%d", &r); by scanf("%f", &r); and r = r%1200 by r = r/1200
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.
My 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 )
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.
What I am confused about is what the N is supposed to stand for. I assumed it was number of x's but can anyone clarify this for me?
#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 x's: ");
scanf("%lf", &N);
for (v=1; v<=N; v++)
{
printf("Enter Value: ");
scanf("%lf", &x);
n=(-1/2);
printf("f(x)= ");
math1 =1/(u*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");
}
N stands for number of inputs
pretty much clear from this part:
for (v=1; v<=N; v++)
If your instructions are as given, then N does indeed stand for the number of x values required.
Your program does just that, asking for N values of x.
First, it declares a variable N at the start of the program:
double u,s, N, x1,math1, math2, math3,n, v, x;
Then it prompts for input as an integer:
printf("Enter number of x's: ");
scanf("%lf", &N);
...and finally uses that integer to read in N values for x.
for (v=1; v<=N; v++)
{