N
N
Nar Nar2020-07-13 17:02:52
C++ / C#
Nar Nar, 2020-07-13 17:02:52

How can I find the sum of the digits of each element of an array using a For loop?

Given an array, any. For example, from 1 to 1000. You need to find the sum of the digits of its each element, but with a for loop, it might sound silly

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergei Chamkin, 2020-07-13
@Sergei1337

int[] arr = new int[] { 1, 2, 3 };
int sum = 0;

//Две различные вариации. 

//Через foreach
foreach (var item in arr)
{
   sum += item;
}
// 

//Если через просто for
for(int i = 0;i<arr.Length;i++)
{
  sum+=arr[i];
}
// 
Console.WriteLine(sum);

V
Vladislav, 2020-07-13
@Vladlen234

int[] Array = new int[] { 1, 2, 3, 4 }; //declaring an array
for(int i = 0;i< Array.Length; i++)
{
Console.WriteLine($"element # {Array[i]}"); // output to the Console of all elements separately
}

D
Dmitry Pavlov, 2020-07-16
@Stalker31

int i=0;
int[]mas=new int[];
for(int l=0;l<mas.Lenght;l++)
i+=mas[l];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question