Curso C# SoloLearn – Códigos de las practicas

Los siguientes códigos son de las prácticas del curso C# de SoloLearn


Vamos a escribir un programa que calculará el área de un círculo. El área encerrada por un círculo de radio r es πr², donde π (pi) es la relación constante entre la circunferencia de cualquier círculo y su diámetro, y r es el radio. El programa dado declara una variable pi constante con un valor de 3,14. Complete el programa para tomar el radio como entrada, luego calcule y genere el área del círculo.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            const double pi = 3.14;
            double radius;
            
            radius = Double.Parse(Console.ReadLine());
            double area = pi * (radius*radius);
         
            Console.WriteLine(area);    
            
        }
    }
}

Múltiplo de 3
Eres un maestro de escuela primaria y explicas la multiplicación a los estudiantes. Usarás la multiplicación por 3 como ejemplo. El programa que se le proporciona toma el número N como entrada.
Escriba un programa para generar todos los números del 1 al N, reemplazando todos los números que son múltiplos de 3 por «*».
Entrada de muestra 7 Salida de muestra 12 * 45 * 7
using System;
using System.Collections.Generic;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = Convert.ToInt32(Console.ReadLine());
            
          for (int i=1;i<=number;i++)
          
           if (i%3==0)
           {Console.WriteLine("*"); } 
           else 
           { Console.WriteLine(i);}
            
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {

            myFunc();
            
        }
        static void myFunc()
        {
            Console.WriteLine("This is my first method");
            
        }
    }
}

El código a entregar es:

using System;
using System.Collections.Generic;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
              int levels = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine(Points(levels));
        }
        

        
        static int Points(int levels)
        {
     
           if (levels == 1)
                  return 1;
              return levels + Points(levels - 1);
        }

    }

}

Mostrará lo siguiente:



Clases y objetos
Social Network


You are making a social network application and want to add post creation functionality.
As a user creates a post, the text «New post» should be automatically outputted so that then the user can add the text he/she wants to share.
The program you are given declares a Post class with a text private field, and the ShowPost() method which outputs the content.
Complete the class with
– a constructor, which outputs «New post» as called,
– Text property, which will allow you to get and set the value of the text field.

Once you have made the changes to the program so that it works correctly, then in main, the program will take the text of the post from the user, create a post object, assign the taken value to the text field and output it.

Sample Input
Hello!

Sample Output
New post
Hello!

el código de respuesta es:

using System;
using System.Collections.Generic;

namespace Code_Coach_Challenge
{
    class Program
    {
        static void Main(string[] args)
        {
            string postText = Console.ReadLine();

            Post post = new Post();
            post.Text = postText;
            post.ShowPost();

        }
    }

    class Post
    {
        private string text;
        //write a constructor here
        public Post()
        {
            Console.WriteLine("New post");
        }        

        public void ShowPost()
        {
            Console.WriteLine(text);
        }
        //write a property for member text
        public string Text
        {
            get{return text;}
            set{text = value;}
        }
    }
}

Arreglos y cadenas

Words

The program you are given defines an array with 10 words and takes a letter as input.
Write a program to iterate through the array and output words containing the taken letter.
If there is no such word, the program should output «No match».

Sample Input
u

Sample Output
fun
Recall the Contains() method.

using System;
using System.Collections.Generic;

namespace Code_Coach_Challenge
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] words = {
                "home",
                "programming",
                "victory",
                "C#",
                "football",
                "sport",
                "book",
                "learn",
                "dream",
                "fun"
            };

            string letter = Console.ReadLine();

            int count = 0;
            int y =1;
            while (
count < 9)
            {
              if(words[count].Contains(letter))
              {
               Console.WriteLine(words[count]);
               y += count;
              }
              count ++;
            }
             if(y == 0 || y == 1)
             {
                 Console.WriteLine("No match");
             }
            
        }
    }
}

Dance


In a ballroom dancing competition, each dancer from a pair is evaluated separately, and then their points are summed up to get the total pair score.
The program you are given takes the names and the points of each dancer as input and creates a DancerPoints objects for each dancer, using the taken name and score values as parameters for constructors.
Complete the given class, using overload + operator to return an new object where the names of dancers are in one string (see sample output) and the score is equal to the sum of their points.
The declaration of that object and the output of its points are already written in Main().

Sample Input
Dave
8
Jessica
7

Sample Output
Dave & Jessica
15

using System;
using System.Collections.Generic;

namespace Code_Coach_Challenge
{
    class Program
    {
        static void Main(string[] args)
        {
            string name1 = Console.ReadLine();
            int points1 = Convert.ToInt32(Console.ReadLine());
            string name2 = Console.ReadLine();
            int points2 = Convert.ToInt32(Console.ReadLine());

            DancerPoints dancer1 = new DancerPoints(name1, points1);
            DancerPoints dancer2 = new DancerPoints(name2, points2);

            DancerPoints total = dancer1 + dancer2;
            Console.WriteLine(total.name);
            Console.WriteLine(total.points);
        }
    }

    class DancerPoints
    {
        public string name;
        public int points;
        public DancerPoints(string name, int points)
        {
            this.name = name;
            this.points = points;
        }

        //overload the + operator
           public static DancerPoints operator+ (DancerPoints a, DancerPoints b) {
            string name = a.name +" "+"&"+" "+ b.name ;
            int points = a.points + b.points ;
            DancerPoints total = new DancerPoints (name, points);
            return total ;
        }

    }
}

using System;
using System.Collections.Generic;

namespace Code_Coach_Challenge
{
    class Program
    {
        static void Main(string[] args)
        {
            Draw pencil = new Draw();
            Draw brush = new Brush();
            Draw spray = new Spray();

            pencil.StartDraw();
            brush.StartDraw();
            spray.StartDraw();

        }
    }

    /*
    Draw => "Using pencil"
    Brush => "Using brush"
    Spray => "Using spray"
    */

    public interface IDraw
    {
        void StartDraw();
    }

    class Draw : IDraw
    {
        public virtual void StartDraw()
        {
            Console.WriteLine("Using pencil");
        }
    }

    //inherit this class from the class Draw
    class Brush:Draw
    {
        //implement the StartDraw() method
            public override  void StartDraw()
        {
            Console.WriteLine("Using brush");
        }
    }

    //inherit this class from the class Draw
    class Spray:Draw
    {
        //implement the StartDraw() method
          public override void StartDraw()
        {
            Console.WriteLine("Using spray");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
           

          
try {
  int drinks = Convert.ToInt32(Console.ReadLine());
  int shelves = Convert.ToInt32(Console.ReadLine());
  Console.WriteLine(drinks / shelves);
}
catch (DivideByZeroException e) {
  Console.WriteLine("At least 1 shelf");
}


catch(Exception e) {
  Console.WriteLine("Please insert an integer");
}

         
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            int discount = Convert.ToInt32(Console.ReadLine());

            Dictionary<string, int> coffee = new Dictionary<string, int>();
            coffee.Add("Americano", 50);
            coffee.Add("Latte", 70);
            coffee.Add("Flat White", 60);
            coffee.Add("Espresso", 60);
            coffee.Add("Cappuccino", 80);
            coffee.Add("Mocha", 90);


            //your code goes here
foreach (KeyValuePair<string, int> entry in coffee)
            {
                Console.WriteLine(entry.Key + ": " + (entry.Value - entry.Value * discount / 100));
            }


        }
    }
}

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.