Finds the sum of the first few terms with for loop - C Statement

C examples for Statement:for

Introduction

The variable power_of_two takes on the values 1.0 , 2.0 , 4.0 , 8.0 , and so on.

Demo Code

#include <stdio.h>

int main(void)
{
    int count;       // term count
    double time, powerResult;
    int limit;/*  www  . ja va  2 s  .  c o  m*/
    
    printf("Enter the number of terms you want: ");
    scanf("%d", &limit);
    for (time=0, powerResult=1, count=1; count <= limit;
                            count++, powerResult *= 2.0)
    {
        time += 1.0/powerResult;
        printf("time = %f when terms = %d.\n", time, count);
    }
    
    return 0;
}

Result


Related Tutorials