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

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…

Related

Console.ReadLine().Contains Console.ReadLine()? why do i get false when the number is present in both? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Could someone help me why do i get true only if the numbers are the same? I need to find out if the second input is in the first one. True/false
int[] firstNumber;
firstNumber = new int[10];
firstNumber[0] =
Convert.ToInt32(Console.ReadLine());
int secondNumber =
Convert.ToInt32(Console.ReadLine()) ;
Console.WriteLine(firstNumber.Contains(secondNumber));
Edit. I need to get true for ex.:firstNumber 5214 and secondNumber 4
how can i get true for 5432(firstNum) and 3(secondNum)
Use strings instead of integers, which will use string.Contains instead of Array.Contains:
string firstNumber = Console.ReadLine();
string secondNumber = Console.ReadLine();
Console.WriteLine(firstNumber.Contains(secondNumber));

calculating multi squares in one textbox in C# [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 3 years ago.
Improve this question
I'm working on windows form application and I have homework to calculate the square of 5 numbers in one textbox and I have to put all 5 numbers in different lines
I expect the results to be like this,
you enter 5 numbers in each line
4
9
16
25
and get them in one single message box saying the results like
2, 3, 4, 5
Pass the Textbox Text into the function and It will Print all the SqureRoot of the Numbers
private void squreRootFinder(string textBox)
{
string[] numbers = textBox.Split('\n');
string data = "";
for (int i = 0; i < numbers.Length; i++)
{
data += Math.Sqrt(Convert.ToInt32(numbers[i])) + " ";
}
MessageBox.Show(data);
}

Segregate elements of a string output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
My C# output is in the form:
Epoch Timestamp1,Cost1,Epoch Timestamp2,Cost2,Epoch Timestamp3,Cost3.
(Costn is the cost for Epoch TImestampn.)
I want to get the cost for each timestamp, in the below manner:
timestamp cost
timestamp cost
..
..
..
I am implementing this in C# in Visual Studio 2017.
How can I implement this? Any ideas please?
Thank you.
Just use Split and a for loop, add pepper and salt to taste
var input = "Epoch Timestamp1,Cost1,Epoch Timestamp2,Cost2,Epoch Timestamp3,Cost3.";
var split = input.Split(',');
var results = new List<string>();
for (int i = 0; i < split.Length; i+=2)
results.Add(split[i] + " " + split[i + 1]);
foreach (var item in results)
Console.WriteLine(item);
See it in action here

VAT at my cost by 25% VAT [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I must add 25% VAT on top of the price specified when you create a product.
How do I add the 25% tax on to the price?
i have tried the following:
string pris = TextBoxpris.Text;
decimal prisMoms = pris * 0.25;
I found the inspiration in this question -> Calculate VAT helper method
You need to parse what's in the text box.
decimal parsed = 0, prisMoms = 0;
var canParse = decimal.TryParse(TextBoxpris.Text, out parsed);
if(canParse)
{
prisMoms = parsed + (parsed * 0.25m);
}

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