Q:

C# program to demonstrate example of assignment operators

belongs to collection: C# Basic Programs | basics

0

Assignment operators (Assignment (=) and compound assignments (+=-+*=/=%=)) are used to assign the value or an expression's result to the left side variable, following are the set of assignment operators,

  1. "=" – it is used to assign value or an expression's result to the left side variable
  2. "+=" – it is used to add second operand to the existing operand's value and assigns it back (a+=b is equal to a=a+b)
  3. "-=" – it is used to subtract second operand from the existing operand's value and assigns it back (a-=b is equal to a=a-b)
  4. "/=" – it is used to divide second operand from the existing operand's value and assigns it back (a/=b is equal to a=a+b)
  5. "*=" – it is used to multiply second operand with the existing operand's value and assigns it back (a*=b is equal to a=a*b)
  6. "%=" – it is used to get the remainder by dividing second operand with the existing operand's value and assigns it back (a%=b is equal to a=a%b)

Example:

    Input:
    int a = 10;
    int b = 3;

    //operations & outputs
    a = 100;    //value of a will be 100
    a += b;     //value of a will be 103
    a -= b;     //value of a will be 100
    a *= b;     //value of a will be 300
    a /= b;     //value of a will be 100
    a %= b;     //value of a will be 1

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C# code to demonstrate example of assignment operators

// C# program to demonstrate example of 
// assignment operators 
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            int a = 10;
            int b = 3;

            Console.WriteLine("a: {0}", a);
            a = 100; //assigment
            Console.WriteLine("a: {0}", a);
            a += b;
            Console.WriteLine("a: {0}", a);
            a -= b;
            Console.WriteLine("a: {0}", a);
            a *= b;
            Console.WriteLine("a: {0}", a);
            a /= b;
            Console.WriteLine("a: {0}", a);
            a %= b;
            Console.WriteLine("a: {0}", a);


            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

a: 10
a: 100
a: 103
a: 100
a: 300
a: 100
a: 1

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | basics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to demonstrate example of sizeof() oper... >>
<< C# | Declare different types of variables, assign ...