samedi 6 mars 2021

how can i break a loop by checking a condition

I am trying to sum some integers and when I press '=' the loop will terminate and print the sum of those values. I am trying to give a space before press equal.

#include <stdio.h>

int main()
{
    int a, sum = 0;
    char b;

    while (1) {
        scanf("%d", &a);
        scanf(" %c", &b);

        sum += a;

        if(b == '=')
            break;
    }

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

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire