03 February 2024

Boost Your Programming Skills: C# Program for Prime Number Verification

 Boost Your Programming Skills: C# Program for Prime Number Verification
using System;
namespace PrimeNumberChecker
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a number: ");
            int number = int.Parse(Console.ReadLine());
            if (IsPrime(number))
            {
                Console.WriteLine($"{number} is a prime number.");
            }
            else
            {
                Console.WriteLine($"{number} is not a prime number.");
            }
        }
        public static bool IsPrime(int number)
        {
            if (number <= 1)
                return false; // Not prime if less than or equal to 1
            if (number == 2)
                return true; // 2 is prime
            // Start checking from 2
            for (int i = 2; i * i <= number; i++)
            {
                if (number % i == 0)
                    return false; // Not prime if divisible by any i
            }
            return true; // Return true if it is prime
        }
    }
}

This program defines a function `IsPrime` that checks whether the input number is prime. It handles special cases (less than or equal to 1 and 2) and then iterates from 2 up to the square root of the number to determine primality. If the number is divisible by any integer in this range, it is not prime; otherwise, it is considered prime¹⁴. Feel free to try it out! .


10 August 2014

Could not find any resources appropriate for the specified culture or the neutral culture

In properties go to Application Tab. Here in applicatin tab verify your assembly name and Default namespace.
You need to change them appropriately in order to eliminate this error.

09 August 2014

A Step-by-Step Guide: Adding Character in the Ascii Sequence to Your String Like a Pro

Today, we will take a One Scenario for  A Step-by-Step Guide: Adding Character in the Ascii Sequence to Your String Like a Pro
Eg:
   Input :LAKSHMI
  Output:LM AB KL ST HI MN IJ.
Here is a Code:
#region Declaration
private string _addValue = string.Empty;
private string _result = string.Empty;
private string _res = string.Empty;
#endregion
 #region Functions
private string AddValues(string c)
{
switch (c)
{
case "A": _addValue = "AB";
break;
 case "B": _addValue = "BC";
 break;
case "C":
 _addValue = "CD";
break;
case "D":
_addValue = "DE";
break;
case "E":
 _addValue = "EF";
break;
case "F":
_addValue = "FG";
break;
case "G":
 _addValue = "GH";
break;
case "H":
 _addValue = "HI";
break;
case "I":
 _addValue = "IJ";
 break;
case "J":
_addValue = "JK";
 break;
case "K":
_addValue = "KL";
break;
case "L":
 _addValue = "LM";
break;
case "M":
_addValue = "MN";
break;
case "N":
_addValue = "NO";
break;
case "O":
_addValue = "OP";
 break;
case "P":
 _addValue = "QR";
break;
case "R":
_addValue = "RS";
break;
case "S":
_addValue = "ST";
 break;
case "T":
 _addValue = "TU";
break;
 case "U":
 _addValue = "UV";
 break;
 case "V":
 _addValue = "VW";
break;
case "W":
 _addValue = "WX";
break;
case "X":
_addValue = "XY";
break;
case "Y":
_addValue = "YZ";
break;
case "Z":
_addValue = "ZA";
 break;
 }
return _addValue;
}
private string FinalResult(TextBox txt)
{
String str = Convert.ToString(txt.Text);
 char[] szArr = str.ToCharArray();
 for (int idx = 0; idx < szArr.Length; idx++)
{
_result = AddValues(Convert.ToString(szArr[idx]));
if (_res == string.Empty)
{
_res = _result;
}
else
{
_res = _res + " " + _result;
 }
 }
 return _res;
}
 #endregion
Call this function in Button Click Event
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = FinalResult(textBox1);
}
Conclusion:
   Thanks for your read this article.