Sums integers entered interactively - C Data Type

C examples for Data Type:int

Description

Sums integers entered interactively

Demo Code

#include <stdio.h>

int main(void){
    long num;// w  ww .  j  av  a  2s.  co m
    long sum = 0L;      /* initialize sum to zero   */
    int status;
    
    printf("Please enter an integer to be summed ");
    printf("(q to quit): ");
    status = scanf("%ld", &num);

    while (status == 1) 
    {
        sum = sum + num;
        printf("Please enter next integer (q to quit): ");
        status = scanf("%ld", &num);
    }
    printf("Those integers sum to %ld.\n", sum);

    return 0;
}

Result


Related Tutorials