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:46

Título: C# Obtener diferentes dígitos de cifra pedir datos por consola Console.ReadLine
Publicado por: Dimitar Stefanov en 18 de Julio 2016, 20:46
Ejercicio:

Citar
Write a program that takes as input a four-digit number in format abcd (e.g. 2011) and performs the following:

    Calculates the sum of the digits (in our example 2 + 0 + 1 + 1 = 4) and prints it on the console.
    Prints on the console the number in reversed order: dcba (in our example 1102) and prints the reversed number.
    Puts the last digit in the first position: dabc (in our example 1201) and prints the result.
    Exchanges the second and the third digits: acbd (in our example 2101) and prints the result.

El código:

Código: [Seleccionar]
using System;

class Program
{
static void Main()
{
int num;
Console.Write("Introduzca un número entero de 4 dígitos: ");
num = int.Parse(Console.ReadLine());
int primerNumero = num%10;
num = num / 10;
int segundoNumero = num%10;
num = num/10;
int tercerNumero = num%10;
num = num/10;
int cuartoNumero = num%10;
Console.WriteLine("Suma: "+(primerNumero+segundoNumero+tercerNumero+cuartoNumero));
Console.WriteLine("Invertidos: "+primerNumero.ToString()+segundoNumero.ToString()+tercerNumero.ToString()+cuartoNumero.ToString());
Console.WriteLine("El último dígito en primer lugar: " + primerNumero.ToString()
+cuartoNumero.ToString() + tercerNumero.ToString() + segundoNumero.ToString());
Console.WriteLine("Invertir el lugar del segundo y el tercer dígito: "+cuartoNumero.ToString()+segundoNumero.ToString()+tercerNumero.ToString()+primerNumero.ToString());
}
}

Saludos.