Ask Question

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.

+4
Answers (1)
  1. 13 December, 23:39
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers