Computing a Sum or Product in a Loop :

Loops are frequently used to compute cumulative results such as sums or products.
In such cases, a variable is initialized before the loop begins and is updated
during each iteration of the loop. By the time the loop terminates, the variable
contains the desired cumulative result.

This technique is widely used in numerical computations and data processing
applications.
EXAMPLE:

#include <stdio.h>

int main() {
    int a, b, total;
    int i =0;

    

    while (i <= 1) {
        printf("Enter two numbers: ");
        scanf("%d %d", &a, &b);

        total = a + b; 

        printf("The total is: %d\n", total);
        
        i++; 
    }

    return 0;
}

Output:

Enter two numbers: 10
10
The total is: 20
Enter two numbers: 10
20
The total is: 30
Previous Next