C# exercises and solutions-Conditional statements switch case


C# Conditional statements switch case

These C# exercises help you practice using the switch...case statement to test a variable against a list of values.

Exercise 1: Write a C# program to detect key presses.

If the user pressed number keys( from 0 to 9), the program will display the number that is pressed, otherwise the program will show "Not allowed".

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
char key;
Console.Write("Press a number key:");
key = (char)Console.Read();
switch (key)
{
case '0': Console.WriteLine("You pressed 0"); break;
case '1': Console.WriteLine("You pressed 1"); break;
case '2': Console.WriteLine("You pressed 2"); break;
case '3': Console.WriteLine("You pressed 3"); break;
case '4': Console.WriteLine("You pressed 4"); break;
case '5': Console.WriteLine("You pressed 5"); break;
case '6': Console.WriteLine("You pressed 6"); break;
case '7': Console.WriteLine("You pressed 7"); break;
case '8': Console.WriteLine("You pressed 8"); break;
case '9': Console.WriteLine("You pressed 9"); break;
default: Console.WriteLine("Not allowed!"); break;

            }


     }
  }
}

Exercise 2: Write a C# program that allows the user to choose the correct answer of a question.

See the example below:

What is the correct way to declare a variable to store an integer value in C#?

a. int 1x=10;

b. int x=10;

c. float x=10.0f;

d. string x="10";

Choose the answer letter: c

Incorrect choice!

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine("What is the correct way to declare a variable to store an integer 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 ans = (char)Console.read();
switch (ans)
{
   case 'a':Console.WriteLine("Invalid choice!"); break;
   case 'b':Console.WriteLine("Congratulation!"); break;
  case 'c':Console.WriteLine("Invalid choice!"); break;
  case 'd':Console.WriteLine("Invalid choice!"); break;
  default:Console.WriteLine("Bad choice!");break;


       }


    }

   }

}




Comments

Vinay Choudhary comment

 Vinay Choudhary

class Program
{
static void Main(string[] args)
{
float quiz_score;
float mid_score;
float final_score;
float avg;
float a;

Console.WriteLine("Enter Quiz Score");
if( float.TryParse(Console.ReadLine(),out a))
{
quiz_score = a;
Console.WriteLine("Enter Mid Score");
mid_score = float.Parse(Console.ReadLine());
Console.WriteLine("Enter Final Score");
final_score = float.Parse(Console.ReadLine());
avg = (quiz_score + mid_score + final_score) / 3;

if (avg >= 90)
{
Console.WriteLine("A Grade");
}
else if ((avg >= 70) && (avg < 90))
{
Console.WriteLine("B Grade");
}
else if ((avg >= 50) && (avg < 70))
{
Console.WriteLine("C Grade");
}
else if (avg < 50)
{
Console.WriteLine("F Grade");
}
else
{
Console.WriteLine("Invalid Inpt");
}

}
else
{
Console.WriteLine("Invalid Inpt Please Enter Float Value");
}


}
}


2017-11-02
Sphilile comment

 Sphilile

Type comment. how the if- else statement works


2017-03-15
Jai Sharma  comment

 Jai Sharma

Label :
Console.WriteLine("What is a correct way to declare a variable which store Int");
Console.WriteLine("a : int a=10;");
Console.WriteLine("b : float a=10;");
Console.WriteLine("c : long a=10;");
Console.WriteLine("d : int[] a=10;");
Console.WriteLine("Your Choice ???");
string choice = Console.ReadLine();
if (choice == "a")
{
Console.WriteLine("you have right choice");
}
else
{
Console.WriteLine("you have wrong choice");
}


goto Label;


2017-02-01
Raja Waleed comment

 Raja Waleed

This Website is best for programming practise....


2015-10-06
arob comment

 arob

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConditionalStatements
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter a number from 0 to 9");
int number = int.Parse(Console.ReadLine());

switch (number)
{
case 1:
Console.WriteLine("You pressed 1");
break;
case 0:
Console.WriteLine("You pressed 0");
break;
case 2:
Console.WriteLine("You pressed 2");
break;
default:
Console.WriteLine("You pressed something else");
break;
}
Console.ReadLine();
}
}
}


2014-09-13
Gufran Ali comment

 Gufran Ali

using System;

namespace LearnCSharpByPractice
{
class Program
{
static void Main(string[] args)
{
string str = "YES";
while (str.Equals("YES"))
{

Console.WriteLine("Press Key 0-9");
string key = Console.ReadLine();
switch (key)
{
case "0": Console.WriteLine("you pressed 0");
break;
case "1": Console.WriteLine("you pressed 1");
break;
case "2":
Console.WriteLine("you pressed 2");
break;
case "3":
Console.WriteLine("you pressed 3");
break;
case "4":
Console.WriteLine("you pressed 4");
break;
case "5":
Console.WriteLine("you pressed 5");
break;
case "6":
Console.WriteLine("you pressed 6");
break;
case "7":
Console.WriteLine("you pressed 7");
break;
case "8":
Console.WriteLine("you pressed 8");
break;
case "9":
Console.WriteLine("you pressed 9");
break;
default:
Console.WriteLine("Not Allowed");
break;
}
Console.WriteLine("To continue enter Yes");
str = Console.ReadLine();
str = str.ToUpper();

}

Console.ReadKey();
}
}
}


2014-08-05
Shahroz Saqib comment

 Shahroz Saqib

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const string T1 = "A", T2 = "B", T3 = "C", T4 = "D";
string keypress;
string Question = "What is the Correct Way To declare a variable to store an integer value in C#?";
string Op1 = "A:int 1x=10", Op2 = "B:int x=10", Op3 = "C:float x=10.0f",Op4="D:string x=\"10\"";

Console.WriteLine ("{0}",Question );
Console.WriteLine ("{0}\n{1}\n{2}\n{3}",Op1,Op2,Op3,Op4 );
Console.WriteLine ("Choose The Answer Letter");
keypress = Console.ReadLine ();
switch (keypress)
{
case T1:
if (keypress == T1)
Console.WriteLine("incorrect");
break;
case T2:
if (keypress == T2)
Console.WriteLine("Correct");
break;
case T3:
if (keypress == T3)
Console.WriteLine("incorrect");
break;
case T4:
if (keypress == T4)
Console.WriteLine("incorrect");
break;
}
Console.ReadKey();


}
}
}


2013-09-24
teymour comment

 teymour

In exercise 2, solution is java. not c#


2013-09-01
Dara comment

 Dara

This bug happens because the read() method of the Console class returns only one character. To solve this problem, you need to do more code. You can try this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String str;
Console.WriteLine("Press a number key:");
str =Console.ReadLine();
if (str.Length == 1)
{
char[] key=str.ToCharArray();
switch (key[0])
{
case '0': Console.WriteLine("0"); break;
case '1': Console.WriteLine("1"); break;
case '2': Console.WriteLine("2"); break;
case '3': Console.WriteLine("3"); break;
case '4': Console.WriteLine("4"); break;
case '5': Console.WriteLine("5"); break;
case '6': Console.WriteLine("6"); break;
case '7': Console.WriteLine("7"); break;
case '8': Console.WriteLine("8"); break;
case '9': Console.WriteLine("9"); break;
default: Console.WriteLine("Not allowed"); break;
}
}
else
Console.WriteLine("Not allowed");
Console.ReadLine();
}
}
}


2013-08-30
Pravin Wagh comment

 Pravin Wagh

In the first example, if i type 11 it shows "1", whereas it should i shown "Not Allowed"...?


2013-08-30



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.