Advertisement
Yonka2019

Untitled

Apr 8th, 2021
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     private static void Main()
  6.     {
  7.         Random rnd = new Random();
  8.         int numOfNumbers = 1000;
  9.         int palindromeCount = 0;
  10.  
  11.         for (int i = 0; i < numOfNumbers; i++)
  12.         {
  13.             if (rnd.Next(100, 1000).IsPalindrome())
  14.             {
  15.                 palindromeCount++;
  16.             }
  17.         }
  18.         Console.WriteLine((100 * palindromeCount + numOfNumbers / 2) / numOfNumbers + "% Of palindromes");
  19.  
  20.         Console.ReadKey();
  21.     }
  22. }
  23. static class MethodExtensions
  24. {
  25.     public static bool IsPalindrome(this int randomized)
  26.     {
  27.         int r, sum = 0, temp;
  28.         temp = randomized;
  29.         while (randomized > 0)
  30.         {
  31.             r = randomized % 10;
  32.             sum = (sum * 10) + r;
  33.             randomized /= 10;
  34.         }
  35.         if (temp == sum)
  36.             return true;
  37.         else
  38.             return false;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement