To learn any programming language you have to practice the programs, this is one of the best and faster way to learn programming language.
This webpage contains java programs on various java topics such as C# string programs, control statements, C# Array Programs, C# loops programs, functions, arrays, etc. All the following C# programs are tested and provided with the output.
C# Example : Print Hello World
1 2 3 4 5 6 7 8 |
static void Main(string[] args) { Console.WriteLine("Hello World"); Console.Read(); } |
C# Example : Add Two Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
static void Main(string[] args) { int num1; int num2; int total; Console.WriteLine("Enter first number :"); num1 = Convert.ToInt32( Console.ReadLine()); Console.WriteLine("Enter second number :"); num2 = Convert.ToInt32(Console.ReadLine()); total = num2 + num1; Console.WriteLine("Total is : "+ total); Console.ReadKey(); } |
C# Example : Swap Values of Two Variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
static void Main(string[] args) { int num1; int num2; int temp; Console.WriteLine("Type value of number 1 :"); num1 = Convert.ToInt32( Console.ReadLine()); Console.WriteLine("Type value of number 2 :"); num2 = Convert.ToInt32(Console.ReadLine()); temp = num1; num1 = num2; num2 = temp; Console.WriteLine("\nAfter swapping values"); Console.WriteLine("Value of number 1 : "+ num1); Console.WriteLine("Value of number 2 : "+ num2); Console.ReadKey(); } |
C# Example : Multiply two Floating Point Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
static void Main(string[] args) { double num1; double num2; double total; Console.WriteLine("Enter first number :"); num1 = Convert.ToDouble( Console.ReadLine()); Console.WriteLine("Enter second number :"); num2 = Convert.ToDouble(Console.ReadLine()); total = num2 * num1; Console.WriteLine("\nTotal is : " + total); Console.ReadKey(); } |
C# Example : Perform all Arithmetic Operations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
static void Main(string[] args) { int num1; int num2; int sum, sub, mult, mod; float div; Console.WriteLine("Enter first number :"); num1 = Convert.ToInt32( Console.ReadLine()); Console.WriteLine("Enter second number :"); num2 = Convert.ToInt32(Console.ReadLine()); sum = num1 + num2; sub = num1 - num2; mult = num1 * num2; mod = num1 % num2; div = (float)num1 / num2; Console.WriteLine("\nSum of number1 and number2 : " + sum); Console.WriteLine("Difference of number1 and number2 : "+ sub); Console.WriteLine("Product of number1 and number2 : " + mult); Console.WriteLine("Modulus of number1 and number2 : " + mod); Console.WriteLine("Quotient of number1 and number2 : " + div); Console.ReadKey(); } |
C# Example : Convert Feet to Meter
1 2 3 4 5 6 7 8 9 10 11 12 |
static void Main(string[] args) { double meter, feet; Console.WriteLine("Enter feet :"); feet = Convert.ToInt32(Console.ReadLine()); meter = feet / 3.2808399; Console.WriteLine("\nFeet in meter : " + meter); Console.ReadKey(); } |
C# Example : Convert Celcius to Farenheit
1 2 3 4 5 6 7 8 9 10 11 |
static void Main(string[] args) { double celsius, fahrenheit; Console.WriteLine("Enter Temperature in Celsius : "); celsius = Convert.ToDouble(Console.ReadLine()); fahrenheit = (1.8 * celsius) + 32; Console.WriteLine("Temperature in Fahrenheit : "+ fahrenheit); Console.ReadKey(); } |
C# Example : Print ASCII Value
1 2 3 4 5 6 7 8 9 10 |
static void Main(string[] args) { char c; Console.WriteLine("Enter a character: "); c = Convert.ToChar( Console.ReadLine()); Console.WriteLine("\nASCII Value of " +c+" "+ Convert.ToInt32(c)); Console.ReadKey(); } |
C# Example : Calculate Area of Circle
1 2 3 4 5 6 7 8 9 10 11 |
static void Main(string[] args) { double radius, area; Console.WriteLine("Enter Radius: "); radius = Convert.ToDouble( Console.ReadLine()); area = Math.PI * radius * radius; Console.WriteLine("\nArea of circle: "+area); Console.ReadKey(); } |
C# Example : Calculate Area of Square
1 2 3 4 5 6 7 8 9 10 11 |
static void Main(string[] args) { double side, area; Console.WriteLine("Enter the Length of Side : "); side = Convert.ToDouble( Console.ReadLine()); area = side * side; Console.WriteLine("\nArea of Square: " + area); Console.ReadKey(); } |
C# Example : Calculate Area of Rectangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
static void Main(string[] args) { double length, breadth, area; Console.WriteLine("Enter length of rectangle : "); length = Convert.ToDouble( Console.ReadLine()); Console.WriteLine("Enter breadth of rectangle : "); breadth = Convert.ToDouble(Console.ReadLine()); area = length * breadth; Console.WriteLine("\nArea of rectangle: " + area); Console.ReadKey(); } |
C# Example : Check whether entered number is odd or even
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
static void Main(string[] args) { int number; Console.WriteLine("Enter a number: "); number = Convert.ToInt32(Console.ReadLine()); // Even number if remainder is 0 if (number % 2 == 0) Console.WriteLine("Entered Number is an Even Number"); else Console.WriteLine("Entered Number is odd Number"); Console.ReadLine(); } |
C# Example : Find the Largest Number Among Three Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
static void Main(string[] args) { int num1, num2, num3; Console.Write("Input the 1st number :"); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Input the 2nd number :"); num2 = Convert.ToInt32(Console.ReadLine()); Console.Write("Input the 3rd number :"); num3 = Convert.ToInt32(Console.ReadLine()); if (num1 > num2) { if (num1 > num3) { Console.Write("The 1st Number is the greatest among three. \n\n"); } else { Console.Write("The 3rd Number is the greatest among three. \n\n"); } } else if (num2 > num3) Console.Write("The 2nd Number is the greatest among three \n\n"); else Console.Write("The 3rd Number is the greatest among three \n\n"); Console.ReadLine(); } |
C# Example : Find the Largest Number using Conditional Operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
static void Main(string[] args) { int num1, num2, num3, largest; Console.WriteLine("Enter three numbers:"); num1 = Convert.ToInt32(Console.ReadLine()); num2 = Convert.ToInt32(Console.ReadLine()); num3 = Convert.ToInt32(Console.ReadLine()); largest = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3); Console.WriteLine(largest + " is the largest number."); Console.ReadLine(); } |
C# Example : Find the Largest among Three Variables using Nested if
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
static void Main(string[] args) { int num1, num2, num3; Console.WriteLine("Enter three numbers: \n"); num1 = Convert.ToInt32(Console.ReadLine()); num2 = Convert.ToInt32(Console.ReadLine()); num3 = Convert.ToInt32(Console.ReadLine()); if (num1 >= num2) { if (num1 >= num3) Console.WriteLine(num1+" is the largest number"); else Console.WriteLine(num3 + " is the largest number"); } else if (num2 >= num3) Console.WriteLine(num2 + " is the largest number"); else Console.WriteLine(num3 + " is the largest number"); Console.ReadLine(); } |
C# Example : Check whether an alphabet is a vowel or not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
static void Main(string[] args) { char ch; Console.Write("Input an Alphabet (A-Z or a-z) : "); ch = Convert.ToChar(Console.ReadLine().ToLower()); int i = ch; if (i >= 48 && i <= 57) { Console.Write("You entered a number, Please enter an alpahbet."); } else { switch (ch) { case 'a': Console.WriteLine("The Alphabet is vowel"); break; case 'i': Console.WriteLine("The Alphabet is vowel"); break; case 'o': Console.WriteLine("The Alphabet is vowel"); break; case 'u': Console.WriteLine("The Alphabet is vowel"); break; case 'e': Console.WriteLine("The Alphabet is vowel"); break; default: Console.WriteLine("The Alphabet is not a vowel"); break; } } Console.ReadLine(); } |
C# Example : Calculate the total marks, percentage and division of student based on three subjects
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
static void Main(string[] args) { double rl, phy, che, ca, total; double per; string nm, div; Console.Write("\n\n"); Console.Write("Calculate the total, percentage and division to take marks of three subjects:\n"); Console.Write("-------------------------------------------------------------------------------"); Console.Write("\n\n"); Console.Write("Input the Roll Number of the student :"); rl = Convert.ToInt32(Console.ReadLine()); Console.Write("Input the Name of the Student :"); nm = Console.ReadLine(); Console.Write("Input the marks of Physics : "); phy = Convert.ToInt32(Console.ReadLine()); Console.Write("Input the marks of Chemistry : "); che = Convert.ToInt32(Console.ReadLine()); Console.Write("Input the marks of Computer Application : "); ca = Convert.ToInt32(Console.ReadLine()); total = phy + che + ca; per = total / 3.0; if (per >= 60) div = "First"; else if (per < 60 && per >= 48) div = "Second"; else if (per < 48 && per >= 36) div = "Pass"; else div = "Fail"; Console.Write("\nRoll No : {0}\nName of Student : {1}\n", rl, nm); Console.Write("Marks in Physics : {0}\nMarks in Chemistry : {1}\nMarks in Computer Application : {2}\n", phy, che, ca); Console.Write("Total Marks = {0}\nPercentage = {1}\nDivision = {2}\n", total, per, div); Console.ReadLine(); } |