Advertisement
Dianov

Basic Syntax, Conditional Statements and Loops - Lab (09. Sum of Odd Numbers)

Mar 11th, 2021
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SumOfOddNumbers
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             int oddNumber = 1;
  15.             int sum = 0;
  16.             for (int i = 1; i <= n; i++)
  17.             {
  18.                 Console.WriteLine(oddNumber);
  19.                 sum += oddNumber;
  20.                 oddNumber += 2;
  21.             }
  22.             Console.WriteLine($"Sum: {sum}");
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement