Factorial nomli rekursiv funksiya tuzing. X sonini quyidagi formula yordamida hisoblovchi Formula


Download 53.48 Kb.
bet1/8
Sana16.06.2023
Hajmi53.48 Kb.
#1489108
  1   2   3   4   5   6   7   8
Bog'liq
algoritm 29-talik topshiriq javoblari


1-savol
N!! ni hisoblovchi butun tipli Factorial nomli rekursiv funksiya tuzing.
X sonini quyidagi formula yordamida hisoblovchi Formula nomli rekursiv funksiya tuzing:

2-savol
K sonining raqamlari yig`indisini hisoblovchi butun toifadagi Yig`indi nomli rekursiv funksiya tuzilsin.

class Program
{
static void Main(string[] args)
{
Console.Write("K sonini kiriting: ");
int k = Convert.ToInt32(Console.ReadLine());

int sum = yigindi(k);


Console.WriteLine("K sonining raqamlari yig'indisi: {0}", sum);


}

public static int yigindi(int k)


{
if (k == 0)
{
return 0;
}
else
{
return k % 10 + yigindi(k / 10);
}
}
}

3-savol
A va b butun sonlari berilgan. A dan b gacha bo`lgan sonlarning yig`indisini hisoblovchi Son nomli rekursiv funksiya tuzing.

class Program
{
static void Main(string[] args)
{
Console.Write("A ni kiriting: ");
int a = Convert.ToInt32(Console.ReadLine());

Console.Write("B ni kiriting: ");


int b = Convert.ToInt32(Console.ReadLine());

int sum = Sum(a, b);


Console.WriteLine("A dan B gacha sonlar yig'indisi: {0}", sum);


}

public static int Sum(int a, int b)


{
if (a == b)
{
return a;
}
else
{
return a + Sum(a + 1, b);
}
}
}

4-savol
N va A butun sonlar berilgan. A ning N-darajasini hisoblovchi Daraja nomli haqiqiy tipli rekursiv funksiya tuzing.


class Program


{
static void Main(string[] args)
{
Console.Write("A ni kiriting: ");
int a = Convert.ToInt32(Console.ReadLine());

Console.Write("N ni kiriting: ");


int n = Convert.ToInt32(Console.ReadLine());

double natija = daraja(a, n);


Console.WriteLine("{0} ning {1}-darajasi: {2}", a, n, natija);


}

public static double daraja(int a, int n)


{
if (n == 0)
{
return 1;
}
else if (n < 0)
{
return 1 / daraja(a, -n);
}
else if (n % 2 == 0)
{
double x = daraja(a, n / 2);
return x * x;
}
else
{
return a * daraja(a, n - 1);
}
}
}

5-savol
N butun soni berilgan. Quyidagi yig`indini hisoblovchi


Download 53.48 Kb.

Do'stlaringiz bilan baham:
  1   2   3   4   5   6   7   8




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling