Jednoduchý program pro začátečníky na výpočet obsahu čtverce. Předpokládáme celočíselný vstup.
Výpočet obsahu:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int hrana; int obsah; Console.Write("Zadejte hranu čtverce: "); hrana = int.Parse(Console.ReadLine()); obsah = hrana * hrana; Console.WriteLine("Obsah čtverce o hraně {0} je {1}.", hrana, obsah); Console.Read(); } } }
Výpočet obsahu pomocí metody (funkce):
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int hrana; int obsah; Console.Write("Zadejte hranu čtverce: "); hrana = int.Parse(Console.ReadLine()); obsah = ObsahCtverce(hrana); Console.WriteLine("Obsah čtverce o hraně {0} je {1}.", hrana, obsah); Console.Read(); } static int ObsahCtverce(int hranaCtverce) { return hranaCtverce * hranaCtverce; } } }