A simple program to print "Hello, World!".
using System; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } } }
A basic calculator that can add, subtract, multiply, and divide two numbers.
using System; namespace SimpleCalculator { class Program { static void Main(string[] args) { Console.Write("Enter first number: "); double num1 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter second number: "); double num2 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Choose an operation: + - * /"); char operation = Console.ReadLine()[0]; double result = 0; switch (operation) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if (num2 != 0) { result = num1 / num2; } else { Console.WriteLine("Error: Division by zero."); return; } break; default: Console.WriteLine("Invalid operation."); return; } Console.WriteLine("The result is: " + result); } } }
A program to find the largest number among three numbers.
using System; namespace LargestNumber { class Program { static void Main(string[] args) { Console.Write("Enter first number: "); double num1 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter second number: "); double num2 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter third number: "); double num3 = Convert.ToDouble(Console.ReadLine()); double largest = num1; if (num2 > largest) { largest = num2; } if (num3 > largest) { largest = num3; } Console.WriteLine("The largest number is: " + largest); } } }
A program to check if a string is a palindrome.
using System; namespace PalindromeChecker { class Program { static void Main(string[] args) { Console.Write("Enter a string: "); string str = Console.ReadLine(); string reversedStr = new string(str.Reverse().ToArray()); if (str.Equals(reversedStr)) { Console.WriteLine(str + " is a palindrome."); } else { Console.WriteLine(str + " is not a palindrome."); } } } }
A program to print the Fibonacci series up to a specified number of terms.
using System; namespace FibonacciSeries { class Program { static void Main(string[] args) { Console.Write("Enter the number of terms: "); int n = Convert.ToInt32(Console.ReadLine()); int firstTerm = 0, secondTerm = 1; Console.Write("Fibonacci Series: " + firstTerm + ", " + secondTerm); for (int i = 1; i <= n - 2; ++i) { int nextTerm = firstTerm + secondTerm; Console.Write(", " + nextTerm); firstTerm = secondTerm; secondTerm = nextTerm; } } } }
A simple game where the user has to guess a randomly generated number within a certain range.
using System; namespace NumberGuessingGame { class Program { static void Main(string[] args) { Random random = new Random(); int numberToGuess = random.Next(1, 101); int numberOfTries = 0; int guess; bool win = false; while (!win) { Console.Write("Enter your guess (1-100): "); guess = Convert.ToInt32(Console.ReadLine()); numberOfTries++; if (guess == numberToGuess) { win = true; Console.WriteLine("Congratulations! You've guessed the number in " + numberOfTries + " tries."); } else if (guess < numberToGuess) { Console.WriteLine("Too low! Try again."); } else { Console.WriteLine("Too high! Try again."); } } } } }
A program to check if a number is prime.
using System; namespace PrimeNumberChecker { class Program { static void Main(string[] args) { Console.Write("Enter a number: "); int number = Convert.ToInt32(Console.ReadLine()); bool isPrime = true; if (number <= 1) { isPrime = false; } else { for (int i = 2; i <= Math.Sqrt(number); i++) { if (number % i == 0) { isPrime = false; break; } } } if (isPrime) { Console.WriteLine(number + " is a prime number."); } else { Console.WriteLine(number + " is not a prime number."); } } } }
Create a system to manage employee details using classes and objects.
using System; namespace EmployeeManagementSystem { class Employee { private string name; private int id; private double salary; public Employee(string name, int id, double salary) { this.name = name; this.id = id; this.salary = salary; } public void DisplayEmployeeDetails() { Console.WriteLine("Employee ID: " + id); Console.WriteLine("Employee Name: " + name); Console.WriteLine("Employee Salary: " + salary); } } class Program { static void Main(string[] args) { Employee emp1 = new Employee("John Doe", 1, 50000); Employee emp2 = new Employee("Jane Smith", 2, 60000); emp1.DisplayEmployeeDetails(); Console.WriteLine(); emp2.DisplayEmployeeDetails(); } } }
Create a simple banking system with classes for BankAccount and Customer.
using System; namespace BankingSystem { class BankAccount { private int accountNumber; private double balance; public BankAccount(int accountNumber, double balance) { this.accountNumber = accountNumber; this.balance = balance; } public void Deposit(double amount) { balance += amount; Console.WriteLine("Deposited: " + amount); } public void Withdraw(double amount) { if (amount <= balance) { balance -= amount; Console.WriteLine("Withdrew: " + amount); } else { Console.WriteLine("Insufficient balance!"); } } public void DisplayBalance() { Console.WriteLine("Account Number: " + accountNumber); Console.WriteLine("Balance: " + balance); } } class Customer { private string name; private BankAccount account; public Customer(string name, BankAccount account) { this.name = name; this.account = account; } public void DisplayCustomerDetails() { Console.WriteLine("Customer Name: " + name); account.DisplayBalance(); } } class Program { static void Main(string[] args) { BankAccount account1 = new BankAccount(12345, 1000.0); Customer customer1 = new Customer("Alice", account1); customer1.DisplayCustomerDetails(); Console.WriteLine(); account1.Deposit(500.0); account1.Withdraw(300.0); Console.WriteLine(); customer1.DisplayCustomerDetails(); } } }
Create a simple library management system using classes for Book and Library.
using System; using System.Collections.Generic; namespace LibraryManagementSystem { class Book { public string Title { get; set; } public string Author { get; set; } public int ID { get; set; } public Book(int id, string title, string author) { ID = id; Title = title; Author = author; } public void DisplayBookInfo() { Console.WriteLine("ID: " + ID); Console.WriteLine("Title: " + Title); Console.WriteLine("Author: " + Author); } } class Library { private Listbooks = new List (); public void AddBook(Book book) { books.Add(book); Console.WriteLine("Book added successfully."); } public void RemoveBook(int id) { books.RemoveAll(b => b.ID == id); Console.WriteLine("Book removed successfully."); } public void DisplayAllBooks() { Console.WriteLine("Books in the library:"); foreach (var book in books) { book.DisplayBookInfo(); Console.WriteLine(); } } } class Program { static void Main(string[] args) { Library library = new Library(); Book book1 = new Book(1, "C# Programming", "John Doe"); Book book2 = new Book(2, "Learn OOP", "Jane Smith"); library.AddBook(book1); library.AddBook(book2); library.DisplayAllBooks(); library.RemoveBook(1); library.DisplayAllBooks(); } } }
Create a student management system using classes for Student and School.
using System; using System.Collections.Generic; namespace StudentManagementSystem { class Student { public string Name { get; set; } public int ID { get; set; } public double GPA { get; set; } public Student(int id, string name, double gpa) { ID = id; Name = name; GPA = gpa; } public void DisplayStudentInfo() { Console.WriteLine("ID: " + ID); Console.WriteLine("Name: " + Name); Console.WriteLine("GPA: " + GPA); } } class School { private Liststudents = new List (); public void AddStudent(Student student) { students.Add(student); Console.WriteLine("Student added successfully."); } public void RemoveStudent(int id) { students.RemoveAll(s => s.ID == id); Console.WriteLine("Student removed successfully."); } public void DisplayAllStudents() { Console.WriteLine("Students in the school:"); foreach (var student in students) { student.DisplayStudentInfo(); Console.WriteLine(); } } } class Program { static void Main(string[] args) { School school = new School(); Student student1 = new Student(1, "Alice Johnson", 3.5); Student student2 = new Student(2, "Bob Brown", 3.8); school.AddStudent(student1); school.AddStudent(student2); school.DisplayAllStudents(); school.RemoveStudent(1); school.DisplayAllStudents(); } } }
Create an inventory management system using classes for Product and Inventory.
using System; using System.Collections.Generic; namespace InventoryManagementSystem { class Product { public string Name { get; set; } public int ID { get; set; } public double Price { get; set; } public Product(int id, string name, double price) { ID = id; Name = name; Price = price; } public void DisplayProductInfo() { Console.WriteLine("ID: " + ID); Console.WriteLine("Name: " + Name); Console.WriteLine("Price: " + Price); } } class Inventory { private Listproducts = new List (); public void AddProduct(Product product) { products.Add(product); Console.WriteLine("Product added successfully."); } public void RemoveProduct(int id) { products.RemoveAll(p => p.ID == id); Console.WriteLine("Product removed successfully."); } public void DisplayAllProducts() { Console.WriteLine("Products in the inventory:"); foreach (var product in products) { product.DisplayProductInfo(); Console.WriteLine(); } } } class Program { static void Main(string[] args) { Inventory inventory = new Inventory(); Product product1 = new Product(1, "Laptop", 999.99); Product product2 = new Product(2, "Smartphone", 499.99); inventory.AddProduct(product1); inventory.AddProduct(product2); inventory.DisplayAllProducts(); inventory.RemoveProduct(1); inventory.DisplayAllProducts(); } } }
A program to perform division and handle exceptions.
using System; namespace DivisionWithExceptionHandling { class Program { static void Main(string[] args) { Console.Write("Enter the numerator: "); int numerator = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the denominator: "); int denominator = Convert.ToInt32(Console.ReadLine()); try { int result = numerator / denominator; Console.WriteLine("Result: " + result); } catch (DivideByZeroException e) { Console.WriteLine("Error: Division by zero is not allowed."); }
A program to handle array index out of bounds exception.
using System; namespace ArrayIndexOutOfBounds { class Program { static void Main(string[] args) { int[] numbers = { 1, 2, 3, 4, 5 }; try { Console.WriteLine("Enter an index to access the array: "); int index = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Value at index " + index + ": " + numbers[index]); } catch (IndexOutOfRangeException e) { Console.WriteLine("Error: " + e.Message); } } } }
A program to handle invalid cast exception.
using System; namespace InvalidCastException { class Program { static void Main(string[] args) { object obj = "Hello"; try { int num = (int)obj; Console.WriteLine("Value: " + num); } catch (InvalidCastException e) { Console.WriteLine("Error: " + e.Message); } } } }
A program to handle divide by zero exception and log the error.
using System; using System.IO; namespace DivideByZeroExceptionWithLogging { class Program { static void Main(string[] args) { Console.Write("Enter the numerator: "); int numerator = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter the denominator: "); int denominator = Convert.ToInt32(Console.ReadLine()); try { int result = numerator / denominator; Console.WriteLine("Result: " + result); } catch (DivideByZeroException e) { Console.WriteLine("Error: Division by zero is not allowed."); File.WriteAllText("error_log.txt", e.ToString()); } } } }
A program to validate user input and handle exceptions.
using System; namespace UserInputValidation { class Program { static void Main(string[] args) { try { Console.Write("Enter an integer: "); int number = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("You entered: " + number); } catch (FormatException e) { Console.WriteLine("Error: Invalid input format. Please enter an integer."); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } } } }
A program to read from and write to a text file.
using System; using System.IO; namespace FileIO { class Program { static void Main(string[] args) { string filePath = "example.txt"; // Write to file string content = "Hello, this is a test file."; File.WriteAllText(filePath, content); // Read from file string readFileContent = File.ReadAllText(filePath); Console.WriteLine("File content: " + readFileContent); } } }
A program to append text to an existing file.
using System; using System.IO; namespace AppendToFile { class Program { static void Main(string[] args) { string filePath = "example.txt"; Console.Write("Enter text to append: "); string newText = Console.ReadLine(); File.AppendAllText(filePath, newText + Environment.NewLine); Console.WriteLine("Text appended to file."); } } }
A program to read a text file line by line.
using System; using System.IO; namespace ReadFileLineByLine { class Program { static void Main(string[] args) { string filePath = "example.txt"; if (File.Exists(filePath)) { string[] lines = File.ReadAllLines(filePath); foreach (string line in lines) { Console.WriteLine(line); } } else { Console.WriteLine("File does not exist."); } } } }
A program to copy a file from one location to another.
using System; using System.IO; namespace CopyFile { class Program { static void Main(string[] args) { string sourceFilePath = "source