Advertisement
Danielos168

rekurencja

Jan 10th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1. namespace ConsoleApp2
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             int liczba = 33222;
  8.             string tekst = Convert.ToString(liczba);
  9.             Console.WriteLine(roznica(tekst));
  10.             Console.ReadKey();
  11.         }
  12.  
  13.         static int roznica(string tekst, int i=0)
  14.         {
  15.             if (i==tekst.Length)
  16.             {
  17.                 return 0;
  18.             }
  19.  
  20.             if (tekst[i] % 2 !=0)
  21.             {
  22.                 return 1 + roznica(tekst, i + 1);
  23.             }
  24.  
  25.             if (tekst[i] % 2 ==0)
  26.             {
  27.                 return roznica(tekst, i + 1)-1;
  28.             }
  29.  
  30.             return roznica(tekst, i + 1);
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement