Created
April 5, 2021 08:07
-
-
Save nathy-mesquita/cf06bc0ae9b21e0d237b15a4fd03667a to your computer and use it in GitHub Desktop.
Soma In C#
This file contains 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
/// <summary> | |
/// Soma de vários inteiros | |
/// </summary> | |
/// <param name="numeros">Números que deseja somar</param> | |
/// <example>SomarVarios(1, 2, 4, 5, 6)</example> | |
/// <returns>Soma</returns> | |
public static int SomarVarios(params int[] numeros) | |
{ | |
int soma = 0; | |
foreach (int numero in numeros) | |
{ | |
soma += numero; | |
} | |
return soma; | |
} | |
/// <summary> | |
/// Soma de pares. Caso a lista seja impar é ignorado o último item da lista. | |
/// </summary> | |
/// <param name="numeros">Números que deseja somar</param> | |
/// <example>SomarNumeros(new int[] {1,2,3,4,5,6,7});</example> | |
public static void SomarNumeros (int[] numeros) | |
{ | |
for (int i = 0; i < numeros.Length-1; i+=2) | |
{ | |
int primeiroNumero = numeros[i]; | |
int segundoNumero = numeros[i + 1]; | |
int soma = primeiroNumero + segundoNumero; | |
Console.WriteLine($"{primeiroNumero} + {segundoNumero} = {soma}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment