C#.Net find output programs (Operators) | set 1

Find the output of C#.Net programs | Operators | Set 1: Enhance the knowledge of C#.Net Operators concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 05, 2021

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int a = 10;
            int b = 20;
            int c = 0;

            c = ++a + ++b * 2+a++;

            Console.WriteLine(a + "," + b + "," + c);
        }
    }
}

Output:

12,21,64
Press any key to continue . . .

Explanation:

In the above program, we created three integer variables a, b, and c initialized with 10, 20, and 0 respectively. Here, we used pre and post-increment operators with variable a and b. The pre-increment operator will evaluate before the expression and post-increment operator will evaluate after the expression.

The value of variable a will be 11 and the value of b will be 21 because of pre-increment operator. Let's evaluate the expression,

c = ++a + ++b * 2+a++;

Now remove all pre and post-increment operators from the expression.

c = a +b * 2+a;
c = 11+21*2+11;
c = 11+42+11’
c = 64;

Then post-increment operator will evaluate for variable a then the final value of a will be 22.

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            double Z = 0.0;

            Z = Math.Abs((int)Math.PI*-23)*Math.Pow(2,0);
           
            Console.WriteLine(Z);
        }
    }
}

Output:

69
Press any key to continue . . .

Explanation:

In the above program, we created a variable Z initialized with "0.0", and in the expression, we used Abs() and Pow() methods and PI property of Math class.

The Abs() method is used to return positive value, Pow() method is used to calculate the power, and TPI property will return 3.14.

Let's evaluate the expression:

Z = Math.Abs((int)Math.PI*-23)*Math.Pow(2,0);
Z = Math.Abs(3*-23)*Math.Pow(2,0);
Z = Math.Abs(-69)*Math.Pow(2,0);
Z = 69*1;
Z = 69;

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int Z = 0;

            Z += sizeof(decimal)+ (Math.pow(2,8)%3);
           
            Console.WriteLine(Z);
        }
    }
}

Output:

main.cs(12,41): error CS0117: `System.Math' does not contain a definition for `pow'
/usr/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error)

Explanation:

The above program will generate syntax error because pow() is not a built-in method. Because, Pow() is a built-in method of Math class. Because C# is a case sensitive language.

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int Z = 0;

            Z += sizeof(decimal)+ ((int)Math.Pow(2,8)%3);
           
            Console.WriteLine(Z);
        }
    }
}

Output:

17
Press any key to continue . . .

Explanation:

The above program will print 17 on the console screen. In the above program, we created an integer variable Z initialized with 0.

Z += sizeof(decimal)+ ((int)Math.Pow(2,8)%3);

In the above expression, we used the sizeof() operator, here sizeof(decimal) will return 16 and Math.Pow() will return 256, the Math.Pow() function returns the value of double type, then we will typecast value returned by Pow() method, and modulus '%' operator is used to find the remainder.

Let's evaluate the expression,

Z += sizeof(decimal)+ ((int)Math.Pow(2,8)%3);
Z += 16+ (256%3);
Z += 16+ (256%3);
Z += 16+ 1;
Z = Z +17;
Z = 0 +17;
Z = 17;

Then the value of Z that is 17.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            const float A = 4.23F;
                  float B = 3.23F;
            float Z = 2;

            Z *= ++B+A%B;
           
            Console.WriteLine(Z);
        }
    }
}

Output:

8.46
Press any key to continue . . .

Explanation:

In the above program, we create one constant A and two variables B, Z that are initialized with "4.23F", "3.23F", and 2 respectively.

Let's evaluate the expression,

Z *= ++B+A%B;

Now evaluate pre increment operator then value of B will be 4.23F.

Z *= B+A%B;
Z *= 4.23+4.23%4.23;
Z *= 4.23+0;
Z = Z *4.23;
Z = 2 *4.23;
Z = 8.46;

Then the final value of Z will print on the console screen.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.