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);
Related
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 2 years ago.
Improve this question
this is my first post so excuse me if I am not being clear enough.
As the title says I want to split a number into smaller parts, for example 1998 to 1,9,9,8. I have looked around but I am stuck trying to figure out the right term to search for.
Reason why I want this done is to later multiply every other number with either 1 or 2 to be able to examine whether it's correct or not.
Sorry I can't provide any code because I am not sure how I should tackle this problem.
As #mjwills said, mod is what you want.
static IEnumerable<int> Digitize(int num)
{
while (num > 0)
{
int digit = num % 10;
yield return digit;
num = num / 10;
}
}
And then call it like so:
static void Main(string[] _)
{
int num = 1998;
int[] digits = Digitize(num).ToArray();
Console.WriteLine($"Original: {num}");
foreach (var digit in digits)
Console.WriteLine(digit);
}
make a string of that number and make it like this
```
int a = 1990;
int[] intArray = new int[a.ToString().Length];
int index = 0;
foreach( char ch in a.ToString())
{
intArray[index] = int.Parse(ch.ToString());
index++;
}
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.
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 7 years ago.
Improve this question
I am writing a program that finds the number of 1's in an array.. but the "scanning" inside the "for" loop takes place only once.
for (i = 0; i < 11; i++ )
{
k = Convert.ToInt32(Console.Read());
if (k==1)
{
ans++;
}
// Console.WriteLine("i == {0}", i);
}
Is this normal in C# or am I doing something wrong? I tried to search for this problem but cannot find any answers!
Do
Console.ReadLine()
instead of
Console.Read()
When you reach the first Console.Read() the inputstream is saved but only the first character is returned by the Read method. Subsequent calls to the Read method retrieve your input one character at a time from the inputstream.
Ex:
First iteration:
k = Console.Read(); //you input "abc1", k = a
Second iteration:
k = Console.Read(); // k = b
and so on.
When the last character in the inputstream is returned, the next call to Console.Read() will display the console again so you can input a new string and hit Enter.
Console.Read() docu
There's no array in your code, actually. Personally I'd rewrite this to:
string input = Console.ReadLine();
for (i = 0; i < input.Length; i++ )
{
int k = Convert.ToInt32(input.Substring(i, 1));
if (k==1)
{
ans++;
}
}
Which lets the user enter a string first and then does all the parsing. Of course, the user may enter more or less than exactly 11 characters in this code, so if you really need 11 numbers, I'd add extra messages and checks.
Also this code (as your original code) fail miserably if the user enters something non-numeric, so exception handling would be on the task list as well.
Try to use this code, I think it will answer your problem
string[] sample = { "1", "1", "3" };
var a = (from w in sample
where w == "1"
select w).ToList();
Console.Write(a.Count);
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 9 years ago.
Improve this question
I have a homework assignment and I am stuck on a certain part of the question.
How do I loop numbers regarding the readline statement.
In other words, suppose I have two numbers that I want to input. Instead of typing out the readline statement twice, how do I get a loop that would allow me to type the readline statement once?
have two numbers that I want to input. Instead of typing out the readline statement twice, how do I get a loop that would allow me to type the readline statement once
Well, if you want two numbers, then you're going to need two variables (or an array), so I assume you're trying to change this:
int firstNumber;
int secondNumber;
firstNumber = int.Parse(Console.ReadLine());
secondNumber = int.Parse(Console.ReadLine());
into something like this:
int[] myNumbers = new int[2];
for(int i = 0; i < 2; i++)
{
myNumbers[i] = int.Parse(Console.ReadLine());
}
but think about this:
Will the prompt be the same for both numbers? Or will the prompt change between inputs
Is it easier to assess the variables independently versus a part of the array/list?
Will having the numbers in a structure benefit other parts of my app (looping to get a sum, etc.)
I think a loop as fine so long as you don't end up with logic inside the lop that changes depending on which number you're using:
int[] myNumbers = new int[2];
for(int i = 0; i < 2; i++)
{
if i == 0 // bad
string prompt = "Enter the first number";
else
string prompt = "Enter the second number";
Console.WriteLine(prompt);
myNumbers[i] = int.Parse(Console.ReadLine());
}
int[] myInputs = new int[2];
for(int i=0; i < 2; ++i)
{
myInputs[i] = Int32.Parse(Console.ReadLine());
}
After this, myInputs[0] is the first value, and myInputs[1] is the second value.
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