Site Loader
Get a Quote

Quiz Program using Switch Case in C#

Quiz Program using Switch Case: In this tutorial, I am going to create a  Quiz Program using Switch Case in C#.

Suppose you want to develop a Quiz Application in which there is four option of a question and user has to choose one of the answers out of four answers. In this tutorial, I am going to create a Quiz Program in C#.

Quiz Program using Switch Case

Let’s Create a Quiz Program using Switch Case in C#.

Step 1- Open Visual Studio and Create a C# Console Application Project.

Quiz Program using Switch Case

Step 2-  First we need to print Question and four answers.

 Console.WriteLine("What is the correct way to declare a variable to store an string value in C#?");
 Console.WriteLine("a. int 1x=10");
 Console.WriteLine("b. int x=10");
 Console.WriteLine("c. float x=10.0f");
 Console.WriteLine("d. string x=\"10\"");
 Console.WriteLine("Choose the answer letter:");

Step 3- Accept answer from the user.

char cAnswer = (char)Console.Read();


Step 4- Now it’s time to check whether the user has entered correct answer or not. This can be done using the following code.

switch (cAnswer)
 {
 case 'a': Console.WriteLine("Incorrect Answer!"); break;
 case 'b': Console.WriteLine("Incorrect Answer!"); break;
 case 'c': Console.WriteLine("Incorrect Answer!"); break;
 case 'd': Console.WriteLine("Correct Answer!"); break;
 default: Console.WriteLine("Invalid choice!"); break;


 }

Here is the Complete code:

 

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace QuizProgramDemo { class Program { static void Main(string[] args) { Console.WriteLine(“What is the correct way to declare a variable to store an string value in C#?”); Console.WriteLine(“a. int 1x=10”); Console.WriteLine(“b. int x=10”); Console.WriteLine(“c. float x=10.0f”); Console.WriteLine(“d. string x=\”10\””); Console.WriteLine(“Choose the answer letter:”); char cAnswer = (char)Console.Read(); switch (cAnswer) { case ‘a’: Console.WriteLine(“Incorrect Answer!”); break; case ‘b’: Console.WriteLine(“Incorrect Answer!”); break; case ‘c’: Console.WriteLine(“Incorrect Answer!”); break; case ‘d’: Console.WriteLine(“Correct Answer!”); break; default: Console.WriteLine(“Invalid choice!”); break; } Console.ReadKey(); } } }

Run the Program and enter one letter (a,b,c,d) to see the output of the program.

Quiz Program using Switch Case

Hope this Post for Quiz Program using Switch Case in C# helps you to understand Switch Case.

Thank You.

Post Author: TechieFlair

Leave a Reply

Your email address will not be published.