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

C# Lab Manual

The document provides examples of 15 C# programs covering topics such as printing text, taking user input, performing calculations, conditional logic, and more. Each program is presented with the full code listing and description of what it demonstrates.

Uploaded by

obsinaafmohammed
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)
30 views

C# Lab Manual

The document provides examples of 15 C# programs covering topics such as printing text, taking user input, performing calculations, conditional logic, and more. Each program is presented with the full code listing and description of what it demonstrates.

Uploaded by

obsinaafmohammed
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/ 10

C# LAB MANUAL

1. C# Program to Print Hello World


class Program
{
static void Main (string [] args)
{
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
2. C# Program to Print an Integer Entered by User

class Program
{
static void Main(string[] args)
{
int number;
Console.Write("Enter a number:");
number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("You entered :{0}",number);
Console.ReadLine();
}
}
3. C# Program to Add Two Integers
class Program
{
static void Main(string[] args)
{
int num1, num2, sum;
Console.WriteLine("Calculate the sum of two numbers:");
Console.Write("Input number1:");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input number2:");
num2 = Convert.ToInt32(Console.ReadLine());
sum = num1 + num2;
Console.Write("Result:"+sum);
Console.ReadKey();
}
}
4. Program to Calculate the Simple Interest in C# Code:
int P, T;
float R, SI;
Console.Write("Enter Amount :");
P = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Rate :");
R = Convert.ToSingle(Console.ReadLine());
Console.Write("Enter Time :");
T = Convert.ToInt32(Console.ReadLine());
SI = P * R * T / 100;
Console.WriteLine("Interest is :{0}", SI);
Console.ReadKey();
Console.ReadLine();

5. Program to finds the average of 3 numbers in C# Code:


class Program
{
static void Main(string[] args)
{
int number1,number2,number3,avarage;
Console.Write("Enter 1st number :");
number1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter 2nd number :");
number2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter 3rd number :");
number3 = Convert.ToInt32(Console.ReadLine());
avarage = (number1 + number2 + number3) / 3;
Console.Write("Avarage of three numbers is {0}",avarage);
Console.ReadKey();
}
}

6. C# Program to Count Number of Words in a String Code:

static void Main(string[] args)


{
string sentence;
Console.Write("Enter String : ");
sentence = Console.ReadLine();
string[] words = sentence.Split(' ');
Console.WriteLine("Count of words :"+words.Length);
Console.ReadKey();
}

7. C# Program to Count Number of Words in a String Code:


static void Main(string[] args)
{
string sentence;
Console.Write("Enter String : ");
sentence = Console.ReadLine();
string[] words = sentence.Split(' ');
Console.WriteLine("Count of words :"+words.Length);
Console.ReadKey();
}
8. C# Calculate Rectangle Area Code:

class Program
{
static void Main(string[] args)
{
int area, length, width;
Console.Write("Please write the length of your rectangle: ");
length = Convert.ToInt32(Console.ReadLine());
Console.Write("Please write the width of your rectangle: ");
width = Convert.ToInt32(Console.ReadLine());
area = length * width;
Console.WriteLine("The area of rectangle : {0}", area);
Console.ReadKey();
}
}

9. Find Number is Even or Odd using if else Statement in C#:

static void Main(string[] args)


{
int n;
Console.Write("Enter an integer : ");
n = Int32.Parse(Console.ReadLine());
if(n%2==0)
{
Console.WriteLine("{0} is even",n);
}
else
{
Console.WriteLine("{0} is odd", n);
}
Console.ReadKey();
}

10. Find Numbers Above and Below the Average in C#


static void Main(string[] args)
{
int counter = 0;
int[] numbers = new int[10];
int sum=0,avg=0,low=0,high=0;
for(int i=0;i<10;i++)
{
Console.Write("Number {0}: ",(i+1));
numbers[i] = Convert.ToInt32(Console.ReadLine());
sum += numbers[i];
}
avg = sum / 10;
//avg = sum / numbers.Length;
for (int i=0;i<10;i++)
{
if(numbers[i]<avg)
{
low++;
}
else
{
high++;
}
}
Console.WriteLine("The average is : {0}", avg);
Console.WriteLine("The numbers above the average are: {0}", high);
Console.WriteLine("The numbers below the average are: {0}", low);
Console.ReadKey();
}
11. C# Program to Print all Prime Numbers in an Interval

static void Main(string[] args)


{
int num1, num2,sayac=0;
Console.Write("Enter lower range: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter upper range: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Prime numbers between {0} and {1} are: ",num1,num2);

Console.WriteLine("==============================================");
for(int i=num1;i<num2;i++)
{
sayac = 0;
if(i>1)
{
for(int j=2;j<i;j++)
{
if(i%j==0)
{
sayac = 1;
break;
}
}
if(sayac==0)
{
Console.WriteLine(i);
}
}
}
Console.ReadKey();
}
12.Finding the biggest of three numbers in C#

static void Main(string[] args)


{
int number1, number2, number3;
string result;
Console.Write("Input the first number :");
number1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the second number :");
number2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the third number :");
number3 = Convert.ToInt32(Console.ReadLine());
if (number1 > number2 && number1 > number3)
{
result= "The 1st Number is the greatest among three. \n";
}
else if (number2 > number1 && number2 > number3)
{
result = "The 2nd Number is the greatest among three \n";
}
else
{
result= "The 3rd Number is the greatest among three \n";
}

Console.WriteLine(result);

Console.ReadLine();
}

13. Counting program, the total letter in text in C# Console Code:


static void Main(string[] args)
{
string myString = Console.ReadLine();
int count = 0;

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


{
// check the char for whitespace. If char is not whitespace, increase the count
variable
if (!char.IsWhiteSpace(myString[i]))
{
count++;
}
}
Console.WriteLine("Total letters: "+ count);
Console.ReadLine();
}
14. Get Month Name from Month Number – C# (Switch Case) C# Code:
class Program case 7:
{ Console.WriteLine("July");
static void Main(string[] args) break;
{ case 8:
int monthNumber; Console.WriteLine("August");
Console.Write("Enter Month Number (1 - 12): "); break;
monthNumber = Convert.ToInt32(Console.ReadLine()); case 9:
switch (monthNumber) Console.WriteLine("September");
{ break;
case 1: case 10:
Console.WriteLine("January"); Console.WriteLine("October");
break; break;
case 2: case 11:
Console.WriteLine("February"); Console.WriteLine("November");
break; break;
case 3: case 12:
Console.WriteLine("March"); Console.WriteLine("December");
break; break;
case 4: default:
Console.WriteLine("April"); Console.WriteLine("you did not enter
break; correct value for month name");
case 5: break;
Console.WriteLine("May"); }
break; Console.ReadLine();
case 6: }
Console.WriteLine("June"); }
break;
15. Program to Find Leap Year in C# Code:
static void Main(string[] args)
{
int year;
Console.Write("Enter the Year :");
year = Convert.ToInt32(Console.ReadLine());
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
Console.WriteLine("{0} is Leap Year",year);
else
Console.WriteLine("{0} is not a Leap Year",year);
Console.ReadLine();
}
16. C# Program to Find the Factorial of a Number
static void Main(string[] args)
{
int i, number, fact;
Console.WriteLine("Enter the Number");
number = int.Parse(Console.ReadLine());
fact = number;
for (i = number - 1; i >= 1; i--)
{
fact = fact * i;
}
Console.WriteLine("\nFactorial of Given Number is: "+fact);
Console.ReadLine();
}
17. Display Numbers Between 1 to N Using for Loop Code:
static void Main(string[] args)
{
int n;
Console.Write("Number :");
n = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
18. Nested For Loop in C# (Multiplication Table)
Code 1
static void Main(string[] args) Console.WriteLine("{0}x{1} = {2}", i, j, i * j);
{ //www.csharp-console-examples.com }
for (int i=1;i<=10;i++)
{ Console.WriteLine("====================");
for (int j = 0; j <= 10; j++) }
{ Console.ReadKey();
Code 2 }
static void Main(string[] args)
{ //www.csharp-console-examples.com
for (int j = 1; j <= 10; j++)
{
for (int i = 1; i <= 10; i++)
{
Console.Write("{0}*{1}={2}\t", i, j, (i * j));
}
Console.WriteLine();
}
Console.ReadKey();
}
19. Display Armstrong Number Between Two Intervals in C# Code:
static void Main(string[] args)
{
int num1,num2, n, sum, r;
Console.Write("Enter positive number1 :");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter positive number2 :");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Armstrong Number from {0} to {1}",num1,num2);
for (int i = num1; i <= num2; i++)
{
sum = 0;
n = i;
while (n != 0)
{
r = n % 10;
sum = sum + (r * r * r);
n = n / 10;
}
if (i == sum)
Console.WriteLine(i);
}
Console.ReadKey();
}

20. Enter Only Numbers in C# Console Application using do-while loop


static void Main(string[] args)
{
double val = 0;
string num = "";
Console.Write("Enter Number: ");
ConsoleKeyInfo chr;
do
{
chr = Console.ReadKey(true);
if (chr.Key != ConsoleKey.Backspace)
{
bool control = double.TryParse(chr.KeyChar.ToString(), out val);
if (control)
{
num += chr.KeyChar;
Console.Write(chr.KeyChar);
}
}
else

{
if (chr.Key == ConsoleKey.Backspace && num.Length > 0)
{
num = num.Substring(0, (num.Length - 1));
Console.Write("\b \b");
}
}
}
while (chr.Key != ConsoleKey.Enter);
Console.ReadKey();
}
21. Calculate Sum and Average of an Array in C#
static void Main()
{
double sum=0, avg=0;
double[] numbers = { 10, 20, 50, 40};
for(int i=0;i<numbers.Length;i++)
{
sum += numbers[i];
}
avg = sum / numbers.Length;
Console.WriteLine("The Sum is : "+sum);
Console.WriteLine("The Average is : "+avg);
Console.ReadKey();
}
22. Find The Second Largest Number in Array Using C#
static void Main(string[] args)
{
int n, i, j = 0, largest, secondLargest;
int[] arr1 = new int[50];
Console.Write("\n\nFind the second largest element in an array :\n");
Console.Write("-----------------------------------------\n");
Console.Write("Input the size of array : ");
n = Convert.ToInt32(Console.ReadLine());
/* Stored values into the array*/
Console.Write("Input {0} elements in the array :\n", n);
for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
/* find location of the largest element in the array */
largest = 0;
for (i = 0; i < n; i++)
{
if (largest < arr1[i])
{
largest = arr1[i];
j = i;
}
}
/* ignore the largest element and find the 2nd largest element in the array */
secondLargest = 0;
for (i = 0; i < n; i++)
{
if (i == j)
{
i++; /* ignoring the largest element */
i--;
}
else
{
if (secondLargest < arr1[i])
{
secondLargest = arr1[i];
}}}
Console.Write("The Second largest element in the array is : {0} \n\n",
secondLargest);

Console.ReadKey();
}

You might also like