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?
Related
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
My professor uses this site (e-olymp.com) that automatically grades your solution in %
For this homework we have to use pointers to solve these tasks. I had a problem with this one:
The array of real numbers is given. Calculate the twice value of the minimum element in array.
Input
First line contains the number n (n ≤ 100) of elements in array. Second line contains n real numbers - the elements of array. Each value does not exceed 100 by absolute value.
Output
Print the twice value of the minimum element in array with 2 decimal digits.
My solution, works perfectly fine in compiler but gives 0%, idk where is the mistake, could you take a look at this one?
#include<stdio.h>
#include <malloc.h>
int z, x;
double fx;
int main(void){
double *c = (double *)malloc(x*sizeof(double));
scanf("%d", &x);
fx=100;
for(z=0; z<x; z++){
scanf("%lf", c+z);
if(fx>*(c+z)) fx=*(c+z);
}
printf("%.2lf", fx*2);
free(c);
return 0;
}
My program to check Armstrong Number of 'n' digits is giving a wrong output, only when the input is 153.
Here is my code.
#include<stdio.h>
#include<math.h>
int main()
{
int p, k, n, t, sum, n1, m;
printf("Enter the number: ");
scanf("%d", &n);
n1=n;
for(p=0, k=1; n/k>=1; ++p, k*=10);
printf("\nThe number of digits: %d", p);
for(sum=0; n>0; n/=10)
{
t= n%10;
printf("\n\nThe base and power: %d and %d", t, p);
m=pow(t, p);
printf("\nThe calculated no is: %d", m);
sum+=pow(t, p);
printf("\nThe sum is: %d", sum);
}
printf("\n\t The original number is : %d", n1);
printf("\n\t The calculated number is: %d", sum);
if(n1==sum)
printf("\n\t%d is an armstrong number\n", n1);
else
printf("\n\t%d is not an armstrong number\n", n1);
return 0;
}
The program is getting 152 when it does the math and is therefore giving a wrong output. I have printed every step to find the exact point of error.
I have used power function instead of for loops
I cannot use t* t*t as this program is for Armstrong numbers of "n" digits, and not only 3
I am compiling the program using Code Blocks 16.01
The problem is, it is calculating the cube of 5 as 124.
Interestingly I am getting the correct answer(125) when I use the power function to calculate the cube of 5 in a separate, simple program.
I also checked the code given here -->https://www.programiz.com/c-programming/examples/check-armstrong-number which is also giving the wrong output. The answers to the somewhat similar questions that I found on this website didn't solve the problem.
Well, I'm not able to reproduce the said problem. I do get the correct result for input 153, i.e. that it is an Armstrong number.
It could be some floating point rounding error due to use of pow (though I would find that strange in this specific case).
For a task like this one, you should not use floating point. Use the largest integer type available. Not only do you avoid nasty rounding errors but you also increases the range of input values that your program can handle.
So, I like to address this part:
I cannot use t* t*t as this program is for Armstrong numbers of "n" digits, and not only 3
You can easily write a function that calculates t^n using integer math. Here is an example:
#include<inttypes.h>
uint64_t intpow(uint64_t t, uint64_t p)
{
uint64_t result = 1;
while(p>0)
{
result = result * t;
--p;
}
return result;
}
Notice: In its current form the function lacks overflow detection as I wanted to keep the function simple.
There are Two ways to solve.
1) Use round() function which will give the nearest integer (because there is some error in floating point calculation in codeblocks).
2) Declare your sum variable as float or double because then it will convert into perfect floating point precision.
I have done the programming for gauss-seidel method,which is working for all inputs,except the following equation:
1.876 x1+2.985 x2-11.620 x3=-0.972
12.214 x1+2.367 x2 +3.672 x3=7.814
2.412 x1+9.879 x2 +1.564 x3 =4.890
When I am running with this input,there is a run time error of "floating point overflow."It is working fine if I am using integer input.My code is as follows:
//GAUSS SEIDEL METHOD
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define e 0.001
void main() {
int i,j,n,count;
double a[10][10],x[10];
double sum,temp,error,big;
printf("Enter the number of equations: ");
scanf("%d",&n) ;
printf("Enter the co-efficients of the equations: \n");
for(i=0;i<n;i++) {
for(j=0;j<n+1;j++) {
printf("a[%d][%d]= ",i,j);
scanf("%lf",&a[i][j]);
}
}
for(i=0;i<n;i++)
x[i]=0;
count=1;
do {
big=0;
for(i=0;i<n;i++) {
sum=0;
for(j=0;j<n;j++) {
if(j!=i) {
sum = sum+a[i][j]*x[j];
}
}
temp = (a[i][n]-sum)/a[i][i];
error = fabs((x[i]-temp)/temp);
if(error>big) {
big=error;
}
x[i]=temp;
printf("%d\tx[%d] =%lf",count,i,x[i]);
}
printf("\n");
count++;
}while(big>=e);
printf("\n\nconverges to solution");
for(i=0;i<n;i++) {
printf("\nx[%d]=%lf",i,x[i]);
}
getch();
}//end
I cannot find what is to be modified.
Though it can be applied to any matrix with non-zero elements on the diagonals, convergence is only guaranteed if the matrix is either diagonally dominant, or symmetric and positive definite.
as it is stated in the wikipedia article.
Your example matrix isn't, so it mustn't come as a too big surprise that the method doesn't converge.
If you reorder the equations, move the first equation last (then the coefficient matrix becomes diagonally dominant), it quickly converges to the approximate solution
x[0]=0.500006
x[1]=0.333334
x[2]=0.250001
(the exact solution is (1/2, 1/3, 1/4)).
What happens is:
round:
first, x[0] gets a negative value (-0.972/1.876),
next, the sum for the second row becomes negative, and x[1] gets a too large value,
then, to compensate for the too large value of x[1], x[2] gets a negative value too.
round:
the sum x[1]*a[0][1] + x[2]*a[0][2] is positive, since both x[2] and a[0][2] are negative, and x[1] and a[0][1] are positive. Thus x[0] gets an even smaller negative value than in the first round,
then x[0]*a[1][0] + x[2]*a[1][2] is negative, and the value of x[1] becomes larger to compensate,
then the value of x[2] becomes a smaller negative value to compensate
and further rounds: see round 2.
After some time, you get infinities and NaNs.
Create a program to find out the first perfect square greater than 1 that occurs in the Fibonacci sequence and display it to the console.
I have no output when I enter an input.
#include <stdio.h>
#include <math.h>
int PerfectSquare(int n);
int Fibonacci(int n);
main()
{
int i;
int number=0;
int fibNumber=0;
int psNumber=0;
printf("Enter fibonacci number:");
scanf("%i",&number);
fibNumber = Fibonacci(number);
psNumber = PerfectSquare(fibNumber);
if(psNumber != 0){
printf("%i\n",psNumber);
}
}
int PerfectSquare(int n)
{
float root = sqrt(n);
if (n == ((int) root)*((int) root))
return root;
else
return 0;
}
int Fibonacci(int n){
if (n==0) return 0;
if (n==1) return 1;
return( Fibonacci(n-1)+Fibonacci(n-2) );
}
Luke is right. If your input is n, then the Fibonacci(n) returns the (n+1)th Fibonacci number.
Your program check whether (number +1)th is perfect square or not actually.
If you enter 12, then there is output. Because the 13th Fibonacci number is 144. And it is perfect square. PS: print fibNumber instead of psNumber.
printf("%i\n", fibNumber);
Right now you're only calculating one Fibonacci number and then testing whether it's a perfect square. To do this correctly you'll have to use a loop.
First suggestion is to get rid of the recursion to create fib numbers. You can use 2 variables and continually track the last 2 fib numbers. They get added something like:
fib1=0;fib2=1;
for(i=3;i<MAXTOCHECK;i++)
{
if(fib1<fib2)
fib1+=fib2;
else
fib2+=fib1;
}
What is nice about this method is that first you can change you seeds to anything you want. This is nice to find fib like sequences. For example Lucas numbers are seeded with 2 and 1. Second, you can put your check for square inline and not completely recalculate the sequence each time.
NOTE: As previously mentioned, your index may be off. There is some arbitrariness of indexing fib numbers from how it is initially seeded. This can seen if you reseed with 1 and 1. You get the same sequence shifted by 1 index. So be sure that you use a consistent definition for indexing the sequence.