martes, 12 de abril de 2016

visual studio C# para principiantes

PROGRAMAS PARA CREAR EN C#

Ya conociendo, como ingresar valores desde teclado y las operaciones y jerarquías de las variables hacer los siguientes programas

1. Hacer un programa que calcule el perímetro de cualquier polígono regular. Debemos tener en cuenta que es que es un poligono regular.
2. Hacer un programa que transforme de grados a radianes.
3. Hacer un programa que transforme de grados centígrados a grados Fahrenheit
4. Hacer un programa que transforme entre dólares y euros y que también pida el tipo de cambio del día

PROGRAMAS RESUELTOS TOMA DE DECISIONES

1. Programa para determinar si el numero ingresado es negativo o positivo. Considere el numero cero como positivo

        static void Main(string[] args)//Desde aqui empieza el programa principal
        {
            //Declaramos variables
            int numero = 0;
            String texto = "";

            //Ingresamos el numero a consultar
            Console.WriteLine("Ingrese el numero a consultar");
            texto = Console.ReadLine();
            numero = Convert.ToInt32(texto);

            //Usamos la decision if ... else
            if (numero >= 0)
                Console.WriteLine("El NUMERO {0} ES POSITIVO", numero);
            else
                Console.WriteLine("El NUMERO {0} ES NEGATIVO", numero);
            Console.ReadKey();
        }
    
2. Programa para ingresar dos números e ingresar la operación aritmética que deseamos hacer: SUMA, RESTA, MULTIPLICACIÓN Y DIVISIÓN  

static void Main(string[] args)
        {//Declaramos e inicializamos variables
            float a = 0.0f, b = 0.0f, resultado = 0.0f;
            string texto = "";
            int opcion = 0;

            //Mostramos Menú
            Console.WriteLine("OPERACIONES ARITMETICAS");
            Console.WriteLine("1. SUMA");
            Console.WriteLine("2. RESTA");
            Console.WriteLine("3. MULTIPLICACION");
            Console.WriteLine("4. DIVISION");
            Console.WriteLine( );

            //Ingresar la operacion que quedemos realizar
            Console.WriteLine("INGRESE LA OPCION DE LA OPERACION QUE QUIERE REALIZAR");
            texto = Console.ReadLine();
            opcion = Convert.ToInt32(texto);

            // Ingresamos el primer numero
            Console.WriteLine("Ingrese el primero numero");
            texto = Console.ReadLine();
            a = Convert.ToSingle(texto);

            //Ingresamos el segundo numero
            Console.WriteLine("Ingrese el segundo numero");
            texto = Console.ReadLine();
            b = Convert.ToSingle(texto);

            //CONDICION SUMA
            if (opcion == 1)
                resultado = a + b;
            else if (opcion == 2)
                resultado = a - b;
            else if (opcion == 3)
                resultado = a * b;
            else if (opcion == 4 && b != 0)
                resultado = a / b;

            //Mostrar resultado
            Console.WriteLine("El resultado es = {0}", resultado);
            Console.ReadKey();

         }

3.Programa para ingresar el numero del dia de la semana y nos salga el nombre del día

 static void Main(string[] args)
        {
            //Declaramos e iniciamos variables
            int opcion = 0;
            string texto = "";

            //Ingresamos el numero del dia de la semana
            Console.WriteLine("INGRESE EL NUMERO DE L DIA DE LA SEMANA");
            texto = Console.ReadLine();
            opcion = Convert.ToInt32(texto);

            Console.WriteLine(); // Damos un salto de linea

            //condiciones
            if (opcion == 1)
                Console.WriteLine("El dia {0} de la semana es LUNES",opcion);
            if (opcion == 2)
                Console.WriteLine("El dia {0} de la semana es MARTES", opcion);
            if (opcion == 3)
                Console.WriteLine("El dia {0} de la semana es MIERCOLES", opcion);
            if (opcion == 4)
                Console.WriteLine("El dia {0} de la semana es JUEVES", opcion);
            if (opcion == 5)
                Console.WriteLine("El dia {0} de la semana es VIERNES", opcion);
            if (opcion == 6)
                Console.WriteLine("El dia {0} de la semana es SABADO", opcion);
            if (opcion == 7)
                Console.WriteLine("El dia {0} de la semana es DOMINGO", opcion);

            Console.ReadLine();

        }

DECISIONES if ó if ... else

1. Hacer una programa que pueda calcular el perímetro y el área de cualquier polígono regular, pero que le pregunte al usuario qué desea calcular.

2. Hacer un programa que calcule el IGV de un producto, pero coloque un impuesto del 0% si el producto es ARROZ

OTROS PROGRAMAS A DESARROLLAR PARA PRACTICAR

1. Programa que ingrese una letra y que diga si la letra es una vocal o no es vocal
2. Programa para ingresar tres números y nos diga cual es el orden de ellos de  mayor a menor
3. Programa que ingrese un numero y que nos muestre su tabla de multiplicación
4. Programa para ingresar dos números y nos calcule su diferencia de cuadrados

ESTRUCTURAS REPETITIVAS O CICLOS

EL CICLO WHILE (CICLO MIENTRAS)

USO:   while (condición) 
            {
                 Sentencias
            }

EJERCICIOS: 
  1. Programa que escriba en pantalla los números del 1 al 10, usando "while".
  2. Programa que escriba en pantalla los números pares del 26 al 10 (descendiendo) 
    usando "while".
EL CICLO do ... while (CICLO  hacer ... MIENTRAS)

USO:   do 
              {
              Sentencias
              }
            while (condición)


    EJERCICIOS: 
    1. Programa que ingrese números positivos, y vaya calculando la suma
      1. de todos ellos (terminará cuando se teclea un número negativo o cero).
    2. Programa que escriba en pantalla los números del 1 al 10, usando "do..while".
    3. Programa que escriba en pantalla los números pares del 26 al 10 (descendiendo), 

      usando "do..while".
    4. Programa que pida al usuario su nombre y su contraseña, y no le permita 

      seguir hasta que introduzca como nombre "Pedro" y como contraseña "Peter".

    CICLO FOR (CICLO PARA)


    USO:  for (valorInicial; Condición; Incremento)
                    Sentencia;

    EJEMPLO programa que cuente del 1 al 10

    {//declaramos variable
       int i;
       for (i=1; i <= 10 ; i=i+1)
             Console.WriteLine("{0}", i);
             Console.ReadKey();


    EJERCICIOS
    1. Crear un programa que muestre los números del 15 al 5, descendiendo (pista: en cada pasada habrá que descontar 1, por ejemplo haciendo i=i-1, que se puede abreviar i--).
    2. Crear un programa que muestre los primeros ocho números pares (pista: en cada pasada habrá que aumentar de 2 en 2, o bien mostrar el doble del valor que hace de contador).


    PROGRAMAS RESUELTOS

    1. FACTORIAL DE UN NUMERO

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication9
    {
        class Program
        {
            static void Main(string[] args)
            {//Declaramos variable e inicializamos
              int num = 0;
                int i = 0, factorial = 1;
    
                Console.WriteLine("INGRESAMOS NUMERO PARA CALCULAR FACTORIAL");
                num = Convert.ToInt32(Console.ReadLine());
    
    
                for ( i = 1; i<=num ; i=i+1 )
                {
                    factorial = factorial * i;
                }
    
                    Console.Write("El factorial del {0} es igual {1}",num, factorial);
                Console.ReadKey();
            }
        }
    }
    
    
    2. INGRESAR 2 NUMEROS Y REALIZAR SUS OPERACIONES ARITMETICAS,
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {//Declaramos e inicializamos variables
                float a = 0.0f, b = 0.0f, resultado = 0.0f;
                int opcion = 0;
                string texto = "";
    
                // Mostramos el menu
    
                Console.WriteLine("OPERACIONES ARITMETICAS");
                Console.WriteLine();// PARA DAR UNA LINEA EN BLANCO
                Console.WriteLine("1. SUMA");
                Console.WriteLine("2. RESTA");
                Console.WriteLine("3. MULTIPLICACION");
                Console.WriteLine("4. DIVISION");
                Console.WriteLine(); // PARA DAR UNA LINEA EN BLANCO
    
                //INGRESAMOS NUMEROS
                Console.WriteLine("INGRESE PRIMER NUMERO");
                texto = Console.ReadLine();
                a = Convert.ToInt32(texto);
    
                Console.WriteLine("INGRESE SEGUNDO NUMERO");
                texto = Console.ReadLine();
                b = Convert.ToInt32(texto);
    
                //INGRESAMOS LA OPCION
                Console.WriteLine("INGRESE OPCION DE LA OPERACION A REALIZAR");
                texto = Console.ReadLine();
                opcion = Convert.ToInt32(texto);
    
                if (opcion == 1)
                    resultado = a + b;
                else
                    if (opcion == 2)
                    resultado = a - b;
                else
                if (opcion == 3)
                    resultado = a * b;
                else
                if (opcion == 4)
                    if (b != 0)
                        resultado = a / b;
                    else Console.WriteLine("ERROR DE DIVISION NO SE ACEPTA EL 0");
    
                Console.WriteLine();//espacio en blanco
                Console.WriteLine("EL RESULTADO ES = {0}", resultado);
                Console.ReadKey();
                {
    
                }
    
            }
        }
    }
    
    
    3. PROGRAMA PARA IDENTIFICAR SI UN NUMERO ES POSITIVO O NEGATIVO. SI ES CERO SALIR SINO SEGUIR INGRESANDO UN NUMERO
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication6
    {
        class Program
        {
            static void Main(string[] args)
            {//Declaramos e inicializamos variables
                int numero = 0;
                string texto = "";
    
                //Ingresar numero
                Console.WriteLine("INGRESE NUMERO (0 para salir) :");
                texto = Console.ReadLine();
                numero = Convert.ToInt32(texto);
                //numero = Convert.ToInt32(Console.ReadLine());
    
                while (numero != 0)
                {
                    if (numero > 0)
                        Console.WriteLine("EL NUMERO {0} ES POSITIVO", numero);
                    else
                        Console.WriteLine("EL NUMERO {0} ES NEGATIVO", numero);
    
                    //Ingresar numero
                    Console.WriteLine();
                    Console.WriteLine("INGRESE NUMERO (0 para salir) :");
                    texto = Console.ReadLine();
                    numero = Convert.ToInt32(texto);
    
                    //Console.ReadKey();
                }
                Console.ReadKey();
            }
        }
    }

    PROGRAMAS PROPUESTOS

    1. HACER UN PROGRAMA QUE CALCULE EL PROMEDIO DE EDAD DE UN GRUPO DE PERSONAS Y DIGA CUAL ES LA EDAD MAS GRANDE Y CUAL ES LA EDAD MAS JOVEN
    2. HACER UN PROGRAMA QUE INGRESE 2 NÚMEROS ENTEROS Y DIGA CUAL ES EL MAYOR DE ELLOS
    3. HACER UN PROGRAMA PARA MULTIPLICAR 2 NÚMEROS. SI SE INGRESA CERO TE DARÁ UN MENSAJE: "CUALQUIER NUMERO MULTIPLICADO POR CERO ES CERO". SINO QUE INGRESE EL SEGUNDO NUMERO Y REALICE LA OPERACIÓN.
    4. PROGRAMA PARA INGRESAR DOS NÚMEROS REALES. SI EL SEGUNDO NO ES CERO, MOSTRARA EL RESULTADO DE DIVIDIR. POR EL CONTRARIO, SI EL SEGUNDO NUMERO ES CERO ESCRIBIRÁ "ERROR: NO SE PUEDE DIVIDIR ENTRE CERO"
    5. PROGRAMA PARA INGRESAR DOS NUMERO ENTEROS Y DIGA "UNO DE LOS NÚMEROS ES POSITIVO", "LOS DOS NÚMEROS SON POSITIVOS", O BIEN "NINGUNO DE LOS NÚMEROS SON POSITIVOS" SEGÚN CORRESPONDA
    6. PROGRAMA PARA INGRESAR 2 NÚMEROS ENTEROS Y DIGA SI SON IGUALES O EN CASO CONTRARIO DIGA CUAL ES EL MAYOR DE ELLOS.
    7. CREAR UN PROGRAMA QUE LEA UNA LETRA TECLEADA Y DIGA SI SE TRATA DE UNA VOCAL, UNA CIFRA NUMÉRICA O UNA CONSONANTE.
    8. PROGRAMA PARA CALCULAR CUANTAS CIFRAS TIENE UN NUMERO.
    9. PROGRAMA QUE PIDA NÚMEROS POSITIVOS AL USUARIO Y VAYA CALCULANDO LA SUMA DE TODOS ELLOS. TERMINARA CUANDO SE INGRESE CERO O NEGATIVO.