NMDanny

Untitled

Apr 29th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 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 Root
  8. {
  9.     class Program
  10.     {
  11.         public static bool RationalRoot(int num)
  12.         {
  13.             int sum = 0, n = 1;
  14.             while (sum < num)
  15.             {
  16.                 sum += n;
  17.                 n += 2;
  18.             }
  19.             return sum==num;
  20.         }
  21.         public static int HaveRootCount()
  22.         {
  23.             int count = 0;
  24.             int input = -1;
  25.             for (int i=0;i<4;i++)
  26.             {
  27.                 while (input <= 0) // בדיקת קלט
  28.                 {
  29.                     Console.WriteLine("Type a positive number.");
  30.                     input = int.Parse(Console.ReadLine());
  31.                 }
  32.                 if (RationalRoot(input))
  33.                 {
  34.                     count++;
  35.                 }
  36.                 input = -1;
  37.             }
  38.  
  39.             return count;
  40.         }
  41.         static void Main(string[] args)
  42.         {
  43.             int count = HaveRootCount();
  44.             Console.WriteLine("{0} of the inputted numbers have a natural number root.",count);
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment