Having trouble counting in c# [closed] - c#

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 am having trouble with this. How can I count a length I want input to.
Lets say I ask a user an input. He enters 10. It counts 1,2,3,4,etc... 10.
Or if users enter 5, 1,2,3,4,5 is output
Thanks.
EDIT:
I am sorry. This isn't homework. School doesn't start till next week and I am practicing.
Sorry, I should have given code.
This is what I had that does work
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i < length; i++)
{
Console.WriteLine(i);
}
I am just assuming since I am new that I did some sloppy code and am looking for maybe something cleaner. Or another point of view for it.

update your code with <=
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}

You just need to change the '<' operator to '<=':
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}

string length;
Console.Write("Enter Length: ");
length= Console.ReadLine();
for (int i = 1; i <= Int32.Parse(length); i++)
{
Console.WriteLine(i);
}
Console.ReadKey();

Related

Generate random sequence of AlphaNumeric [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 1 year ago.
Improve this question
I want a code to generate sequence of Alphanumeric character like LLNNLLNNLL where L is the Letter and N is the number. For example if the length is 5 the sequence will be LLNNL and if the length is 6 the sequence will be LLNNLL and if 7 it would be LLNNLLN.
string alphabets = "ABCDEFGHIJKLMNPQRSTUVWX";
int length = model.VALUE_INT;
string result = "";
for (int i = 0; i < length; i++)
{
int next = _random.Next(23);
result += alphabets.ElementAt(next);
result += _randomNum.Next(1, 9);
}
This is what I have tried but condition is not fulfilling
For each position (value of i) of your output string you need to check if you need a character or an integer.
Since the pattern repeats every 4 positions, you can achieve this using the modulo operator:
for (int i = 0; i < length; i++)
{
switch (i % 4)
{
case 0:
case 1:
int next = _random.Next(23);
result += alphabets.ElementAt(next);
break;
case 2:
case 3:
result += _randomNum.Next(1, 9);
break;
}
}
Or another possibility: Add blocks of LLNN and then truncate the result to the needed length...
for (int i = 0; i <= (length/4); i++)
{
result += alphabets.ElementAt(_random.Next(23));
result += alphabets.ElementAt(_random.Next(23));
result += _randomNum.Next(1, 9);
result += _randomNum.Next(1, 9);
}
result = result.Substring(0,length);
If you want to improve these lines, you can use a StringBuilder

How to Zip two numbers using c# [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 6 years ago.
Improve this question
string A = "1234"
string B = "567890"
I want to zip the numbers. Out put should display as "1526374890"
what is the best way to achieve this using C# code.
You can do that with the following code. It does not make any assumptions about which string is longer.
string A = "1234";
string B = "567890";
char[] chars = new char[A.Length + B.Length];
int charsIndex = 0;
for (int i = 0; i < A.Length || i < B.Length; i++)
{
if(i < A.Length)
chars[charsIndex++] = A[i];
if(i < B.Length)
chars[charsIndex++] = B[i];
}
string result = new string(chars);
Console.WriteLine(result);

c# home works can't solve [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 have to make console program that asks the user to enter number, then the program has to make square of stars (*) equal to the number that the user input.
Console.WriteLine("enter number and press ENTER");
int userNumber = int.Parse(Console.ReadLine());
int rowNumber = 0;
int lineNumber = 0;
int counter = 0;
while(counter < userNumber) {
Console.Write("*");
while(lineNumber < userNumber) {
Console.WriteLine("*");
lineNumber++;
}
counter++;
}
Console.WriteLine();
example:
user input :5
*****
* *
* *
* *
*****
you can do the following:
explanation for the code:
Square is formed of 2 equal sides, so in order to draw the square using *, you will think of it like a matrix
you need a loop to draw the rows and another loop to draw the columns, and both loop has upper limit which is the number entered by the user.
the rule of drawing is like this i will put star if i am in the first row or the last one or in the first column or last, so by addressing this i used the if statement ( i==0 || i== number-1 || j==0 || j== number -1) where i is the row and j is the column, and if this condition is not satisfied, print space
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Please enter a number:");
var number=Convert.ToInt32(Console.ReadLine());
for(int i=0; i < number; i++)
{
for(int j=0; j < number; j++)
{
if(i==0 || i == number-1 || j==0 || j == number-1)
Console.Write("*");
else
Console.Write(" ");
}
Console.Write("\n");
}
}
}
here a working DEMO
hope this will help you

My C# array only sums and averages the last user input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I built a C# program in which the user is prompted to enter 10 different values and then the program is supposed to sum and average these values and print the sum and average. The problem I am having is that my program is only capturing the last value entered...please help!
namespace ConsoleApplication4
{
class Program
{
const int count = 10;
static void Input(double[] numbers, int num)
{
for (int i = 0; i <= 9; i++)
{
Console.Write("Enter integer {0}: ", i + 1);
numbers[num] = Convert.ToDouble(Console.ReadLine());
}
}
static void Average(double[] numbers, int num)
{
double sum = 0;
double avg = 0;
for (int i = 0; i < numbers.Length; i++)
{
sum += numbers[i];
}
avg = sum / numbers.Length;
Console.WriteLine("The sum of the inputs is {0} and the average is {1}", sum, avg);
}
static void Main(string[] args)
{
double[] numbers = new double[count];
for (int num = 0; num < 1; num++)
{
Input(numbers, num);
Average(numbers, num);
Console.WriteLine("Press the Enter Key");
Console.ReadLine();
}
}
}
}
}
In you Input method, you're assigning user input to numbers[num] instead of numbers[i].
If you look at your Input() method, you're always storing in the num index in your for loop, but num doesn't change in the loop. You should be using numbers[i] instead of numbers[num].
for (int i = 0; i <= 9; i++)
{
Console.Write("Enter integer {0}: ", i + 1);
numbers[num] = Convert.ToDouble(Console.ReadLine());
That should be
numbers[i] = Convert.ToDouble(Console.ReadLine());

why the output starts from 402 althoug the initial is zero? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 1000; i++)
{
if (i%2==0)
{
Console.WriteLine(i);
}
}
}
}
It's start from 0 but you can't see.Try this:
for (int i = 0; i < 1000; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
if (i % 100 == 0) Console.ReadKey();
}
Press enter to see next numbers.That will show 100 numbers at once,you can change it if you want to display less number..

Categories