Tip of the day
News
Instructions
Objective
Write a program to solve math problems in C#.
Requirements and Specifications
Description: A C# console program that allows users to select different types of math problems, show the user the appropriate problem, and check whether or not they gave the correct answer. MUST BE DONE IN FUNCTIONAL PROGRAMMING.
Source Code
using System;using System.Collections.Generic;namespace WangApp{ // Create a class to represent a Math problem class MathProblem { private string description; private double answer; public MathProblem(string desc, double ans) { description = desc; answer = ans; } public string getDescription() { return description; } public double getAnswer() { return answer; } public bool checkAnswer(double ans) { return answer == ans; } } class Program { static void Main(string[] args) { // Create a list of math problems List problems = new List (); problems.Add(new MathProblem("Solve for x: x + 5 = 2x-17", 22)); problems.Add(new MathProblem("Calculate 7^2", 49)); problems.Add(new MathProblem("Calculate 10/(100-98) + 4^2", 21)); problems.Add(new MathProblem("Solve for y: (8y - 4)/(4y+12) = 1", 4)); problems.Add(new MathProblem("Calculate 1567^0", 1)); display_menu(problems); // Get option from user int option = getMenuOption(1, 5); // between 1 and 5 because we have 5 problems // Display problem again: Console.WriteLine(problems[option - 1].getDescription()); Console.Write("Answer = "); // Now, ask for answer double answer = Convert.ToDouble(Console.ReadLine()); // Check if it is correct bool correct = problems[option - 1].checkAnswer(answer); if(correct) { Console.WriteLine("That is correct!"); } else { Console.WriteLine("Sorry, that is not correct."); } Console.Read(); } // Create a function to display the Math rpblems (Menu) static void display_menu(List problems) { // Loop through problems and prints them int i = 1; foreach(MathProblem p in problems) { Console.WriteLine(i.ToString() + ") " + p.getDescription()); i += 1; } } // Create a function that requests the user an integer that is inside a range static int getMenuOption(int lower, int upper) { int option; // Create a while-loop that will keep requesting user for an option until s/he enters a valid option while(true) { Console.Write("Enter option: "); option = Convert.ToInt32(Console.ReadLine()); if(option >= lower && option <= upper) { return option; } else { Console.WriteLine("Please enter a valid option between " + lower.ToString() + " and " + upper.ToString() + "."); } } } }}
Related Samples
Discover our C# Assignments Sample Section, featuring expertly crafted solutions. From basic syntax to object-oriented principles and LINQ queries, explore annotated code examples. Whether you're learning GUI development or database connectivity, these samples provide clarity and guidance to excel in C# programming assignments effortlessly.
C#
Word Count
4575 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
4622 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
6079 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
Web Development
Word Count
6269 Words
Writer Name:Prof. Samuel Fox
Total Orders:852
Satisfaction rate:
Web Development
Word Count
6356 Words
Writer Name:Dr. Chelsea Moran
Total Orders:987
Satisfaction rate:
C#
Word Count
5049 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
5770 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
4648 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
15336 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
6148 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
5654 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
3787 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
5694 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
3064 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
8569 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
5951 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
6031 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
5993 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
4526 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate:
C#
Word Count
3093 Words
Writer Name:Emily Davis
Total Orders:1227
Satisfaction rate: