Advertisement
Dianov

Basic Syntax, Conditional Statements and Loops - Exercise (04. Print and sum)

Mar 26th, 2021
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.76 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 PrintAndSum
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int number1 = int.Parse(Console.ReadLine());
  14.             int number2 = int.Parse(Console.ReadLine());
  15.             int sum = 0;
  16.  
  17.             for (int i = number1; i <= number2; i++)
  18.             {
  19.                 if (i == number2)
  20.                 {
  21.                     Console.WriteLine($"{i}");
  22.                 }
  23.                 else
  24.                 {
  25.                     Console.Write($"{i} ");
  26.                 }
  27.                 sum += i;
  28.             }
  29.             Console.WriteLine($"Sum: {sum}");
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement