Venciity

[Jumpstart C#] loops demo - product N...M

Jan 24th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.48 KB | None | 0 0
  1. using System;
  2.  
  3. class Product
  4. {
  5.     static void Main()
  6.     {
  7.         Console.Write("n = ");
  8.         int n = Int32.Parse(Console.ReadLine());
  9.         Console.Write("m = ");
  10.         int m = Int32.Parse(Console.ReadLine());
  11.  
  12.         if (n < m)
  13.         {
  14.             int num = n;
  15.             decimal product = 1;
  16.             do
  17.             {      
  18.                 product *= num;
  19.                 num++;
  20.             }
  21.             while(num <= m);
  22.  
  23.             Console.WriteLine("product[n..m] = " + product);
  24.         }
  25.         else
  26.         {
  27.             Console.WriteLine("Error: n should be smaller than m.");
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment