Answer the question
In order to leave comments, you need to log in
How to write a program to find the "sum" of values in a sequence?
There is such a task from the book:
Please explain what exactly is required to find in this task?
And explain why the "Hint" is indicated in the text of the problem?
And it is not clear to what value the sequences should converge. (if they should at all)
I sort of wrote a solution, but something doesn't seem right.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
int main(void)
{
float value = 1.0;
int length;
float result_1 = 0.0;
float result_2 = 0.0;
float number = 1.0;
printf("Input length: ");
while (scanf("%d", &length) == 1 && length > 0)
{
for (int i = 0; i < length; i++, number++)
{
result_1 += (value / number);
if (i % 2 == 0)
result_2 += (value / number);
else
result_2 -= (value / number);
}
printf("1.0 + 1.0 / 2.0 + ... = %f\n", result_1);
printf("1.0 - 1.0 / 2.0 + ... = %f\n", result_2);
printf("Input the next length: ");
}
printf("Done!");
_getch();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
int main(void)
{
float value = 1.0;
int length;
float result_1 = 0.0;
float result_2 = 0.0;
float number = 1.0;
printf("Input length: ");
while (scanf("%d", &length) == 1 && length > 0)
{
for (int i = 0; i < length; i++, number++)
{
result_1 += (value / number);
}
number = 1.0;
for (int i = 0; i < length; i++, number++)
{
if (i % 2 == 0)
result_2 += (value / number);
else
result_2 -= (value / number);
}
printf("1.0 + 1.0 / 2.0 + ... = %f\n", result_1);
printf("1.0 - 1.0 / 2.0 + ... = %f\n", result_2);
printf("Input the next length: ");
}
printf("Done!");
_getch();
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question