Floating-Point Loop Control Variables - C Statement

C examples for Statement:for

Introduction

The loop control variable can also be a floating-point variable.

Here's a loop to sum the fractions from 1/1 to 1/10:

Demo Code

#include <stdio.h> 

int main(void) { 

    double sum = 0.0;

    for(double x = 1.0 ; x < 11 ; x += 1.0)
      sum += 1.0/x;/*from w w  w. ja  v a2s .  c  om*/
        
    return 0; 
}

Related Tutorials