I need help to fix code that has multiple errors in it. The code is below: Part
ID: 3885977 • Letter: I
Question
I need help to fix code that has multiple errors in it. The code is below:
Part 1: Syntax errors - Locate the syntax errors in the code below and fix the errors, then run the correct code. static void Main(string[] args) { //declare variables double num1, num2, quotient; Console.Write("Please enter a number: "); num1 = Console.ReadLine(); Console.Write("Please enter another number: "); num2 = Convert.ToDouble(Console.ReadLine()); if (num2 == 0) Console.WriteLine("Division by 0 is not allowed"); Console.WriteLine("This program will terminate"); else { quotient = num1 / num2; Console.WriteLine("The quotient of " + num1 " and " + num2 + " is " + quotient); Console.WriteLine("Thank you!"); } Console.ReadLine(); }
Part 2: Logic errors - Locate the logic errors in the code below and fix the errors. Then run the correct code. static void Main(string[] args) { int testScore = 0; Console.Write("Please enter the test score: "); testScore = Convert.ToInt32(Console.ReadLine()); if (testScore > 60) Console.WriteLine("Your grade is an D"); else if (testScore > 70) Console.WriteLine("Your grade is a C"); else if (testScore > 80) Console.WriteLine("Your grade is a B"); else if (testScore > 90) Console.WriteLine("Your grade is an A"); else Console.WriteLine("Your grade is an F"); Console.ReadLine(); }
Explanation / Answer
Part1
using System.IO;
using System;
class Program
{
static void Main(string[] args) {
//declare variables
double num1, num2, quotient;
Console.Write("Please enter a number: ");
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Please enter another number: ");
num2 = Convert.ToDouble(Console.ReadLine());
if (num2 == 0) {
Console.WriteLine("Division by 0 is not allowed");
Console.WriteLine("This program will terminate");
}
else {
quotient = num1 / num2;
Console.WriteLine("The quotient of " + num1 +" and " + num2 + " is " + quotient);
Console.WriteLine("Thank you!");
}
}
}
Output:
Part 2
using System.IO;
using System;
class Program
{
static void Main(string[] args) {
int testScore = 0;
Console.Write("Please enter the test score: ");
testScore = Convert.ToInt32(Console.ReadLine());
if (testScore >= 90)
Console.WriteLine("Your grade is an A");
else if (testScore >= 80 && testScore<90)
Console.WriteLine("Your grade is a B");
else if (testScore >= 70 && testScore < 80)
Console.WriteLine("Your grade is a C");
else if (testScore >= 60 && testScore < 70)
Console.WriteLine("Your grade is an D");
else
Console.WriteLine("Your grade is an F");
Console.ReadLine();
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.