0% found this document useful (0 votes)
321 views9 pages

C# Practice Exercise On (Switch, While, Do While, For Loop)

The document contains examples of C# code using conditional statements like switch case, while loops, do while loops, and for loops. It provides exercises and solutions to practice using these statements and loops. The exercises include detecting key presses, choosing correct answers, printing characters by ASCII code, and printing patterns using loops.

Uploaded by

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

C# Practice Exercise On (Switch, While, Do While, For Loop)

The document contains examples of C# code using conditional statements like switch case, while loops, do while loops, and for loops. It provides exercises and solutions to practice using these statements and loops. The exercises include detecting key presses, choosing correct answers, printing characters by ASCII code, and printing patterns using loops.

Uploaded by

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

`` C# Practice Exercise on (Switch, While, Do While, For

Loop)
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#?");
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;

}
}

C# Loops: while loop exercises


Exercise 1: Write C# program to prompt the user to
choose the correct answer from a list of answer
choices of a question.
The user can choose to continue answering the question or
stop answering it. See the example below:
What is the command keyword to exit a loop in C#?
a. int
b. continue
c. break
d. exit
Enter your choice: b
Incorrect!
Again? press y to continue:
Solution:
uusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
string choice;
string con="y";
Console.WriteLine("What is the command keyword to exit a
loop in C#?");
Console.WriteLine("a.quit");
Console.WriteLine("b.continue");
Console.WriteLine("c.break");
Console.WriteLine("d.exit");

while (con=="y")
{
Console.Write("Enter your choice:");
choice =Console.ReadLine();

if (choice == "c")
{
Console.WriteLine("Congratulation!");
}
else if (choice == "q" || choice == "e")
{ Console.WriteLine("Exiting...!"); break; }
else Console.WriteLine("Incorrect!");
Console.Write("Again? press y to continue:");
con = Console.ReadLine().ToString();

}
}
}

Exercise 2: Write C# program to print the table of


characters that are equivalent to the Ascii codes
from 1 to 122.
The program will print the 10 characters per line.

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

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

int i =1;
while (i <=122)
{
Console.Write((char)i+"\t");
if (i % 10 == 0)
Console.Write("\n");

i++;
}

Console.ReadLine();
}
}
}

C# Loops: do while loop exercises


Exercise 1:Write C# program to prompt the user to
choose the correct answer from a list of answer
choices of a question.
The user can choose to continue answering the question or
stop answering it. See the example below:
What is the command keyword to exit a loop in C#?
a. int
b. continue
c. break
d. exit
Enter your choice: b
Incorrect!
Again? press y to continue:

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

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

string choice;
string con = "y";
Console.WriteLine("What is the command keyword to exit a
loop in C#?");
Console.WriteLine("a.quit");
Console.WriteLine("b.continue");
Console.WriteLine("c.break");
Console.WriteLine("d.exit");

do
{
Console.Write("Enter your choice:");
choice = Console.ReadLine();

if (choice == "c")
{
Console.WriteLine("Congratulation!");
}
else if (choice == "q" || choice == "e")
{ Console.WriteLine("Exiting...!"); break; }
else Console.WriteLine("Incorrect!");

Console.Write("Again? press y to continue:");


con = Console.ReadLine().ToString();
} while (con == "y");
}
}
}

Exercises 2: Write C# program to print the table of


characters that are equivalent to the Ascii codes
from 1 to 122.
The program will print the 10 characters per line.

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

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

int i =1;
do
{
Console.Write((char)i+"\t");
if (i % 10 == 0)
Console.Write("\n");

i++;
} while(i<=122);

Console.ReadLine();
}
}
}
C# Loops: for loop exercises
Exercise 1: Write C# code to produce the output
shown below:
*******
******
*****
****
***
**
*
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int i,j;
for(i=0;i<=6;i++){
for(j=1;j<=7-i;j++) Console.Write("*");
Console.Write("\n");
}

Console.ReadLine();

}
}
}
Exercise 2: Write C# code to print the following
pattern:
1******
12*****
123****
1234***
12345**
123456*
1234567
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int i,j,k;

for (i = 1; i <= 7; i++)


{
for (j = 1; j <= i; ++j)
Console.Write(j);

for (k = 7 - i; k >= 1; k--)


Console.Write("*");

Console.Write("\n");
}

Console.ReadLine();

}
}
}

You might also like