C# exercises and solutions programming


C# exercises

According to my own experience as software developer and lecturer, practicing exercises is an important activity to learn a programming language. 

In this  page you will find a lot of C# exercises to help you test your knowledge and skill of writing code in C# and practice the C# programming lessons. You will start from basic C# exercises to more complex exercises. The solution is provided for each exercise. If you have any questions regarding to each problem, you can post them at our forum. If you want to download free C# programs with source code, visit this page: http://www.worldbestlearningcenter.com/index_files/home-ass.htm.

Exercise 1: Write C# code to declare a variable to store the age of a person. Then the output of the program is as an example shown below:

You are 20 years old.

Solution:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Csharp_exercises

{

class Program

{

static void Main(string[] args)

{

int age = 20;// declaring variable and assign 20 to it.

Console.WriteLine("You are {0} years old.",age);

Console.ReadLine();

}

}

}

Exercise 2: Write C# code to display the asterisk pattern as 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)
{

   Console.WriteLine("*****");
   Console.WriteLine("*****");
   Console.WriteLine("*****");
   Console.WriteLine("*****");
   Console.WriteLine("*****");
   Console.ReadLine();
        }
    }
}

Exercise 3: Write C# code to declare two integer variables, one float variable, and one string variable and assign 10, 12.5, and "C# programming" to them respectively. Then display their values on the screen.

Solution:

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

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

int x;
float y;
string s;
x = 10;
y = 12.5f;
s = "C# programming";
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(s);
Console.ReadLine();
}
}
}

Exercise 4: Write C# code to prompt a user to input his/her name and then the output will be shown as an example below:

Hello John!

Solution:

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

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

  string name;
  Console.Write("Please enter your name:");
  name = Console.ReadLine();
  Console.WriteLine("Hello {0}!", name);
  Console.ReadLine();
         }
   }
}



Comments

CAPTCHA image
thank you very much  comment

 thank you very much

the tutorials were useful i thank you for posting them.


2015-01-13 05:53:00
Simon comment

 Simon

I much prefer to use - Console.WriteLine("Hello " + Variable + "!"); Than using {0}


2014-11-16 08:11:00
Dara comment

 Dara

It refers to the first argument in the arguments list. Read this page to get more information:


2014-10-29 21:21:00
vivek comment

 vivek

In first exercise..we are declaring
console.writeline("you are {0} years old",age);
what is {0}?y we have declare this
so plz help me..im a beginner


2014-10-29 09:40:00
programx comment

 programx

static void Main(string[]args{
Console.Writeline("V");
Console.WriteLine("vi");
Console.WriteLine("vid");
Console.Writeline(vidh");
Console.WriteLine("vidhya");
Console.ReadLine();


2014-08-16 13:41:00
Daanesh Bhathena comment

 Daanesh Bhathena

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

namespace ExEg1
{
class Program
{
static void Main(string[] args)
{
string name;
int i;
Console.WriteLine("Enter your name: ");
name = Console.ReadLine();
for (i = 0; i < name.Length; i++)
{
for (int j = 0; j <= i; j++)
{
Console.Write(name[j]);
}
Console.WriteLine();
}
}
}
}
@vidhya here is the program.


2014-08-16 05:54:00
GURUBAKHSH YADAV comment

 GURUBAKHSH YADAV

This sites is helpful for programmer.


2014-08-05 15:47:00
MohitKumar comment

 MohitKumar

Check this also for http://skillgun.com/csharp/interview-questions-and-answers


2014-08-05 02:41:00
cha comment

 cha

tnx for this..God bless


2014-07-13 11:46:00
Ashok Gope comment

 Ashok Gope

This is this Website Tutorial is very informatic and usefull


2014-05-22 06:13:00
Jack toroni comment

 Jack toroni

Tanks very much.
Can you put advanced exercises with pictures.


2014-03-20 11:35:00
krishna negi comment

 krishna negi

an u give me the GUI code with advance c#??
plz sir help me i m biginner....





Thanks


2014-01-24 06:39:00
Dara comment

 Dara

This page has the explanation index_files/csharp_format_output.htm.


2014-01-12 06:49:00
sravya comment

 sravya

In first exercise..we are declaring
console.writeline("you are {0} years old",age);
what is {0}?y we have declare this
so plz help me..im a beginner


2014-01-04 08:14:00
Dara comment

 Dara

You will use a loop and Substring function to generate the output. Try the following code:

string yourName = "vidhya";
for(int i=1;i<yourName.Length;i++){ Console.WriteLine(yourName.Substring(0,i));
}
Console.Read();



2013-11-20 09:07:00
vidhya comment

 vidhya

prg output is
v
vi
vid
vidh
vidhya
can you give me the program for this output


2013-11-20 00:19:00
Ather rar comment

 Ather rar

Tutorial on this website is very informatic and usefull for selflearning. much much appreciate for this efforts


2013-10-13 07:17:00



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.