Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

1. C# Program to Check whether the Entered Number is Even or Odd.

using System;

namespace ConsoleApp5

class Program

static void Main(string[] args)

int num1, rem;

Console.Write("To check whether a number is even or odd :\n");

Console.Write("\n");

Console.Write("Input an integer : ");

num1 = Convert.ToInt32(Console.ReadLine());

rem = num1 % 2;

if (rem == 0)

Console.WriteLine("{0} is an even integer.\n", num1);

else

Console.WriteLine("{0} is an odd integer.\n", num1);

}
2. C# Program to Swap 2 Numbers.

using System;

namespace ConsoleApp6

class Program

static void Main(string[] args)

int num1, num2, temp;

Console.Write("Enter the First Number : ");


num1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the Second Number : ");

num2 = Convert.ToInt32(Console.ReadLine());

temp = num1;

num1 = num2;

num2 = temp;

Console.WriteLine("After Swapping : ");

Console.Write("First Number :{0} \nSecond Number :{1}", num1,


num2);

Console.ReadKey();

}
3. C# Program to Get a Number and Display the Sum of the Digits.

using System;

namespace ConsoleApp7

class Program

static void Main(string[] args)

Console.Write("Input a number(integer): ");

int num = Convert.ToInt32(Console.ReadLine());

int sum = 0;
while (num != 0)

sum =sum + num % 10;

num = num / 10;

Console.WriteLine("Sum of the digits of the said integer: " + sum);

4. C# Program to Get a Number and Display the Number with its Reverse.

using System;
namespace ConsoleApp8

class Program

static void Main(string[] args)

int num, r, sum = 0, t;

Console.Write("Display the number in reverse order:\n");

Console.Write("Input a number: ");

num = Convert.ToInt32(Console.ReadLine());

for (t = num; t != 0; t = t / 10)

r = t % 10;

sum = sum * 10 + r;

Console.Write("The number in reverse order is : {0} \n", sum);

}
5. C# Program to Illustrate the Use of Access Specifiers.

using System;

namespace ConsoleApp9

class Program

static void Main(string[] args)

B obj = new B();

obj.show();

}
}

class A

private int x;

protected int y;

internal int z;

public int a;

protected internal int b;

class B : A

public void show()

Console.WriteLine("Values are : ");

//x=10;

y = 20;

z = 30;

a = 40;

b = 50;

// Console.WriteLine(+x); // Error x is not accessible

Console.WriteLine(y);

Console.WriteLine(z);

Console.WriteLine(a);

Console.WriteLine(b);
Console.ReadLine();

6. C# program for type conversion from double to int.

using System;

namespace ConsoleApp10

{
class Program

static void Main(string[] args)

double a = Convert.ToDouble(Console.ReadLine());

int b = 0;

b = (int)a;

Console.WriteLine("value of a: {0}", a);

Console.WriteLine("value of b( Int value of a): {0}", b);

Console.ReadLine();

}
7. C# program to define various types of constants.

using System;

namespace ConsoleApp11

class Program

static void Main(string[] args)

const int a = 10;

const float b = 20.23f;

const double c = 10.2384;


const char d = 'Y';

const string e = "Hello";

Console.WriteLine("a: {0}", a);

Console.WriteLine("b: {0}", b);

Console.WriteLine("c: {0}", c);

Console.WriteLine("d: {0}", d);

Console.WriteLine("e: {0}", e);

Console.ReadLine();

}
8. C# program to read the grade of students and print the appropriate
description of grade.

using System;

namespace ConsoleApp12

class Program

static void Main(string[] args)

char student_grade;

Console.Write("Enter the student grade: ");

student_grade = Convert.ToChar(Console.ReadLine());

switch (student_grade)

case 'A':

Console.WriteLine("Outstanding");

break;

case 'B':

Console.WriteLine("Excellent");

break;

case 'C':
Console.WriteLine("Good");

break;

case 'D':

Console.WriteLine("Satisfactory");

break;

case 'E':

Console.WriteLine("Just Pass");

break;

case 'F':

Console.WriteLine("Failed");

break;

default:

Console.WriteLine("Invalid GRADE");

break;

}
9. C# program to demonstrate example of nested switch statement

using System;

namespace ConsoleApp13

class Program

static void Main(string[] args)

int number;

Console.Write("Enter a number (1-3): ");

number = Convert.ToInt32(Console.ReadLine());
switch (number)

case 1:

char color;

Console.Write("Enter color value (R/Y/B): ");

color = Console.ReadLine()[0];

//validating it using switch case

//inner switch

switch (color)

case 'R':

case 'r':

Console.WriteLine("You've chosen the color red");

break;

case 'Y':

case 'y':

Console.WriteLine("You've chosen the color yellow");

break;

case 'B':

case 'b':

Console.WriteLine("You've chosen the color red");

break;

default:

Console.WriteLine("Invalid color");
break;

break;

case 2:

Console.WriteLine("Input is 2");

break;

case 3:

Console.WriteLine("Input is 3");

break;

default:

Console.WriteLine("Invalid number");

break;

//hit ENTER to exit the program

Console.ReadLine();

}
10.C# program to read two numbers and find maximum, minimum
number.

using System;

namespace ConsoleApp14

class Program

static void Main(string[] args)

int a;

int b;
int min;

int max;

Console.Write("Enter the first number : ");

a = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second number: ");

b = Convert.ToInt32(Console.ReadLine());

if (a > b)

max = a;

else

max = b;

if (a < b)

min = a;

else

min = b;

Console.WriteLine("Using if-else...");

Console.WriteLine("Maximum number = {0}", max);

Console.WriteLine("Minimum number = {0}", min);

}
11. C# program to find largest of three numbers.

using System;

namespace ConsoleApp15

class Program

static void Main(string[] args)

int num1, num2, num3;

num1 = Convert.ToInt32(Console.ReadLine()); ;

num2 = Convert.ToInt32(Console.ReadLine()); ;
num3 = Convert.ToInt32(Console.ReadLine()); ;

if (num1 > num2)

if (num1 > num3)

Console.Write("Number one is the largest!\n"+ num1);

else

Console.Write("Number three is the largest!\n"+ num3);

else if (num2 > num3)

Console.Write("Number two is the largest!\n"+ num2);

else

Console.Write("Number three is the largest!\n"+ num3);

}
12.C# Program printing an integer array using foreach loop.

using System;

namespace ConsoleApp16

class Program

static void Main(string[] args)

int[] data = { 20, 48, 66, 87, 98 };

foreach (int element in data)

{
Console.WriteLine(element);

Console.ReadKey();

13.C# Program Two-dimensional array with fixed row size and variable
columns size

using System;

namespace ConsoleApp17

{
class Program

static void Main(string[] args)

int[][] jag = new int[3][];

jag[0] = new int[] { 20, 30, 55, 86, 97 };

jag[1] = new int[] { 25, 52 };

jag[2] = new int[] { 11, 22, 33 };

for (int i = 0; i < 3; i++)

for (int j = 0; j < jag[i].Length; j++)

Console.Write("{0}\t", jag[i][j]);

Console.WriteLine();

Console.ReadKey();

}
14. C# program to demonstrate the example of hierarchical inheritance.

using System;

namespace ConsoleApp18

class Program

static void Main(string[] args)

Parent p = new Parent();

p.display();

Son s = new Son();


s.display();

s.displayOne();

Daughter d = new Daughter();

d.displayTwo();

Console.ReadKey();

class Parent

public void display()

Console.WriteLine("Parent");

class Son : Parent

public void displayOne()

Console.WriteLine("Son");

class Daughter : Parent

public void displayTwo()

{
Console.WriteLine("Daughter");

15. C# program to demonstrate the example of multilevel inheritance with


method overriding

using System;

namespace ConsoleApp19

class Program
{

static void Main(string[] args)

Parent p = new Parent();

p.display();

p.GetInfo();

Son s = new Son();

s.displayOne();

s.GetInfo();

Daughter d = new Daughter();

d.displayTwo();

Console.ReadKey();

class Parent

public virtual void GetInfo()

Console.WriteLine("Name: {0}");

Console.WriteLine();

public void display()

Console.WriteLine("Parent");

}
}

class Son : Parent

public override void GetInfo()

Console.WriteLine("Name: {0}");

Console.WriteLine();

public void displayOne()

Console.WriteLine("Son");

class Daughter : Son

public void displayTwo()

Console.WriteLine("Daughter");

You might also like