Skip to main content

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:

Prime Numbers

Numbers divisible only by 1 and themselves

Odd Numbers

Numbers not divisible by 2

Multiples of 7

Numbers divisible by 7

Solution

Finding Prime Numbers

To find prime numbers, we use a nested loop to check if a number has any divisors:
//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');
  }
}
1

Iterate through the range

The outer loop goes from 100 to 150
2

Check for divisors

The inner loop tests if the number is divisible by any number from 2 to i-1
3

Count divisors

If we find a divisor, increment the counter
4

Determine if prime

If the counter is 0, the number has no divisors and is therefore prime

Finding Odd Numbers

For odd numbers, we can start at 101 and increment by 2:
//Números Impares
print('\nNúmeros Impares');
for (var i = 101; i < 150; i += 2) {
  print('Número Impar: $i');
}
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.

Finding Multiples of 7

For multiples of 7, we use the modulo operator:
//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');
  }
}
The modulo operator % returns the remainder of a division. If i % 7 == 0, it means i is perfectly divisible by 7.

Complete Program

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

For Loops

Used to iterate over a range of numbers

Nested Loops

A loop inside another loop, useful for complex algorithms

Modulo Operator

% returns the remainder of a division

Loop Optimization

Using i += 2 skips unnecessary iterations

Practice Challenges

Modify the program to work with a range of 1 to 1000. What optimizations could you make to the prime number algorithm?
Add a section to print all even numbers in the range.
Add a section to print all perfect squares (4, 9, 16, 25, etc.) in the range.

Build docs developers (and LLMs) love