Mavzuga doir bir nechta misollar:
N haqiqiy toifadagi son berilsa, uning butun va kasr qismini ajratuvchi dastur tuzing.
C#
|
class Decompose
{
public int parts(double n, out double frac) {
int whole;
whole = (int) n;
frac = n - whole;
return whole;
}
}
class Program
{
static void Main(string[] args)
{
Decompose ob = new Decompose();
int i;
double f;
double n = double.Parse(Console.ReadLine());
i = ob.parts(n, out f);
Console.WriteLine("Sonning butun qismi " + i);
Console.WriteLine("Sonning kasr qismi" + f);
Console.ReadKey();
}
}
|
Ref parametridan foydalangan holda ixtiyoriy butun sonning kvadratini hisoblovchi dastur tuzing.
C#
|
class RefTest
{
public void sqr(ref int i) {
i = i * i;
}
}
class Program
{
static void Main(string[] args)
{
RefTest ob = new RefTest();
int a = int.Parse(Console.ReadLine());
Console.WriteLine("a dastur boshida " + a);
ob.sqr(ref a);
Console.WriteLine("a dastur so'ngida " + a);
Console.ReadKey();
}
}
|
Haqiqiy toifadagi soni berilgan. shu sonning yarmini hisoblovchi dastur tuzing. Dasturda ref parametridan foydalaning.
C#
|
class refexam
{
public void show(ref double x)
{
x = x / 2;
}
}
class Program
{
static void Main(string[] args)
{
refexam obj = new refexam();
double a = double.Parse(Console.ReadLine());
Console.WriteLine("before call a :" + a);// this will display 35
obj.show(ref a);
Console.WriteLine("after call a :" + a);// this will display 17
Console.ReadKey();
}
}
|
Butun toifadagi a soni kiritilsin. Kiritilgan a sonini, uning yarmini butun qismini va a sonining 2 ga bo’lganddagi qoldiqini chiqaruvchi dastur tuzing. Dasturda ref va out parametrlaridan foydalaning.
C#
|
class refexam
{
public void show(ref int x, out int y)
{
y = x % 2;
x = x / 2;
}
}
class Program
{
static void Main(string[] args)
{
int a=int.Parse(Console.ReadLine());
int reminder;
refexam obj = new refexam();
Console.WriteLine("dastlabki a :" + a);
obj.show(ref a, out reminder);
Console.WriteLine("keyingi a :" + a);
Console.WriteLine("dastlabki a ni keyingi a ga bo'lgandagi qoldiq :" + reminder);
Console.ReadKey();
}
}
|
Do'stlaringiz bilan baham: |