Skip to content

Instantly share code, notes, and snippets.

@HugoRoca
Created November 18, 2022 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoRoca/1430848cf938359deadc6d7f52ecac5a to your computer and use it in GitHub Desktop.
Save HugoRoca/1430848cf938359deadc6d7f52ecac5a to your computer and use it in GitHub Desktop.
using System;
namespace week06
{
internal class Program
{
static void Main(string[] args)
{
example1();
}
static void example1()
{
// Declaramos la variable el cual la usaremos para que almacene lo ingresado
int input;
do
{
// Pintamos en pantalla
Console.Write("Ingresa un numero: ");
// Solicitamos al usuario ingresar el valor
input = int.Parse(Console.ReadLine() ?? "0");
// Validamos si la variable y en caso que sea 0,
// saltamos el bucle y volvemos a solicitar
if (input <= 0) continue;
double result = 0;
// Empezamos con le for y como tope será nuestro valor ingresado
for (int i = 1; i <= input; i++)
{
// Al no usar el método pow, realizamos la
// potencia al cuadrado multiplicando asi mismo 2 veces
result += (i * i);
}
// Pintamos el resultado final
Console.WriteLine("Result is: {0}", Math.Round(result, 4));
// El bucle termina siempre y cuando el numero ingresado es mayor que cero
} while (input <= 0);
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment