Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm calculating the sum of numbers in a series but I only want the console to output the final sum but I don't know how

int a=Convert.ToInt32(Console.ReadLine());  

int b=Convert.ToInt32(Console.ReadLine());

int c = a / b;

int b2 = b;

    for(int i=0; i<=c; i++)
{
    b += (b2 * i);
    Console.WriteLine(b-b2);
}

this is basic code that asks for a base number, and a secondary number. only numbers divisible by the second number are added together. I only want to output the final number, but right now it outputs every step it takes and I’m to noob to know an easy solution.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Write the console.WriteLine() Outside the for loop to avoid printing it in every step.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading