Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7811639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:14:26+00:00 2026-06-02T04:14:26+00:00

I just wrote my first C# program. It’s a simple piece of code which

  • 0

I just wrote my first C# program.

It’s a simple piece of code which solves quadratic equations.

It works with some functions (such as -6×2-6x+12) perfectly, while with others, (4×2-20x+25) it exhibits what I suspect are rounding errors.

I’m completely new to C#, and I can’t see an problems; would someone be able to help me debug this code?

namespace ConsoleApplication {
    class Program {
        static int ObtainInput(string prompt, bool canBeZero) {
            double a = ObtainInput("A? ", false);
            double b = ObtainInput("B? ", true);
            double c = ObtainInput("C? ", true);
            double d, x1, x2;

            while (true) {
                Console.Write(prompt);
                string input = Console.ReadLine();
                int result;
                bool success = int.TryParse(input, out result);
                if (success && (canBeZero || result != 0))
                    return result;
                Console.WriteLine("Invalid input!");
            }

            // Calculating a discriminant
            d = b * b - 4 * a * c;

            if (d == 0) {
                x1 = -b / (2 * a);
                Console.WriteLine("The only solution is x={0}.", x1);
                Console.ReadLine();
            }

            // If d < 0, no real solutions exist
            else if (d < 0) { 
                Console.WriteLine("There are no real solutions");
                Console.ReadLine();
            }

            // If d > 0, there are two real solutions 
            else {
                x1 = (-b - Math.Sqrt(d)) / (2 * a);
                x2 = (-b + Math.Sqrt(d)) / (2 * a);
                Console.WriteLine("x1={0} and x2={1}.", x1, x2);
                Console.ReadLine();
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:14:27+00:00Added an answer on June 2, 2026 at 4:14 am

    I just wrote my first C# program.

    Awesome. Now would be a great time to not get into bad habits:

    entA: Console.Write("a?");   
    try { a = Convert.ToInt32(Console.ReadLine()); }
    catch 
    { /*If a=0, the equation isn't quadratic*/
      Console.WriteLine("Invalid input"); 
      goto entA;             
    } 
    

    Problems abound. First off, use int.TryParse, rather than putting a try-catch around something that can fail.

    Second, the comment does not match the action of the code. The code determines if the result is an integer; the comment says that it checks for zero.

    Third, do not use a goto when what you are attempting to represent is a loop.

    Fourth, look at all that duplicated code! You have the same code repeated three times with minor variations.

    Make yourself a helper method:

     static int ObtainInput(string prompt, bool canBeZero)
     {
         while(true) // loop forever!
         {
             Console.Write(prompt);
             string input = Console.ReadLine();
             int result;
             bool success = int.TryParse(input, out result);
             if (success && (canBeZero || result != 0))
                 return result;
             Console.WriteLine("Invalid input!");
         }
     }
    

    And now your mainline is:

    int a = ObtainInput("A? ", false);
    int b = ObtainInput("B? ", true);
    int c = ObtainInput("C? ", true);
    

    Your bug though is here:

    x1 = x2 = -b / (2 * a);   
    

    You do the arithmetic in integers, and then convert to doubles. That is, you do the division, round to the nearest integer, and then convert to double. Do it in doubles (or, less likely, in decimals) from the start. It should be:

    double a = ObtainInput("A? ", false);
    double b = ObtainInput("B? ", true);
    double c = ObtainInput("C? ", true);
    

    That is, a, b, and c should not ever be integers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wrote my first OpenMP program that parallelizes a simple for loop. I
I just wrote my first Python program and it works! It is a program
I just wrote some code to test the behavior of std::equal, and came away
I'll show some code, first: echo exec(compile\\save.exe Untitled.c tmpUntitled.c); I have a program, named
I was just doing some stuff and wrote this program. I got the following
I recently wrote a piece of code which did SomeClass someObject; mysqlpp::StoreQueryResult result =
I just wrote my first nodejs program using the node-mongodb-native driver. I used the
I just start coding in PHP, i wrote my first php + mysql program
I am working on a simple program to just draw circles around some fixed
Just wrote my first python program! I get zip files as attachment in mail

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.