Modifying the for Loop Control Variable - C Statement

C examples for Statement:for

Introduction

You are not limited to incrementing the loop control variable by 1.

You can change it by any amount, positive or negative.

You could sum the first n integers backward.

Demo Code

#include <stdio.h>
int main(void)
{
  unsigned  long long sum = 0LL;            // Stores the sum of the integers
  unsigned int count = 0;                   // The number of integers to be summed

  printf("\nEnter the number of integers you want to sum: ");
  scanf(" %u", &count);

  // Sum integers from count to 1
  for(unsigned int i = count ; i >= 1 ; sum += i--);

  printf("\nTotal of the first %u numbers is %llu\n", count, sum);
  return 0;/*  w  ww  .  j a  v  a2 s . c  o  m*/
}

Result


Related Tutorials