C# exercises and solutions programming


C# exercises

For my own experience as software developer, 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: Programs.

The exercises below help you practice declaring variables, getting inputs from keyboard, and outputting the results on the console window.

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

dharshan comment

 dharshan

Generated (a,b) does not generate any na or nb n=>( where n => 1,2,3,4, .. )
plz help for this que


2016-11-18
dharshan comment

 dharshan

Generated (a,b) does not generate any na or nb n=>( where n => 1,2,3,4, .. )
plz help for this que


2016-11-18
Nathan McCausland comment

 Nathan McCausland

Cool stuff


2016-10-12
Grazel Diola comment

 Grazel Diola

very useful. the best. undoubtedly excellent. lovely.


2016-10-04
venkat  comment

 venkat

hi everyone can you hepl me to write c# program to implement day for the specific date



2016-09-18
Alexandru comment

 Alexandru

@Dazzler here is a solution for your exercises, a very basic one, but a solution nevertheless:
using System;

namespace Students
{
public class Student
{
int studentCode { get; set; }
string studentName { get; set; }
int Age { get; set; }

public void GetStudent()
{
studentCode = 1;
studentName = "John Doe";
Age = 21;
}
public void PrintStudent()
{
Console.WriteLine(studentCode);
Console.WriteLine(studentName);
Console.WriteLine(Age);
}
}
class Program
{
static void Main(string[] args)
{
Student JohnDoe = new Student();
JohnDoe.GetStudent();
JohnDoe.PrintStudent();
Console.ReadKey();
}
}
}


2016-08-03
Nainika comment

 Nainika

Really very nice blog information for this one and more technical skills are improve,i like that kind of post.


<a href="http://www.saptraining-chennai.in/sap-training-in-chennai.php"> SAP training in Chennai </a>


2016-05-04
Dazzler comment

 Dazzler

I am a beginner too. I have made a simple program to print multiplication table of a number.

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

namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
Console.WriteLine("Enter any number:");
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < 10; i++)
{
sum=sum + n;
Console.Write(n);
Console.Write("x");
Console.Write(i);
Console.Write("=");
Console.Write(sum);
Console.Write("\n");
}
Console.ReadLine();
}
}
}
I need help in developing a program as follows:
Write a c# program to define a class named student with data members as code, name and age. Get the record of student and print the same using property procedure.
Can anyone help?


2016-03-29
faraz ahmrd comment

 faraz ahmrd

Develop a class Post that has following attributes
o Name: a string
o To : a string that holds the reciever's address
o StampCost: a float that holds the value of postal stamp required
The class should include following:
 A constructor that initializes StampCost to 1$ and To to empty address
 Read(): a method that reads data member's values from user
 Print(): a method that displays data member's values on screen
 TotalCost(): a method that returns stampCost value
Design another class RegisteredPost that inherits from Post class and has following additional attributes:
 Weight: a float that holds the weight of post
 RegistrationCost: a float that holds registration charges
Also include following in the class.
 A constructor that initializes weight to 20 gms and RegisterationCost to 10$
 Read(): a method that reads data member's values from user
 Print(): a method that displays data member's values on screen
TotalCost(): a method that returns stampCost+RegistrationCost
solve this code


2016-03-08
faraz ahmrd comment

 faraz ahmrd

Develop a class Post that has following attributes
o Name: a string
o To : a string that holds the reciever's address
o StampCost: a float that holds the value of postal stamp required
The class should include following:
 A constructor that initializes StampCost to 1$ and To to empty address
 Read(): a method that reads data member's values from user
 Print(): a method that displays data member's values on screen
 TotalCost(): a method that returns stampCost value
Design another class RegisteredPost that inherits from Post class and has following additional attributes:
 Weight: a float that holds the weight of post
 RegistrationCost: a float that holds registration charges
Also include following in the class.
 A constructor that initializes weight to 20 gms and RegisterationCost to 10$
 Read(): a method that reads data member's values from user
 Print(): a method that displays data member's values on screen
TotalCost(): a method that returns stampCost+RegistrationCost
solve this code


2016-03-08
reymart fernandez comment

 reymart fernandez

hello everyone can you help me in my c# programming about function?


2016-01-13
jai comment

 jai

1.find nearest number of 89
2.prime number upto 50 will be display
3.which one s greatest num not using relational operator.
4.fibonaci series upto 100
write the progm using c#.. reply pls


2016-01-12
programmer comment

 programmer

write a program that reads in two numbers and adds them to a list if they aren't already contained in the list , when the list contains ten numbers the program displays the contents and quits
using c#


2015-12-28
programmer comment

 programmer

write a program that reads in two numbers and adds them to a list if they aren't already contained in the list , when the list contains ten numbers the program displays the contents and quits
using c#


2015-12-28
mat comment

 mat

aleuto


2015-12-03
Usman Sarwar comment

 Usman Sarwar

Now You may try this one

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

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


2015-11-14
Emmanuel Mahaso comment

 Emmanuel Mahaso

exercise 2 can also be written like so:
for (int i = 0; i < 5; i++)
{
Console.WriteLine("*****\n");
}

Its short and neat :)


2015-08-11
Bojan comment

 Bojan

I think that the solution in exercise 2 is not right. It's almost right but there is a small failure. Between every Console.WriteLine("*****"); should be a Console.WriteLine(); line to make it more accurate as the needed executable. It's minor difference but those kind of things can mean a lot in bigger programs.


2015-07-15
Tiotuico Daniel comment

 Tiotuico Daniel

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

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

Console.WriteLine("Write your Awsome name: ");
InputName = Console.ReadLine();
Console.WriteLine("Hello " + InputName + "!");
//or try this one, any will do
Console.WriteLine("Hello {0}!", InputName);
Console.ReadLine();
}
}
}


2015-07-09
Thanks ! comment

 Thanks !

I appericiate your effort. This web-site is useful for beginners like me.


2015-06-30
suresh comment

 suresh

usefull for beginers


2015-06-01
PAULINA NKELO comment

 PAULINA NKELO

this site is very useful,i m a beginner but i have already created a running program..

static void Main(string []args)
{
string name;

Console.write("enter name");
name=console.ReadLine();
console.write("name[levi]",name);
console.Read();
}
}
}
help me friends...i m just a beginner


2015-05-19
Dara comment

 Dara

Anon, you are right. However, in the variables and data type exercises, i would like you to learn how to declare variables, assign values to variables, using data types, and output data on the console. It is not about using loop in this page.


2015-05-05
Anon comment

 Anon

I doubt the solution for 2 is the way to go. I did it with a for loop. This way you don't have however many Console.WriteLine commands for what you want, all you have to do is change the condition for the loop from 6 to 1000 if you want. Having a single command that simply repeats is way more efficient.

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

namespace Practice
{
class Program
{
static void Main(string[] args)
{
int a;
for(a=1; a < 6; a++)

Console.Write("**********\n");
Console.ReadKey();

}
}
}


2015-04-28
Mike comment

 Mike

It would be nice to have your solution hidden.


2015-03-15
coolj comment

 coolj

Design a solution that prints the amount of pay a commissioned salesperson takes home


2015-02-13
PHORN Ya comment

 PHORN Ya

Good tutorial with the best solution for students who study in Information Technology, and I want to say many thanks


2015-02-05
thank you very much  comment

 thank you very much

the tutorials were useful i thank you for posting them.


2015-01-13
Simon comment

 Simon

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


2014-11-16
Dara comment

 Dara

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


2014-10-29
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
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
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
GURUBAKHSH YADAV comment

 GURUBAKHSH YADAV

This sites is helpful for programmer.


2014-08-05
MohitKumar comment

 MohitKumar

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


2014-08-05
cha comment

 cha

tnx for this..God bless


2014-07-13
Ashok Gope comment

 Ashok Gope

This is this Website Tutorial is very informatic and usefull


2014-05-22
Jack toroni comment

 Jack toroni

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


2014-03-20
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
Dara comment

 Dara

This page has the explanation index_files/csharp_format_output.htm.


2014-01-12
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
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
vidhya comment

 vidhya

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


2013-11-20
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
CAPTCHA image



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.