For loop examples in C# Console Application.
Example 1:
Code:
1 2 3 4 5 6 7 8 9 10 | static void Main(string[] args) { for(int i=1;i<=5;i++) { Console.WriteLine("i :" +i); } Console.ReadKey(); } |
Output:
Example 2:
Code:
1 2 3 4 5 6 7 8 9 10 | static void Main(string[] args) { for(int i=5;i<=10;i++) { Console.WriteLine("i :" +i); } Console.ReadKey(); } |
Output:
Example 3:
Code:
1 2 3 4 5 6 7 8 9 10 | static void Main(string[] args) { for(int i=1;i<=10;i++) { Console.WriteLine("www.csharp-console-examples.com"); } Console.ReadKey(); } |
Output:
Example 4:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | static void Main(string[] args) { int strt, fnsh; Console.Write("Start : "); strt = Convert.ToInt32(Console.ReadLine()); Console.Write("Finish : "); fnsh = Convert.ToInt32(Console.ReadLine()); for (int i=strt;i<=fnsh;i++) { Console.WriteLine("i : "+i); } Console.ReadKey(); } |
Output:
Example 5:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | static void Main(string[] args) { string txt; int count; Console.Write("Text : "); txt = Console.ReadLine(); Console.Write("Counter : "); count = Convert.ToInt32(Console.ReadLine()); for (int i=1;i<=count;i++) { Console.WriteLine(txt); } Console.ReadKey(); } |
Output:
Example 6:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | static void Main(string[] args) { for (int i=1;i<=15;i++) { if(i%2==0) { Console.WriteLine(i); } } Console.ReadKey(); } |
Output:
Example 7:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | static void Main(string[] args) { for (int i=1;i<=10;i++) { if(i%2==0) { Console.WriteLine(i + " >> even number"); } else { Console.WriteLine(i + " >> odd number"); } } Console.ReadKey(); } |
Output:
Example 8:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | static void Main(string[] args) { char chr; for (chr = 'a'; chr <= 'z'; chr++) { Console.Write(chr + " "); } Console.WriteLine(); for (chr = 'A'; chr <= 'Z'; chr++) { Console.Write(chr + " "); } Console.ReadKey(); } |
Output:
Example 9:
Code:
1 2 3 4 5 6 7 8 9 10 11 | static void Main(string[] args) { for (int i = 1; i <= 100; i++) { if (i % 5 == 0 && i % 7 == 0) { Console.WriteLine(i); } } Console.ReadKey(); } |
Output:
Example 10 :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | static void Main(string[] args) { int sum_even=0,sum_odd=0; for (int i = 1; i < 100; i++) { if(i%2==0) { sum_even += i; } else { sum_odd += i; } } Console.WriteLine("Sum of odd numbers from 1 to 100 : " + sum_odd); Console.WriteLine("Sum of even numbers from 1 to 100 : " + sum_even); Console.ReadKey(); } |
Output:
Example 11:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 | static void Main(string[] args) { int sum_number=0; for (int i = 1; i < 100; i++) { sum_number += i; } Console.WriteLine("Sum of numbers from 1 to 100 : " + sum_number); Console.ReadKey(); } |
Output:
Example 12 :In this example we are finding out the maximum and minimum values from an int array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class Program { static void Main(string[] args) { //www.csharp-console-examples.com int[] numbers = new int[10]; Random rnd = new Random(); int min, max; for (int i = 0; i < numbers.Length; i++) { numbers[i] = rnd.Next(1, 100); Console.WriteLine(numbers[i]); } min = numbers[0]; max = numbers[0]; for (int i = 1; i < numbers.Length; i++) { if (min > numbers[i]) min = numbers[i]; if (max < numbers[i]) max = numbers[i]; } Console.WriteLine("====================================="); Console.WriteLine("The highest number in the array: > > > " + max); Console.WriteLine("The lowest number in the array: > > > " + min); Console.ReadKey(); } } |
Output: