C# exercises and solutions-C# operators


C# operators exercises

These C# exercises help you practice arithmetic, compound assignation, increment, and decrement operators in C# programming language.

Exercise 1: Write C# code to produce the output shown below:

x value        y value        expression                 result

10                       5              x=y+3                     x=8

10                       5              x=y-2                      x=3

10                       5              x=y*5                     x=25

10                       5              x=x/y                      x=2

10                       5              x=x%y                    x=0

 

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=10;

int y=5;

Console.WriteLine("Result:");

Console.WriteLine("x value\t\ty value\t\tExpressions\tResult");

Console.WriteLine("{0,-8}\t{1,-8}\tx=y+3 \t x={2,-8}",x,y,y + 3);
Console.WriteLine("{0,-8}\t{1,-8}\tx=y-2 \t x={2,-8}", x, y, y-2);
Console.WriteLine("{0,-8}\t{1,-8}\tx=y*5 \t x={2,-8}", x, y, y*5);
Console.WriteLine("{0,-8}\t{1,-8}\tx=x/y \t x={2,-8}", x, y, (float)x/y);
Console.WriteLine("{0,-8}\t{1,-8}\tx=x%y \t x={2,-8}", x, y, x%y);


Console.ReadLine();
   }
  }
}

Exercise 2: Write C# code to display the output as shown below:

Results: 

x value                 y value        expressions                results

10                        5                  x+=y                          x=15

10                        5                  x-=y-2                        x=7

10                        5                  x*=y*5                       x=250        

10                        5                  x/=x/y                         x=5

10                        5                  x%=y                          x=0

 

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=10;

int y=5;


Console.WriteLine("Result:");

Console.WriteLine("x value\t\ty value\t\tExpressions\tResult");

Console.WriteLine("{0,-8}\t{1,-8}\tx+=y \t x={2,-8}",x,y,x+y);
Console.WriteLine("{0,-8}\t{1,-8}\tx-=y-2 \t x={2,-8}", x, y,x-y+2);
Console.WriteLine("{0,-8}\t{1,-8}\tx*=y*5 \t x={2,-8}", x, y, x*y*5);
Console.WriteLine("{0,-8}\t{1,-8}\tx=x/y \t x={2,-8}", x, y, (float)x/(x/y));
Console.WriteLine("{0,-8}\t{1,-8}\tx%=y \t x={2,-8}", x, y, x%y);


Console.ReadLine();
}
}
}

Exercise 3: Write C# code to prompt a user to enter an integer value.

The value is stored in a variable called a. Then the program will show the output as seen below:

The value of a is 10.

................................

The value of ++a is 11.

The value of a is 11.

The value of a++ is 11.

The value of a is 12.

The value of --a is 11.

The value of a is 11.

The value of a-- is 11.

The value of a is 10.

Solution:

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

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


int a;
int b;

Console.Write("Enter a value:");


a=int.Parse(Console.ReadLine()) ;

Console.WriteLine("The value of a is {0}.",a);

b=++a;

Console.WriteLine("The value of ++a is {0}.",b);

Console.WriteLine("The value of a is {0}.",a);
b = a++;

Console.WriteLine("The value of a++ is {0}.", b);
Console.WriteLine("The value of a is {0}.", a);

b=--a;

Console.WriteLine("The value of --a is {0}.", b);
Console.WriteLine("The value of a is {0}.", a);


b=a--;

Console.WriteLine("The value of a-- is {0}.", b);
Console.WriteLine("The value of a is {0}.", a);


Console.ReadLine();
    }
  }
}


Comments

Moully comment

 Moully

List of array and matrix programming exercises.
1. Write a C# program to read and print elements of array. - using recursion.
2. Write a C# program to print all negative elements in an array.
3. Write a C# program to find sum of all array elements. - using recursion.
4. Write a C# program to find maximum and minimum element in an array. - using recursion.
5. Write a C# program to find second largest element in an array.
6. Write a C# program to count total number of even and odd elements in an array.
7. Write a C# program to count total number of negative elements in an array.
11. Write a C# program to count frequency of each element in an array.
12. Write a C# program to print all unique elements in the array.
13. Write a C# program to count total number of duplicate elements in an array.
.





2018-05-23
Andrew comment

 Andrew

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

namespace Enter_An_Integer
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter an integer and press Enter");
int Integer = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("The value of Integer is {0}", Integer);
Console.WriteLine("****************************");
Console.WriteLine("The Value of ++Integer is " + ++Integer);
Console.WriteLine("The value of Integer is {0}", Integer);
Console.WriteLine("The value of Integer++ is " + Integer++);
Console.WriteLine("The value of Integer is {0}", Integer);
Console.WriteLine("The value of --Integer is " + --Integer);
Console.WriteLine("The value of Integer is {0}", Integer);
Console.WriteLine("The value of Integer-- is " + Integer--);
Console.WriteLine("The value of Integer is " + Integer);
}
}
}


2018-05-07
Nagaraju comment

 Nagaraju

Converting Two dimensional To One Dimensional

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

namespace TwoDimenToOneDimension
{
class Program
{
static void Main(string[] args)
{
try
{
int[] OneDimension = new int[8] ;
int[,] TwoDimension = new int[2, 4] {
{2,2,2,2},
{3,3,3,3}
};

int size = 0;
for (int row = 0; row < 2; row++)
{
for (int col = 0; col < 4; col++)
{

OneDimension[size] = TwoDimension[row, col];
Console.WriteLine(OneDimension[size]);
size++;
}

}

Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}


2017-12-08
Pooja Thakur comment

 Pooja Thakur

using System.IO;
using System;

class Program
{
static void Main()
{
Console.WriteLine("*****************************************************************************");
Console.WriteLine("x value"+"\t"+"\t"+"y value"+"\t"+"\t"+" Expression"+"\t"+"\t"+"result");
Console.WriteLine("*****************************************************************************");
Console.WriteLine(" 10 "+"\t"+"\t"+" 5 "+"\t"+"\t"+" x=y*5 "+"\t"+"\t"+"x=3");
Console.WriteLine(" 10 "+"\t"+"\t"+" 5 "+"\t"+"\t"+" x=y-2 "+"\t"+"\t"+"x=25");
Console.WriteLine(" 10 "+"\t"+"\t"+" 5 "+"\t"+"\t"+" x=x/y "+"\t"+"\t"+"x=2");
Console.WriteLine(" 10 "+"\t"+"\t"+" 5 "+"\t"+"\t"+" x=x%y "+"\t"+"\t"+"x=0");
Console.WriteLine("*****************************************************************************");
}
}


2016-12-19
omg comment

 omg

"-8" could be the world's most unnecessary thing you can see on this web page.


2016-08-21
zied comment

 zied

how ?


2016-05-22
Jimmy comment

 Jimmy

Hey guys!
plz help me with a c# program:
write an application that creates and returns a one-dimensional array containing all the elements in the two-dimensional array.store the values in a row major format.For testing purposes,you may do a compile-time initialization of a 12*5 two dimensional array.Display both the two-dimensional and the one-dimensional array.Be sure the values in the array are number aligned.
Thank you!


2015-11-20
raeshvanth comment

 raeshvanth

Friends help me with a program that uses Checkbox,image map,image button, for the usage of hidden field..


2015-09-16
kashi 0331 5485418 comment

 kashi 0331 5485418

this is Good For Learning


2015-03-26
Matt comment

 Matt

Exercise #3 with TryParse to check input type:

public static void Main (string[] args)
{

while (true) {

Console.WriteLine ("Enter an integer:\n");

string printStatement = "The value of {0} is {1}.";

string userInput = Console.ReadLine ();

int a;

if (Int32.TryParse (userInput, out a)) {

Console.WriteLine (printStatement, "a", a);
Console.WriteLine ("******************");
Console.WriteLine (printStatement, "++a", ++a);
Console.WriteLine (printStatement, "a", a);
Console.WriteLine (printStatement, "a++", a++);
Console.WriteLine (printStatement, "a", a);
Console.WriteLine (printStatement, "--a", --a);
Console.WriteLine (printStatement, "a", a);
Console.WriteLine (printStatement, "a--", a--);
Console.WriteLine (printStatement, "a", a);

Console.ReadLine ();

return;

} else {

Console.WriteLine ("That's not an integer.");

}
}
}


2015-03-19
Daniel comment

 Daniel

(challenge 2) It seems {x, -8} is an alternative way to use \t. I managed to format mine perfectly using \t, and I also could do it using -8 but -8 is a bit more tedious for me. I posted my challenge 2 code below. As for challenge 3, that can be done with a single value as another user pointed out. I was confused by the need for a 2nd value


int x = 10;
int y = 5;

Console.WriteLine("Result:");
Console.WriteLine("x value \t y value \t expressions \t results");
Console.WriteLine("{0} \t\t {1} \t\t x+=y \t\t x={2}", x, y, x + y);
Console.WriteLine("{0} \t\t {1} \t\t x-=y-2 \t x={2}", x, y, x - y + 2);
Console.WriteLine("{0} \t\t {1} \t\t x*=y*5 \t x={2}", x, y, x * +y * 5);
Console.WriteLine("{0} \t\t {1} \t\t x/=x/y \t x={2}", x, y, x/(x/y));
Console.WriteLine("{0} \t\t {1} \t\t x%=y \t\t x={2}", x, y, x% y);



2015-03-16
Candy comment

 Candy

So we're given the value of x, but the answer is also equal to x? Is this just poor execution on part of the creator of this lesson or is it some weird programming voodoo that I do not understand?


2015-01-26
Somebody comment

 Somebody

I did Exercize 3 that way:



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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Give a number for variable a ");
int a = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("The value of a is {0}", a);
Console.WriteLine("..................");
Console.WriteLine("The value of ++a is {0}", ++a);
Console.WriteLine("The new value of a is {0}", a);
Console.WriteLine("The value of a++ is {0}", a++);

Console.WriteLine("The value of a is {0}", a);
Console.WriteLine("The value of --a is {0}", --a);
Console.WriteLine("The value of a is {0}", a);
Console.WriteLine("The value of a-- is {0}", a--);
Console.WriteLine("The value of a is {0}", a);
Console.ReadKey();
}
}
}


2015-01-18
Never Mind comment

 Never Mind

Did you try running the program without -8??

Its used for formatting(Spacing).

Run the program with and without -8, you'll know the difference.

Cheers!


2014-09-16
Minimalist comment

 Minimalist

Exercise 3 doesn't require two integer valuables.



string rep = "The value of {0} is {1}.";
int a = int.Parse(Console.ReadLine());

Console.WriteLine (rep, "a", a);
generatePoints();
Console.WriteLine (rep, "++a", ++a);
Console.WriteLine (rep, "a", a);
Console.WriteLine (rep, "a++", a++);
Console.WriteLine (rep, "a", a);
Console.WriteLine (rep, "--a", --a);
Console.WriteLine (rep, "a", a);
Console.WriteLine (rep, "a--", a--);
Console.WriteLine (rep, "a", a);

Console.ReadLine();


2014-08-25
what comment

 what

Was so confused about the whole explanation simply because of that silly -8. Still dont understand the need for it..


2014-08-17
Dara comment

 Dara

This page contains the explanation about this.
csharp_format_output.htm


2014-06-09
H Rashid comment

 H Rashid

{0,-8}\t{1,-8}\tx=y+3\t x={2,-8}
why we used -8 ??


2014-06-03



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.