1
1)WAP to check prime number.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace primenum
{
class Prime
{
static void Main(string[] args)
{
int n, x,i;
int flag = 0;
[Link]("Enter Number== ");
n = [Link]([Link]());
x = n / 2;
for(i=2; i<=x; i++)
{
if (n%i == 0)
{
[Link]("Number is Not Prime.");
flag = 1;
break;
}
}
if (flag == 0)
{
[Link]("Number is Prime.");
[Link]();
}}}}
2
OUTPUT:
2)WAP to find sum of even numbers from 2 to 20 using for loop.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
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;
}
}
[Link]("Sum of even number from 2
to 20 ==: " + sum);
[Link]();
3
}}}}
OUTPUT:
3) Write a program of foreach loop.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace arrayfor
{
class Program
{
static void Main(string[] args)
{
char[] myArray = { 'H','A', 'R','P' ,'R','E', 'E', 'T' };
foreach (char ch in myArray)
{
[Link](ch);
4
}
[Link]();
}}}
OUTPUT:
4)WAP to print a table of any number using while loop.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace table
{
class Program
{
static void Main(string[] args)
5
{
int i = 1, n = 5, product;
do
{
product = n * i;
[Link]("{0} * {1} = {2}", n, i, product);
i++;
} while (i <= 10);
[Link]();
}}}
OUTPUT:
5)WAP to print factorial of a number.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace fact
{
class Program
{
6
static void Main(string[] args)
{
int i, fact = 1, number;
[Link]("Enter any Number: ");
number = [Link]([Link]());
for (i = 1; i <= number; i++)
{
fact = fact * i;
}
[Link]("Factorial of " + number + " is: " + fact);
[Link]();
}}}
OUTPUT:
6)WAP to check Armstrong number.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace armstromgnum
{
7
class Program
{
static void Main(string[] args)
{
int n, r, sum = 0, temp;
[Link]("Enter the Number== ");
n = [Link]([Link]());
temp = n;
while (n > 0)
{
r = n % 10;
sum = sum + (r * r * r);
n = n / 10;
}
if (temp == sum)
[Link]("Armstrong Number:");
else
[Link]("Not Armstrong Number:");
[Link]();
}}}
OUTPUT:
8
7) WAP to check palindrome no..
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace palidrome
{
class Program
{
static void Main(string[] args)
{
int n, r, sum = 0, temp;
[Link]("Enter the Number==");
n = [Link]([Link]());
temp = n;
while (n > 0)
{
r = n % 10;
sum = (sum * 10) + r;
n = n / 10;
}
if (temp == sum)
[Link]("Number is Palindrome.");
else
[Link]("Number is not Palindrome");
[Link]();
}
}
}
OUTPUT:
9
8)WAP to reverse given number.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace reversenum
{
class Program
{
static void Main(string[] args)
{
int n, reverse = 0, rem;
[Link]("Enter a number: ");
n = [Link]([Link]());
while (n != 0)
{
rem = n % 10;
reverse = reverse * 10 + rem;
n /= 10;
}
10
[Link]("Reversed Number: " + reverse);
[Link]();
}}}
OUTPUT:
9)WAP to swap two number using third variable.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace swapnum
{
class Program
{
static void Main(string[] args)
{
int x = 20, y = 30, temp;
[Link]("Values before swapping with
3rd variable are:");
[Link]("x=" + x);
[Link]("y=" + y);
temp = x;
11
x = y;
y = temp;
[Link]("Values after swapping with
3rd variable are:");
[Link]("x=" + x);
[Link]("y=" + y);
[Link]();
}
}
}
OUTPUT:
10)WAP to find the area of rectangle and triangle.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace area
{
class Program
{
12
float length, breadth, hieght, breadthfortriangle;
static void Main(string[] args)
{
Program a = new Program();
[Link]();
[Link]();
[Link]();
}
public void Rectangle()
{
[Link]("Enter the Length for Rectangle");
length = [Link]([Link]());
[Link]("Enter the breadth for Rectangle");
breadth = [Link]([Link]());
[Link](2000);
[Link]("Area of rectangle is :{0}", length *
breadth);
[Link]("______________________________________
____________");
}
public void Triangle()
{
[Link]("Enter the Breadth for Triangle ");
breadthfortriangle = [Link]([Link]());
[Link]("Enter the Hieght for Triangle ");
hieght = [Link]([Link]());
[Link](2000);
[Link]("Area of Triangle is:{0}",
(breadthfortriangle * hieght) / 2);
}
}
}
13
OUTPUT:
11)WAP of switch statement.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace switchcase
{
class Program
{
static void Main(string[] args)
{
char ch;
[Link]("Enter an alphabet");
ch = [Link]([Link]());
14
switch ([Link](ch))
{
case 'a':
[Link]("Vowel");
break;
case 'e':
[Link]("Vowel");
break;
case 'i':
[Link]("Vowel");
break;
case 'o':
[Link]("Vowel");
break;
case 'u':
[Link]("Vowel");
break;
default:
[Link]("Not a vowel");
break;
}
[Link]();}}}
OUTPUT:
15
12) WAP to print the even number using if statement.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace ifelse
{
class Program
{
static void Main(string[] args)
{
int num1, evn1;
[Link]("\n\n");
[Link]("Entered a number is even or odd:\n");
num1 = Convert.ToInt32([Link]());
evn1 = num1 % 2;
if (evn1 == 0)
[Link]("{0} is an even number.\n", num1);
else
[Link]("{0} is an odd number.\n", num1);
[Link]();}}}
OUTPUT:
16
13)WAP to find the grade of student using if else ladder
statement.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace studentgrade
{
class Program
{
static void Main(string[] args)
{
float num;
[Link]("\n\n");
[Link]("Enter the student marks:\n");
num = [Link]([Link]());
if (num >= 90) [Link]("Grade A");
else if ((num >= 70) && (num < 90))
[Link]("Grade B");
else if ((num >= 50) && (num < 70))
[Link]("Grade C");
else if (num < 50) [Link]("Grade F");
else [Link]("Invalid input");
[Link]();}}}
OUTPUT:
17
14) WAP to print the star triangle.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace star
{
class Program
{
static void Main(string[] args)
{ int i, j, n;
[Link]("Enter number of rows for star pattern :");
n = Convert.ToInt32([Link]());
for (i = 0; i < n; i++)
{
for (j = 1; j <= n - i; j++)
[Link](" ");
for (j = 1; j <= 2 * i - 1; j++)
[Link]("*");
[Link]("\n");
}
[Link]();}}}
OUTPUT:
18
15)WAP of boxing and unboxing.
using System;
using [Link];
using [Link];
using [Link];
using [Link]. Tasks;
namespace boxinandunboxing
{
class Program
{
static void Main(string[] args)
{
//boxing
int i = 987;
object o = i;
i = 145;
[Link]. WriteLine ("The value-type value ={0} ",
i);
[Link]. WriteLine ("The object-type value ={0} ",
o);
//unboxing
int j = 987;
object p = j; // implicit boxing
try
{
int jj = (int)p; // attempt to unbox
[Link]("Unboxing OK.");
}
catch ([Link] e)
{
[Link]("{0} Error: Incorrect
unboxing.", [Link]);
}
19
[Link]();
}
}
}
OUTPUT:
16)WAP od 1D and 2D.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace array1dand2d
{
class Program
{
static void Main(string[] args)
{
20
[Link](" show one dimensional
array");
string[] Names = new string[5];
Names[0] = "harpreet";
Names[1] = "preet";
Names[2] = "husan";
Names[3] = "ritu";
Names[4] = "taran";
[Link](" All the element of Names array
is:\n");
int i = 0;
for (i = 0; i < 5; i++)
{
[Link]("{0}\t", Names[i]);
}
[Link]("\n\n2D array");
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }
};
[Link](array2D[0, 0]);
[Link](array2D[0, 1]);
[Link](array2D[1, 0]);
[Link](array2D[1, 1]);
[Link](array2D[3, 0]);
[Link]();}}}
OUTPUT:
21
17)WAP of jagged array.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace jaggedarray
{
class Program
{
static void Main(string[] args) {
int[][] jagged_arr = new int[4][];
jagged_arr[0] = new int[] { 1, 2, 3, 4 };
jagged_arr[1] = new int[] { 11, 34, 67 };
jagged_arr[2] = new int[] { 89, 23 };
jagged_arr[3] = new int[] { 0, 45, 78, 53, 99 };
for (int n = 0; n < jagged_arr.Length; n++)
{
[Link]("Row({0}): ", n);
for (int k = 0; k < jagged_arr[n].Length; k++)
{
[Link]("{0} ", jagged_arr[n][k]);
}
[Link]();
}
[Link]();
}
}
}
OUTPUT:
22