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); } } }
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:
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;} } } }
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"); } } } }
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)); } } } }