//Program to store 20 Random no in List and find sum of them.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Random rdm=new Random(); // Random class is there in system namespace used to generate random numbers
List<int> lst=new List<int>();
for(int i=0;i<20;i++)
{
int n=rdm.Next(0,300); // Next Is a instance method of Random class used to generate Next Random no with in the specified Range.
lst.Add(n);
}
int sum=0;
Console.WriteLine();
foreach(int n in lst)
{
Console.Write("{0} ",n);
sum+=n;
}
Console.WriteLine("\n");
Console.WriteLine("Sum: {0}",sum);
}
}
No comments:
Post a Comment