C code that finds the number pi using leibniz formula [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am asked to write the C code that finds the number pi using the Leibniz formula.
However, the result should be 3.14 but result turns 3.23. What is the reason for this?
//Calculating the value of PI
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
//function main begins program execution
int main( void )
{
float pi = 0;
size_t k,n;
for ( n = 0 , k = 0; n <= 10 , k <= 10; n++ ,k++) {
pi += ( pow( -1, n ) * 4 )/ ( 2 * k + 1 );
}//end for
printf(" pi is %.2f\n",pi );
getch();
return 0;
}//end main

I played with it by increasing your iterations.
At "200" I got 3.15.
Basically "10" isn't even close to enough.

Related

Can someone tell me where I went wrong [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Can someone help me out with correcting this. The question was supposed to be (Write a code to get the value for expression 1 + x + x^2 + x^3 + ...... + x^n)
#include<stdio.h>
int main()
{
int power = 1,sum = 0,n,x , i;
printf("Enter the number : ");
scanf("%d",&x);
printf("Enter the limit to fill the following series : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum = sum + power;
while (i>0)
{
power = power * x;
i = i - 1;
}
}
printf("The sum is %d",sum);
return 0;
}
Change
while (i>0)
{
power = power * x;
i = i - 1;
}
to
power = power * x;
As this loop is not required

Sweet numbers , problems with checking if the number is sweet [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
We put a range from keyboard from what number to what number we want to search the ''Sweet number''.
One number is sweet if each of the digits is an even number. So I started and don't know how to go on
int min,max,i,sweetnumber;
scanf("%d%d",&min,&max);
for(i=min;i<=max;i++)
{
// and now what ? i don't know if the number is three-digit or four-digit or five digit so i can check a digit by digit .. Someone help !
}
It seems you need something like the following
for ( i = min; i <= max; i++ )
{
int tmp = i;
while ( tmp != 0 && tmp % 10 % 2 == 0 ) tmp /= 10;
if ( tmp == 0 ) printf( "%d is a sweet number\n", i );
}

How does this code calculate pi with high precision? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Here is the code:
#include <stdio.h>
long f[2801];
int main()
{
long i = 0, c = 2800, d = 0, e = 0, g = 0;
for (i = 0; i < c; ++i)
f[i] = 2000;
for (;;) {
d = 0;
g = c * 2;
if (!g)
break;
i = c;
for(;;) {
d += f[i] * 10000;
--g;
f[i] = d % g;
d /= g;
--g;
--i;
if (!i) break;
d *= i;
}
printf("%.4ld",e+d/10000);
e = d % 10000;
c -= 14;
}
return 0;
}
My question is: How does this code calculate pi with high decimal precision and what is the math formula it uses?
This is a formatted copy of the PI program written by Dik T. Winter of the CWI institute of Holland. Originally written in an obfuscated form, in two or three lines, there are several variations by Dik and other that output different numbers of places of PI (e.g. 800, 15,000, etc.) based on evaluation of a mathematical series.
It is a class of programs known as 'spigot algorithms', designed to output a specific number of digits. You can find out more via a Google search on Dik Winter and 'spigot algorithms'. Some example hits:
Computing Pi in C a detailed analysis of the algorithm with unanswered questions.
Pi the Number, not the Movie

What is the logic is used in this program [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
A 5 digit positive number is entered by user.how to calculate the sum of the Digits entered by the user with the help of function?
We beginners should help each other.:)
Here you are.
#include <stdio.h>
unsigned int digits_sum( unsigned int value )
{
const unsigned int Base = 10;
unsigned int sum = 0;
do { sum += value % Base; } while ( value /= Base );
return sum;
}
int main( void )
{
unsigned int value = 12345;
printf( "The sum of digits of number %u is %u\n", value, digits_sum( value ) );
return 0;
}
The program output is
The sum of digits of number 12345 is 15
The logic is simple. To get the last digit of a number you should apply operator %. Then you need to divide the number by 10 that in the next step to get the digit before the last and so on.

C how to check and point a '0' in a int number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Imagine we've got an int number = 1040 or int number = 105 in a C program, and we want to know if this number contains a 0 and in which position/s are they. How could we do it?
Examples
1040 -> position 0 and 2.
1000 -> position 0, 1 and 2.
104 -> position 1.
56 -> NO ZEROS.
Thanks!
I would divide by 10 and check the remainder. If remainder is 0, then last position of the number is 0. Then repeat the same step until number is less than 10
#include<iostream>
int main(void)
{
long int k = 6050404;
int iter = 0;
while (k > 10) {
long int r = k % 10;
if( r == 0) {
std::cout << iter << " ";
}
k = k / 10;
iter++;
}
}
I would convert it to a string; finding a character in a string is trivial.
As a rule of thumb, if you are doing maths on something, it's a number; otherwise, it's probably (or should be treated as) a string.
Alternatively, something like:
#include <stdio.h>
int main(void) {
int input=1040;
int digitindex;
for (digitindex=0; input>0; digitindex++) {
if (input%10==0) {
printf("0 in position %i\n",digitindex);
}
input/=10;
}
return 0;
}
This basically reports if the LAST digit is 0, then removes the last digit; repeat until there is nothing left. Minor modifications would be required for negative numbers.
You can play with this at http://ideone.com/oEyD7N

Resources