Home c# C #. Create an array of 10 numbers. Calculate the amount of...

C #. Create an array of 10 numbers. Calculate the amount of odd array elements

Author

Date

Category

I tried to do it manually pointing all 10 variables in the array. Now I do not know how to withdraw the amount of odd items. Help please.

Code:

int [] a = new int [10];
int a = a [0];
int b = a [1];
int c = a [2];
int d = a [3];
int e = a [4];
int f = a [5];
int g = a [6];
int h = a [7];
int i = a [8];
int j = a [9];
Float S = 0;
Console.WriteLine ("Enter A");
a = convert.TOINT32 (Console.Readline ());
Console.WriteLine ("Enter b");
B = convert.TOINT32 (Console.ReadLine ());
Console.WriteLine ("Enter C");
c = convert.TOINT32 (Console.ReadLine ());
Console.WriteLine ("Enter D");
d = convert.TOINT32 (Console.Readline ());
Console.WriteLine ("Enter E");
E = convert.TOINT32 (Console.ReadLine ());
Console.WriteLine ("Enter F");
F = convert.TOINT32 (Console.Readline ());
Console.WriteLine ("Enter G");
G = convert.TOINT32 (Console.ReadLine ());
Console.WriteLine ("Enter H");
h = convert.TOINT32 (Console.ReadLine ());
Console.Writeline ("Enter i");
i = convert.TOINT32 (Console.Readline ());
Console.WriteLine ("Enter J");
J = convert.TOINT32 (Console.ReadLine ());

Answer 1, Authority 100%

Arrays and cycles, cycles and arrays. If you have an array in the initial stages of training, but there is no cycle, it means that something went wrong.

int [] a = new int [10];
for (int i = 0; i & lt; a.length; i ++)
{
  Console.Write ($ "enter a [{i}] =");
  a [i] = int.parse (Console.ReadLine ());
}
int sum = 0;
for (int i = 0; i & lt; a.length; i ++)
{
  IF (A [I]% 2 == 1)
    SUM + = A [I];
}
Console.WriteLine ($ "amount of odd numbers = {SUM}");

And there is a LINQ version

int [] a = enumerable.range (0, 10)
  .Select (i = & gt;
  {
    Console.Write ($ "enter a [{i}] =");
    Return int.Parse (Console.Readline ());
  }). Toarray ();
int sum = a.where (x = & gt; x% 2 == 1) .sum ();
Console.WriteLine ($ "amount of odd numbers = {SUM}");

Answer 2

To begin with, you incorrectly read the array. It is necessary to Console.Read (A [I]), the I-index of the element. In order to determine whether the element is odd, it is necessary to divide it to 2 and see whether its residue is equal to 1. A% 2 == 1

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions