Foros aprenderaprogramar.com

Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: Dimitar Stefanov en 18 de Julio 2016, 20:37

Título: C# leer un número desde consola y evaluar si es Par o Impar (ood ó even) ejemplo
Publicado por: Dimitar Stefanov en 18 de Julio 2016, 20:37
Ejercicio:

Citar
Write a program that reads an integer from the console, uses an expression to check if given integer is odd or even, and prints "even NUMBER" or "odd NUMBER", where you should print the input number's value instead of NUMBER.

El código:

Código: [Seleccionar]
using System;

class Program
{
static void Main()
{
sbyte a = 31;
while(a < -30 || a > 30)
{
Console.WriteLine("Introduzca un número entero entre -30 y 30: ");
a = Convert.ToSByte(Console.ReadLine());
}
if(a % 2 == 0){
Console.WriteLine("El número: " + a + " es par");
}else{
Console.WriteLine("El número: " + a + " es impar");
}
}
}

Saludos.