Asking for a help. How do I fix this? [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 22 days ago.
Improve this question
`static void Main(string[] args)
{
double celsius, Fahrenheit;
Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Fahrenheit temperature : ");
celsius = (Fahrenheit - 32) * 5 / 9;
Console.WriteLine();
Console.WriteLine("\nThe converted Celsius temperature is : " + celsius);
Console.ReadLine();
}
}
}`
I tried to different methods but it's seems like it doesn't work. I expect that you could help me and this program will work! thanks

You're printing the prompt to enter the temperature after reading the input for it with ReadLine.
You never assign the read value to anything.
Try this:
Console.Write("Enter Fahrenheit temperature : ");
var fahrenheit = Convert.ToDouble(Console.ReadLine());
var celsius = (fahrenheit - 32) * 5 / 9;

Related

C# "if" and "for" [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last month.
The community is reviewing whether to reopen this question as of 4 days ago.
Improve this question
in C#, if i am using "if" and "for" to ask someone how many products did he/she bought and the prices and show in the program the cheapast and more expensive, how do i do?
Console.WriteLine("How many products will you buy: ");
double amountofproducts = double.Parse(Console.ReadLine());
for (double conter = 0; contador < amountofproducts; conter++)
{
Console.WriteLine("insert the price " + (conter + 1) + "º product");
double price = double.Parse(Console.ReadLine());
}
double moreexpensive = 0;
double cheapest = 0;
if (moreexpensive > cheapest)
{
Console.WriteLine("The cheapest product is: ");
}
else if (moreexpensive < cheapest)
{
Console.WriteLine("The more expensive product is: ");
}
You don’t have an array with the whole elements or even a list which you’re creating items using console, so it’s impossible to compare item by item with the same input of data…

C# double value displaying incorrect value [duplicate]

This question already has answers here:
Difference between Console.Read() and Console.ReadLine()?
(12 answers)
Closed 1 year ago.
Hello I am practicing with C$ specifically with decimal numeric values. I have this simple program just to check if the inputted data is correct. I input 12.9 but get 49. Any help would be appreciated.
static void Main(string[] args)
{
Console.Write("Enter the first number 1: ");
double num1 = Console.Read();
Console.Write(num1);
//Console.WriteLine(num1+" + "+num2+" + "+num3 + " = "+total);
}
You need to use Console.ReadLine() and not Console.Read().
Read method - Reads the next character from the standard input stream.
ReadLine method - Reads the next line of characters from the standard input stream.
Try this:
static void Main(string[] args)
{
Console.Write("Enter the first number 1: ");
// It might cause an error if the user doesn't write a number
// which is parsable to double
double num1 = double.Parse(Console.ReadLine());
Console.Write(num1);
}
Or a safe way:
static void Main(string[] args)
{
Console.Write("Enter the first number 1: ");
// You may also ask the user to write again an input of number.
if (double.TryParse(Console.ReadLine(), out double num1))
{
Console.Write(num1);
}
}

Is there a function that swaps two variables in a string? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
So we have to have code that swaps the integers in a two-digit number, such as "43" being "34". The user inputs a random two digit number and that number must be swapped.
I am not sure how to separate or mess with the two digit number that the user inputs into the console, so I have not had much luck in doing this.
static void Main(string[] args)
{
Console.WriteLine("Please enter a two-digit integer");
string input = Console.ReadLine();
int number = Convert.ToInt32(input);
Console.ReadKey();
}
You can also just reverse the string before you parse it:
string input = string.Concat(Console.ReadLine().Reverse());
// If the user entered "34", 'input' will equal "43"
You can try modulo arithmetics:
number = number % 10 * 10 + number / 10;
you could do:
Console.WriteLine("Enter a No. to reverse");
int Number = int.Parse(Console.ReadLine());
int Reverse = 0;
while(Number>0)
{
int remainder = Number % 10;
Reverse = (Reverse * 10) + remainder;
Number = Number / 10;
}
Console.WriteLine("Reverse No. is {0}",Reverse);
Console.ReadLine();
this will give you 34 if you entered 43.
You can checkout this https://www.c-sharpcorner.com/blogs/reverse-a-number-and-string-in-c-sharp1 for more info.

My output is always "1" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to create a miles per gallon calculator with C#, but my output is always "1" no matter the input.
Is my equation not created correctly? I am supposed to base my code off of a previous assignment which looks very similar, so (if possible) try to help me find a way to keep the same relative structure to the code.
Thank you so much for any help!
Here is my code:
namespace Miles_Per_Gallon
{
class Program
{
static void Main(string[] args)
{
float Miles, Gallons, MPG;
string textline;
Console.Write("Miles Traveled :");
textline = Console.ReadLine();
Console.Write("Gallons of Gas Used :");
textline = Console.ReadLine();
Miles = float.Parse(textline);
Gallons = float.Parse(textline);
MPG = Miles / Gallons;
Console.Write("Miles Per Gallon : ");
Console.WriteLine(MPG.ToString());
Console.ReadLine();
Here is the fix one, try this and compare with your old code, you should figure out the problem.
float Miles, Gallons, MPG;
string textline;
Console.Write("Miles Traveled :");
textline = Console.ReadLine();
Miles = float.Parse(textline); //Store to variable
Console.Write("Gallons of Gas Used :");
textline = Console.ReadLine();
Gallons = float.Parse(textline); //Store to variable
MPG = Miles / Gallons;
Console.Write("Miles Per Gallon : ");
Console.WriteLine(MPG.ToString());
Console.ReadLine();
You are using the same variable textline to store both miles and gallons input. And when you divide them together you'll get 1.
Use different variables to get input of both miles and gallons

Storing integers from Console to an array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Im writing a program that asks the console for x numbers from the console. If they pick the number 4, then 4 different numbers should be stored. Then the program must store all these inputed numbers in an array, and then add the numbers together and print it out in the console.
So, i tried to do:
Console.WriteLine("Write out a number: ");
int[] x = int[].Parse(Console.ReadLine());
and apparently you cant read in array elements from the console on that way, so do I need to store them inside an variabel and then add them to an new array?
Console.Writeline("Enter the number of numbers to add: ");
//Yes, I know you should actually do validation here
var numOfNumbersToAdd = int.Parse(Console.Readline());
int value;
int[] arrayValues = new int[numOfNumbersToAdd];
for(int i = 0; i < numOfNumbersToAdd; i++)
{
Console.Writeline("Please enter a value: ");
//Primed read
var isValidValue = int.TryParse(Console.Readline(), out value);
while(!isValidValue) //Loop until you get a value
{
Console.WriteLine("Invalid value, please try again: "); //Tell the user why they are entering a value again...
isValidValue = int.TryParse(Console.Readline(), out value);
}
arrayValues[i] = value; //store the value in the array
}
//I would much rather do this with LINQ and Lists, but the question asked for arrays.
var sum = 0;
foreach(var v in arrayValues)
{
sum += v;
}
Console.Writeline("Sum {0}", sum);

Categories