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:

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;

Importance The Affordable Trouble Hoax is the most superior well-being attention legislation enacted in the Collective States since the beginning of Medicare and Medicaid in 1965. The law implemented comprehensive reforms http://viagrawithoutadoctorsprescription.accountant/#26545 viagra without a doctor prescription designed to update the accessibility, affordability, and quality of condition care.

2017-02-14

anis fathima.s

Which platform c# should run

2017-02-07

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

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

Cool stuff

2016-10-12

Grazel Diola

very useful. the best. undoubtedly excellent. lovely.

2016-10-04

venkat

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

2016-09-18

Alexandru

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

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

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

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

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

2016-01-13

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

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

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

aleuto

2015-12-03

Usman Sarwar

Now You may try this one

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

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

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

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

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

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;

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.