How do I save every value of k in the loop? | SoloLearn: Learn to code for FREE!


How do I save every value of k in the loop?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { double a = 2; double b = 3; double c = 4; double g = 2; double t = 3; double n = 4; int A = 0; while (A<10){ double d = 4/(a*b*c); double e = -4/(g*t*n); double k = d+e; if(A%2 == 0){ g+=2; t+=2; n+=2; A++; } else if(A%2 == 1){ a+=2; b+=2; c+=2; Console.WriteLine(k); A++; } } } } }


5/9/2021 4:26:01 PM


8 Answers

New Answer


On second thought, since you said "sum of all the values of <k>", I think you can do it without using array. Add one `double` variable before the while-loop. Let's name it. <SumOfK>, and have it initialized by zero. In the while-loop, you can add value of <k> into <SumOfK>, just after <k> is calculated. I think this will do the trick.


Save every value of <k>? into what? an array? what do you want to do with values of <k> anyway?


because I need to know the sum of all the values of k.


Thank you!!


can be tried through an array


every time the loop happens I want to save the value of k


how do I do that with an array?


... int A = 0; int size = 10; //Example to use array... double[] ok = new double[size]; ... while(A<size){ ... double k = d+e; ok[A]=k; ... ... A++; ...... ....etc } //for example to read array.. foreach(var u in ok){ System.Console.WriteLine(u);