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

C#

Uploaded by

soul killer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

C#

Uploaded by

soul killer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

------C# program to print messages

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";

Console.Write("This is a number: ");


Console.WriteLine(number);

Console.Write("This is a name: ");


Console.WriteLine(name);
}
}
-----------C# program to print a new line .
using System;

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;

Console.WriteLine("Double value: " + doubleValue);


Console.WriteLine("Int value: " + intValue);
}
}
---------C# program to convert various data type to string using ToString()
method .
using System;

class Program
{
static void Main()
{
int intValue = 42;
double doubleValue = 3.14;
bool boolValue = true;
char charValue = 'A';

string intString = intValue.ToString();


string doubleString = doubleValue.ToString();
string boolString = boolValue.ToString();
string charString = charValue.ToString();

Console.WriteLine("Int value as string: " + intString);


Console.WriteLine("Double value as string: " + doubleString);
Console.WriteLine("Bool value as string: " + boolString);
Console.WriteLine("Char value as string: " + charString);
}
}
---------C# program to declare different types of variables, assign the values and
print .
using System;

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;

Console.WriteLine("Sum: " + sum);


Console.WriteLine("Difference: " + difference);
Console.WriteLine("Product: " + product);
Console.WriteLine("Quotient: " + quotient);
Console.WriteLine("Remainder: " + remainder);
}
}
---------C# program to demonstrate example of assignment operators .
using System;

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;

bool isEqual = (a == b);


bool isNotEqual = (a != b);

Console.WriteLine("a == b: " + isEqual);


Console.WriteLine("a != b: " + isNotEqual);
}
}
-------------C# program to demonstrate example of relational operators .
using System;

class Program
{
static void Main()
{
int a = 5;
int b = 10;

bool isGreater = (a > b);


bool isLess = (a < b);
bool isGreaterOrEqual = (a >= b);
bool isLessOrEqual = (a <= b);

Console.WriteLine("a > b: " + isGreater);


Console.WriteLine("a < b: " + isLess);
Console.WriteLine("a >= b: " + isGreaterOrEqual);
Console.WriteLine("a <= b: " + isLessOrEqual);
}
}
----------------------- C# program to demonstrate example of bitwise operators .
using System;

class Program
{
static void Main()
{
int a = 5; // Binary: 00000101
int b = 3; // Binary: 00000011

int bitwiseAnd = a & b; // Binary: 00000001


int bitwiseOr = a | b; // Binary: 00000111
int bitwiseXor = a ^ b; // Binary: 00000110
int bitwiseComplementA = ~a; // Binary: 11111010
int leftShift = a << 2; // Binary: 00010100
int rightShift = a >> 2; // Binary: 00000001

Console.WriteLine("Bitwise AND: " + bitwiseAnd);


Console.WriteLine("Bitwise OR: " + bitwiseOr);
Console.WriteLine("Bitwise XOR: " + bitwiseXor);
Console.WriteLine("Bitwise complement of a: " + bitwiseComplementA);
Console.WriteLine("Left shift: " + leftShift);
Console.WriteLine("Right shift: " + rightShift);
}
}

----------C# program to find the addition of two integer numbers .


using System;

class Program
{
static void Main()
{
int num1, num2, sum;

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


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

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


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

sum = num1 + num2;


Console.WriteLine("The sum of {0} and {1} is: {2}", 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;

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


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

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


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

Console.WriteLine("Before swapping: num1 = {0}, num2 = {1}", num1, num2);

temp = num1;
num1 = num2;
num2 = temp;

Console.WriteLine("After swapping: num1 = {0}, num2 = {1}", num1, num2);


}
}
2.without third variable:
using System;

class Program
{
static void Main()
{
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());

Console.WriteLine("Before swapping: num1 = {0}, num2 = {1}", num1, num2);

temp = num1;
num1 = num2;
num2 = temp;

Console.WriteLine("After swapping: num1 = {0}, num2 = {1}", num1, num2);


}
}

------------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();
}
}

------------------C# program to swap numbers using XOR operator .


using System;

class Program
{
static void Main()
{
int num1, num2;

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


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

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


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

Console.WriteLine("Before swapping: num1 = {0}, num2 = {1}", num1, num2);

num1 = num1 ^ num2;


num2 = num1 ^ num2;
num1 = num1 ^ num2;

Console.WriteLine("After swapping: num1 = {0}, num2 = {1}", num1, num2);


}
}

----------C# program to demonstrate the example of the left-shift operator .


using System;
class Program
{
static void Main()
{
int num = 5; // Binary: 00000101

Console.WriteLine("Before left-shift: " + num);

num = num << 2; // Left-shift by 2 positions

Console.WriteLine("After left-shift: " + num);


}
}

----------C# program to demonstrate the example of the right shift operator .


using System;

class Program
{
static void Main()
{
int num = 20; // Binary: 00010100

Console.WriteLine("Before right-shift: " + num);

num = num >> 2; // Right-shift by 2 positions

Console.WriteLine("After right-shift: " + num);


}
}

-------------- 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;
}

Console.WriteLine("Grade Description: " + description);


}
}

---------------------------C# program to calculate the size of the area in square-


feet based on specified length
and width .
using System;

class Program
{
static void Main()
{
Console.Write("Enter the length in feet: ");
double length = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter the width in feet: ");


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

double area = length * width;

Console.WriteLine("The area of the room is: " + area + " square feet");
}
}

--------------------- C# program to print a message without using the WriteLine()


method .
using System;

class Program
{
static void Main()
{
string message = "Hello, world!";

Console.Write(message);
}
}

---------------C# program to convert a binary number into a decimal number .


using System;

class Program
{
static void Main()
{
Console.Write("Enter a binary number: ");
string binary = Console.ReadLine();

int decimalNumber = Convert.ToInt32(binary, 2);


Console.WriteLine("Decimal equivalent: " + decimalNumber);
}
}

-------------C# program to convert a decimal number into a binary number .


using System;

class Program
{
static void Main()
{
Console.Write("Enter a decimal number: ");
int decimalNumber = Convert.ToInt32(Console.ReadLine());

string binary = Convert.ToString(decimalNumber, 2);

Console.WriteLine("Binary equivalent: " + binary);


}
}

--------------C# program to convert a meter into kilo-meter and vice versa .


using System;

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());

double kilometers = meters / 1000;

Console.WriteLine("Distance in kilometers: " + kilometers);


}
else if (choice == 2)
{
Console.Write("Enter distance in kilometers: ");
double kilometers = Convert.ToDouble(Console.ReadLine());

double meters = kilometers * 1000;

Console.WriteLine("Distance in meters: " + meters);


}
else
{
Console.WriteLine("Invalid choice!");
}
}
}

----------C# program to convert a temperature from Celsius to Fahrenheit .


using System;

class Program
{
static void Main()
{
Console.Write("Enter temperature in Celsius: ");
double celsius = Convert.ToDouble(Console.ReadLine());

double fahrenheit = (celsius * 9 / 5) + 32;

Console.WriteLine("Temperature in Fahrenheit: " + fahrenheit);


}
}

-----------C# program to convert a temperature from Fahrenheit into Celsius .


using System;

class Program
{
static void Main()
{
Console.Write("Enter temperature in Fahrenheit: ");
double fahrenheit = Convert.ToDouble(Console.ReadLine());

double celsius = (fahrenheit - 32) * 5 / 9;

Console.WriteLine("Temperature in Celsius: " + celsius);


}
}

---------------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());

int years = totalDays / 365;


int weeks = (totalDays % 365) / 7;
int days = (totalDays % 365) % 7;

Console.WriteLine("Years: " + years);


Console.WriteLine("Weeks: " + weeks);
Console.WriteLine("Days: " + days);
}
}

You might also like