C# Operator's Exercises
C# Operator's Exercises
C# operator’s exercises
These C# exercises help you practice arithmetic, compound
assignation, increment, and decrement operators in C#
programming language.
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.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("{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()) ;
b=++a;
b=--a;
b=a--;
Console.ReadLine();
}
}
}