CSharp examples for Language Basics:for
Using the for loop to print the average of 10 random numbers that are from 1 to 10.
class average//from w w w . ja va2 s .c o m { public static void Main() { int ttl = 0; int nbr = 0; int i = 0; System.Random rnd = new System.Random(); for ( i = 1; i <= 10; i++ ) { nbr = (int) rnd.Next(1,11); System.Console.WriteLine("Number {0} is {1}", (i), nbr); ttl += nbr; //add nbr to total } System.Console.WriteLine("\nThe total of the 10 numbers is {0}", ttl); System.Console.WriteLine("\nThe average of the numbers is {0}", ttl/10 ); } }