> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/NormandoRamirezDelgado/6C-Febrero-2026/llms.txt
> Use this file to discover all available pages before exploring further.

# Loop Exercises

> Practice working with loops in Dart through practical exercises

## Exercise Overview

This exercise demonstrates how to use loops to solve mathematical problems. We'll create a program that finds and prints:

* Prime numbers
* Odd numbers
* Multiples of 7

All within the range of 100 to 150.

## Problem Statement

Create a program that prints the following within the range of 100 to 150:

<CardGroup cols={3}>
  <Card title="Prime Numbers" icon="1">
    Numbers divisible only by 1 and themselves
  </Card>

  <Card title="Odd Numbers" icon="2">
    Numbers not divisible by 2
  </Card>

  <Card title="Multiples of 7" icon="3">
    Numbers divisible by 7
  </Card>
</CardGroup>

## Solution

### Finding Prime Numbers

To find prime numbers, we use a nested loop to check if a number has any divisors:

```dart theme={null}
//Números Primos
print('\nNúmeros Primos');
for (var i = 100; i <= 150; i++) {
  int contador = 0;
  for (var j = 2; j <= i - 1; j++) {
    if (i % j == 0){
      contador++;
    }
  }
  if (contador == 0){
    print('El Número $i es Primo');
  }
}
```

<Accordion title="How the prime number algorithm works">
  <Steps>
    <Step title="Iterate through the range">
      The outer loop goes from 100 to 150
    </Step>

    <Step title="Check for divisors">
      The inner loop tests if the number is divisible by any number from 2 to i-1
    </Step>

    <Step title="Count divisors">
      If we find a divisor, increment the counter
    </Step>

    <Step title="Determine if prime">
      If the counter is 0, the number has no divisors and is therefore prime
    </Step>
  </Steps>
</Accordion>

### Finding Odd Numbers

For odd numbers, we can start at 101 and increment by 2:

```dart theme={null}
//Números Impares
print('\nNúmeros Impares');
for (var i = 101; i < 150; i += 2) {
  print('Número Impar: $i');
}
```

<Note>
  By starting at 101 (an odd number) and incrementing by 2, we skip all even numbers automatically. This is much more efficient than checking each number.
</Note>

### Finding Multiples of 7

For multiples of 7, we use the modulo operator:

```dart theme={null}
//Múltiplos de 7
print('\nNúmeros Múltiplos de 7');
for (var i = 100; i < 150; i++) {
  if (i % 7 == 0){
    print('Número Múltiplo de 7: $i');
  }
}
```

<Tip>
  The modulo operator `%` returns the remainder of a division. If `i % 7 == 0`, it means i is perfectly divisible by 7.
</Tip>

## Complete Program

```dart theme={null}
void main() {
  //Hacer un programa que imprima dentro del rango comprendido del 100 al 150 lo siguiente:
  //- Números primos 
  //- Números Impares 
  //- Múltiplos de 7

  //Números Primos
  print('\nNúmeros Primos');
  for (var i = 100; i <= 150; i++) {
    int contador = 0;
    for (var j = 2; j <= i - 1; j++) {
      if (i % j == 0){
        contador++;
      }
    }
    if (contador == 0){
      print('El Número $i es Primo');
    }
  }

  //Números Impares
  print('\nNúmeros Impares');
  for (var i = 101; i < 150; i += 2) {
    print('Número Impar: $i');
  }

  //Múltiplos de 7
  print('\nNúmeros Múltiplos de 7');
  for (var i = 100; i < 150; i++) {
    if (i % 7 == 0){
      print('Número Múltiplo de 7: $i');
        
    }
  }
}
```

## Key Concepts

<CardGroup cols={2}>
  <Card title="For Loops" icon="repeat">
    Used to iterate over a range of numbers
  </Card>

  <Card title="Nested Loops" icon="layer-group">
    A loop inside another loop, useful for complex algorithms
  </Card>

  <Card title="Modulo Operator" icon="percent">
    `%` returns the remainder of a division
  </Card>

  <Card title="Loop Optimization" icon="gauge-high">
    Using `i += 2` skips unnecessary iterations
  </Card>
</CardGroup>

## Practice Challenges

<AccordionGroup>
  <Accordion title="Challenge 1: Extend the range">
    Modify the program to work with a range of 1 to 1000. What optimizations could you make to the prime number algorithm?
  </Accordion>

  <Accordion title="Challenge 2: Even numbers">
    Add a section to print all even numbers in the range.
  </Accordion>

  <Accordion title="Challenge 3: Perfect squares">
    Add a section to print all perfect squares (4, 9, 16, 25, etc.) in the range.
  </Accordion>
</AccordionGroup>
