C# exercises and solutions-C# Loops: do while loop

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();
}
}
}




Comments

jkl comment

 jkl

kl

08-21-2014
Why? comment

 Why?

Why it prints in 10 characters per line even without following?:
if (i % 10 == 0)
Console.Write("\n");

08-05-2014
Name:
Comment:
Enter the text:
CAPTCHA image

....................................................................................................................Home | Forum | About | Contact
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.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like