Ask Question
10 January, 14:32

program that reads numbers from scanf (keyboard) and then sums them, stopping when 0 has been entered. Construct three versions of this program, using the while, do-while, and for loops.

+3
Answers (1)
  1. 10 January, 15:22
    0
    I'm going to write three examples in C language:

    int main () {

    int n, i, sum = 0;

    printf ("Enter the number: ");

    scanf ("%d", &n);

    for (i = 1; i < = n; + +i) {

    sum + = i;

    }

    printf ("Sum = %d", sum);

    return 0;

    }

    int main () {

    int n, i, sum = 0;

    printf ("Enter a number: ");

    scanf ("%d", &n);

    i = 1;

    while (i < = n) {

    sum + = i;

    ++i;

    }

    printf ("Sum = %d", sum);

    return 0;

    }

    int main () {

    int n, i, sum = 0;

    do {

    printf ("Enter a number: ");

    scanf ("%d", &n);

    } while (n < = 0);

    for (i = 1; i < = n; + +i) {

    sum + = i;

    }

    printf ("Sum = %d", sum);

    return 0;

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “program that reads numbers from scanf (keyboard) and then sums them, stopping when 0 has been entered. Construct three versions of this ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers