Write C# program to find sum of even numbers between 1 to n


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class csharpExercise
  7. {
  8. static void Main(string[] args)
  9. {
  10.  
  11. int i, num, sum = 0;
  12.  
  13. // Reading number
  14. Console.Write("Enter any number: ");
  15. num = Convert.ToInt32(Console.ReadLine());
  16.  
  17. for (i = 2; i <= num; i += 2)
  18. {
  19. //Adding current even number to sum variable
  20. sum += i;
  21. }
  22. Console.WriteLine("Sum of all even number between 1 to " + num + " = "+ sum);
  23.  
  24. Console.ReadLine();
  25. }
  26. }

Result

C# language techstudy

     





Related Topics




Popular Posts