﻿ C# exercises and solutions-C# operators

# 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);

}
}
}

### 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);

}
}
}

### 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:");

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