Created with <3 with dartpad.dev.
Created
October 31, 2023 01:40
-
-
Save leonus96/4515fa79295122591fd293ca860027e8 to your computer and use it in GitHub Desktop.
1. suma-pares
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Ejercicio 1 - Suma de Números Pares: | |
| Escribe una función que tome un número n | |
| como parámetro y devuelva la suma de todos | |
| los números pares desde 1 hasta n. | |
| * | |
| * */ | |
| /// Como hallar un número par?: | |
| /// % => Operador módulo | |
| /// Devuelve el residuo de una operación | |
| /// 6 % 2 = 0 --> par | |
| /// 5 % 2 = 1 --> inpar | |
| void main() { | |
| print(sumaPares(4)); | |
| } | |
| /// Caso: | |
| /// n = 4 | |
| /// 2 + 4 = 6 | |
| int sumaPares(int n) { | |
| int suma = 0; | |
| for (int i = 1; i <= n; i++) { | |
| if (i % 2 == 0) { | |
| suma = suma + i; | |
| } | |
| } | |
| return suma; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment