In the following example, we will find the average of numbers between 1 and 10 using for loop.
Example
C# Compiler
using System;
namespace myApp
{
class Program
{
static void Main(string[] args)
{
int start =1;
int end =10;
int total =0;
int count = (end - start) +1;
float average =0;
Console.WriteLine("Average of numbers between {0} and {1}:\n", start, end);
for(start=start; start<=end; start++)
total += start;
Console.WriteLine("Total = "+total);
Console.WriteLine("Count = "+count);
average = (float) total / count;
Console.WriteLine("Average is "+average);
}
}
}
Output
Average of numbers between 1 and 10:
Total = 55
Count = 10
Average is 5.5
Using while loop
In the following example, we will find the average of numbers between 1 and 10 using while loop.
Example
C# Compiler
using System;
namespace myApp
{
class Program
{
static void Main(string[] args)
{
int start =1;
int end =10;
int total =0;
int count = (end - start) +1;
float average =0;
Console.WriteLine("Average of numbers between {0} and {1}:\n", start, end);
while(start <= end)
{
total += start;
start++;
}
Console.WriteLine("Total = "+total);
Console.WriteLine("Count = "+count);
average = (float) total / count;
Console.WriteLine("Average is "+average);
}
}
}
Output
Average of numbers between 1 and 10:
Total = 55
Count = 10
Average is 5.5
Using do while loop
In the following example, we will find the average of numbers between 1 and 10 using do while loop.
Example
C# Compiler
using System;
namespace myApp
{
class Program
{
static void Main(string[] args)
{
int start =1;
int end =10;
int total =0;
int count = (end - start) +1;
float average =0;
Console.WriteLine("Average of numbers between {0} and {1}:\n", start, end);
do{
total += start;
start++;
}while(start <= end);
Console.WriteLine("Total = "+total);
Console.WriteLine("Count = "+count);
average = (float) total / count;
Console.WriteLine("Average is "+average);
}
}
}
Output
Average of numbers between 1 and 10:
Total = 55
Count = 10
Average is 5.5
Find Average of N Numbers for any Given Range
In the following example, we will find the average of N numbers between the user given range.
Example
C# Compiler
using System;
namespace myApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a (int) starting number: ");
int start = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter an (int) ending number: ");
int end = Convert.ToInt32(Console.ReadLine());
int total =0;
int count = (end - start) +1;
float average =0;
Console.WriteLine("Average of numbers between {0} and {1}:\n", start, end);
for(start=start; start<=end; start++)
total += start;
Console.WriteLine("Total = "+total);
Console.WriteLine("Count = "+count);
average = (float) total / count;
Console.WriteLine("Average is "+average);
}
}
}
Output
Enter a (int) starting number: 5
Enter an (int) ending number: 15
Average of numbers between 5 and 15:
Total = 110
Count = 11
Average is 10
Reminder
Hi Developers, we almost covered 90% of String functions and Interview Question on C# with examples for quick and easy learning.
We are working to cover every Single Concept in C#.