50 C# (C Sharp) For Loop programming questions and Solutions for Developers and college freshers

Written by: Idika Destiny

Reading time:
Published 17 hours Ago On Wednesday, August 9, 2023
Updated 17 hours Ago On Wednesday, August 9, 2023
466 Views



Write a C# program to display numbers from 1 to 10 using a for loop.

for (int i = 1; i <= 10; i++)

{

    Console.WriteLine(i);

}


Write a C# program to calculate the sum of all even numbers between 1 and 20 using a for loop.

int sum = 0;

for (int i = 2; i <= 20; i += 2)

{

    sum += i;

}

Console.WriteLine("Sum: " + sum);


Write a C# program to print the multiplication table of a given number using a for loop.

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

for (int = 1; i <= 10; i++)

{

    Console.WriteLine($"{number} x {i} = {number * i}");

}


Write a C# program to calculate the factorial of a number using a for loop.

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

int factorial = 1;

for (int = 1; i <= numberi++)

{

    factorial *= i;

}

Console.WriteLine("Factorial: " + factorial);


Write a C# program to find the sum of all the digits of a given number using a for loop.

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

int sum = 0;

while (number > 0)

{

    sum += number % 10;

    number /= 10;

}

Console.WriteLine("Sum of digits: " + sum);


Write a C# program to check if a given number is prime using a for loop.

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

bool isPrime = true;

for (int i = 2; i <= Math.Sqrt(number); i++)

{

    if (number % i == 0)

    {

        isPrime = false;

        break;

    }

}

Console.WriteLine("Is Prime: " + isPrime);


Write a C# program to generate the Fibonacci series up to a given number using a for loop.

Console.Write("Enter the number of terms: ");

int n = Convert.ToInt32(Console.ReadLine());

int first = 0, second = 1;

Console.Write("Fibonacci Series: " + first + ", " + second);

for (int i = 2; i < n; i++)

{

    int next = first + second;

    Console.Write(", " + next);

    first = second;

    second = next;

}

Console.WriteLine();


Write a C# program to find the largest element in an array using a for loop.

int[] array = { 10, 5, 20, 15, 30 };

int max = array[0];

for (int i = 1; i < array.Length; i++)

{

    if (array[i] > max)

    {

        max = array[i];

    }

}

Console.WriteLine("Largest element: " + max);


Write a C# program to count the number of vowels in a given string using a for loop.

Console.Write("Enter a string: ");

string input = Console.ReadLine();

int count = 0;

string vowels = "aeiou";

for (int i = 0; i < input.Length; i++)

{

    if (vowels.Contains(input[i]))

    {

        count++;

    }

}

Console.WriteLine("Number of vowels: " + count);


Write a C# program to reverse a given string using a for loop.

Console.Write("Enter a string: ");

string input = Console.ReadLine();

string reversed = "";

for (int i = input.Length - 1; i >= 0; i--)

{

    reversed += input[i];

}

Console.WriteLine("Reversed string: " + reversed);


Write a C# program to find the sum of the digits of a given number until the sum becomes a single digit using a for loop.

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

while (number > 9)

{

    int sum = 0;

    while (number > 0)

    {

        sum += number % 10;

        number /= 10;

    }

    number = sum;

}

Console.WriteLine("Single digit sum: " + number);



Write a C# program to print the ASCII values of all the characters from A to Z using a for loop.

for (2 c = 'A'; c <= 'Z'; c++)

{

    Console.WriteLine($"{c}: {(int)c}");

}


Write a C# program to find the second largest element in an array using a for loop.

int[] array = { 10, 5, 20, 15, 30 };

int max = array[0];

int secondMax = int.MinValue;

for (int i = 1; i < array.Length; i++)

{

    if (array[i] > max)

    {

        secondMax = max;

        max = array[i];

    }

    else if (array[i] > secondMax && array[i] != max)

    {

        secondMax = array[i];

    }

}

Console.WriteLine("Second largest element: " + secondMax);


Write a C# program to check if a given string is a palindrome using a for loop.

Console.Write("Enter a string: ");

string input = Console.ReadLine();

bool isPalindrome = true;

for (int i = 0; i < input.Length / 2; i++)

{

    if (input[i] != input[input.Length - i - 1])

    {

        isPalindrome = false;

        break;

    }

}

Console.WriteLine("Is Palindrome: " + isPalindrome);


Write a C# program to count the occurrences of a specific character in a given string using a for loop.

Console.Write("Enter a string: ");

string input = Console.ReadLine();

Console.Write("Enter a character: ");

char character = Console.ReadLine()[0];

int count = 0;

for (int i = 0; i < input.Length; i++)

{

    if (input[i] == character)

    {

        count++;

    }

}

Console.WriteLine("Occurrences: " + count);


Write a C# program to find the GCD (Greatest Common Divisor) of two numbers using a for loop.

Console.Write("Enter the first number: ");

int number1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second number: ");

int number2 = Convert.ToInt32(Console.ReadLine());

int gcd = 1;

for (int i = 1; i <= number1 && i <= number2; i++)

{

    if (number1 % i == 0 && number2 % i == 0)

    {

        gcd = i;

    }

}

Console.WriteLine("GCD: " + gcd);


Write a C# program to check if a given number is an Armstrong number using a for loop.

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

int originalNumber = number;

int sum = 0;

int digitCount = (int)Math.Log10(number) + 1;

while (number > 0)

{

    int digit = number % 10;

    sum += (int)Math.Pow(digit, digitCount);

    number /= 10;

}

bool isArmstrong = sum == originalNumber;

Console.WriteLine("Is Armstrong: " + isArmstrong);


Write a C# program to find the LCM (Least Common Multiple) of two numbers using a for loop.

Console.Write("Enter the first number: ");

int number1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second number: ");

int number2 = Convert.ToInt32(Console.ReadLine());

int lcm = Math.Max(number1, number2);

while (true)

{

    if (lcm % number1 == 0 && lcm % number2 == 0)

    {

        break;

    }

    lcm++;

}

Console.WriteLine("LCM: " + lcm);


Write a C# program to generate a series of prime numbers between two given numbers using a for loop.

Console.Write("Enter the lower bound: ");

int lowerBound = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the upper bound: ");

int upperBound = Convert.ToInt32(Console.ReadLine());

Console.Write("Prime numbers: ");

for (int number = lowerBound; number <= upperBound; number++)

{

    bool isPrime = true;

    for (int i = 2; i <= Math.Sqrt(number); i++)

    {

        if (number % i == 0)

        {

            isPrime = false;

            break;

        }

    }

    if (isPrime && number > 1)

    {

        Console.Write(number + " ");

    }

}

Console.WriteLine();


Write a C# program to calculate the sum of all odd numbers between two given numbers using a for loop.

Console.Write("Enter the lower bound: ");

int lowerBound = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the upper bound: ");

int upperBound = Convert.ToInt32(Console.ReadLine());

int sum = 0;

for (int number = lowerBound; number <= upperBound; number++)

{

    if (number % 2 != 0)

    {

        sum += number;

    }

}

Console.WriteLine("Sum of odd numbers: " + sum);


Write a C# program to print the first n natural numbers using a for loop.

Console.Write("Enter the value of n: ");

int n = Convert.ToInt32(Console.ReadLine());

Console.Write("Natural numbers: ");

for (int i = 1; i <= n; i++)

{

    Console.Write(i + " ");

}

Console.WriteLine();


Write a C# program to find the sum of the first n natural numbers using a for loop.

Console.Write("Enter the value of n: ");

int n = Convert.ToInt32(Console.ReadLine());

int sum = 0;

for (int i = 1; i <= n; i++)

{

    sum += i;

}

Console.WriteLine("Sum: " + sum);


Write a C# program to print the first n even numbers using a for loop.

Console.Write("Enter the value of n: ");

int n = Convert.ToInt32(Console.ReadLine());

Console.Write("Even numbers: ");

for (int i = 1; i <= n; i++)

{

    Console.Write(2 * i + " ");

}

Console.WriteLine();


Write a C# program to calculate the sum of the squares of the first n natural numbers using a for loop.

Console.Write("Enter the value of n: ");

int n = Convert.ToInt32(Console.ReadLine());

int sum = 0;

for (int i = 1; i <= n; i++)

{

    sum += i * i;

}

Console.WriteLine("Sum of squares: " + sum);


Write a C# program to print the pattern:

*

**

***

****

*****

using a for loop.

Console.Write("Enter the number of rows: ");

int rows = Convert.ToInt32(Console.ReadLine());

for (int i = 1; i <= rows; i++)

{

    for (int j = 1; j <= i; j++)

    {

        Console.Write("*");

    }

    Console.WriteLine();

}


Write a C# program to print the pattern:

*****

****

***

**

*

using a for loop.

Console.Write("Enter the number of rows: ");

int rows = Convert.ToInt32(Console.ReadLine());

for (int i = rows; i >= 1; i--)

{

    for (int j = 1; j <= i; j++)

    {

        Console.Write("*");

    }

    Console.WriteLine();

}


Write a C# program to print the pattern:

    *

   **

  ***

 ****

*****

using a for loop.

Console.Write("Enter the number of rows: ");

int rows = Convert.ToInt32(Console.ReadLine());

for (int i = 1; i <= rows; i++)

{

    for (int j = 1; j <= rows - i; j++)

    {

        Console.Write(" ");

    }

    for (int k = 1; k <= i; k++)

    {

        Console.Write("*");

    }

    Console.WriteLine();

}


Write a C# program to print the pattern:

*****

 ****

  ***

   **

    *

using a for loop.

Console.Write("Enter the number of rows: ");

int rows = Convert.ToInt32(Console.ReadLine());

for (int i = 1; i <= rows; i++)

{

    for (int j = 1; j < i; j++)

    {

        Console.Write(" ");

    }

    for (int k = i; k <= rows; k++)

    {

        Console.Write("*");

    }

    Console.WriteLine();

}


Write a C# program to print the pattern:

    1

   121

  12321

 1234321

123454321

using a for loop.

Console.Write("Enter the number of rows: ");

int rows = Convert.ToInt32(Console.ReadLine());

for (int i = 1; i <= rows; i++)

{

    for (int j = 1; j <= rows - i; j++)

    {

        Console.Write(" ");

    }

    for (int k = 1; k <= i; k++)

    {

        Console.Write(k);

    }

    for (int l = i - 1; l >= 1; l--)

    {

        Console.Write(l);

    }

    Console.WriteLine();

}


Write a C# program to find the sum of the first n terms of the series: 1 + 1/2 + 1/3 + 1/4 + ... using a for loop.

Console.Write("Enter the value of n: ");

int n = Convert.ToInt32(Console.ReadLine());

double sum = 0;

for (int i = 1; i <= n; i++)

{

    sum += 1.0 / i;

}

Console.WriteLine("Sum: " + sum);


Write a C# program to calculate the average of the first n natural numbers using a for loop.

Console.Write("Enter the value of n: ");

int n = Convert.ToInt32(Console.ReadLine());

double sum = 0;

for (int i = 1; i <= n; i++)

{

    sum += i;

}

double average = sum / n;

Console.WriteLine("Average: " + average);


Write a C# program to find the sum of all prime numbers between two given numbers using a for loop.

Console.Write("Enter the lower bound: ");

int lowerBound = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the upper bound: ");

int upperBound = Convert.ToInt32(Console.ReadLine());

int sum = 0;

for (int number = lowerBound; number <= upperBound; number++)

{

    bool isPrime = true;

    for (int i = 2; i <= Math.Sqrt(number); i++)

    {

        if (number % i == 0)

        {

            isPrime = false;

            break;

        }

    }

    if (isPrime && number > 1)

    {

        sum += number;

    }

}

Console.WriteLine("Sum of prime numbers: " + sum);


Write a C# program to find the sum of the digits of a given number using a for loop (without using division or modulus operator).

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

int sum = 0;

while (number > 0)

{

    sum += number - (number / 10) * 10;

    number /= 10;

}

Console.WriteLine("Sum of digits: " + sum);


Write a C# program to find the reverse of a given number using a for loop.

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

int reverse = 0;

while (number > 0)

{

    reverse = reverse * 10 + number % 10;

    number /= 10;

}

Console.WriteLine("Reversed number: " + reverse);


Write a C# program to check if a given number is a palindrome using a for loop (without converting to a string).

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

int originalNumber = number;

int reverse = 0;

while (number > 0)

{

    reverse = reverse * 10 + number % 10;

    number /= 10;

}

bool isPalindrome = reverse == originalNumber;

Console.WriteLine("Is Palindrome: " + isPalindrome);


Write a C# program to check if a given string is a palindrome using a for loop (case-insensitive and ignoring spaces).

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

string strippedInput = input.Replace(" ", "").ToLower();

bool isPalindrome = true;

for (int i = 0; i < strippedInput.Length / 2; i++)

{

    if (strippedInput[i] != strippedInput[strippedInput.Length - i - 1])

    {

        isPalindrome = false;

        break;

    }

}

Console.WriteLine("Is Palindrome: " + isPalindrome);


Write a C# program to find the factorial of a given number using a for loop.

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

int factorial = 1;

for (int i = 1; i <= number; i++)

{

    factorial *= i;

}

Console.WriteLine("Factorial: " + factorial);


Write a C# program to find the Fibonacci series up to a given number using a for loop.

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

int a = 0;

int b = 1;

Console.Write("Fibonacci series: " + a + " " + b + " ");

for (int i = 3; i <= number; i++)

{

    int c = a + b;

    Console.Write(c + " ");

    a = b;

    b = c;

}

Console.WriteLine();


Write a C# program to find the number of digits in a given number using a for loop.

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

int digitCount = 0;

while (number != 0)

{

    number /= 10;

    digitCount++;

}

Console.WriteLine("Number of digits: " + digitCount);


Write a C# program to find the largest element in an array using a for loop.

int[] array = { 10, 5, 20, 15, 30 };

int max = array[0];

for (int i = 1; i < array.Length; i++)

{

    if (array[i] > max)

    {

        max = array[i];

    }

}

Console.WriteLine("Largest element: " + max);


Write a C# program to count the number of even and odd elements in an array using a for loop.

int[] array = { 10, 5, 20, 15, 30 };

int evenCount = 0;

int oddCount = 0;

for (int i = 0; i < array.Length; i++)

{

    if (array[i] % 2 == 0)

    {

        evenCount++;

    }

    else

    {

        oddCount++;

    }

}

Console.WriteLine("Even count: " + evenCount);

Console.WriteLine("Odd count: " + oddCount);


Write a C# program to find the sum of all elements in a 2D array using a for loop.

int[,] array = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

int sum = 0;

for (int i = 0; i < array.GetLength(0); i++)

{

    for (int j = 0; j < array.GetLength(1); j++)

    {

        sum += array[i, j];

    }

}

Console.WriteLine("Sum: " + sum);


Write a C# program to find the transpose of a matrix using a for loop.

int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

int rows = matrix.GetLength(0);

int columns = matrix.GetLength(1);

int[,] transpose = new int[columns, rows];

for (int i = 0; i < rows; i++)

{

    for (int j = 0; j < columns; j++)

    {

        transpose[j, i] = matrix[i, j];

    }

}

Console.WriteLine("Original matrix:");

for (int i = 0; i < rows; i++)

{

    for (int j = 0; j < columns; j++)

    {

        Console.Write(matrix[i, j] + " ");

    }

    Console.WriteLine();

}

 Console.WriteLine("Transposed matrix:");

for (int i = 0; i < columns; i++)

{

    for (int j = 0; j < rows; j++)

    {

         Console.Write(transpose[i, j] + " ");

    }

     Console.WriteLine();

}


Write a C# program to check if a given matrix is symmetric using a for loop.

int[,] matrix = { { 1, 2, 3 }, { 2, 4, 5 }, { 3, 5, 6 } };

bool isSymmetric = true;

int rows = matrix.GetLength(0);

int columns = matrix.GetLength(1);

for (int i = 0; i < rows; i++)

{

    for (int j = 0; j < columns; j++)

    {

        if (matrix[i, j] != matrix[j, i])

        {

            isSymmetric = false;

            break;

        }

    }

    if (!isSymmetric)

    {

        break;

    }

}

 Console.WriteLine("Is symmetric: " + isSymmetric);


Write a C# program to find the sum of elements in the main diagonal of a matrix using a for loop.

int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

int sum = 0;

int size = matrix.GetLength(0);

for (int i = 0; i < size; i++)

{

    sum += matrix[i, i];

}

 Console.WriteLine("Sum of main diagonal elements: " + sum);


Write a C# program to find the sum of elements in the secondary diagonal of a matrix using a for loop.

int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

int sum = 0;

int size = matrix.GetLength(0);

for (int i = 0; i < size; i++)

{

    sum += matrix[i, size - i - 1];

}

 Console.WriteLine("Sum of secondary diagonal elements: " + sum);


Write a C# program to find the number of vowels and consonants in a given string using a for loop.

 Console.Write("Enter a string: ");

string input = Console.ReadLine();

int vowelCount = 0;

int consonantCount = 0;

string vowels = "aeiouAEIOU";

for (int i = 0; i < input.Length; i++)

{

    if (Char.IsLetter(input[i]))

    {

        if (vowels.Contains(input[i]))

        {

            vowelCount++;

        }

        else

        {

            consonantCount++;

        }

    }

}

 Console.WriteLine("Vowel count: " + vowelCount);
 Console.WriteLine("Consonant count: " + consonantCount);


Write a C# program to count the number of occurrences of a given character in a string using a for loop.

 Console.Write("Enter a string: ");

string input = Console.ReadLine();

 Console.Write("Enter a character: ");

char character = Convert.ToChar(Console.ReadLine());

int count = 0;

for (int i = 0; i < input.Length; i++)

{

    if (input[i] == character)

    {

        count++;

    }

}

 Console.WriteLine("Occurrences of " + character + ": " + count);


Write a C# program to find the length of the longest word in a given sentence using a for loop.

 Console.Write("Enter a sentence: ");

string sentence = Console.ReadLine();

string[] words = sentence.Split(' ');

int maxLength = 0;

for (int i = 0; i < words.Length; i++)

{

    if (words[i].Length > maxLength)

    {

        maxLength = words[i].Length;

    }

}

 Console.WriteLine("Length of longest word: " + maxLength);


Write a C# program to check if a given string is a pangram (contains all letters of the alphabet) using a for loop.

 Console.Write("Enter a string: ");

string input = Console.ReadLine().ToLower();

bool[] letters = new bool[26];

for (int i = 0; i < input.Length; i++)

{

    if (Char.IsLetter(input[i]))

    {

        letters[input[i] - 'a'] = true;

    }

}

bool isPangram = true;

for (int i = 0; i < letters.Length; i++)

{

    if (!letters[i])

    {

        isPangram = false;

        break;

    }

}

 Console.WriteLine("Is pangram: " + isPangram);



The need for a top business owner or organization to have a professional, scalable, Fast, Optimized,Efficient, Very Secured web application (website) can never be over emphasized.
However, With this great tool (Web Application) Business Owners will definitely and Undoubtedly solidify their online presence, improve their Search Engine ranking, eliminate the likelihood of Missing out on search engine queries / results by prospective clients whom may search for a business like theirs on search engines like Bing and google, stay toe to toe with Compititors who already have a web application etc.
Read Now Top 15 Reasosns why you need a website for your Business
You don’t need to do all of these alone, We got you covered!! Contact us now your satisfaction is always our priority. price definitely won't be a problem.

Thanks for reading



Simplified Instruction to the Difference between C# IEnumerable, IEnumerator and IQueryable

Major Types of Phishing Attacks scammers use to fool their victims