I like to calculate the following formula using c programming. The formula is as follows:
I have written the c program based on this formula
#include<stdio.h>
#include<math.h>
int main(){
int n,i;
double sum=0;
printf("Enter the value for n");
scanf("%d",&n);
for (i=0; i<=n; i++){
sum = sum + 4*(pow(-1,i))/((2*i)+1);
}
printf("sum of the series: %lf",sum);
}
return 0;
I am sharing the result that I have got after run the program and the result I got while calculating the result mathematically.
As you can see from picture 1 if I try to calculate the sum for different value of n like 1, 4,13 the sum was 2.67, 3.339, 3.0702 respectively.
However, when I tried to calculate this formula mathematically I got some different result.
Mathematically I got
n= 1 ans: 3.2
n=4 ans: 0.96825
Can anyone please guide what I am missing in code?
Thank you.
In 2nd iteration of your manual calculation, 2x1 + 1 = 3 but you're taking it as 5
Related
This is the code I wrote. Please tell me the mistakes and knowledge gap I may have
#include <stdio.h>
int main()
{
int i,n,c=1;
printf("enter the number 1:");
scanf("%d",&i);
printf("enter the second number:");
scanf("%d",&n);
while (i!=n)
{
c++;
i=i*c;
n=n*c;
}
printf("the lcm is %d",i);
return 0;
}
Input I put: 2 & 3
The output I get: The lcm is 0
Output expected: The lcm is 6
Your algorithm is simply wrong.
Take a basic example i=3 and n=4.
The LCM is 12 and to get 12 you multiply both numbers by a different number. Whereas you're assuming that you need to multiply both numbers by the same factor c.
You may have found the solution yourself by doing a very easy debugging step by step. You can even do that online for a basic code like yours.
There are other issues in your code, like the fact your are using signed integer (you probably need unsigned integer instead) and the fact you are nottaking care about integer overflow.
The natural logarithm can be approximated by the following series.
If x is input through the keyboard, write a program to calculate the
sum of first seven terms of this series.
I wrote the program as:
#include <stdio.h>
#include<math.h>
int main()
{
float x, i, sum, log_sum;
printf("Enter the value of x : ");
scanf("%f",&x);
sum=0;
for (i=2; i<=7; i++)
{
sum=sum+((1/2)*pow(((x-1)/x), i));
}
log_sum=((x-1)/x)+sum;
printf("\nSum of log series is %f\n",log_sum);
return 0;
}
The output is not matching with calculator answer. What maybe wrong here?
In the for loop you multiply everything by (1/2), given 1 and 2 are integers, the answer will be 0. Try multiplying by 0.5 instead, does that solve the problem?
I'm writing a program that calculates the value of the normal distribution function given to me here:
The program is supposed to ask the user for the mean μ and the Standard Deviation σ for the normal distribution showed above. The program then asks for N values of x and then asks for each value of x, one by one. After each value x it writes out the corresponding value of the function.
This is my code so far:
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>
int main() {
int j;
double u, stddev, N, result, x;
printf("Enter u and stddev for the Normal Distribution:\n");
scanf("%lf %lf",&u, &stddev);
printf("Enter how many values of x (N) for the Normal Distribution:\n");
scanf("%lf",&N);
for (j=0; j<N; j++) {
printf("Enter a value for x: \n");
scanf("%lf",&x);
result = ((1)/(stddev*sqrt(2*M_PI)))*exp(-(1/2)*((x-u)/stddev)*((x-u)/stddev));
printf("%.6lf\n", result);
}
}
I'm basically done but the answers the program is giving me are wrong when compared to my answers from my calculator. For instance, when I make N = 3 no matter what I put for the 3 values of x, the answer it gives me for each are the same when they shouldn't be.
So I know my issue lies in this line of code:
result = ((1)/(stddev*sqrt(2*M_PI)))*exp(-(1/2)*((x-u)/stddev)*((x-u)/stddev));
Am I just writing the function wrong in the program? I must be, for it not to work.
Your code is clean and functional. Good work so far.
You were correct about which line needed changing - The issue is that using integer division results in an integer, so 1 / 2 results in a value of 0. This can be remedied by using a single double value: 0.5, or by dividing using doubles 1.0 / 2.0.
result = ((1)/(stddev*sqrt(2*M_PI)))*exp(-(0.5)*((x-u)/stddev)*((x-u)/stddev));
I tested out your code after making these changes try it online, and they match up perfectly with the formula on wolfram alpha, as well as what you said in the comments.
For example:
μ=2, σ=3, x=7 results in 0.033159
μ=3, σ=7, x=0 results in 0.051991
μ=4, σ=4, x=4 results in 0.099736
I used the following code to generate a simple sieve-
#include<stdio.h>
int main()
{
int a,b,i,j;
scanf("%d%d",&a,&b);
int m = (a)/2;
int d[m+1];
for (i=0;i<m;i++)
d[i]=1;
for (i=1;i<=m;i++)
for (j=i;j<=((m-i)/(2*i+1));j++)
d[i+j+2*i*j]=0;
if (b<=2) printf("2\n");
for (i=0;i<m;i++)
if(d[i]!=0&&i!=0) printf("%d\n",2*(i)+1);
}
but I want to eliminate the extra time used for finding primes upto b, for this I made m = (a-b)/2 and tried starting the main for loop from b/2 and sqrt b/2 instead of zero but it doesn't seem to work, how can I reduce the extra calculation ? Thanks in advance.
I'm trying to implement my own sin(x) function via maclaurin series using C program,
this is what I have so far, I worked everything on paper first then I tried to implement it it in a code
#include<stdio.h>
#define PI 3.141592653589793238462643383
int main()
{
int x,nOfterms,term=1,i,j;
double numerator,sum=0.0,radius;
long int denominator;
printf("\n\t\tINPUT:");
printf("\n\t\t------");
printf("\n\t\tEnter the value for x in degrees: ");
scanf("%d",&x);
printf("\n\t\tEnter the value for number of terms: ");
scanf("%d",&nOfterms);
printf("\n\t\tOUTPUT:");
printf("\n\t\t-------");
radius=x*(PI/180.0);
printf("\n\t\tThe radius value for the given x value is: %lf",radius);
for(i=1;i<=nOfterms;i+=2)
{
numerator=1.0;
denominator=1;
for(j=1;j<=i;j++)
{
numerator=numerator*radius;
denominator=denominator*j;
}
sum=(sum+numerator)/(denominator*term);
term=term*-1;
}
printf("\n\t\tThe value for sin %d is: %lf",x,sum);
printf("\n\n\n");
return 0;
}
I can't use the math.h at all in my code or so does the professor say,
My output is all zeros which is not right obviously, also I would appreciate if anyone could help me with this, also if anyone can offer a way where I can see each iteration for each number of term of the series on the screen before I arrive at the answer, tried to place the last printf in the for loop but I'll end up with weird symbols and numbers.
The problem in in the implementation of the Mac Laurin series. The line
sum=(sum+numerator)/(denominator*term);
is wrong. It should be simply :
sum += numerator/denominator*term;
Remaining is fine. I tried it and found sin(30) = 0.5, sin(60) = 0.866 and even sin(90) ~ 1.
But you algorythm is not very efficient since for each term you start back from 1/1 when the first elements of in / i! have already been computed on previous term.
You could change your loops to :
numerator=1.0;
denominator=1;
j = 1;
for(i=1;i<=nOfterms;i+=1)
{
numerator=numerator*radius;
denominator=denominator*j++;
sum += numerator/denominator*term;
term=term*-1;
numerator=numerator*radius;
denominator=denominator*j++;
}
Tested and give same (correct) results.
Here are some problems with your code to help you along:
You want to increment i by 1, not 2 as you count the number of terms.
For each term, you want only the odd powers of x, so it's j you need to increment by 2 (change the upper limit and denominator factorial calculation appropriately)
This line is wrong: sum=(sum+numerator)/(denominator*term); and should presumably be sum=sum+numerator/(denominator*term);
Hope that helps with your assignment.
Your inner for loop is not correct. Manually trace through it for a few values of i:
1: numerator=radius, denominator=1 (correct)
2: numerator=radius, denominator=1 (incorrect)
3: numerator=radius*radius, denominator=3 (incorrect)
Correct inner loop should be:
for (j = 1; j < i*2; ++j)
{
numerator = numerator * radius;
denominator = denominator * j;
}