C# count #times number can be halved [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
As per title; I have tried multiple ways I can't seem to get my head around where I'm going wrong. I feel this is as close as I've got, but something still seems scrambled. Can someone help me out on finalising this? (This current code consistently outputs 0).
To clarify; I want the code to be able to read the number inputted by a user and figure out how many times it can be halved before reaching 1.
Console.WriteLine("Please enter a number to find how many time it can be divided without becoming less than 1");
Int32 DiviNum = Int32.Parse(Console.ReadLine());
Int32 count = 0;
for (int i = 0; i > 1; i = i / 2)
{
count++;
}
Console.WriteLine("Number of times " + DiviNum + " is divisible by 2 is " + count);
Thanks in advance

Solution is
Console.WriteLine("Please enter a number to find how many time it can be divided without becoming less than 1");
Int32 DiviNum = Int32.Parse(Console.ReadLine());
Int32 count = 0;
for (int i = DiviNum/2 ; i > 1; i = i / 2)
{
count++;
}
Console.WriteLine("Number of times " + DiviNum + " is divisible by 2 is " + count);

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…

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.

Getting string length won't work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm using below code to find out what is the length of a given string:
string foo = "Welcome to stack overflow";
int strlen;
for (int i=0; i<foo.length; strlen++){}
Console.WriteLine(strlen.ToString());
But the code never leaves the loop.
Well, that's weird.
I don't understand your logic, you already have the string length, why use a loop to apply it to another int?
But, well, who am I to judge?
The problem on your loop is that you're not increasing the value of i.
Do this instead:
for (int i=0; i<foo.Length; i++)
{
strlen++;
}
You could remove the loop and do this to your code:
string foo = "Welcome to stack overflow";
Console.WriteLine("String length: " + foo.Length.ToString());
Edit:
As mentioned in the comments:
the length property must have it's first letter uppercased, since C# is case sensitive. - Jon Skeet
You are never increasing "i" so the "i < foo.length" will always be true
You should iterate over i, not over strlen:
for (int i=0; i<foo.length; i++){}
You have one typo (foo.Length, not foo.length) and two errors:
Do not forget to assign 0 on local variabale declaration (int strlen = 0)
Do not forget to increment counter (i++)
Something like that:
string foo = "Welcome to stack overflow";
// error: assign 0 to strlen
int strlen = 0;
// Typo: foo.Length instead of foo.length
// error: do not forget to increment "i" as well as "strlen"
for (int i = 0; i < foo.Length; strlen++, i++) {}
// 25
Console.WriteLine(strlen.ToString());
Test:
// 25
Console.WriteLine(foo.Length);

C# calculator doesn't work with unlike addends [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm developing a calculator in C#, and I have gotten pretty far except for the fact that when I am adding numbers, it only succeeds if the addends are the same number (e.g. 9 + 9 = 18, 6 + 9 = 12). So I'm pretty confused. Will someone help me please?
private void button5_Click(object sender, EventArgs e)
{
b = false;
number = decimal.Parse(richTextBox1.Text);
richTextBox1.Text = number + " + ";
number2 = decimal.Parse(richTextBox1.Text);
addReady = true;
}
private void button17_Click(object sender, EventArgs e)
{
if (addReady == true)
{
answer = number + number2;
richTextBox1.Text = Convert.ToString(answer);
b = true;
}
}
Also, when I put this in, my positive/negative button suddenly started malfunctioning, converting everything to 0. What's up with that?
decimal neg;
neg = number * 2;
number = number - neg;
richTextBox1.Text = Convert.ToString(number);
Alright. What you're trying to do is parse the string from your text box two times in the row. So, both times you'll get the exact same number. It doesn't cycle the way you wrote it. On top of that, you're not extracting the expression. To make your life easier, I suggest using Calculator.NET third party library. It parses mathematical expressions:
http://weblogs.asp.net/pwelter34/archive/2007/05/05/calculator-net-calculator-that-evaluates-math-expressions.aspx
Then there's NCalc (just like Calculator.NET, this library is unbelievably simple to use):
http://ncalc.codeplex.com/
You may also take advantage of the Microsoft Script Control, as demonstrated in this answer:
https://stackoverflow.com/a/392355/2006048
Just in case the link doesn't work I'll copy the code here for demonstration (all credit to the original poster):
MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
sc.Language = "VBScript";
string expression = "1 + 2 * 7";
object result = sc.Eval(expression);
MessageBox.Show(result.ToString());

Inversing integers and printing it in console [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
I have an array of integers, which have to be converted into its inverses, so that, my program reads a series of integers from a user, fills an array with it, and then print it's inverses in a writeline. I had an idea to put inversed integers into double array, but still I don't know how to inverse (so that it looks like that - 1/N) it.
Finally, inversed integers should be printed in WriteLines.
You can use double inverse = 1.0 / number
If I understand correctly, this should work:
int arrayOfIntegers[] = <Your Array of Integers>;
foreach (input in arrayOfIntegers){
Console.WriteLine(1.0 / (double)in);
}
for(int i = 0; i < numbers.Length(); i++)
{
Console.WriteLine("1 / " + numbers[i].ToString());
}
or the foreach version:
foreach(int i in numbers)
{
Console.WriteLine("1 / " + i.ToString());
}
or to get cute:
foreach(int i in numbers)
{
Console.WriteLine("1 / {0} = {1}", i, 1.0 / i);
}
Of course this is a very basic exercise, I would like to introduce LINQ to you:
var output = yourArray.Select(x=> {
float f = 1f/x;
Console.Write(((decimal)f) + " ");
return f;
}).ToArray();
//This way you can still store the array while print all the inversed elements

Categories