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

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

Question 1:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            float[] ARR = { 10.2, 11.5, 22.6 };

            foreach (float X in ARR)
            {
                Console.WriteLine(X % 5);
            }
        }
    }
}

Output:

main.cs(10,29): error CS0664: Literal of type double cannot be implicitly converted to 
type `float'. Add suffix `f' to create a literal of this type
main.cs(10,35): error CS0664: Literal of type double cannot be implicitly converted to 
type `float'. Add suffix `f' to create a literal of this type
main.cs(10,41): 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 errors because, in the above float array, we assigned with double values. Here, we need to use 'F' suffix to initialized float values. The correct way is given below,

float[] ARR = { 10.2F, 11.5F, 22.6F };

Question 2:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            float[] ARR = { 13.6F, 11.5F, 22.6F };
            float R = 0.0F;
            foreach (float X in ARR)
            {
                R = X%5;
                Console.WriteLine(R);
            }
        }
    }
}

Output:

3.6
1.5
2.6
Press any key to continue . . .

Explanation:

In the above program, we created an array of float values ARR, and we created one more float variable 'R'.

foreach (float X in ARR)
{
    R = X%5;
    Console.WriteLine(R);
}

Here, we accessed each element of an array using the foreach loop and find the remainder then print the remainder on the console screen.

Question 3:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            float[] ARR1 = { 13.6F, 11.5F, 22.6F };
            float[] ARR2;

            ARR2 = ARR1;

            float R = 0.0F;
            foreach (float X in ARR2)
            {
                R = X % 5;
                Console.WriteLine(R);
            }

        }
    }
}

Output:

3.6
1.5
2.6
Press any key to continue . . .

Explanation:

In the above program, we created an array of float values ARR1, and we created one more float variable R, Initialized with "0.0F". Here, we assigned the address of ARR2 to the ARR1.

foreach (float X in ARR2)
{
    R = X%5;
    Console.WriteLine(R);
}

Here, we accessed each element of the array using foreach loop and find remainder then print the Remainder on the console screen.

Question 4:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            float[] ARR1 = { 13.6F, 11.5F, 22.6F };
            float[] ARR2;

            ARR2 = ARR1;

            ARR2[1] = 20.5F;

            foreach (float X in ARR1)
            {
                Console.WriteLine(X);
            }
        }
    }
}

Output:

13.6
20.5
22.6
Press any key to continue . . .

Explanation:

In the above program, we created an array of float values ARR1, and we assigned the address of ARR2 to the ARR1. It means we made the change in anyone that will reflect in another array also because both are pointing to the same array internally.

Here, we assigned value 20.5F in the 1st position of ARR2, but it will also reflect in ARR1. So here we accessed each element of array ARR1 one by one using foreach loop, and print them on the console screen.

Question 5:

using System;

namespace Demo
{
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            string[] str = { 'Hello', 'Hii', 'Bye' };

            foreach (string X in str)
            {
                Console.WriteLine(X);
            }
        }
    }
}

Output:

main.cs(10,30): error CS1012: Too many characters in character literal
main.cs(10,39): error CS1012: Too many characters in character literal
main.cs(10,46): error CS1012: Too many characters in character literal

Explanation:

The above program will generate syntax errors because we enclosed the string within single quotes instead of double-quotes. Single quotes are used for a single character, not for the strings in C#.





Comments and Discussions!

Load comments ↻





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