0% found this document useful (0 votes)
75 views

C# Operator's Exercises

This document contains examples of C# code demonstrating the use of arithmetic, assignment, increment and decrement operators. It includes 3 exercises with sample code and output showing how to use various operators to manipulate and display variable values.

Uploaded by

nani031nani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

C# Operator's Exercises

This document contains examples of C# code demonstrating the use of arithmetic, assignment, increment and decrement operators. It includes 3 exercises with sample code and output showing how to use various operators to manipulate and display variable values.

Uploaded by

nani031nani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C# Practice Exercise on Basics

C# operator’s 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();
}
}
}

You might also like