Files
oop/labs/lab3_done/Comp/Program.cs
2025-09-30 14:26:54 +03:00

84 lines
2.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Globalization;
class Program
{
static void FuncWhile()
{
var ci = CultureInfo.InvariantCulture;
double x1 = double.Parse(Console.ReadLine(), ci);
double x2 = double.Parse(Console.ReadLine(), ci);
double h = double.Parse(Console.ReadLine(), ci);
double x = x1;
while (x <= x2 + 1e-12)
{
double y = Math.Sin(x);
Console.WriteLine($"while: {x.ToString(ci)} {y.ToString(ci)}");
x += h;
}
}
static void FuncDoWhile()
{
var ci = CultureInfo.InvariantCulture;
double x1 = double.Parse(Console.ReadLine(), ci);
double x2 = double.Parse(Console.ReadLine(), ci);
double h = double.Parse(Console.ReadLine(), ci);
double x = x1;
if (h <= 0) return;
do
{
double y = Math.Sin(x);
Console.WriteLine($"do-while: {x.ToString(ci)} {y.ToString(ci)}");
x += h;
} while (x - h < x2 + 1e-12);
}
static int GcdWhile(int a, int b)
{
a = Math.Abs(a);
b = Math.Abs(b);
while (b != 0)
{
int temp = a % b;
a = b;
b = temp;
}
return a;
}
static int GcdDoWhile(int a, int b)
{
a = Math.Abs(a);
b = Math.Abs(b);
int temp;
if (b == 0) return a;
do
{
temp = a % b;
a = b;
b = temp;
} while (b != 0);
return a;
}
static void Main()
{
Console.WriteLine("=== Функция с while ===");
FuncWhile();
Console.WriteLine("=== Функция с do-while ===");
FuncDoWhile();
Console.WriteLine("Введите два числа для НОД (while):");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
Console.WriteLine($"НОД (while): {GcdWhile(a, b)}");
Console.WriteLine("Введите два числа для НОД (do-while):");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
Console.WriteLine($"НОД (do-while): {GcdDoWhile(a, b)}");
}
}