Finds sum of first n integers with while loop, read the upper limit from user - C Data Type

C examples for Data Type:int

Description

Finds sum of first n integers with while loop, read the upper limit from user

Demo Code

#include <stdio.h>  
int main(void)    
{  //  w ww .  j  a va  2 s .  c o  m
  int count, sum;              
  int n;  
    
  printf("Enter the upper limit: ");  
  scanf("%d", &n);  
  count = 0;                    
  sum = 0;                    
  while (count++ < n)  
     sum = sum + count;   
  printf("sum = %d\n", sum);  
  return 0;  
}

Result


Related Tutorials