Creating array of elements c# console [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
i just try to make this.
writing a program which create an array of of integer numbers and initializes each the elements with the element's index value, multiplid by five.
print the result
and my code is :..
Console.Title = " Arrays ";
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.Clear();
Console.WriteLine(" This Program Is About Partial Automation Array :-");
Console.WriteLine(" ====================================================");
Console.Write("\n Please Enter The Size Of Array : ");
int n = Convert.ToInt32(Console.ReadLine());
int[] arr1 = new int[n];
Console.WriteLine(" =====================================");
// Multible Process :-
for (int i = 0; i < n; i++)
{
arr1[i] = arr1[i] * 5;
}
// Displaying Process :-
Console.WriteLine(" \nThe Result Is :-");
Console.WriteLine(" ================");
for (int i = 0; i < n; i++)
{
Console.Write(" "+ arr1[i] + "\t");
}

Please change arr1[i] = arr1[i] * 5; to arr1[i] = i * 5;

use linq:
const int N = 10;
var array = Enumerable.Range(0, N)
.Select((x, i) => i * 5)
.ToArray();
foreach (var element in array) {
Console.WriteLine($"Value: {element}");
}

Related

Not all code paths return a value on a for loop C# [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 5 months ago.
Improve this question
I'm trying to build a square shape
string BuildSquare(int n)
{
for (int p = 1; p <= n; p++)
{
for (int i = 1; i <= n; i++)
{
Console.Write("+");
}
Console.WriteLine();
}
}
When done, the above error keeps showing not all code paths return a value I've tried returning 0, null, true; but they all fail
BuildSquare must return a string. Your code is not returning anything.
Console.Write/Console.WriteLine is not a way to return anything.
Based on your code I think you need this:
string BuildSquare(int n)
{
var sb = new StringBuilder();
for (int p = 1; p <= n; p++)
{
for (int i = 1; i <= n; i++)
{
sb.Append("+");
}
sb.AppendLine();
}
return sb.ToString();
}

C# - What's with this weird 0 at the end? [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 1 year ago.
Improve this question
I'm a newbie.
I have a problem with C#, I don't know why program prints me a "0" at the end of the first row :( Plz halp.
namespace Tabliczka_mnożenia
{
class Program
{
static void NumbersInRow(int x)
{
Console.Write(" ");
for (int i = x; i <= 10; i++)
{
Console.Write(i);
Console.Write(" ");
}
}
static void NumbersInColumn(int y)
{
for (int i = y; i <= 10; i++)
{
Console.WriteLine(i);
Console.Write(" ");
}
}
static void Main(string[] args)
{
int x = 1;
int y = 0;
Console.Write(" ");
NumbersInRow(x);
NumbersInColumn(y);
}
}
https://i.stack.imgur.com/ljsLW.jpg
The 0 is printed intentionally from the NumbersInColumn function. You have just forgotten to print a line break after the NumbersInRow function, like this:
static void NumbersInRow(int x)
{
Console.Write(" ");
for (int i = x; i <= 10; i++)
{
Console.Write(i);
Console.Write(" ");
}
Console.WriteLine("");
}
0 is printed by NumbersInColumn method.
WriteLine methods writes 0(y=0) and moves cursor to a new line.
In your case y starts with 0. You are then calling console.writeline() which writes whatever you want and then goes to the next line.
You should return a new line before starting to iterate through the rows.

I dont get the correct answer of the factorial of the 2nd time I enter a number [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 1 year ago.
Improve this question
I don't get the correct answer of the factorial the second time I enter a number.
using System;
namespace Outcome2
{
class Program
{
static void Main(string[] args)
{
int t, FactR = 1;
do
{
Console.Write("Enter no.: ");
t = Convert.ToInt32(Console.ReadLine());
if (t < 0)
Console.WriteLine("Factorial of Negative number's can't be found ");
else if (t <= 1)
Console.WriteLine("{0}! = {1}", t, FactR);
else
{
for (int counter = t; counter >= 2; counter--)
{
FactR = FactR * counter;
}
Console.WriteLine("{0}! = {1}", t, FactR);
}
Console.WriteLine("Do you wish to quit? Y/N");
} while (Console.ReadKey().KeyChar != 'Y');
}
}
}
its probably because you don't reset the FactRvariable you can do it like this.
do{
if (t < 0)
Console.WriteLine("Factorial of Negative number's can't be found ");
else if (t <= 1)
Console.WriteLine("{0}! = {1}", t, FactR);
else
{
FactR = 1;
for (int counter = t; counter >= 2; counter--)
{
FactR = FactR * counter;
}
Console.WriteLine("{0}! = {1}", t, FactR);
}
Console.WriteLine("Do you wish to quit? Y/N");
} while (Console.ReadKey().KeyChar != 'Y');

Why is my for loop stopping before last index? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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 want to read 20 items but only 19 are read through console. Any idea why ?
What I'd like my code to do is :
1st : read the interger k from the standard input buffer (console)
2nd : read 20 integers into the array int x[]
static void Main(string[] args)
{
int k ;
int[] x = new int [20];
int[] y = new int [20];
int[] yval = new int[20];
int i;
Console.WriteLine("Enter k value");
k = Console.Read();
Console.WriteLine("Enter x values\n ");
for (i = 0; i <=19 ; i+=1)
{
x[i] = int.Parse(Console.ReadLine());
yval[i] = (x[i] + k) % 26;
}
}
Jon addressed the root of the problem in the comments, but I wanted to try to answer more fully. Console.Read() will only read a single character and return an integer representing the character, probably not what you want. Any characters after that are immediately read on the first pass, probably not what you expected. The issue can be illustrated by inspecting the values:
static void Main(string[] args)
{
int k;
int[] x = new int[20];
int[] y = new int[20];
int[] yval = new int[20];
int i;
Console.WriteLine("Enter k value");
k = Console.Read();
Console.WriteLine("k = {0}", k);
Console.WriteLine("Enter x values\n ");
for (i = 0; i <= 19; i += 1)
{
var input = Console.ReadLine();
x[i] = int.Parse(input);
yval[i] = (x[i] + k) % 26;
Console.WriteLine("x[{0}] = {1}", i, x[i]);
}
Console.WriteLine("Press any key...");
Console.ReadKey();
}
If you enter "12" for k, the output looks like:
Enter k value
12
k = 49
Enter x values
x[0] = 2
The first character '1' is saved to k as int value of 49 (ASCII value for '1'). The buffer still has a '2' and a newline to be read. They get read on the first pass through the loop and stored in the first element of the array.
As Jon said, you probably want k = int.Parse(Console.ReadLine()); instead.

Find numbers that don't divide by 3 or 7 [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 9 years ago.
Improve this question
I need help with a program.
I need to write a program that prints on the console numbers from 1 to N that can't divide in the same time by 3 or 7. I need to parse N so the user inputs a value for it. Here is my code for now, can you tell me what is wrong?
Console.Write("Enter a number: ");
int n = int.Parse(Console.ReadLine());
for (n = 1; n <= 99999; n++) {
//n % (3 * 7) == 0
I thought out how I will check it but I can't think out how to make the other part. I think there is something wrong with my loop too. Can you give me some hints where I am mistaken and what I can do? Thank you!
You're getting N from the user then immediately overwriting it. Use a different variable for your loop:
int n = int.Parse(Console.ReadLine());
for (int i = 1; i <= n; i++)
// do stuff
You can use Modulus %
Console.Write("Enter a number: ");
string input = Console.ReadLine();
int n;
if (int.TryParse(input, out n))
{
for (int i = 1; i < n; i++)
{
if(i % 3 != 0 || i% 7!= 0) Console.WriteLine(i);
}
}
Your loop is wrong because your condition is wrong.It should be i < n, you are getting an input but then you overwriting it's value by n = 1;
Console.Write("Enter a number: ");
int n = int.Parse(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
if (i % 21 != 0) { Console.Write(i + " "); }
}
Example: http://ideone.com/ThASen

Categories

Resources