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


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





Comments

Rajesh.R comment

 Rajesh.R

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

namespace WhileDemo
{
/************************************************************************************************************************************************************
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:
************************************************************************************************************************************************************/

class Program
{
static void Main(string[] args)
{
// Instantiation.
WhileLoopDemo demo = new WhileLoopDemo();
demo.QuestionAndAnswer();
Console.ReadKey();
}
}


// Class that will help to Read and Answer the question.
public class WhileLoopDemo
{
// Properties to get answer and to set decision from user.
private char _ans { get; set; }
public char Ans
{
get { return _ans; }
set { _ans = value; }
}
private char _decision { get; set; }
public char Decision
{
get { return _decision; }
set { _decision = value; }
}



// Method to get input from user and show the results in result window.
public void QuestionAndAnswer()
{
Console.Write("What is the command keyword to exit a loop in C#?\n\n");
Console.WriteLine();
Console.WriteLine("a. int");
Console.WriteLine();
Console.WriteLine("b. continue");
Console.WriteLine();
Console.WriteLine("c. break");
Console.WriteLine();
Console.WriteLine("d. exit");
Console.WriteLine();
Console.WriteLine("Please press y to begin the test..");
Decision = char.Parse(Console.ReadLine());


while (Decision == 'y' || Decision == 'Y')
{
Console.WriteLine("Please enter the correct answer:");
Ans = char.Parse(Console.ReadLine());

if (Ans == 'c')
{
Console.WriteLine("Correct Answer..!");
break;
}
else if (Ans == 'a' || Ans == 'b' || Ans == 'd')
{
Console.WriteLine("Wrong Answer");
Console.WriteLine("Would you like to continue...Press 'y'");
Decision = char.Parse(Console.ReadLine());
}

}

}

}
}


2017-06-13
nikita comment

 nikita

there is misspelling in uusing System;


2016-05-20
Kushal comment

 Kushal

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

namespace while_example
{
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");

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

if (choice == "c")
{
Console.WriteLine("Congratulation!");
}
else {
while (con == "y")

{
Console.WriteLine("Incorrect!");
Console.Write("Again? press y to continue:");
con = Console.ReadLine().ToString();
if (con == "y")
{
Console.Write("Enter your choice:");
choice = Console.ReadLine();
if (choice == "c")
{
Console.WriteLine("Congratulation!");
con = "d";

}

}
}
}

Console.ReadLine();
}
}
}


2016-01-18
MattCro comment

 MattCro

One more way for Exercise 1:

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

namespace f1
{
class Program
{
static void Main(string[] args)
{
string choice;

do
{
Console.WriteLine("---------------------------------------------------------------------------------------------------");
Console.WriteLine("The user can choose to continue answering the question or stop answering it. See the example below:");
Console.WriteLine("---------------------------------------------------------------------------------------------------");

Console.WriteLine("\nWhat is the command keyword to exit a loop in C#?");
Console.WriteLine("-------------------------------------------------");


Console.WriteLine("A) int");
Console.WriteLine("B) continue");
Console.WriteLine("C) exit");
Console.WriteLine("D) break");

Console.Write("\nEnter your choice: ");
string Answer = Console.ReadLine();

switch (Answer)
{
case "A":
Console.WriteLine("\nIncorrect");
break;

case "B":
Console.WriteLine("\nIncorrect");
break;

case "C":
Console.WriteLine("\nIncorrect");
break;

case "D":
Console.WriteLine("\nCorrect");
break;

default:
Console.WriteLine("\nPick the letter from A to D");
break;
}

Console.Write("\nAgain? press Y to continue: ");
choice = Console.ReadLine();

} while (choice.ToUpper() == "Y");
}
}
}


2015-10-14
Jonathan comment

 Jonathan

You probably found out by looking at other websites and stuff but the % sign computes division and collects the remainder. So (i % 5 == 0) is seeing if some value i divided by 5 will have a remainder of 0.


2014-10-08
Hmm comment

 Hmm

Can someone explain the (i % 5 == 0)?


2014-08-17



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.