Question

Identify the errors in the following C# program. Write the correct program. Explain the errors. class...

Identify the errors in the following C# program. Write the correct program. Explain the
errors.
class Program
{
static Main(string[] args)
{
int x = 10
Console.WriteLine("{0}" + x);
Console.ReadLine();
}

Homework Answers

Answer #1

The funcrions used in the code have no issues but there are so many syntactical errors in the code provided in question.

Error 1: Main() method and class Program must have modifiers.

I have added access modifier public to main() and class Program.

Error 2: Main() method must have a return type. If it doesn't return anything then void data type should be mentioned.

I have added void return type to main() function.

Error 3: There should be semi colon after declaration of variable x.

I have added semi colon to int x=10;

Error 4:Main() method is ended properly but class has not closed in given code. A '}' (Closing curly brace) is missing at the end of class Program.

I have added a closing curly brace for class Program at the end of code.

The correct C# code is:

using System;

// making Program class available through out the code
public class Program
{
// Main method with no return type
public static void Main(string[] args)
{
        //Declaring a variable x of type integer
        int x = 10;
        //Console.WriteLine() function is used to display output to console
        Console.WriteLine("{0}" + x);
        //Console.ReadLine() function is used to take user input from console
        Console.ReadLine();
}
}

In the code provided in questiontwo functions Console.WriteLine() and Console.ReadLine() are used. The functions syntax and usage is correct but Console.ReadLine() has no application in the program. The read line not assigned to any variable nor the read input is used for any operation. So there is no need of Console.ReadLine() in the program. I haven't removed it in the above code because it is given in question. However if you feel the same, you can remove it.

The output of the above code is:

Hope this answer helps you.

Thank you :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
What is the output of the following program? [ Explain the working of the while loop...
What is the output of the following program? [ Explain the working of the while loop in this program as per your understanding. namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int number = 100; while (true) { Console.WriteLine(number); number+=100; if (number <= 1000) continue; else break; } Console.ReadLine(); } } }
What will be the output of the given code? using System; class MyProgram { static void...
What will be the output of the given code? using System; class MyProgram { static void Main(string[] args) { try { int a, b; b = 0; a = 5 / b; Console.WriteLine("No exception will occur."); } catch (ArithmeticException e) { Console.WriteLine("Exception occurs."); } finally { Console.WriteLine("Program is executed."); } Console.ReadLine(); } } A Program is executed. B No exception will occur. C Exception occurs. Program is executed. D No exception will occur. Program is executed.
in C# What Will Be The Output Of The Following Code Snippet:? using System; namespace ProgrammingExercise...
in C# What Will Be The Output Of The Following Code Snippet:? using System; namespace ProgrammingExercise { class FindOutput   { static void Main(string[] args)   { int i; int div = 8, num = 32; for (i = 0; i <= 10; i++) { if ((num / div * 3)== 6) { Console.WriteLine( i + " "); continue; } else if (i != 5) Console.Write(i + " "); else break;   }   Console.ReadLine();   } } }
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes...
In Chapter 9, you created a Contestant class for the Greenville Idol competition. The class includes a contestant’s name, talent code, and talent description. The competition has become so popular that separate contests with differing entry fees have been established for children, teenagers, and adults. Modify the Contestant class to contain a field that holds the entry fee for each category, and add get and set accessors. Extend the Contestant class to create three subclasses: ChildContestant, TeenContestant, and AdultContestant. Child...
Need this in C#. Revisit the program you developed for Problem 1 of Assignment 2. Now,...
Need this in C#. Revisit the program you developed for Problem 1 of Assignment 2. Now, you need to satisfy the following additional requirement. If the user enters a number out of range (e.g., non-positive value), the program displays a warning message about the invalid. The program keeps displaying the warning message until the user enters valid inputs. Use loop statement(s). Here is my code for Problem1 of Assignment 2: using System; namespace Assignment2Problem1 { class Program { static void...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
Modify the C# program to allow it to calculate the average of three numbers instead of...
Modify the C# program to allow it to calculate the average of three numbers instead of the sum. then submit your program to This Drop Box. C# Program: using System; class MainClass { public static void Main (string[] args) { Console.WriteLine ("Hello World"); } }
Trace and document every step of execution of the following program: public class Trace {   ...
Trace and document every step of execution of the following program: public class Trace {    public static void main(String[] args) {        int[] num = {1, 3, 6, 9, 12 };        for (int i = 4; i >= 0; i--) {            num[i] +=1;        }            num[0] *=num[4];        }    }
The following code should print X is greater than 0. However, the code has errors. Fix...
The following code should print X is greater than 0. However, the code has errors. Fix the code so that it compiles and runs correctly. (Rewrite the program with all the fixes) public class Test1 { public static void main(String[] args) { int x = 3; if (x > 0 System.out.println("x is greater than 0") else System.out.println(x is less than or equal 0"); } } Must be in java and easy to understand grade 11 course
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT