C#.Net find output programs (Data Types) | set 3

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

Question 1:

using System;

namespace MyRefDemo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            int A = 10;
            Int32 B = 20;
            Int64 C = 0;

            C = A * B + sizeof(double);

            Console.WriteLine(C);
        }
    }
}

Output:

208
Press any key to continue . . .

Explanation:

The above program will print "208" on the console screen. In the above program, we created three variables of types int, Int32, and Int64 respectively.

Let's evaluate the expression:

C = A * B + sizeof(double);
C  = 10 * 20 + 8; 
C  = 200 + 8;
C  = 208;

Then the value of variable C that is 208 will be printed on the webpage.

Question 2:

using System;

namespace MyRefDemo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            long double A = 10.32;
            Int32 B = 20;
            long double C = 0;

            C = A * B + sizeof(long double);

            Console.WriteLine(C);
        }
    }
}

Output:

main.cs(10,17): error CS1525: Unexpected symbol `double'
main.cs(12,17): error CS1525: Unexpected symbol `double'
main.cs(14,36): error CS1525: Unexpected symbol `double'

Explanation:

The above program will generate syntax error, because long double is not a built-in data type in C#. Here, we use decimal instead of long double.

Question 3:

using System;

namespace MyRefDemo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            decimal A = 10.32;
            Int32 B = 20;
            decimal C = 0;

            C = A * B + sizeof(decimal);

            Console.WriteLine(C);
        }
    }
}

Output:

main.cs(10,25): error CS0664: Literal of type double cannot be implicitly converted to type `decimal'. 
Add suffix `m' to create a literal of this type

Explanation:

The above program will generate syntax error because here created variable A of decimal type and assigned a double value. Here, we need to use suffix 'M'. Then the correct declaration will be:

decimal A = 10.32M;
Int32 B = 20;
decimal C = 0.0M;

Question 4:

using System;

namespace MyRefDemo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            decimal A = 10.32M;
            int B = 20;
            decimal C = 0.0M;

            C = A * B + sizeof(decimal);

            Console.WriteLine(C);
        }
    }
}

Output:

222.40
Press any key to continue . . .

Explanation:

In the above program, we created three variables A, B, and C initialized with 10.32M, 20, and 0.0M respectively.

Let's evaluate the expression,

C = A * B + sizeof(decimal);
C = 10.32 * 20 + 16;
C = 206.40 + 16;
C = 222.40;

Then the value of C that is 222.40 will be printed on the console screen.

Question 5:

using System;

namespace MyRefDemo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            float A = 3.14;
            Int32 B = 20;
            decimal C = 0.0M;

            C = (decimal)(A * B + sizeof(double));

            Console.WriteLine(C);
        }
    }
}

Output:

main.cs(10,23): error CS0664: 
Literal of type double cannot be implicitly converted to 
type `float'. Add suffix `f' to create a literal of this type

Explanation:

The above program will generate syntax error because here created variable A of float type and assigned a double value. Here, we need to use suffix 'F'. Then the correct declaration will be:

float A = 10.32F;


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.