skipter

C# Sum of Odd Numbers

Jun 1st, 2017
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.63 KB | None | 0 0
  1. using System;
  2. namespace ConsoleApp8
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             int num = int.Parse(Console.ReadLine());
  9.             int sum = 0;
  10.             int counter = 0;
  11.  
  12.             for (int i = 1; ; i++)
  13.             {
  14.                 if (i % 2 == 1)
  15.                 {
  16.                     sum += i;
  17.                     counter++;
  18.                     Console.WriteLine(i);
  19.                 }
  20.                 if (counter == num)
  21.                 {
  22.                     break;
  23.                 }
  24.              }
  25.             Console.WriteLine("Sum: " + sum);
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment