|
-Laboratoriya. For takrorlash operatori
|
bet | 129/154 | Sana | 08.01.2024 | Hajmi | 5,29 Mb. | | #131939 |
Bog'liq Majmua10-Laboratoriya. For takrorlash operatori.
Misol1. Matnni 5 marta chiqarish
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EKUB
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("C# For takrorlash operatori: Iteratsiya {0}", i);
}
Console.ReadKey();
}
}
}
Misol2. Ko’rsatilgan n sonigacha bo’lgan natural sonlarni yig’indisini topish
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EKUB
{
class Program
{
static void Main(string[] args)
{
int n = 5,sum = 0;
for (int i=1; i<=n; i++)
{
// sum = sum + i;
sum += i;
}
Console.WriteLine("1 dan {0} gacha bo'lgan natural sonlar yig'indisi {1}", n, sum);
Console.ReadKey();
}
}
}
Misol3. Parametrli chiqarish
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EKUB
{
class Program
{
static void Main(string[] args)
{
for (int i = 0, j = 0; i + j <= 5; i++, j++)
{
Console.WriteLine("i = {0} and j = {1}", i, j);
}
Console.ReadKey();
}
}
}
Misol4. Fibonachi sonlarini toping: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EKUB
{
class Program
{
static void Main(string[] args)
{
int n, a = 0, b = 1, c, i;
Console.Write("Qaysi songacha topsin : ");
n = Int32.Parse(Console.ReadLine());
for (i = 0; i < n; i++)
{
if (i <= 1)
c = i;
else
{
c = a + b;
a = b;
b = c;
}
Console.Write(c + " ");
}
Console.ReadKey();
}
}
}
Misol5. Berilgan natural n va m uchun hisoblansin:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EKUB
{
class Program
{
static void Main(string[] args)
{
int n, m, s = 0, p;
n = Int32.Parse(Console.ReadLine());
m = Int32.Parse(Console.ReadLine());
for (int i = 1; i < n; i++)
{
p = 1;
for (int j = 1; j < m; j++)
{
p *= (i + j);
s += p;
Console.WriteLine("Har bir qadam uchun " + s);
}
}
Console.WriteLine("Yig'indi s=" + s);
Console.ReadKey();
}
}
}
Misol 6. while operatorida 10 gacha bo’lgan sonlarni chiqaring
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EKUB
{
class Program
{
static void Main(string[] args)
{
int i = 0; // dastlabki qiymat yuklash inisilizatsiya
while (i < 10) // shart
{
Console.WriteLine("i = {0}", i);
i++; // increment
}
Console.ReadKey();
}
}
}
Misol7. break operatoriga misol
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EKUB
{
class Program
{
static void Main(string[] args)
{
int i = 0;
while (true)
{
Console.WriteLine("i = {0}", i);
i++;
if (i > 10)
break;
}
Console.ReadKey();
}
}
}
Misol8. 2 ta sikl hosil qilish
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EKUB
{
class Program
{
static void Main(string[] args)
{
int i = 0, j = 1;
while (i < 2)
{
Console.WriteLine("i = {0}", i);
i++;
while (j < 2)
{
Console.WriteLine("j = {0}", j);
j++;
}
}
Console.ReadKey();
}
}
}
Misol9. Berilgan natural n va m uchun hisoblansin:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n, m, i = 2, j;
float p, s=0;
n = Int32.Parse(Console.ReadLine());
m = Int32.Parse(Console.ReadLine());
while (i <= n)
{
p = 1; j = 3;
while (j <= m)
{
p *= (i * i) / j;
j += 1;
}
s += p;
i += 1;
}
Console.WriteLine(s);
Console.ReadKey();
}
}
}
Misol10. 2 ta son uchun EKUB ni hisoblang
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n, m;
n = Int32.Parse(Console.ReadLine());
m = Int32.Parse(Console.ReadLine());
while (n != m)
{
if (n > m)
{
n = n - m;
}
else
{
m = m - n;
}
}
Console.WriteLine(n);
Console.ReadKey();
}
}
}
Boshqacha usulda
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n, m;
n = Int32.Parse(Console.ReadLine());
m = Int32.Parse(Console.ReadLine());
while (n != 0 && m != 0)
{
if (n > m)
{
n = n % m;
}
else
{
m = m % n;
}
}
Console.WriteLine(n + m);
Console.ReadKey();
}
}
}
Misol11. do while operatori yordamida 10 gacha bo’lgan natural sonlarni 5 ga ko’paytirishdan hosil bo’lgan sonlarni toping
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i = 1, n = 5, kupaytma;
do
{
kupaytma = n * i;
Console.WriteLine("{0} * {1} = {2}", n, i, kupaytma);
i++;
}
while (i <= 10);
Console.ReadKey();
}
}
}
|
| |