C# Console Programs: Digit Sum, Dice Rolls, Averages and Matrix Sums
The following collection of C# console methods demonstrates digit sums, dice rolls, coin tosses, averages, array squares and matrix calculations. Each method is written in a simple, readable style suitable for beginners.
1. Digit Sum of a Number
public static void vsotastevk()
{
int n, vsota = 0, r;
Console.WriteLine("Vnesi stevilo: ");
n = int.Parse(Console.ReadLine());
while (n != 0)
{
r = n % 10;
n = n / 10;
vsota = vsota + r;
}
Console.WriteLine("Vsota stevk stevila je: " + vsota);
Console.ReadLine();
}2. Dice Throw Simulation
public static void metanjekock()
{
Random randNum = new Random();
int Stevilometa = 1;
int kocka1;
int kocka2;
int stetje1 = 0;
int stetje2 = 0;
int stetje3 = 0;
for (Stevilometa = 1; Stevilometa < 1001; Stevilometa++)
{
kocka1 = randNum.Next(1, 7);
kocka2 = randNum.Next(1, 7);
if (kocka1 > kocka2) { stetje1++; }
if (kocka1 < kocka2) { stetje2++; }
if (kocka1 == kocka2) { stetje3++; }
}
Console.WriteLine("Rezultat kocka1>kocka2: " + stetje1 + " x");
Console.WriteLine("Rezultat kocka2>kocka1: " + stetje2 + " x");
Console.WriteLine("Rezultat kocka1=kocka2: " + stetje3 + " x");
Console.ReadLine();
}3. Average of Random Even Numbers
public static void povprecjestevil()
{
int[] tabela = new int[1000];
int j = 0;
int vsota = 0;
Random randNum = new Random();
for (int i = 0; i < 1000; i++)
{
j = randNum.Next(0, 101);
if (j % 2 != 0) { j++; }
tabela[i] = j;
vsota = vsota + j;
}
int povprecje = vsota / 1000;
Console.WriteLine(povprecje);
Console.ReadLine();
}4. Sum of Squares in an Array
public static int kvadratitabele(int[] tabela)
{
int vsota = 0;
int j = tabela.Length;
int num = 0;
int kvadrat = 0;
for (int i = 0; i < j; i++)
{
num = tabela[i];
kvadrat = num * num;
vsota = kvadrat + vsota;
}
return vsota;
}5. Central and Diagonal Elements of a Matrix
public static void centralnielement()
{
Random rnd = new Random();
int[,] tabela = new int[9, 9];
int a = 0;
int b = 0;
int vsota = 0;
int vsotad = 0;
for (int i = 0; i < 9; i++)
{
vsota = 0;
for (int j = 0; j < 9; j++)
{
int suka = rnd.Next(1, 10);
tabela[i, j] = suka;
vsota = vsota + suka;
}
Console.WriteLine(vsota);
}
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
if (x == y)
{
vsotad = vsotad + tabela[x, y];
}
}
}
Console.WriteLine("Vsota vsake vrstice je : ");
int d = tabela.GetLength(1) - 1;
for (int c = 0; c < 9; c++)
{
if (c != d)
{
vsotad = vsotad + tabela[c, d - c];
}
}
Console.WriteLine("Vsota stevil v diag. je: ");
Console.WriteLine(vsotad);
Console.ReadLine();
}6. Digit Sum (Alternative Version)
public static void vsota_stevk()
{
Console.WriteLine("Vnesi stevilo: ");
int n, vsota = 0, r;
n = int.Parse(Console.ReadLine());
while (n != 0)
{
r = n % 10;
n = n / 10;
vsota = vsota + r;
}
Console.WriteLine("Vsota stevk stevila je: " + vsota);
}7. Tossing Two Coins
public static void metanje_dveh_kovancev()
{
Random rand = new Random();
int kovanec1 = 0;
int kovanec2 = 0;
int stetje1 = 0;
for (int i = 0; i < 1001; i++)
{
kovanec1 = rand.Next(0, 2);
kovanec2 = rand.Next(0, 2);
Console.WriteLine(kovanec2);
Console.WriteLine(kovanec1); // cifra 0
if (kovanec1 == 0 || kovanec2 == 0)
{
if (kovanec1 == kovanec2)
{
stetje1++;
}
}
}
Console.WriteLine("Pri :" + stetje1++);
Console.ReadLine();
}8. Average of Random Numbers
public static void povprecje_stevil()
{
Random rand = new Random();
int[] tabela = new int[1000];
int j = 0;
int vsota = 0;
for (int i = 0; i < 1000; i++)
{
j = rand.Next(1, 101);
tabela[i] = j;
vsota = vsota + j;
}
int rezultat = vsota / 1000;
Console.WriteLine("Povprecje je: " + rezultat);
}9. Squares of an Array
public static void kvadrati_tabele(int[] tabela)
{
int j = tabela.Length;
int num = 0;
int kvadrat = 0;
for (int i = 0; i < j; i++)
{
num = tabela[i];
kvadrat = num * num;
tabela[i] = kvadrat;
}
Console.WriteLine(tabela[kvadrat]);
for (int a = 0; a < j; a++)
{
Console.WriteLine(tabela[a]);
}
}10. Central Element Matrix (Second Version)
public static void centralni_element_dos()
{
Random rand = new Random();
int[,] tabela = new int[9, 9];
int vsota = 0;
int x = (tabela.GetLength(0) - 1) / 2;
int y = (tabela.GetLength(1) - 1) / 2;
for (int i = 0; i < 9; i++)
{
for (int t = 0; t < 9; t++)
{
if (!(i == x && t == y))
{
int suka = rand.Next(1, 10);
tabela[i, t] = suka;
vsota = vsota + suka;
}
}
}
tabela[x, y] = vsota;
for (int i = 0; i < 9; i++)
{
for (int t = 0; t < 9; t++)
{
Console.Write(tabela[i, t] + " ");
}
Console.Write("\n");
}
Console.ReadLine();
}