Search

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday, June 16, 2023

Top 50 C# For Loop programming FAQ

Top 50  C# For Loop programming FAQ


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

XAML

<Window x:Class="WpfApp1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="WPF Loop Example" Height="350" Width="525">

    <StackPanel>

        <TextBlock x:Name="outputTextBlock" />

        <Button Content="Print Numbers" Click="PrintNumbers_Click" />

    </StackPanel>

</Window>

C# Code

using System;

using System.Windows;

namespace WpfApp1

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void PrintNumbers_Click(object sender, RoutedEventArgs e)

        {

            outputTextBlock.Text = ""; // Clear previous output

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

            {

                outputTextBlock.Text += i.ToString() + Environment.NewLine;

            }

        }

    }

}


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

XAML

<Window x:Class="EvenNumberSumCalculator.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Even Number Sum Calculator" Height="150" Width="300">

    <StackPanel>

        <TextBlock Text="Sum of Even Numbers: " />

        <TextBlock x:Name="sumTextBlock" />

        <Button Content="Calculate" Click="CalculateButton_Click" />

    </StackPanel>

</Window>

C# CODE 

using System;

using System.Windows;

namespace EvenNumberSumCalculator

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CalculateButton_Click(object sender, RoutedEventArgs e)

        {

            int sum = 0;

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

            {

                sum += i;

            }

            sumTextBlock.Text = sum.ToString();

        }

    }

}


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

XAML

<Window x:Class="MultiplicationTable.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Multiplication Table" Height="200" Width="300">

    <StackPanel>

        <TextBlock Text="Enter a number:" />

        <TextBox x:Name="inputTextBox" />

        <Button Content="Generate Table" Click="GenerateTableButton_Click" />

        <TextBlock x:Name="outputTextBlock" />

    </StackPanel>

</Window>

C# Code 

using System;

using System.Windows;

namespace MultiplicationTable

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void GenerateTableButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(inputTextBox.Text, out int number))

            {

                outputTextBlock.Text = ""; // Clear previous output

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

                {

                    outputTextBlock.Text += $"{number} x {i} = {number * i}" + Environment.NewLine;

                }

            }

            else

            {

                outputTextBlock.Text = "Invalid input. Please enter a valid number.";

            }

        }

    }

}

 

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

The factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 is 1*2*3*4*5*6 = 720 . Factorial is not defined for negative numbers, and the factorial of zero is one, 0!


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

XAML

<Window x:Class="FactorialCalculator.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Factorial Calculator" Height="200" Width="300">

    <StackPanel>

        <TextBlock Text="Enter a number:" />

        <TextBox x:Name="inputTextBox" />

        <Button Content="Calculate Factorial" Click="CalculateFactorialButton_Click" />

        <TextBlock x:Name="outputTextBlock" />

    </StackPanel>

</Window>

C# Code 

using System;

using System.Windows;

namespace FactorialCalculator

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CalculateFactorialButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(inputTextBox.Text, out int number))

            {

                int factorial = 1;

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

                {

                    factorial *= i;

                }

                outputTextBlock.Text = "Factorial: " + factorial;

            }

            else

            {

                outputTextBlock.Text = "Invalid input. Please enter a valid number.";

            }

        }

    }

}


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

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number is only divisible by 1 and itself.

<Window x:Class="PrimeNumberChecker.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Prime Number Checker" Height="200" Width="300">

    <StackPanel>

        <TextBlock Text="Enter a number:" />

        <TextBox x:Name="inputTextBox" />

        <Button Content="Check Prime" Click="CheckPrimeButton_Click" />

        <TextBlock x:Name="outputTextBlock" />

    </StackPanel>

</Window>



using System;
using System.Windows;
namespace PrimeNumberChecker
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void CheckPrimeButton_Click(object sender, RoutedEventArgs e)
        {
            if (int.TryParse(inputTextBox.Text, out int number))
            {
                bool isPrime = true;
                if (number <= 1)
                {
                    isPrime = false;
                }
                else
                {
                    for (int i = 2; i <= Math.Sqrt(number); i++)
                    {
                        if (number % i == 0)
                        {
                            isPrime = false;
                            break;
                        }
                    }
                }
                outputTextBlock.Text = "Is Prime: " + isPrime;
            }
            else
            {
                outputTextBlock.Text = "Invalid input. Please enter a valid number.";
            }
        }
    }
}

 

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

<Window x:Class="FibonacciSeriesGenerator.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Fibonacci Series Generator" Height="250" Width="400">

    <StackPanel>

        <TextBlock Text="Enter the number of terms:" />

        <TextBox x:Name="inputTextBox" />

        <Button Content="Generate Fibonacci Series" Click="GenerateFibonacciButton_Click" />

        <TextBlock x:Name="outputTextBlock" />

    </StackPanel>

</Window>


using System;

using System.Windows;

namespace FibonacciSeriesGenerator

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void GenerateFibonacciButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(inputTextBox.Text, out int n) && n > 0)

            {

                int first = 0, second = 1;

                string fibonacciSeries = "Fibonacci Series: " + first + ", " + second;

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

                {

                    int next = first + second;

                    fibonacciSeries += ", " + next;

                    first = second;

                    second = next;

                }

                outputTextBlock.Text = fibonacciSeries;

            }

            else

            {

                outputTextBlock.Text = "Invalid input. Please enter a valid positive number.";

            }

        }

    }

}


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

<Window x:Class="MaxArrayElementFinder.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Max Array Element Finder" Height="150" Width="300">

    <StackPanel>

        <TextBlock Text="Array elements (comma-separated):" />

        <TextBox x:Name="inputTextBox" />

        <Button Content="Find Largest Element" Click="FindLargestElementButton_Click" />

        <TextBlock x:Name="outputTextBlock" />

    </StackPanel>

</Window>


using System;

using System.Linq;

using System.Windows;

namespace MaxArrayElementFinder

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void FindLargestElementButton_Click(object sender, RoutedEventArgs e)

        {

            string[] elements = inputTextBox.Text.Split(',');

            int[] array = Array.ConvertAll(elements, int.Parse);

            if (array.Length > 0)

            {

                int max = array[0];

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

                {

                    if (array[i] > max)

                    {

                        max = array[i];

                    }

                }

                outputTextBlock.Text = "Largest element: " + max;

            }

            else

            {

                outputTextBlock.Text = "Please enter valid array elements.";

            }

        }

    }

}

 

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

<Window x:Class="VowelCounter.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Vowel Counter" Height="200" Width="300">

    <StackPanel>

        <TextBlock Text="Enter a string:" />

        <TextBox x:Name="inputTextBox" />

        <Button Content="Count Vowels" Click="CountVowelsButton_Click" />

        <TextBlock x:Name="outputTextBlock" />

    </StackPanel>

</Window>


using System;

using System.Linq;

using System.Windows;

namespace VowelCounter

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CountVowelsButton_Click(object sender, RoutedEventArgs e)

        {

            string input = inputTextBox.Text.ToLower(); // Convert input to lowercase

            string vowels = "aeiou";

            int count = 0;

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

            {

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

                {

                    count++;

                }

            }

            outputTextBlock.Text = "Number of vowels: " + count;

        }

    }

}

 

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

<Window x:Class="StringReverser.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="String Reverser" Height="200" Width="300">

    <StackPanel>

        <TextBlock Text="Enter a string:" />

        <TextBox x:Name="inputTextBox" />

        <Button Content="Reverse String" Click="ReverseStringButton_Click" />

        <TextBlock x:Name="outputTextBlock" />

    </StackPanel>

</Window>


using System;

using System.Windows;

namespace StringReverser

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void ReverseStringButton_Click(object sender, RoutedEventArgs e)

        {

            string input = inputTextBox.Text;

            string reversed = "";

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

            {

                reversed += input[i];

            }

            outputTextBlock.Text = "Reversed string: " + reversed;

        }

    }

}


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

<Window x:Class="ASCIIPrinter.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="ASCII Printer" Height="250" Width="300">

    <StackPanel>

        <Button Content="Print ASCII Values" Click="PrintASCIIValuesButton_Click" />

        <TextBlock x:Name="outputTextBlock" />

    </StackPanel>

</Window>


using System;

using System.Windows;

namespace ASCIIPrinter

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void PrintASCIIValuesButton_Click(object sender, RoutedEventArgs e)

        {

            string asciiOutput = "";

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

            {

                asciiOutput += $"{c}: {(int)c}" + Environment.NewLine;

            }

            outputTextBlock.Text = asciiOutput;

        }

    }

}


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

<Window x:Class="SecondLargestElementFinder.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Second Largest Element Finder" Height="200" Width="300">

    <StackPanel>

        <Button Content="Find Second Largest Element" Click="FindSecondLargestElementButton_Click" />

        <TextBlock x:Name="outputTextBlock" />

    </StackPanel>

</Window>


using System;

using System.Linq;

using System.Windows;

namespace SecondLargestElementFinder

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void FindSecondLargestElementButton_Click(object sender, RoutedEventArgs e)

        {

            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];

                }

            }

            outputTextBlock.Text = "Second largest element: " + secondMax;

        }

    }

}

 

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

<Window x:Class="PalindromeChecker.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Palindrome Checker" Height="200" Width="300">

    <StackPanel>

        <TextBlock Text="Enter a string:" />

        <TextBox x:Name="inputTextBox" />

        <Button Content="Check Palindrome" Click="CheckPalindromeButton_Click" />

        <TextBlock x:Name="outputTextBlock" />

    </StackPanel>

</Window>

using System;
using System.Windows;
namespace PalindromeChecker
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CheckPalindromeButton_Click(object sender, RoutedEventArgs e)
        {
            string input = inputTextBox.Text;
            bool isPalindrome = true;

            for (int i = 0; i < input.Length / 2; i++)
            {
                if (input[i] != input[input.Length - i - 1])
                {
                    isPalindrome = false;
                    break;
                }
            }

            outputTextBlock.Text = "Is Palindrome: " + isPalindrome;
        }
    }
}


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

using System;

using System.Windows;

using System.Windows.Controls;

namespace CharacterCountApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CountButton_Click(object sender, RoutedEventArgs e)

        {

            string inputString = InputTextBox.Text;

            char targetChar = CharTextBox.Text[0]; // Get the first character from the input

            int count = 0;

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

            {

                if (inputString[i] == targetChar)

                {

                    count++;

                }

            }

            ResultLabel.Content = $"The character '{targetChar}' appears {count} times in the string.";

        }

    }

}


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

<Window x:Class="GCDApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="GCD Calculator" Height="250" Width="350">

    <Grid>

        <Label Content="Number 1:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="Number1TextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Margin="100,20"/>

                <Label Content="Number 2:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,50"/>

        <TextBox x:Name="Number2TextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Margin="100,50"/>

                <Button Content="Calculate GCD" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="110,80"

                Click="CalculateButton_Click"/>

                <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,120"/>

    </Grid>

</Window>


using System;

using System.Windows;

namespace GCDApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CalculateButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(Number1TextBox.Text, out int number1) && int.TryParse(Number2TextBox.Text, out int number2))

            {

                int gcd = CalculateGCD(number1, number2);

                ResultLabel.Content = $"The GCD of {number1} and {number2} is: {gcd}";

            }

            else

            {

                ResultLabel.Content = "Please enter valid integers.";

            }

        }

        private int CalculateGCD(int a, int b)

        {

            while (b != 0)

            {

                int temp = b;

                b = a % b;

                a = temp;

            }

            return a;

        }

    }

}

 

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

<Window x:Class="ArmstrongNumberApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Armstrong Number Checker" Height="200" Width="300">

    <Grid>

        <Label Content="Enter a number:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="NumberTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="130,20"/>

        <Button Content="Check" HorizontalAlignment="Left" VerticalAlignment="Top" Width="80" Margin="110,60"

                Click="CheckButton_Click"/>

        <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,100"/>

    </Grid>

</Window>

 

using System;
using System.Windows;

namespace ArmstrongNumberApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CheckButton_Click(object sender, RoutedEventArgs e)
        {
            if (int.TryParse(NumberTextBox.Text, out int number) && number >= 0)
            {
                bool isArmstrong = IsArmstrongNumber(number);
                if (isArmstrong)
                {
                    ResultLabel.Content = $"{number} is an Armstrong number.";
                }
                else
                {
                    ResultLabel.Content = $"{number} is not an Armstrong number.";
                }
            }
            else
            {
                ResultLabel.Content = "Please enter a non-negative integer.";
            }
        }

        private bool IsArmstrongNumber(int num)
        {
            int originalNum = num;
            int sum = 0;
            int numDigits = (int)Math.Floor(Math.Log10(num) + 1);
            while (num > 0)
            {
                int digit = num % 10;
                sum += (int)Math.Pow(digit, numDigits);
                num /= 10;
            }
            return sum == originalNum;
        }
    }
}

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

<Window x:Class="LCMApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="LCM Calculator" Height="250" Width="350">

    <Grid>

        <Label Content="Number 1:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="Number1TextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Margin="100,20"/>

        

        <Label Content="Number 2:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,50"/>

        <TextBox x:Name="Number2TextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Margin="100,50"/>

                <Button Content="Calculate LCM" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="110,80"

                Click="CalculateButton_Click"/>

                <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,120"/>

    </Grid>

</Window>


using System;

using System.Windows;

namespace LCMApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CalculateButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(Number1TextBox.Text, out int number1) && int.TryParse(Number2TextBox.Text, out int number2))

            {

                int lcm = CalculateLCM(number1, number2);

                ResultLabel.Content = $"The LCM of {number1} and {number2} is: {lcm}";

            }

            else

            {

                ResultLabel.Content = "Please enter valid integers.";

            }

        }

       private int CalculateLCM(int a, int b)

        {

            int max = Math.Max(a, b);

            int min = Math.Min(a, b);

            for (int i = max; ; i += max)

            {

                if (i % min == 0)

                {

                    return i;

                }

            }

        }

    }

}

 

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

<Window x:Class="PrimeNumberSeriesApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Prime Number Series" Height="300" Width="350">

    <Grid>

        <Label Content="Start Number:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="StartTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="120,20"/>

        <Label Content="End Number:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,50"/>

        <TextBox x:Name="EndTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="120,50"/>

        <Button Content="Generate Prime Numbers" HorizontalAlignment="Left" VerticalAlignment="Top" Width="160" Margin="90,90"

                Click="GenerateButton_Click"/>

        <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,130"/>

    </Grid>

</Window>


using System;

using System.Windows;

namespace PrimeNumberSeriesApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void GenerateButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(StartTextBox.Text, out int start) && int.TryParse(EndTextBox.Text, out int end))

            {

                if (start <= end)

                {

                    string primeSeries = GeneratePrimeSeries(start, end);

                    ResultLabel.Content = "Prime Numbers between " + start + " and " + end + ": \n" + primeSeries;

                }

                else

                {

                    ResultLabel.Content = "Start number must be less than or equal to end number.";

                }

            }

            else

            {

                ResultLabel.Content = "Please enter valid integers.";

            }

        }

        private string GeneratePrimeSeries(int start, int end)

        {

            string primeSeries = "";

            for (int num = start; num <= end; num++)

            {

                if (IsPrime(num))

                {

                    primeSeries += num + " ";

                }

            }

            return primeSeries.Trim();

        }

        private bool IsPrime(int num)

        {

            if (num <= 1)

                return false;

            for (int i = 2; i * i <= num; i++)

            {

                if (num % i == 0)

                    return false;

            }

            return true;

        }

    }

}

 

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

<Window x:Class="SumOfOddNumbersApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Sum of Odd Numbers" Height="250" Width="350">

    <Grid>

        <Label Content="Start Number:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="StartTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="120,20"/>


        <Label Content="End Number:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,50"/>

        <TextBox x:Name="EndTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="120,50"/>

        <Button Content="Calculate Sum" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="110,90"

                Click="CalculateButton_Click"/>

        <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,130"/>

    </Grid>

</Window>


using System;

using System.Windows;

namespace SumOfOddNumbersApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CalculateButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(StartTextBox.Text, out int start) && int.TryParse(EndTextBox.Text, out int end))

            {

                if (start <= end)

                {

                    int sumOfOdds = CalculateSumOfOddNumbers(start, end);

                    ResultLabel.Content = "Sum of odd numbers between " + start + " and " + end + ": " + sumOfOdds;

                }

                else

                {

                    ResultLabel.Content = "Start number must be less than or equal to end number.";

                }

            }

            else

            {

                ResultLabel.Content = "Please enter valid integers.";

            }

        }

        private int CalculateSumOfOddNumbers(int start, int end)

        {

            int sum = 0;

            for (int num = start; num <= end; num++)

            {

                if (num % 2 != 0)

                {

                    sum += num;

                }

            }

            return sum;

        }

    }

}

 

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

<Window x:Class="FirstNNaturalNumbersApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Print First n Natural Numbers" Height="250" Width="350">

    <Grid>

        <Label Content="Enter n:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="NTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="80,20"/>

        <Button Content="Print Numbers" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="100,60"

                Click="PrintButton_Click"/>

        <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,100"/>

    </Grid>

</Window>


using System;

using System.Windows;

namespace FirstNNaturalNumbersApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void PrintButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(NTextBox.Text, out int n) && n > 0)

            {

                string numbers = PrintFirstNNaturalNumbers(n);

                ResultLabel.Content = "First " + n + " natural numbers: " + numbers;

            }

            else

            {

                ResultLabel.Content = "Please enter a valid positive integer.";

            }

        }

        private string PrintFirstNNaturalNumbers(int n)

        {

            string numbers = "";

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

            {

                numbers += i + " ";

            }

            return numbers.Trim();

        }

    }

}

 

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

<Window x:Class="SumOfFirstNNaturalNumbersApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Sum of First n Natural Numbers" Height="250" Width="350">

    <Grid>

        <Label Content="Enter n:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="NTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="80,20"/>

        <Button Content="Calculate Sum" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="100,60"

                Click="CalculateButton_Click"/>

        <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,100"/>

    </Grid>

</Window>


using System;

using System.Windows;

namespace SumOfFirstNNaturalNumbersApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CalculateButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(NTextBox.Text, out int n) && n > 0)

            {

                int sum = CalculateSumOfFirstNNaturalNumbers(n);

                ResultLabel.Content = "Sum of first " + n + " natural numbers: " + sum;

            }

            else

            {

                ResultLabel.Content = "Please enter a valid positive integer.";

            }

        }

        private int CalculateSumOfFirstNNaturalNumbers(int n)

        {

            int sum = 0;

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

            {

                sum += i;

            }

            return sum;

        }

    }

}

 

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

<Window x:Class="PrintFirstNEvenNumbersApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Print First n Even Numbers" Height="250" Width="350">

    <Grid>

        <Label Content="Enter n:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="NTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="80,20"/>

        <Button Content="Print Numbers" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="100,60"

                Click="PrintButton_Click"/>

        <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,100"/>

    </Grid>

</Window>


using System;

using System.Windows;

namespace PrintFirstNEvenNumbersApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void PrintButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(NTextBox.Text, out int n) && n > 0)

            {

                string evenNumbers = PrintFirstNEvenNumbers(n);

                ResultLabel.Content = "First " + n + " even numbers: " + evenNumbers;

            }

            else

            {

                ResultLabel.Content = "Please enter a valid positive integer.";

            }

        }

        private string PrintFirstNEvenNumbers(int n)

        {

            string evenNumbers = "";

            for (int i = 1; n > 0; i++)

            {

                if (i % 2 == 0)

                {

                    evenNumbers += i + " ";

                    n--;

                }

            }

            return evenNumbers.Trim();

        }

    }

}

 

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

<Window x:Class="SumOfSquaresApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Sum of Squares of First n Natural Numbers" Height="250" Width="350">

    <Grid>

        <Label Content="Enter n:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="NTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="80,20"/>

        <Button Content="Calculate Sum" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="100,60"

                Click="CalculateButton_Click"/>

        <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,100"/>

    </Grid>

</Window>


using System;

using System.Windows;

namespace SumOfSquaresApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CalculateButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(NTextBox.Text, out int n) && n > 0)

            {

                int sumOfSquares = CalculateSumOfSquares(n);

                ResultLabel.Content = "Sum of squares of first " + n + " natural numbers: " + sumOfSquares;

            }

            else

            {

                ResultLabel.Content = "Please enter a valid positive integer.";

            }

        }

        private int CalculateSumOfSquares(int n)

        {

            int sum = 0;

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

            {

                sum += i * i;

            }

            return sum;

        }

    }

}

 

Write a C# program to print the pattern:

*

**

***

****

*****

using a for loop.

<Window x:Class="PatternPrintApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Pattern Print" Height="200" Width="200">

    <Grid>

        <TextBlock x:Name="PatternTextBlock" HorizontalAlignment="Center" VerticalAlignment="Center"/>

    </Grid>

</Window>


 

using System.Windows;

namespace PatternPrintApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

            PrintPattern();

        }

        private void PrintPattern()

        {

            string pattern = "";

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

            {

                pattern += new string('*', i) + "\n";

            }

            PatternTextBlock.Text = pattern;

        }

    }

}

 

Write a C# program to print the pattern:

*****

****

***

**

*

using a for loop.

<Window x:Class="PatternPrintApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Pattern Print" Height="200" Width="200">

    <Grid>

        <TextBlock x:Name="PatternTextBlock" HorizontalAlignment="Center" VerticalAlignment="Center"/>

    </Grid>

</Window>


using System.Windows;

namespace PatternPrintApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

            PrintPattern();

        }

        private void PrintPattern()

        {

            string pattern = "";

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

            {

                pattern += new string('*', i) + "\n";

            }

            PatternTextBlock.Text = pattern;

        }

    }

}

 

Write a C# program to print the pattern:

    *

   **

  ***

 ****

*****

using a for loop.

<Window x:Class="PatternPrintApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Pattern Print" Height="250" Width="250">

    <Grid>

        <TextBlock x:Name="PatternTextBlock" HorizontalAlignment="Center" VerticalAlignment="Center"/>

    </Grid>

</Window>


using System.Windows;

namespace PatternPrintApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

            PrintPattern();

        }

        private void PrintPattern()

        {

            string pattern = "";

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

            {

                pattern += new string(' ', 5 - i) + new string('*', i) + "\n";

            }

            PatternTextBlock.Text = pattern;

        }

    }

}

 

Write a C# program to print the pattern:

*****

 ****

  ***

   **

    *

using a for loop.

<Window x:Class="PatternPrintApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Pattern Print" Height="250" Width="250">

    <Grid>

        <TextBlock x:Name="PatternTextBlock" HorizontalAlignment="Center" VerticalAlignment="Center"/>

    </Grid>

</Window>


using System.Windows;

namespace PatternPrintApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

            PrintPattern();

        }

        private void PrintPattern()

        {

            string pattern = "";

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

            {

                pattern += new string(' ', 5 - i) + new string('*', i) + "\n";

            }

            PatternTextBlock.Text = pattern;

        }

    }

}

 

Write a C# program to print the pattern:

    1

   121

  12321

 1234321

123454321

using a for loop.

<Window x:Class="PatternPrintApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Pattern Print" Height="250" Width="250">

    <Grid>

        <TextBlock x:Name="PatternTextBlock" HorizontalAlignment="Center" VerticalAlignment="Center"/>

    </Grid>

</Window>


 using System.Windows;

namespace PatternPrintApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

            PrintPattern();

        }

        private void PrintPattern()

        {

            string pattern = "";

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

            {

                // Print spaces

                pattern += new string(' ', 5 - i);

                // Print numbers in increasing order

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

                {

                    pattern += j;

                }

                // Print numbers in decreasing order

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

                {

                    pattern += j;

                }

                pattern += "\n";

            }

            PatternTextBlock.Text = pattern;

        }

    }

}


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.

<Window x:Class="SeriesSumApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Series Sum" Height="250" Width="350">

    <Grid>

        <Label Content="Enter n:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="NTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="80,20"/>

        <Button Content="Calculate Sum" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="100,60"

                Click="CalculateButton_Click"/>

        <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,100"/>

    </Grid>

</Window>


using System;

using System.Windows;

namespace SeriesSumApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CalculateButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(NTextBox.Text, out int n) && n > 0)

            {

                double sum = CalculateSeriesSum(n);

                ResultLabel.Content = "Sum of the first " + n + " terms: " + sum;

            }

            else

            {

                ResultLabel.Content = "Please enter a valid positive integer.";

            }

        }

        private double CalculateSeriesSum(int n)

        {

            double sum = 0;

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

            {

                sum += 1.0 / i;

            }

            return sum;

        }

    }

}

 

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

<Window x:Class="AverageOfNaturalNumbersApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Average of First n Natural Numbers" Height="250" Width="350">

    <Grid>

        <Label Content="Enter n:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="NTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="80,20"/>

        <Button Content="Calculate Average" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="100,60"

                Click="CalculateButton_Click"/>

        <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,100"/>

    </Grid>

</Window>


using System;

using System.Windows;

namespace AverageOfNaturalNumbersApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CalculateButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(NTextBox.Text, out int n) && n > 0)

            {

                double average = CalculateAverageOfNaturalNumbers(n);

                ResultLabel.Content = "Average of the first " + n + " natural numbers: " + average;

            }

            else

            {

                ResultLabel.Content = "Please enter a valid positive integer.";

            }

        }

        private double CalculateAverageOfNaturalNumbers(int n)

        {

            double sum = 0;

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

            {

                sum += i;

            }

            return sum / n;

        }

    }

}

 

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

<Window x:Class="SumOfPrimesApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Sum of Prime Numbers" Height="300" Width="350">

    <Grid>

        <Label Content="Start Number:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="StartTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="120,20"/>

        <Label Content="End Number:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,50"/>

        <TextBox x:Name="EndTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="120,50"/>

        <Button Content="Calculate Sum" HorizontalAlignment="Left" VerticalAlignment="Top" Width="140" Margin="100,90"

                Click="CalculateButton_Click"/>

        <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,130"/>

    </Grid>

</Window>


using System;

using System.Windows;

namespace SumOfPrimesApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CalculateButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(StartTextBox.Text, out int start) && int.TryParse(EndTextBox.Text, out int end))

            {

                if (start <= end)

                {

                    int sumOfPrimes = CalculateSumOfPrimes(start, end);

                    ResultLabel.Content = "Sum of prime numbers between " + start + " and " + end + ": " + sumOfPrimes;

                }

                else

                {

                    ResultLabel.Content = "Start number must be less than or equal to end number.";

                }

            }

            else

            {

                ResultLabel.Content = "Please enter valid integers.";

            }

        }

        private int CalculateSumOfPrimes(int start, int end)

        {

            int sum = 0;

            for (int num = start; num <= end; num++)

            {

                if (IsPrime(num))

                {

                    sum += num;

                }

            }

            return sum;

        }

        private bool IsPrime(int num)

        {

            if (num <= 1)

                return false;

            for (int i = 2; i * i <= num; i++)

            {

                if (num % i == 0)

                    return false;

            }

            return true;

        }

    }

}

 

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).

<Window x:Class="SumOfDigitsApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Sum of Digits" Height="200" Width="350">

    <Grid>

        <Label Content="Enter a number:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20"/>

        <TextBox x:Name="NumberTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="150,20"/>

        <Button Content="Calculate Sum" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="100,60"

                Click="CalculateButton_Click"/>

        <Label x:Name="ResultLabel" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20,100"/>

    </Grid>

</Window>


using System;

using System.Windows;

namespace SumOfDigitsApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CalculateButton_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(NumberTextBox.Text, out int number))

            {

                int sum = CalculateSumOfDigits(number);

                ResultLabel.Content = "Sum of digits: " + sum;

            }

            else

            {

                ResultLabel.Content = "Please enter a valid integer.";

            }

        }

        private int CalculateSumOfDigits(int number)

        {

            if (number < 0)

            {

                number = -number; // Convert to positive if negative

            }

            int sum = 0;

            while (number != 0)

            {

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

                {

                    if (number >= i * 10)

                    {

                        continue;

                    }

                    sum += i;

                    number -= i * 10;

                    break;

                }

            }

            return sum;

        }

    }

}

 

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

<Window x:Class="ReverseNumberWpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Reverse Number Calculator" Height="250" Width="400">

    <Grid>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

            <TextBlock Text="Enter a number:" Margin="5" />

            <TextBox x:Name="numberTextBox" Width="150" Margin="5" />

            <Button Content="Calculate Reverse" Width="100" Margin="10" Click="CalculateReverse_Click" />

            <TextBlock x:Name="resultTextBlock" Text="" Margin="5" />

        </StackPanel>

    </Grid>

</Window>

using System;
using System.Windows;

namespace ReverseNumberWpfApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CalculateReverse_Click(object sender, RoutedEventArgs e)
        {
            if (int.TryParse(numberTextBox.Text, out int number))
            {
                int reverse = CalculateReverse(number);
                resultTextBlock.Text = $"Reverse: {reverse}";
            }
            else
            {
                resultTextBlock.Text = "Invalid input. Please enter a valid number.";
            }
        }

        private int CalculateReverse(int number)
        {
            int reverse = 0;
            while (number > 0)
            {
                int digit = number % 10;
                reverse = reverse * 10 + digit;
                number /= 10;
            }
            return reverse;
        }
    }
}


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

<Window x:Class="PalindromeNumberWpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Palindrome Number Checker" Height="250" Width="400">

    <Grid>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

            <TextBlock Text="Enter a number:" Margin="5" />

            <TextBox x:Name="numberTextBox" Width="150" Margin="5" />

            <Button Content="Check Palindrome" Width="100" Margin="10" Click="CheckPalindrome_Click" />

            <TextBlock x:Name="resultTextBlock" Text="" Margin="5" />

        </StackPanel>

    </Grid>

</Window>


using System;

using System.Windows;

namespace PalindromeNumberWpfApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void CheckPalindrome_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(numberTextBox.Text, out int number))

            {

                bool isPalindrome = IsPalindrome(number);

                if (isPalindrome)

                {

                    resultTextBlock.Text = "It's a palindrome!";

                }

                else

                {

                    resultTextBlock.Text = "It's not a palindrome.";

                }

            }

            else

            {

                resultTextBlock.Text = "Invalid input. Please enter a valid number.";

            }

        }

        private bool IsPalindrome(int number)

        {

            int originalNumber = number;

            int reverse = 0;

            while (number > 0)

            {

                int digit = number % 10;

                reverse = reverse * 10 + digit;

                number /= 10;

            }          

            return originalNumber == reverse;

        }

    }

}

 

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

<Window x:Class="PalindromeStringWpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Palindrome String Checker" Height="250" Width="400">

    <Grid>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

            <TextBlock Text="Enter a string:" Margin="5" />

            <TextBox x:Name="stringTextBox" Width="200" Margin="5" />

            <Button Content="Check Palindrome" Width="100" Margin="10" Click="CheckPalindrome_Click" />

            <TextBlock x:Name="resultTextBlock" Text="" Margin="5" />

        </StackPanel>

    </Grid>

</Window>


using System;

using System.Windows;


namespace PalindromeStringWpfApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void CheckPalindrome_Click(object sender, RoutedEventArgs e)

        {

            string input = stringTextBox.Text;

            bool isPalindrome = IsPalindrome(input);

            

            if (isPalindrome)

            {

                resultTextBlock.Text = "It's a palindrome!";

            }

            else

            {

                resultTextBlock.Text = "It's not a palindrome.";

            }

        }


        private bool IsPalindrome(string input)

        {

            // Remove spaces and convert to lowercase

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


            int length = formattedInput.Length;

            for (int i = 0; i < length / 2; i++)

            {

                if (formattedInput[i] != formattedInput[length - i - 1])

                {

                    return false;

                }

            }

            return true;

        }

    }

}

 

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

<Window x:Class="FactorialCalculatorWpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Factorial Calculator" Height="250" Width="400">

    <Grid>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

            <TextBlock Text="Enter a number:" Margin="5" />

            <TextBox x:Name="numberTextBox" Width="150" Margin="5" />

            <Button Content="Calculate Factorial" Width="120" Margin="10" Click="CalculateFactorial_Click" />

            <TextBlock x:Name="resultTextBlock" Text="" Margin="5" />

        </StackPanel>

    </Grid>

</Window>


using System;

using System.Windows;


namespace FactorialCalculatorWpfApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void CalculateFactorial_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(numberTextBox.Text, out int number) && number >= 0)

            {

                long factorial = CalculateFactorial(number);

                resultTextBlock.Text = $"Factorial: {factorial}";

            }

            else

            {

                resultTextBlock.Text = "Invalid input. Please enter a non-negative integer.";

            }

        }


        private long CalculateFactorial(int number)

        {

            if (number == 0 || number == 1)

            {

                return 1;

            }


            long factorial = 1;

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

            {

                factorial *= i;

            }

            return factorial;

        }

    }

}

 

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

<Window x:Class="FibonacciSeriesWpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Fibonacci Series Generator" Height="250" Width="400">

    <Grid>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

            <TextBlock Text="Enter a number:" Margin="5" />

            <TextBox x:Name="numberTextBox" Width="150" Margin="5" />

            <Button Content="Generate Fibonacci Series" Width="180" Margin="10" Click="GenerateFibonacci_Click" />

            <TextBlock x:Name="resultTextBlock" Text="" Margin="5" TextWrapping="Wrap" />

        </StackPanel>

    </Grid>

</Window>


using System;

using System.Windows;


namespace FibonacciSeriesWpfApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void GenerateFibonacci_Click(object sender, RoutedEventArgs e)

        {

            if (int.TryParse(numberTextBox.Text, out int number) && number >= 0)

            {

                string fibonacciSeries = GenerateFibonacciSeries(number);

                resultTextBlock.Text = $"Fibonacci Series:\n{fibonacciSeries}";

            }

            else

            {

                resultTextBlock.Text = "Invalid input. Please enter a non-negative integer.";

            }

        }


        private string GenerateFibonacciSeries(int n)

        {

            if (n == 0)

            {

                return "0";

            }

            if (n == 1)

            {

                return "0, 1";

            }


            string series = "0, 1";

            long prev = 0, curr = 1;


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

            {

                long next = prev + curr;

                series += $", {next}";

                prev = curr;

                curr = next;

            }

            return series;

        }

    }

}

 


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

<Window x:Class="LargestElementArrayWpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Largest Element in Array" Height="250" Width="400">

    <Grid>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

            <TextBlock Text="Enter elements (comma-separated):" Margin="5" />

            <TextBox x:Name="inputTextBox" Width="200" Margin="5" />

            <Button Content="Find Largest Element" Width="160" Margin="10" Click="FindLargest_Click" />

            <TextBlock x:Name="resultTextBlock" Text="" Margin="5" />

        </StackPanel>

    </Grid>

</Window>


using System;

using System.Linq;

using System.Windows;


namespace LargestElementArrayWpfApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void FindLargest_Click(object sender, RoutedEventArgs e)

        {

            string input = inputTextBox.Text;

            string[] inputArray = input.Split(',');


            if (inputArray.Length > 0)

            {

                int[] numbers = new int[inputArray.Length];

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

                {

                    if (int.TryParse(inputArray[i], out int num))

                    {

                        numbers[i] = num;

                    }

                    else

                    {

                        resultTextBlock.Text = "Invalid input. Please enter valid numbers separated by commas.";

                        return;

                    }

                }


                int largest = FindLargestElement(numbers);

                resultTextBlock.Text = $"Largest Element: {largest}";

            }

            else

            {

                resultTextBlock.Text = "Please enter at least one number.";

            }

        }


        private int FindLargestElement(int[] array)

        {

            int largest = array[0];

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

            {

                if (array[i] > largest)

                {

                    largest = array[i];

                }

            }

            return largest;

        }

    }

}

 

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

<Window x:Class="EvenOddCounterArrayWpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Even and Odd Element Counter" Height="250" Width="400">

    <Grid>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

            <TextBlock Text="Enter elements (comma-separated):" Margin="5" />

            <TextBox x:Name="inputTextBox" Width="200" Margin="5" />

            <Button Content="Count Even and Odd" Width="160" Margin="10" Click="CountEvenOdd_Click" />

            <TextBlock x:Name="resultTextBlock" Text="" Margin="5" />

        </StackPanel>

    </Grid>

</Window>

using System;
using System.Linq;
using System.Windows;

namespace EvenOddCounterArrayWpfApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CountEvenOdd_Click(object sender, RoutedEventArgs e)
        {
            string input = inputTextBox.Text;
            string[] inputArray = input.Split(',');

            if (inputArray.Length > 0)
            {
                int[] numbers = new int[inputArray.Length];
                for (int i = 0; i < inputArray.Length; i++)
                {
                    if (int.TryParse(inputArray[i], out int num))
                    {
                        numbers[i] = num;
                    }
                    else
                    {
                        resultTextBlock.Text = "Invalid input. Please enter valid numbers separated by commas.";
                        return;
                    }
                }

                int evenCount, oddCount;
                CountEvenOdd(numbers, out evenCount, out oddCount);

                resultTextBlock.Text = $"Even Count: {evenCount}\nOdd Count: {oddCount}";
            }
            else
            {
                resultTextBlock.Text = "Please enter at least one number.";
            }
        }

        private void CountEvenOdd(int[] array, out int evenCount, out int oddCount)
        {
            evenCount = 0;
            oddCount = 0;

            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] % 2 == 0)
                {
                    evenCount++;
                }
                else
                {
                    oddCount++;
                }
            }
        }
    }
}

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

<Window x:Class="TwoDArraySumCalculator.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="2D Array Sum Calculator" Height="250" Width="400">

    <Grid>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

            <Button Content="Calculate Sum" Width="120" Height="40" Click="CalculateButton_Click"/>

            <Label x:Name="ResultLabel" Content="" Margin="0,20,0,0" HorizontalAlignment="Center"/>

        </StackPanel>

    </Grid>

</Window>


using System;

using System.Windows;

using System.Windows.Controls;


namespace TwoDArraySumCalculator

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void CalculateButton_Click(object sender, RoutedEventArgs e)

        {

            int[,] array = {

                { 1, 2, 3 },

                { 4, 5, 6 },

                { 7, 8, 9 }

            };


            int sum = 0;


            int rows = array.GetLength(0);

            int columns = array.GetLength(1);


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

            {

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

                {

                    sum += array[i, j];

                }

            }


            ResultLabel.Content = $"Sum: {sum}";

        }

    }

}

 

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

<Window x:Class="MatrixTransposeCalculator.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Matrix Transpose Calculator" Height="300" Width="400">

    <Grid>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

            <Button Content="Calculate Transpose" Width="150" Height="40" Click="TransposeButton_Click"/>

            <TextBlock x:Name="TransposeResultTextBlock" Text="" Margin="0,20,0,0" HorizontalAlignment="Center"/>

        </StackPanel>

    </Grid>

</Window>


using System;

using System.Windows;

using System.Windows.Controls;


namespace MatrixTransposeCalculator

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void TransposeButton_Click(object sender, RoutedEventArgs e)

        {

            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];

                }

            }


            DisplayTransposedMatrix(transpose);

        }


        private void DisplayTransposedMatrix(int[,] matrix)

        {

            TransposeResultTextBlock.Text = "Transposed Matrix:\n";


            int rows = matrix.GetLength(0);

            int columns = matrix.GetLength(1);


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

            {

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

                {

                    TransposeResultTextBlock.Text += matrix[i, j] + "\t";

                }

                TransposeResultTextBlock.Text += "\n";

            }

        }

    }

}

 


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

<Window x:Class="SymmetricMatrixChecker.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Symmetric Matrix Checker" Height="250" Width="400">

    <Grid>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

            <Button Content="Check Symmetry" Width="120" Height="40" Click="CheckSymmetryButton_Click"/>

            <TextBlock x:Name="SymmetryResultTextBlock" Text="" Margin="0,20,0,0" HorizontalAlignment="Center"/>

        </StackPanel>

    </Grid>

</Window>


using System;

using System.Windows;

using System.Windows.Controls;


namespace SymmetricMatrixChecker

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void CheckSymmetryButton_Click(object sender, RoutedEventArgs e)

        {

            int[,] matrix = {

                { 1, 2, 3 },

                { 2, 4, 5 },

                { 3, 5, 6 }

            };


            bool isSymmetric = IsMatrixSymmetric(matrix);


            SymmetryResultTextBlock.Text = isSymmetric ? "Matrix is symmetric." : "Matrix is not symmetric.";

        }


        private bool IsMatrixSymmetric(int[,] matrix)

        {

            int rows = matrix.GetLength(0);

            int columns = matrix.GetLength(1);


            if (rows != columns)

                return false;


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

            {

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

                {

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

                    {

                        return false;

                    }

                }

            }


            return true;

        }

    }

}

 

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

<Window x:Class="MainDiagonalSumCalculator.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Main Diagonal Sum Calculator" Height="250" Width="400">

    <Grid>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

            <Button Content="Calculate Diagonal Sum" Width="160" Height="40" Click="CalculateSumButton_Click"/>

            <TextBlock x:Name="MainDiagonalSumResultTextBlock" Text="" Margin="0,20,0,0" HorizontalAlignment="Center"/>

        </StackPanel>

    </Grid>

</Window>


using System;

using System.Windows;

using System.Windows.Controls;


namespace MainDiagonalSumCalculator

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void CalculateSumButton_Click(object sender, RoutedEventArgs e)

        {

            int[,] matrix = {

                { 1, 2, 3 },

                { 4, 5, 6 },

                { 7, 8, 9 }

            };


            int sum = CalculateMainDiagonalSum(matrix);


            MainDiagonalSumResultTextBlock.Text = $"Sum of Main Diagonal: {sum}";

        }


        private int CalculateMainDiagonalSum(int[,] matrix)

        {

            int rows = matrix.GetLength(0);

            int columns = matrix.GetLength(1);


            if (rows != columns)

                throw new ArgumentException("Matrix must be square.");


            int sum = 0;


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

            {

                sum += matrix[i, i];

            }


            return sum;

        }

    }

}

 

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

<Window x:Class="SecondaryDiagonalSumCalculator.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Secondary Diagonal Sum Calculator" Height="250" Width="400">

    <Grid>

        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

            <Button Content="Calculate Diagonal Sum" Width="160" Height="40" Click="CalculateSumButton_Click"/>

            <TextBlock x:Name="SecondaryDiagonalSumResultTextBlock" Text="" Margin="0,20,0,0" HorizontalAlignment="Center"/>

        </StackPanel>

    </Grid>

</Window>


using System;

using System.Windows;

using System.Windows.Controls;


namespace SecondaryDiagonalSumCalculator

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void CalculateSumButton_Click(object sender, RoutedEventArgs e)

        {

            int[,] matrix = {

                { 1, 2, 3 },

                { 4, 5, 6 },

                { 7, 8, 9 }

            };


            int sum = CalculateSecondaryDiagonalSum(matrix);


            SecondaryDiagonalSumResultTextBlock.Text = $"Sum of Secondary Diagonal: {sum}";

        }


        private int CalculateSecondaryDiagonalSum(int[,] matrix)

        {

            int rows = matrix.GetLength(0);

            int columns = matrix.GetLength(1);


            if (rows != columns)

                throw new ArgumentException("Matrix must be square.");


            int sum = 0;


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

            {

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

            }


            return sum;

        }

    }

}

 

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

 
<Window x:Class="VowelConsonantCounter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Vowel and Consonant Counter" Height="300" Width="400">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock Text="Enter a string:"/>
            <TextBox x:Name="InputTextBox" Width="200" Margin="0,5"/>
            <Button Content="Count" Width="100" Height="30" Click="CountButton_Click" Margin="0,10"/>
            <TextBlock x:Name="VowelCountResultTextBlock" Text="" Margin="0,10"/>
            <TextBlock x:Name="ConsonantCountResultTextBlock" Text="" Margin="0,5"/>
        </StackPanel>
    </Grid>
</Window>

using System;
using System.Windows;
using System.Windows.Controls;

namespace VowelConsonantCounter
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CountButton_Click(object sender, RoutedEventArgs e)
        {
            string input = InputTextBox.Text.ToLower();

            int vowelCount = 0;
            int consonantCount = 0;

            for (int i = 0; i < input.Length; i++)
            {
                char ch = input[i];
                if (Char.IsLetter(ch))
                {
                    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                        vowelCount++;
                    else
                        consonantCount++;
                }
            }

            VowelCountResultTextBlock.Text = $"Vowels: {vowelCount}";
            ConsonantCountResultTextBlock.Text = $"Consonants: {consonantCount}";
        }
    }
}

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

 
<Window x:Class="CharacterOccurrenceCounter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Character Occurrence Counter" Height="300" Width="400">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock Text="Enter a string:"/>
            <TextBox x:Name="InputTextBox" Width="200" Margin="0,5"/>
            <TextBlock Text="Enter a character:" Margin="0,5"/>
            <TextBox x:Name="TargetCharacterTextBox" Width="30" Margin="0,5"/>
            <Button Content="Count" Width="100" Height="30" Click="CountButton_Click" Margin="0,10"/>
            <TextBlock x:Name="OccurrenceCountResultTextBlock" Text="" Margin="0,10"/>
        </StackPanel>
    </Grid>
</Window>

using System;
using System.Windows;
using System.Windows.Controls;

namespace CharacterOccurrenceCounter
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CountButton_Click(object sender, RoutedEventArgs e)
        {
            string input = InputTextBox.Text.ToLower();
            char targetCharacter = TargetCharacterTextBox.Text.ToLower()[0];

            int occurrenceCount = 0;

            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] == targetCharacter)
                {
                    occurrenceCount++;
                }
            }

            OccurrenceCountResultTextBlock.Text = $"Occurrences of '{targetCharacter}': {occurrenceCount}";
        }
    }
}

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

 
<Window x:Class="LongestWordLengthFinder.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Longest Word Length Finder" Height="300" Width="400">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock Text="Enter a sentence:"/>
            <TextBox x:Name="InputTextBox" Width="200" Margin="0,5"/>
            <Button Content="Find" Width="100" Height="30" Click="FindButton_Click" Margin="0,10"/>
            <TextBlock x:Name="LongestWordLengthResultTextBlock" Text="" Margin="0,10"/>
        </StackPanel>
    </Grid>
</Window>

using System;
using System.Windows;
using System.Windows.Controls;

namespace LongestWordLengthFinder
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void FindButton_Click(object sender, RoutedEventArgs e)
        {
            string input = InputTextBox.Text;
            int longestWordLength = FindLongestWordLength(input);

            LongestWordLengthResultTextBlock.Text = $"Length of Longest Word: {longestWordLength}";
        }

        private int FindLongestWordLength(string sentence)
        {
            string[] words = sentence.Split(new[] { ' ', '\t', '\n', '\r', '.', ',', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);

            int maxLength = 0;

            foreach (string word in words)
            {
                if (word.Length > maxLength)
                {
                    maxLength = word.Length;
                }
            }

            return maxLength;
        }
    }
}

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

 
<Window x:Class="PangramChecker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Pangram Checker" Height="300" Width="400">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock Text="Enter a string:"/>
            <TextBox x:Name="InputTextBox" Width="200" Margin="0,5"/>
            <Button Content="Check" Width="100" Height="30" Click="CheckButton_Click" Margin="0,10"/>
            <TextBlock x:Name="PangramResultTextBlock" Text="" Margin="0,10"/>
        </StackPanel>
    </Grid>
</Window>


using System;

using System.Linq;

using System.Windows;

using System.Windows.Controls;


namespace PangramChecker

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void CheckButton_Click(object sender, RoutedEventArgs e)

        {

            string input = InputTextBox.Text.ToLower();


            bool isPangram = IsPangram(input);


            PangramResultTextBlock.Text = isPangram ? "The string is a pangram." : "The string is not a pangram.";

        }


        private bool IsPangram(string input)

        {

            bool[] alphabetFlags = new bool[26]; // One flag for each letter of the alphabet


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

            {

                char ch = input[i];

                if (char.IsLetter(ch))

                {

                    alphabetFlags[ch - 'a'] = true;

                }

            }


            return alphabetFlags.All(flag => flag);

        }

    }

}

 


No comments:

Post a Comment