C#
C#
using System;
class Program
{
static void Main()
{
string message = "Hello, world!"; // Replace with your desired message
Console.WriteLine(message);
}
}
------C# program to demonstrate example of Console.Write() and Console.WriteLine().
using System;
class Program
{
static void Main()
{
int number = 42;
string name = "John Doe";
class Program
{
static void Main()
{
Console.WriteLine("This is the first line.");
Console.WriteLine(); // Prints a new line
Console.WriteLine("This is the second line.");
}
}
------------C# program to print backslash (\).
using System;
class Program
{
static void Main()
{
Console.WriteLine("\\"); // Prints a backslash
}
}
---------------C# program to print size of various data types .
using System;
class Program
{
static void Main()
{
Console.WriteLine("Size of int: " + sizeof(int) + " bytes");
Console.WriteLine("Size of float: " + sizeof(float) + " bytes");
Console.WriteLine("Size of double: " + sizeof(double) + " bytes");
Console.WriteLine("Size of char: " + sizeof(char) + " bytes");
Console.WriteLine("Size of bool: " + sizeof(bool) + " bytes");
}
}
-----------C# program for type conversion from double to int .
using System;
class Program
{
static void Main()
{
double doubleValue = 3.14;
int intValue = (int)doubleValue;
class Program
{
static void Main()
{
int intValue = 42;
double doubleValue = 3.14;
bool boolValue = true;
char charValue = 'A';
class Program
{
static void Main()
{
// Declare and assign values to variables
int intValue = 42;
double doubleValue = 3.14;
bool boolValue = true;
char charValue = 'A';
string stringValue = "Hello, world!";
// Print the values of variables
Console.WriteLine("Int value: " + intValue);
Console.WriteLine("Double value: " + doubleValue);
Console.WriteLine("Bool value: " + boolValue);
Console.WriteLine("Char value: " + charValue);
Console.WriteLine("String value: " + stringValue);
}
}
------C# program to input and print an integer number .
using System;
class Program
{
static void Main()
{
Console.Write("Enter an integer number: ");
string input = Console.ReadLine();
int number;
bool isValid = int.TryParse(input, out number);
if (isValid)
{
Console.WriteLine("You entered: " + number);
}
else
{
Console.WriteLine("Invalid input. Please enter a valid integer
number.");
}
}
}
----------C# program to demonstrate example of arithmetic operators .
using System;
class Program
{
static void Main()
{
int a = 10;
int b = 5;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
class Program
{
static void Main()
{
int a = 10;
int b = 5;
// Assignment operators
a += b; // Equivalent to: a = a + b;
Console.WriteLine("a += b: " + a);
a -= b; // Equivalent to: a = a - b;
Console.WriteLine("a -= b: " + a);
a *= b; // Equivalent to: a = a * b;
Console.WriteLine("a *= b: " + a);
a /= b; // Equivalent to: a = a / b;
Console.WriteLine("a /= b: " + a);
a %= b; // Equivalent to: a = a % b;
Console.WriteLine("a %= b: " + a);
}
}
----------C# program to demonstrate example of sizeof() operator .
using System;
class Program
{
static void Main()
{
Console.WriteLine("Size of int: " + sizeof(int) + " bytes");
Console.WriteLine("Size of float: " + sizeof(float) + " bytes");
Console.WriteLine("Size of double: " + sizeof(double) + " bytes");
Console.WriteLine("Size of char: " + sizeof(char) + " bytes");
Console.WriteLine("Size of bool: " + sizeof(bool) + " bytes");
}
}
-------------C# program to demonstrate example of equal to and not equal to
operators .
using System;
class Program
{
static void Main()
{
int a = 5;
int b = 10;
class Program
{
static void Main()
{
int a = 5;
int b = 10;
class Program
{
static void Main()
{
int a = 5; // Binary: 00000101
int b = 3; // Binary: 00000011
class Program
{
static void Main()
{
int num1, num2, sum;
--------------------C# program to swap two numbers with and without using third
variable .
1 with third variable:
using System;
class Program
{
static void Main()
{
int num1, num2, temp;
temp = num1;
num1 = num2;
num2 = temp;
class Program
{
static void Main()
{
int num1, num2, temp;
temp = num1;
num1 = num2;
num2 = temp;
------------C# | print type, max and min value of various data types .
using System;
class Program
{
static void Main()
{
Console.WriteLine("Type: int");
Console.WriteLine("Maximum Value: " + int.MaxValue);
Console.WriteLine("Minimum Value: " + int.MinValue);
Console.WriteLine();
Console.WriteLine("Type: float");
Console.WriteLine("Maximum Value: " + float.MaxValue);
Console.WriteLine("Minimum Value: " + float.MinValue);
Console.WriteLine();
Console.WriteLine("Type: double");
Console.WriteLine("Maximum Value: " + double.MaxValue);
Console.WriteLine("Minimum Value: " + double.MinValue);
Console.WriteLine();
Console.WriteLine("Type: char");
Console.WriteLine("Maximum Value: " + char.MaxValue);
Console.WriteLine("Minimum Value: " + char.MinValue);
Console.WriteLine();
Console.WriteLine("Type: bool");
Console.WriteLine("Maximum Value: " + bool.TrueString);
Console.WriteLine("Minimum Value: " + bool.FalseString);
Console.WriteLine();
}
}
class Program
{
static void Main()
{
int num1, num2;
class Program
{
static void Main()
{
int num = 20; // Binary: 00010100
-------------- C# program to read the grade of students and print the appropriate
description of
grade .
using System;
class Program
{
static void Main()
{
Console.Write("Enter the grade: ");
string grade = Console.ReadLine();
string description;
switch (grade)
{
case "A":
description = "Excellent";
break;
case "B":
description = "Good";
break;
case "C":
description = "Average";
break;
case "D":
description = "Below Average";
break;
case "F":
description = "Fail";
break;
default:
description = "Invalid Grade";
break;
}
class Program
{
static void Main()
{
Console.Write("Enter the length in feet: ");
double length = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("The area of the room is: " + area + " square feet");
}
}
class Program
{
static void Main()
{
string message = "Hello, world!";
Console.Write(message);
}
}
class Program
{
static void Main()
{
Console.Write("Enter a binary number: ");
string binary = Console.ReadLine();
class Program
{
static void Main()
{
Console.Write("Enter a decimal number: ");
int decimalNumber = Convert.ToInt32(Console.ReadLine());
class Program
{
static void Main()
{
Console.WriteLine("1. Convert meters to kilometers");
Console.WriteLine("2. Convert kilometers to meters");
Console.Write("Enter your choice (1 or 2): ");
int choice = Convert.ToInt32(Console.ReadLine());
if (choice == 1)
{
Console.Write("Enter distance in meters: ");
double meters = Convert.ToDouble(Console.ReadLine());
class Program
{
static void Main()
{
Console.Write("Enter temperature in Celsius: ");
double celsius = Convert.ToDouble(Console.ReadLine());
class Program
{
static void Main()
{
Console.Write("Enter temperature in Fahrenheit: ");
double fahrenheit = Convert.ToDouble(Console.ReadLine());
---------------C# program to convert entered days into years, weeks, and days .
using System;
class Program
{
static void Main()
{
Console.Write("Enter the number of days: ");
int totalDays = Convert.ToInt32(Console.ReadLine());