C.net example
// Declare variables and then initialize to zero. int num1 = 0; int num2 = 0; // Display title as the C# console calculator app. Console.WriteLine("Console Calculator in C#\r"); Console.WriteLine("------------------------\n"); // Ask the user to type the first number. Console.WriteLine("Type a number, and then press Enter"); num1 = Convert.ToInt32(Console.ReadLine()); // Ask the user to type the second number. Console.WriteLine("Type another number, and then press Enter"); num2 = Convert.ToInt32(Console.ReadLine()); // Ask the user to choose an option. Console.WriteLine("Choose an option from the following list:"); Console.WriteLine("\ta - Add"); Console.WriteLine("\ts - Subtract"); Console.WriteLine("\tm - Multiply"); Console.WriteLine("\td - Divide"); Console.Write("Your option? "); // Use a switch statement to do the math. switch (Console.ReadLine()) { case "a": Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2)); break; case "s": Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2)); break; case "m": Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2)); break; case "d": Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2)); break; } // Wait for the user to respond before closing. Console.Write("Press any key to close the Calculator console app..."); Console.ReadKey();
Arrays:
using System; using System.Collections; namespace MyProgramApp { class MyProgram { static void Main(string[] args){ int[] my_array = {1,2,3,4,5}; Hashtable my_ht = new Hashtable(); my_ht.Add("name", "Alex Merced"); my_ht.Add("age", "35"); Console.WriteLine(my_array); Console.WriteLine(my_ht); } } }
Make a quiz:
using System; using System.Collections.Generic; using System.Linq; class Quiz { static void Main() { List
questions = new List
{ new Question("What is the capital of France?", new List
{"A. Paris", "B. London", "C. Berlin", "D. Madrid"}, "A"), new Question("Which planet is known as the Red Planet?", new List
{"A. Earth", "B. Mars", "C. Jupiter", "D. Venus"}, "B"), new Question("What is the largest mammal on Earth?", new List
{"A. Elephant", "B. Blue Whale", "C. Giraffe", "D. Lion"}, "B"), new Question("Who wrote 'Romeo and Juliet'?", new List
{"A. Charles Dickens", "B. William Shakespeare", "C. Jane Austen", "D. Mark Twain"}, "B"), new Question("What is the capital of Japan?", new List
{"A. Beijing", "B. Seoul", "C. Tokyo", "D. Bangkok"}, "C"), }; Console.WriteLine("Welcome to the Quiz!"); Console.WriteLine("Answer the following questions:"); int score = 0; foreach (var question in questions) { Console.WriteLine(question.QuestionText); foreach (var option in question.Options) { Console.WriteLine(option); } Console.Write("Your answer: "); string userAnswer = Console.ReadLine().ToUpper(); if (userAnswer == question.CorrectAnswer) { Console.WriteLine("Correct!\n"); score++; } else { Console.WriteLine($"Incorrect. The correct answer is {question.CorrectAnswer}.\n"); } } Console.WriteLine($"Quiz completed! Your score: {score}/{questions.Count}"); } } class Question { public string QuestionText { get; } public List
Options { get; } public string CorrectAnswer { get; } public Question(string questionText, List
options, string correctAnswer) { QuestionText = questionText; Options = options; CorrectAnswer = correctAnswer; } }