VelizarAvramov

08. Sum of Odd Numbers

Jul 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.55 KB | None | 0 0
  1. Write a program that prints the next n odd numbers (starting from 1) and on the last row prints the sum of them.
  2. using System;
  3.  
  4. namespace _08._Sum_of_Odd_Numbers
  5. {
  6.     class SumOfOddNumbers
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int n = int.Parse(Console.ReadLine());
  11.  
  12.             int sum = 0;
  13.             for (int i = 1; i <= n; i++)
  14.             {
  15.                 Console.WriteLine("{0}", 2 * i - 1);
  16.                 sum += 2 * i - 1;
  17.             }
  18.             Console.WriteLine($"Sum: {sum}");
  19.         }
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment