Question
This is the code from the main page of my Visual Studio UWP program: using System; public class Program { public static void Main() //Main
This is the code from the main page of my Visual Studio UWP program:
using System;
public class Program
{
public static void Main() //Main function
{
int num; //integer variable declaration
String name,sel; //String variable declaration
bool cont=true; //Boolean variable declaration
Console.WriteLine("Enter your Name:"); //Displays text
name=Console.ReadLine(); //Takes user input, stores it in varaiable name
Console.WriteLine("Hi "+name); //Displays value of variable name
Console.WriteLine("Enter a number ");
try{ //resolves run time error if user enters string instead of integer
String t=Console.ReadLine();
num=Convert.ToInt32(t);
Console.WriteLine("You have entered number "+num);
while(cont){ //Runs loop until user enters a number other than 1 or 2
Console.WriteLine("====================================================================");
Console.WriteLine("Enter 1 to see if entered number is odd or even");
Console.WriteLine("Enter 2 to calculate the factorial of the number" );
Console.WriteLine("Press any other key to Exit");
Console.WriteLine("====================================================================");
sel=Console.ReadLine();
switch(sel) //switch case to perform function selected
{
case "1": //check whether number is even or odd
if(num%2==0){ //even number has remainder 0 when divided by 2
Console.WriteLine("Number "+num+" is an even number.");
}
else{
Console.WriteLine("Number "+num+" is an odd number.");
}
break; //to terminate the current execution of switch
case "2": //
if(num==1 || num==0){ //factorial of 1 or 0 is 1
Console.WriteLine("Factorial of number "+num+" is 1");
}
else{
int fact=1;
for (int i=num;i>=1 ;i--){ //calculate factorial using for loop
fact=fact*i;
}
Console.WriteLine("Factorial of number "+num+" is "+fact);
}
break;
default: //execute when user press any key other than 0 or 1
Console.WriteLine("Thank You ");
cont=false; //terminate the while loop as the condition will turn to false
break;
}
}
}
catch(FormatException e){ //handling run time error while converting string to integer
Console.WriteLine("Please Enter an Integer" ); //display this message if runtime error occurs
}
}
}
When I debug it, I get error: Data at the root level is invalid, line 16 postion 1. Why is this and how can I make this project compile?
Line 16 is: using System;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started