Here is an example code in C# that will take user input and add it to an array using a loop, and then display the contents of the array through printing:
using System;
class Program {
static void Main(string[] args) {
// Initialize an array of size 5
int[] arr = new int[5];
// Loop through the array to take user input
for (int i = 0; i < arr.Length; i++) {
Console.Write("Enter a number: ");
int input = Convert.ToInt32(Console.ReadLine());
arr[i] = input;
}
// Display the contents of the array through printing
Console.WriteLine("The array contains: ");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i]);
}
}
}
Explanation:
arr
of size 5.for
loop to loop through the array from index 0 to 4. Inside the loop, we prompt the user to enter a number and store it in the input
variable. We then add the input
value to the current index of the array arr[i]
.for
loop to loop through the array again from index 0 to 4. Inside the loop, we print out the value at each index of the array using Console.WriteLine(arr[i])
.Sample Output:
Enter a number: 10
Enter a number: 20
Enter a number: 30
Enter a number: 40
Enter a number: 50
The array contains:
10
20
30
40
50
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-05-06 11:00:00 +0000
Seen: 15 times
Last updated: Sep 08 '22
How to provide Arduino input parameters of any size without creating a struct?
How can the objects that are duplicated within a nested array be removed?
How can I resolve the ValueError regarding incompatible shapes between (None, 20, 9) and (None, 9)?
What is a more effective method to verify the type of an Array containing elements of any type?
What is the way to add up items in an array with two dimensions using Google Apps Script?
How can an array be filtered using the values in another array?
Can an Array of Elements be made in Android?
What is the method to separate two values in a single column of a numpy array?