0
Sum of numbers in prime indexes using c#
If the input is {2,3,10,15,11,19} then the output should be 44. We know that the prime numbers are 2,3,5,7 and so on. Here in index number 2 we have 10, 3 we have 15, 5 we have 19 Now the sum of those numbers is 10+15+19=44.
6 Answers
+ 15
int[] arr = {...}
int size = arr.Length;
First you'll have to generate the primes number from 0 to size (excluding) and store them in a list called primes. (you do this part yourself :D)
int sum = 0;
foreach(int p in primes)
sum += arr[p];
Console.WriteLine(sum);
+ 11
So question solved?
(if you want to generate primes, search for codes on SoloLearn or Google)
+ 2
Thanks for ur great advice... Already I did that
+ 1
I described it clearly na....
The numbers which are existing in the prime indexes... of that array should be added
+ 1
That is vat the logic I'm asking... I too know the thing you typed
0
A={2,3,10,15,11,19}
A[2]=10, A[3] =15, A[5] =19....
add those numbers 10,15,19 and the output is 44
The index numbers 2,3,5 are primes.