0% found this document useful (0 votes)
9 views

C#Programs

The document contains a series of C# programming exercises, each demonstrating different programming concepts such as checking for prime numbers, calculating the sum of even numbers, using loops, and implementing various algorithms like factorial, Armstrong number, and palindrome checks. Each exercise includes code snippets and expected outputs. The document serves as a practical guide for learning basic programming techniques in C#.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C#Programs

The document contains a series of C# programming exercises, each demonstrating different programming concepts such as checking for prime numbers, calculating the sum of even numbers, using loops, and implementing various algorithms like factorial, Armstrong number, and palindrome checks. Each exercise includes code snippets and expected outputs. The document serves as a practical guide for learning basic programming techniques in C#.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1

1)WAP to check prime number.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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:

2)WAP to find sum of even numbers from 2 to 20 using for loop.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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:

3) Write a program of foreach loop.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace arrayfor
{
class Program
{
static void Main(string[] args)
{
char[] myArray = { 'H','A', 'R','P' ,'R','E', 'E', 'T' };

foreach (char ch in myArray)


{
Console.WriteLine(ch);
4

}
Console.ReadKey();

}}}

OUTPUT:

4)WAP to print a table of any number using while loop.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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:

5)WAP to print factorial of a number.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace fact
{
class Program
{
6

static void Main(string[] args)


{
int i, fact = 1, number;
Console.Write("Enter any Number: ");
number = int.Parse(Console.ReadLine());
for (i = 1; i <= number; i++)
{
fact = fact * i;
}
Console.Write("Factorial of " + number + " is: " + fact);
Console.ReadKey();
}}}

OUTPUT:

6)WAP to check Armstrong number.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

7) WAP to check palindrome no..


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

8)WAP to reverse given number.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

Console.Write("Reversed Number: " + reverse);


Console.ReadKey();
}}}

OUTPUT:

9)WAP to swap two number using third variable.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace swapnum
{
class Program
{
static void Main(string[] args)
{

int x = 20, y = 30, temp;


Console.WriteLine("Values before swapping with
3rd variable are:");
Console.WriteLine("x=" + x);
Console.WriteLine("y=" + y);
temp = x;
11

x = y;
y = temp;
Console.WriteLine("Values after swapping with
3rd variable are:");
Console.WriteLine("x=" + x);
Console.WriteLine("y=" + y);
Console.ReadKey();

}
}
}
OUTPUT:

10)WAP to find the area of rectangle and triangle.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace area
{
class Program
{
12

float length, breadth, hieght, breadthfortriangle;


static void Main(string[] args)
{

Program a = new Program();


a.Rectangle();
a.Triangle();
Console.ReadKey();
}
public void Rectangle()
{
Console.WriteLine("Enter the Length for Rectangle");
length = float.Parse(Console.ReadLine());
Console.WriteLine("Enter the breadth for Rectangle");
breadth = float.Parse(Console.ReadLine());
System.Threading.Thread.Sleep(2000);
Console.WriteLine("Area of rectangle is :{0}", length *
breadth);

Console.WriteLine("______________________________________
____________");
}

public void Triangle()


{
Console.WriteLine("Enter the Breadth for Triangle ");
breadthfortriangle = float.Parse(Console.ReadLine());
Console.WriteLine("Enter the Hieght for Triangle ");
hieght = float.Parse(Console.ReadLine());
System.Threading.Thread.Sleep(2000);
Console.WriteLine("Area of Triangle is:{0}",
(breadthfortriangle * hieght) / 2);

}
}
}
13

OUTPUT:

11)WAP of switch statement.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

12) WAP to print the even number using if statement.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

13)WAP to find the grade of student using if else ladder


statement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

14) WAP to print the star triangle.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

15)WAP of boxing and unboxing.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading. Tasks;

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:

16)WAP od 1D and 2D.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace array1dand2d
{
class Program
{
static void Main(string[] args)
{
20

System.Console.WriteLine(" show one dimensional


array");
string[] Names = new string[5];
Names[0] = "harpreet";
Names[1] = "preet";
Names[2] = "husan";
Names[3] = "ritu";
Names[4] = "taran";
Console.WriteLine(" All the element of Names array
is:\n");
int i = 0;
for (i = 0; i < 5; i++)
{
Console.Write("{0}\t", Names[i]);
}
System.Console.WriteLine("\n\n2D array");
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }
};
System.Console.WriteLine(array2D[0, 0]);
System.Console.WriteLine(array2D[0, 1]);
System.Console.WriteLine(array2D[1, 0]);
System.Console.WriteLine(array2D[1, 1]);
System.Console.WriteLine(array2D[3, 0]);
Console.ReadLine();}}}
OUTPUT:
21

17)WAP of jagged array.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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++)
{
System.Console.Write("Row({0}): ", n);
for (int k = 0; k < jagged_arr[n].Length; k++)
{
System.Console.Write("{0} ", jagged_arr[n][k]);
}
System.Console.WriteLine();
}
Console.ReadLine();
}
}
}
OUTPUT:
22

You might also like