I can’t solve this equation.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int n, result, sum, product;
sum = 0; product = 1, result = 0;
for (int n = 1; n <= 20; n++) {
for (int i = 1; i <= n; i++) {
sum = sum + ((i - n) / (i + n));
product *= sum;
result = sum + product;
}
}
printf("result::%d",result);
return 0;
}
You are on the right track, but note these problems:
you should use double arithmetics for the computation and values, otherwise the result will be meaningless.
you should initialize sum to 0 before the inner loop
you should move the product outside the inner loop
n is redefined in the outer for loop
result is probably not needed.
Here is a modified version:
#include <stdio.h>
int main() {
double product = 1.0;
for (int n = 1; n <= 20; n++) {
double sum = 0.0;
for (int i = 1; i <= n; i++) {
sum += (double)(i - n) / (double)(i + n);
}
product *= sum;
}
printf("result: %f\n", product);
return 0;
}
The above direct translation of the formula outputs -0.000000. The explanation is simple: if you look at the formula, you will see that the first term of the product for n=1 is sum for i from 1 to 1 of (i - n) / (i + n), which is (1 - 1) / (1 + 1), that is 0. Multiplying that by any finite quantity will stay invariably null.
Hence the formula can be solved algebraically and produce a much simplified version. A good algorithm beats any brute force approach:
#include <stdio.h>
int main() {
printf("result: 0\n");
return 0;
}
Related
The question was to add first seven terms of the following series using for loop
1/1! + 2/2! + 3/3! ....
I thought i might be loosing decimal point due to int but i am not even getting wrong answer.
I ran the code in terminal but it just keeps running no output.
Thanks for helping out.
#include<stdio.h>
int main()
{
int n;
float a;
float v=0.0;
for(n=1;n<=7;n++)
{
a=1.0;
while(n>0)
{
a=a/n;
n--;
}
v=v+n*a;
}
printf(" sum is %f ",v);
return 0;
}
#include <stdio.h>
int fact(int n) {
int product = 1;
for (int i = 1; i < n+1; i++) {
product *= i;
}
return product;
}
int main() {
double sum = 0.0;
for(int i = 1; i < 7+1; i++) {
sum += double(i) / fact(i);
}
printf("Sum is %f\n", sum);
return 0;
}
The series is 1/1! + 2/2! + .. n terms can be written as 1 + 1/1! + 1/2! + 1/3! + ...(n-1) terms .
So it is 1 + sum of 1/i! where i=1 to i=n-1 terms.
So I have taken sum=1 as initial value.
#include <stdio.h>
//this is the function for calculating n!
int fact(int n) {
int product = 1;
for (int i = 1; i < n+1; i++) {
product *= i;
}
return product;
}
int main() {
int n=5;
//we are taking sum=1 as initial value
double sum = 1.0;
//now we run 1/1! + 1/2! + 1/3! + 1/4! i.e. upto (n-1) terms (here n=5 => so n-1 = 4)
for(int i = 1; i < n; i++)
{
sum += (1.0) / fact(i);
}
printf("Sum is %f\n", sum);
return 0;
}
As #DeBARtha mentioned in the original code i was incrementing and then decreasing n causing to loop to run repeatedly.
By using one more variable (m) for the while loop and then decreasing it while increasing the 'n' in the for loop the problem gets resolved.
#include<stdio.h>
int main()
{
int n,m;
float a;
float v=0.0;
for(n=1;n<=7;n++)
{
m=n;
a=1.0;
while(m>0)
{
a=a/m;
m--;
}
v=v+n*a;
}
printf(" sum is %f ",v);
return 0;
}
#include <stdio.h>
#include <math.h>
int main() {
int n, count, sum;
printf("Enter upper bound n \n");
scanf("%d", &n);
for (int a = 1; a <= n; a++) {
count = 0;
sum = 0;
for (int i = 2; i <= sqrt(a); ++i) {
if (a % i == 0) {
count++;
break;
}
}
if (count == 0 && a != 1) {
sum = a + sum;
}
}
printf("%d", sum);
}
The program is my attempt to print summation of primes < n. I am getting sum = 0 every time and I am unable to fix this issue.
The reason you do not get the sum of primes is you reset the value of sum to 0 at the beginning of each iteration. sum will be 0 or the value of the n if n happens to be prime.
Note also that you should not use floating point functions in integer computations: i <= sqrt(a) should be changed to i * i <= a.
The test on a != 1 can be removed if you start the loop at a = 2.
Here is a modified version:
#include <stdio.h>
int main() {
int n = 0, sum = 0;
printf("Enter upper bound n: \n");
scanf("%d", &n);
// special case 2
if (n >= 2) {
sum += 2;
}
// only test odd numbers and divisors
for (int a = 3; a <= n; a += 2) {
sum += a;
for (int i = 3; i * i <= a; i += 2) {
if (a % i == 0) {
sum -= a;
break;
}
}
}
printf("%d\n", sum);
return 0;
}
For large values of n, a much more efficient approach would use an array and perform a Sieve of Eratosthenes, a remarkable greek polymath, chief librarian of the Library of Alexandria who was the first to compute the circumference of the earth, 2300 years ago.
Here is an improved version:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int n = 0;
long long sum = 0;
if (argc > 1) {
sscanf(argv[1], "%i", &n);
} else {
printf("Enter upper bound n: \n");
scanf("%i", &n);
}
// special case 2
if (n >= 2) {
sum += 2;
}
unsigned char *p = calloc(n, 1);
for (int a = 3; a * a <= n; a += 2) {
for (int b = a * a; b < n; b += a + a) {
p[b] = 1;
}
}
for (int b = 3; b < n; b += 2) {
sum += p[b] * b;
}
free(p);
printf("%lld\n", sum);
return 0;
}
Error about sum getting set to zero inside the loop has been already pointed out in previous answers
In current form also, your code will not return zero always. It will return zero if value of upper bound is given as non prime number. If prime number is given as upper bound, it will return that number itself as sum.
As mentioned in comment you should initialize sum before first loop something like
int n, count, sum=0;
or you can initialize sum in the loop like
for(a=1,sum=0;a <= n; a++)
and remove sum=0; inside the first loop because it changes sum to 0 every time first loop executes. You can check this by inserting this lines to your code
printf("Before sum %d",sum);
sum = 0;
printf("After Sum %d",sum);
make sure sure that if you are initializing sum in the loop, define "a" in outer of the loop if not the sum goes to local variable to for loop and it hides the outer sum.
Following needs to be calculated (iterative und recursive Solution)
Sum = 1 – 1/2 + 1/3 – 1/4 + - . . . 1/n (n > 0) with float reihe (int n)
We have the following so far:
#include <stdio.h>
float reihe(int n)
{
float sum = 0;
if (n = 1)
sum = 1;
else
{
for (int i = 1; i < n; i++)
{
sum += (1 / i) - (1 / (1 + i));
}
}
return sum;
}
int main(void)
{
float z;
z = reihe(5);
printf("%d", z);
return 0;
}
Would appreciate any of your help. Have a good day.
What about something like this? Not tested, but it compiles and runs...
BTW/ take a close look at the use of 1.0. If you use 1 it is interpreted as an integer and not a float and you division is always rounded to 1 or 0 and you lose all decimal values...
float reihe(int n)
{
float sum = 0;
float base = 1.0;
for (int i = 1; i <= n; i++) {
if ( i % 2 ) {
sum += base/i;
}
else {
sum -= base/i;
}
}
return sum;
}
I have a problem when trying to print the numbers in the n given row of Pascal's triangle in C:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
int n, k;
double result1, result2;
scanf("%d", &n);
scanf("%d", &k);
result2 = knumberinnrowofpascal(8, 4);
printf("%f\n", result2);
int i = 0;
for (i; i<n; i++) {
result2 = knumberinnrowofpascal(n, i);
printf("%f\n", result2);
}
return 0;
}
int combinations(int n, int k) // calculates combinations of (n,k).
{
if (k == 0)
return 1;
else if (k > n)
return 0;
else
return (combinations(n - 1, k) + combinations(n - 1, k - 1));
}
int knumberinnrowofpascal(int n, int k)
{
double rightmultipier, leftmultiplier, result;
rightmultipier = (double)(n + 1 - k) / k;
leftmultiplier = (double)combinations(n, k - 1);
result = (double)leftmultiplier * rightmultipier;
return result;
}
The function "knumberinnrowofpascal" works, I've tested it above (the 4th element in the 8th row ). The problem is when I try to print these results in a for loop.
rightmultipier = (double)(n + 1 - k) / k;
This will fail if k is 0. And even if it didn't, it would on the next row, because you would have infinite recursion there.
Change:
int i = 0;
for (i; i <= n; i++) {
to
int i;
for (i=1; i <= n; i++) {
I made two improvements there. I fixed the bug and moved the initialization to the for header.
return (combinations(n - 1, k) + combinations(n - 1, k - 1));
when k!=0 and n<k, you recurse with combinations(n-1,k). Decrementing n in this recursion does not change k!=0, and it certainly does not make n > k until it overflows, which means you are in a practically infinite recursion and it segfaults.
i'm trying to compute "2^0 + 2^1 + 2^2 + ... + 2^14", using the following program(i'm a newbie and can only compute a exponent by multiply itself a certain times). The result should be 32767, but i ran it and got 270566475, i thought for long but can't figure out why...
#include <stdio.h>
int main(void)
{
int i, e, exponent, sum;
e = 1;
exponent = 1;
sum = 1;
for (i = 1; i <=14; i++)
{
for (e = 1; e <= i; e++)
{
exponent *= 2;
}
sum += exponent;
}
printf("%d\n", sum);
return 0;
}
So what's wrong with this??? Thanks!!!
You don't need the inner loop. Just execute exponent *= 2 once, directly inside the outer loop. BTW, I think you have to do it after the sum += ....
Also, you could start with sum = 0 and i = 0, which is closer to the math you described.
Look at your inner loop by itself. It's trying to calculate, for one specific value of i, 2^i.
But exponent does not start at 1 every time. So you go into that loop with exponent already some very large value.
for (i = 1; i <=14; i++)
{
exponent = 1;
for (e = 1; e <= i; e++)
{
exponent *= 2;
}
sum += exponent;
}
Now you've reset exponent (which, to be clear, isn't the exponent at all but the calculated result) for each new power of 2.
If you have right to create a function it better to do it like this with a recursive function :
#include <stdio.h>
int power(int x, int exp) {
if (exp == 0)
return 1;
else
return x * power(x, exp-1);
}
int main (int argc, const char * argv[])
{
int i;
int sum = 0;
for (i = 0; i <= 14; i++) {
sum += power(2, i);
}
printf("%d",sum);
return 0;
}
I hope it helps.
You just need one loop because each you already have the result of n-1 value. I had correct your code it works.
#include <stdio.h>
int main (int argc, const char * argv[])
{
int i, e, exponent, sum;
e = 1;
exponent = 1;
sum = 1;
for (i = 1; i <= 14; i++)
{
exponent *= 2;
sum += exponent;
}
printf("%d\n", sum);
return 0;
}
Both codes work