C#Programs
C#Programs
namespace primenum
{
class Prime
{
static void Main(string[] args)
{
int n, x,i;
int flag = 0;
System.Console.WriteLine("Enter Number== ");
n = int.Parse(Console.ReadLine());
x = n / 2;
for(i=2; i<=x; i++)
{
if (n%i == 0)
{
System.Console.WriteLine("Number is Not Prime.");
flag = 1;
break;
}
}
if (flag == 0)
{
System.Console.WriteLine("Number is Prime.");
Console.ReadKey();
}}}}
2
OUTPUT:
namespace evennum
{
class Program
{
static void Main(string[] args)
{
int i;
int sum = 0;
for(i=2;i<=20; i++)
{
if (i % 2 == 0)
{
sum = sum + i;
}
}
System.Console.WriteLine("Sum of even number from 2
to 20 ==: " + sum);
Console.ReadKey();
3
}}}}
OUTPUT:
namespace arrayfor
{
class Program
{
static void Main(string[] args)
{
char[] myArray = { 'H','A', 'R','P' ,'R','E', 'E', 'T' };
}
Console.ReadKey();
}}}
OUTPUT:
namespace table
{
class Program
{
static void Main(string[] args)
5
{
int i = 1, n = 5, product;
do
{
product = n * i;
Console.WriteLine("{0} * {1} = {2}", n, i, product);
i++;
} while (i <= 10);
Console.ReadKey();
}}}
OUTPUT:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fact
{
class Program
{
6
OUTPUT:
namespace armstromgnum
{
7
class Program
{
static void Main(string[] args)
{
int n, r, sum = 0, temp;
Console.Write("Enter the Number== ");
n = int.Parse(Console.ReadLine());
temp = n;
while (n > 0)
{
r = n % 10;
sum = sum + (r * r * r);
n = n / 10;
}
if (temp == sum)
Console.Write("Armstrong Number:");
else
Console.Write("Not Armstrong Number:");
Console.ReadKey();
}}}
OUTPUT:
8
namespace palidrome
{
class Program
{
static void Main(string[] args)
{
int n, r, sum = 0, temp;
Console.Write("Enter the Number==");
n = int.Parse(Console.ReadLine());
temp = n;
while (n > 0)
{
r = n % 10;
sum = (sum * 10) + r;
n = n / 10;
}
if (temp == sum)
Console.Write("Number is Palindrome.");
else
Console.Write("Number is not Palindrome");
Console.ReadKey();
}
}
}
OUTPUT:
9
namespace reversenum
{
class Program
{
static void Main(string[] args)
{
int n, reverse = 0, rem;
Console.Write("Enter a number: ");
n = int.Parse(Console.ReadLine());
while (n != 0)
{
rem = n % 10;
reverse = reverse * 10 + rem;
n /= 10;
}
10
OUTPUT:
namespace swapnum
{
class Program
{
static void Main(string[] args)
{
x = y;
y = temp;
Console.WriteLine("Values after swapping with
3rd variable are:");
Console.WriteLine("x=" + x);
Console.WriteLine("y=" + y);
Console.ReadKey();
}
}
}
OUTPUT:
namespace area
{
class Program
{
12
Console.WriteLine("______________________________________
____________");
}
}
}
}
13
OUTPUT:
namespace switchcase
{
class Program
{
static void Main(string[] args)
{
char ch;
Console.WriteLine("Enter an alphabet");
ch = Convert.ToChar(Console.ReadLine());
14
switch (Char.ToLower(ch))
{
case 'a':
Console.WriteLine("Vowel");
break;
case 'e':
Console.WriteLine("Vowel");
break;
case 'i':
Console.WriteLine("Vowel");
break;
case 'o':
Console.WriteLine("Vowel");
break;
case 'u':
Console.WriteLine("Vowel");
break;
default:
Console.WriteLine("Not a vowel");
break;
}
Console.ReadLine();}}}
OUTPUT:
15
namespace ifelse
{
class Program
{
static void Main(string[] args)
{
int num1, evn1;
Console.Write("\n\n");
Console.Write("Entered a number is even or odd:\n");
num1 = Convert.ToInt32(Console.ReadLine());
evn1 = num1 % 2;
if (evn1 == 0)
Console.WriteLine("{0} is an even number.\n", num1);
else
Console.WriteLine("{0} is an odd number.\n", num1);
Console.ReadKey();}}}
OUTPUT:
16
namespace studentgrade
{
class Program
{
static void Main(string[] args)
{
float num;
Console.WriteLine("\n\n");
Console.Write("Enter the student marks:\n");
num = float.Parse(Console.ReadLine());
if (num >= 90) Console.WriteLine("Grade A");
else if ((num >= 70) && (num < 90))
Console.WriteLine("Grade B");
else if ((num >= 50) && (num < 70))
Console.WriteLine("Grade C");
else if (num < 50) Console.WriteLine("Grade F");
else Console.WriteLine("Invalid input");
Console.ReadLine();}}}
OUTPUT:
17
namespace star
{
class Program
{
static void Main(string[] args)
{ int i, j, n;
Console.Write("Enter number of rows for star pattern :");
n = Convert.ToInt32(Console.ReadLine());
for (i = 0; i < n; i++)
{
for (j = 1; j <= n - i; j++)
Console.Write(" ");
for (j = 1; j <= 2 * i - 1; j++)
Console.Write("*");
Console.Write("\n");
}
Console.ReadLine();}}}
OUTPUT:
18
namespace boxinandunboxing
{
class Program
{
static void Main(string[] args)
{
//boxing
int i = 987;
object o = i;
i = 145;
System.Console. WriteLine ("The value-type value ={0} ",
i);
System.Console. WriteLine ("The object-type value ={0} ",
o);
//unboxing
int j = 987;
object p = j; // implicit boxing
try
{
int jj = (int)p; // attempt to unbox
System.Console.WriteLine("Unboxing OK.");
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("{0} Error: Incorrect
unboxing.", e.Message);
}
19
Console.ReadLine();
}
}
}
OUTPUT:
namespace array1dand2d
{
class Program
{
static void Main(string[] args)
{
20
namespace jaggedarray
{
class Program
{
static void Main(string[] args) {