So basically my question is how I can write a program that will take the input of three numbers and display the sum on console?
I tried a few solutions and I watched a couple of Youtube videos, but still didn't quite understand.
CodePudding user response:
Here is an example how you can read the input from the console, convert it to an integer, calculate the sum, and then display the result in the console.
static void Main(string[] args)
{
Console.WriteLine("Enter the first number: "); // Prints "Enter the first number:" in the console.
var number1string = Console.ReadLine(); // Reads the input which the user supplies. This is a string.
Console.WriteLine("Enter the second number: ");
var number2string = Console.ReadLine();
Console.WriteLine("Enter the third number: ");
var number3string = Console.ReadLine();
var number1 = int.Parse(number1string); // Convert the string from the user to an integer value
var number2 = int.Parse(number2string);
var number3 = int.Parse(number3string);
var sum = number1 number2 number3; // Calculate the sum of the 3 inputs
Console.WriteLine($"The sum of {number1}, {number2} and {number3} is: {sum}"); // Show the result in the console.
}
CodePudding user response:
If this is not homework and not passing values into the app but instead prompting for values consider the following code which by installing Spectre.Console NuGet package provides a method to get numbers with validation.
In the code below TextPrompt<int>
means to get an int, if you want a double TextPrompt<double>
etc.
using System;
using System.Collections.Generic;
using System.Linq;
using Spectre.Console;
namespace YourNamespaceGoesHere
{
partial class Program
{
static void Main(string[] args)
{
string[] prompts =
{
"first",
"second",
"third"
};
List<int> list = new List<int>();
for (int index = 0; index < 3; index )
{
int value = AnsiConsole.Prompt(
new TextPrompt<int>($"[cyan]Enter {prompts[index]} number[/]")
.PromptStyle("yellow")
.ValidationErrorMessage("[white on red]Please enter a number[/]")
.DefaultValue(0)); ;
list.Add(value);
Console.Clear();
}
AnsiConsole.MarkupLine($"[white on blue]Total is {list.Sum()}[/]");
Console.ReadLine();
}
}
}
If using a library is not for you than consider the following where if the value entered is not a number the value is 0.
static void Main(string[] args)
{
string[] prompts =
{
"first",
"second",
"third"
};
List<int> list = new List<int>();
for (int index = 0; index < 3; index )
{
Console.WriteLine($"Enter {prompts[index]} number");
var userValue = Console.ReadLine();
if (int.TryParse(userValue, out var value))
{
list.Add(value);
}
Console.Clear();
}
Console.WriteLine($"Total is {list.Sum()}");
Console.ReadLine();
}
Finally, for either solution to see the values entered Console.WriteLine(string.Join(",", list));
CodePudding user response:
// Paste that into your program.cs (dotnet 6)
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
var lineString = Console.ReadLine();
// lineString could be "123 456 789"
// now we split the numbers into an array
if (lineString != null && lineString.Length > 0)
{
var splittedNumbers = lineString.Split();
// splittedNumbers will look like:
// splittedNumbers[0] -> 123
// splittedNumbers[1] -> 456
// splittedNumbers[2] -> 789
// now we need to transform the string into an number and add summarize it
var sumNumber = 0;
foreach (var split in splittedNumbers)
{
var number = 0;
// try to parse your input if possible - without throwing an arror
int.TryParse(split, out number);
sumNumber = number;
}
Console.WriteLine("Your sum: " sumNumber.ToString());
}
// Append that to let the console not quid directly without sseing the sum....
Console.ReadLine();
CodePudding user response:
using System;
public class Exercise10
{
public static void Main()
{
int number1, number2, number3,sum;
Console.Write("Enter first number - ");
number1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number - ");
number2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter third number - ");
number3 = Convert.ToInt32(Console.ReadLine());
sum = number1 number2 number3;
Console.WriteLine("Sum of three number is : " sum);
} }