Exercícios de Estruturas de Repetição em C++ com Exemplos Práticos

Grego5,67 KB

Exercícios de Estruturas de Repetição em C++

A seguir estão exercícios práticos sobre estruturas de repetição em linguagem C++, com foco em laços for e while, estruturas condicionais e acumuladores.

1. Contagem de valores maiores que 10

#include <iostream>
using namespace std;

int main() {
    int x, i, c = 0;
    for (i = 1; i <= 100; i++) {
        cout << "Entre com um valor: ";
        cin >> x;
        if (x > 10) {
            c++;
        }
    }
    cout << "Número de valores maiores do que 10: " << c;
    system("pause");
    return 0;
}

2. Soma de valores menores que 100

#include <iostream>
using namespace std;

int main() {
    int x, i, s = 0;
    for (i = 1; i <= 200; i++) {
        cout << "Entre com um valor: ";
        cin >> x;
        if (x < 100) {
            s += x;
        }
    }
    cout << "Soma de valores menores do que 100: " << s;
    system("pause");
    return 0;
}

3. Multiplicação de valores entre 10 e 20

#include <iostream>
using namespace std;

int main() {
    int i;
    float x, p = 1;
    for (i = 1; i <= 10; i++) {
        cout << "Entre com um valor: ";
        cin >> x;
        if (x > 10 && x < 20) {
            p *= x;
        }
    }
    cout << "Multiplicação: " << p << "\n";
    system("pause");
    return 0;
}

4. Média aritmética dos valores pares

#include <iostream>
using namespace std;

int main() {
    float s = 0, c = 0, x;
    int i;
    for (i = 1; i <= 200; i++) {
        cout << "Entre com um valor: ";
        cin >> x;
        if ((int)x % 2 == 0) {
            s += x;
            c++;
        }
    }
    if (c > 0) {
        cout << "Média Aritmética: " << s / c << "\n";
    } else {
        cout << "Nada a calcular\n";
    }
    system("pause");
    return 0;
}

5. Percentual de valores negativos

#include <iostream>
using namespace std;

int main() {
    int c = 0, i, x;
    for (i = 1; i <= 100; i++) {
        cout << "Entre com um valor: ";
        cin >> x;
        if (x < 0) {
            c++;
        }
    }
    cout << "Percentual: " << c << "\n";
    system("pause");
    return 0;
}

6. Média aritmética de valores ímpares maiores que 100 (com repetição controlada)

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    float s = 0, c = 0;
    int x;
    char resp = 's';
    while (resp == 's') {
        cout << "Entre com um valor: ";
        cin >> x;
        if (x % 2 != 0 && x > 100) {
            s += x;
            c++;
        }
        cout << "Deseja entrar com mais algum valor (s/n)? ";
        cin >> resp;
        fflush(stdin);
    }
    if (c > 0) {
        cout << "Média Aritmética: " << s / c << "\n";
    } else {
        cout << "Nada a calcular\n";
    }
    system("pause");
    return 0;
}

7. Percentual de pares entre 20 e 50

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int c = 0, ct = 0, x;
    char resp = 's';
    while (resp == 's') {
        cout << "Entre com um valor: ";
        cin >> x;
        if (x % 2 == 0 && x > 20 && x < 50) {
            c++;
        }
        ct++;
        cout << "Deseja entrar com mais algum valor (s/n)? ";
        cin >> resp;
        fflush(stdin);
    }
    cout << "Percentual: " << c * 100.0 / ct << "\n";
    system("pause");
    return 0;
}

8. Maior valor digitado

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int x, maior, primeiro = 1;
    char resp = 's';
    while (resp == 's') {
        cout << "Entre com um valor: ";
        cin >> x;
        if (primeiro) {
            maior = x;
            primeiro = 0;
        } else if (x > maior) {
            maior = x;
        }
        cout << "Deseja entrar com mais algum valor (s/n)? ";
        cin >> resp;
        fflush(stdin);
    }
    cout << "Maior: " << maior << "\n";
    system("pause");
    return 0;
}

9. Verificação de número primo

#include <iostream>
using namespace std;

int main() {
    int x, i, c = 0;
    cout << "Entre com um valor: ";
    cin >> x;
    for (i = 1; i <= x; i++) {
        if (x % i == 0) {
            c++;
        }
    }
    if (c == 2) {
        cout << "Valor é número primo\n";
    } else {
        cout << "Valor NÃO é número primo\n";
    }
    system("pause");
    return 0;
}

10. Verificação de números primos com repetição

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int x, i, c;
    char resp = 's';
    while (resp == 's') {
        cout << "Entre com um valor: ";
        cin >> x;
        c = 0;
        for (i = 1; i <= x; i++) {
            if (x % i == 0) {
                c++;
            }
        }
        if (c == 2) {
            cout << "Valor é número primo\n";
        } else {
            cout << "Valor NÃO é número primo\n";
        }
        cout << "Deseja entrar com mais algum valor (s/n)? ";
        cin >> resp;
        fflush(stdin);
    }
    system("pause");
    return 0;
}

Baixar

PDF Word HTML