C Sharp

1. An Overview of C# 2. OOP 3. C# Environment 4. IDE 5. Basic Requirement 6. Comments 7. My first program 8. Line Break 9. Escape Sequence 10. Identifiers 11. Variables 12. Datatypes 13. Range of datatypes in C# 14. Declare Variables 15. Reserved Key Words 16. Console.Write() 17. Chained & Embedded state 18. Prog.to print sum of 2 nos 19. Console.ReadLine() 20. Prg.to print sum & average 21. Area & Perimeter of Rectangle 22. Area Circumference of Circle 23. Prg.for Simple Calculator 24. Operatos in C# 25. Ex. Of Operators 26. Prg. to Swap two numbers 27. Hierarchy of Operators 28. if( ) statement 29. Prg.to check +ve,-ve or zero 30. if..else statement 31. Prg.to check Odd or Even 32. Prg.to print larger of 2 nos 33. Prg.to print largest of 3 nos 34. Prg.to check divisibility 35. Prg.to check print range 36. for. . . loop statement 37. prg.to print series of nos 38. Prg.to check divisibility 39. prg.to print sum of series 40. Prg.to print Pyramid 41. prg.for Factorial 42. prg.to fill screen 43. prg.for largest/smallest no 44. prg.to print reverse no 45. prg.to add each digit 46. prg.for sum of Factorial 47. prg.for fibonacci series 48. prg.to check Prime nos 49. prg. Prime no from 1 to 100 50. prg.Specified Prime nos 51. while( ) statement 52. do. . While statement 53. goto statement 54. break & continue statement 55. switch case statement 56. Array in C# 57. print reverse order using array 58. Ascending/Descending order 59. prg.to search no in Array 60. Multidimensional arrays 61. String in C# 62. String with spaces 63. prg.to print string in reverse 64. prg.to count A,E,I,O,U vowel 65. User Define Methods 66. Methods program example 67. General Purpose Programs 68. Loan & EMI calculation prg. 69. Table print prg. 70. Leap year program 71. lower to UPPER case prg. 72. Age Distribution prg. 73. Bank note calculation prg. 74. Simple Interest prg. 75. Compound Interest prg. 76. Simple Depreciation prg. 77. Reducing bal.Depreciation prg. 78. Marksheet prg. 79. Income Tax prg. 80. Time calculator prg. 81. Distance converter prg. 82. Volume Air Calculation prg. 83. Time to fill Water Tank prg. 84. Salary Calculation prg. 85. Total Sale Calculation prg. 86. Male/Female pecentage prg. 87. Library Rent prg. 88. Office Expance prg. 89. Total Salary Calculation prg. 90. Profit or Loss prg. 91. Total Profit/Loss prg. 92. Virtual Function . 93. C# Interview Part1 94. C# Interview Part2 95. C# Interview Part3 96. C# Interview Part4 97. C# Interview Part5 98. C# Interview Part6 99. C# Interview Part7 100. C# Interview Part8 101. C# Interview Part9 102. C# Interview Part10 103. C# Interview Part11 104. C# Interview Part12 105. C# Interview Part13 106. C# Interview Part14 107. C# Interview Part15
Pr.Pg Next Pg

Switch, case, default statement tutorials

  • A switch statement allows a variable to be tested for equality against a list of values.

  • Each value is called a case, and the variable being switched on is checked for each switch case.

  • The following rules apply to a switch statement:

  • The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.

  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

  • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.

  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

  • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.

  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Syntax:

switch(expression)

{

case constant-expression:

statement(s);

break;

case constant-expression:

statement(s);

break;

default: // optional

terminates;

}


 

Example:C# program to enter month number and print month in character using switch case

 Sam Sir

//program name test060.cs

//C# program to enter month number and print month in character using switch case

using System;

class Program

{

static void Main()

{

int mno;

Console.Write("Enter month number from 1 to 12 : ");

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

switch (mno)

{

default:

Console.WriteLine("Month number not within 1 to 12 \n");

break;

case 1:

Console.WriteLine("January\n");

break;

case 2:

Console.WriteLine("February\n");

break;

case 3:

Console.WriteLine("March\n");

break;

case 4:

Console.WriteLine("April\n");

break;

case 5:

Console.WriteLine("May\n");

break;

case 6:

Console.WriteLine("June\n");

break;

case 7:

Console.WriteLine("July\n");

break;

case 8:

Console.WriteLine("August\n");

break;

case 9:

Console.WriteLine("September\n");

break;

case 10:

Console.WriteLine("October\n");

break;

case 11:

Console.WriteLine("November\n");

break;

case 12:

Console.WriteLine("December\n");

break;

}

}

}

Output

Enter month number from 1 to 12 : 2

February

 

Enter month number from 1 to 12 : 15

Month number not within 1 to 12


 

Example:C# program to enter month in number and print number of days for that month

 Sam Sir

//program name test061.cs

//C# program to enter month in number and Print number of days for that month

using System;

class Program

{

static void Main()

{

short mno;

Console.Write("Enter month number from 1 to 12: ");

mno = short.Parse(Console.ReadLine());

switch (mno)

{

default:

Console.WriteLine("Month no is Not within 1 to 12");

break;

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

Console.WriteLine("31 Days in month");

break;

case 4:

case 6:

case 9:

case 11:

Console.WriteLine("30 Days in month");

break;

case 2:

int yr;

Console.Write("Input year in 4 digit : ");

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

if (yr % 4 == 0 && yr % 100 != 0 || yr % 400 == 0)

Console.WriteLine("29 Days as Leap Year");

else

Console.WriteLine("28 Days in Month");

break;

}

}

}

Output

Enter month number from 1 to 12 : 5

31 Days in month

 

Enter month number from 1 to 12 : 2

Input year in 4 digit : 2014

28 Days in Month

 

Enter month number from 1 to 12 : 16

Month no is Not within 1 to 12

 

 

Example: C# program to enter character and print enter character is Vovel or not

 Sam Sir

//program name test062.cs

//C# program to enter character and print enter character is Vovel or not

using System;

class Program

{

static void Main()

{

char ch;

Console.Write("Enter a character : ");

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

switch (ch)

{

case 'a':

case 'A':

case 'e':

case 'E':

case 'i':

case 'I':

case 'o':

case 'O':

case 'u':

case 'U':

Console.Write("{0} is a Vowel.\n", ch);

break;

default:

Console.Write("{0} is not a Vowel.\n", ch);

break;

}

}

}

Output

Enter a character : e

e is a Vowel.

 

Enter a character : k

k is not a Vowel.

Pr.Pg border                                              Next Pg