0% found this document useful (0 votes)
13 views

C#Lab5 7

C#lab program
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

C#Lab5 7

C#lab program
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

******************************************************************

Program 5: Write a C#program to check a number if it is Prime;otherwise display


the factor of that number.
******************************************************************

using System;
class Prime
{
static void Main(string[] args)
{
int n, i, m = 0, flag = 0;
Console.Write("Enter the Number to check Prime: ");
n = int.Parse(Console.ReadLine());
m = n / 2;
for (i = 2; i <= m; i++)
{
if (n % i == 0)
{
if (flag == 0)
{
Console.WriteLine("Number is not Prime. ");
Console.WriteLine("The factors are: ");
}

Console.WriteLine(i);
flag = 1;

}
if (flag == 0)
Console.Write("Number is Prime.");
Console.ReadLine();

}
}
Output:

Enter the Number to check Prime: 7

Number is Prime.

Enter the Number to check Prime: 8

Number is not Prime.

The factors are:

4
******************************************************************
Program 6: Wtite a C# program define a class Salary which contain member
variable Emp_no,Emp_name,Dob,Basic write a program using constructor and a
method.
******************************************************************
using System;

class Salary
{
int Emp_No, Basic;
string Emp_name,Dob;
double da, hra, pf, gross, net, pt;
public Salary(int empno, string Empname, string dob, int basic)
{
this.Emp_No = empno;
this.Emp_name = Empname;
this.Dob = dob;
this.Basic = basic;
}
public void calculate()
{
if (Basic <= 20000)
{
da = Basic * 0.4;
hra = Basic * 0.1;
gross = Basic + da + hra;
pf = gross * 0.12;
pt = 100;
net = gross - pf - pt;
}
else
{
da = Basic * 0.5;
hra = Basic * 0.15;
gross = Basic + da + hra;
pf = gross * 0.12;
pt = 150;
net = gross - pf - pt;
}
Console.WriteLine("Employee Number : {0}", Emp_No);
Console.WriteLine("Employee Name : {0}", Emp_name);
Console.WriteLine("Employee DOB : {0}", Dob);
Console.WriteLine("Employee Basic Salary :{0}", Basic);
Console.WriteLine("DA : {0}", da);
Console.WriteLine("HRA : {0}", hra);
Console.WriteLine("PF : {0}", pf);
Console.WriteLine("PT : {0}", pt);
Console.WriteLine("Gross Salary : {0}", gross);
Console.WriteLine("Net Salary : {0}", net);

}
}
class Employee
{
static void Main(string[] args)
{
int empno, basic;
string Empname,dob;
Console.WriteLine("Enter the Employee Number");
empno = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the Employee Name");
Empname = Console.ReadLine();
Console.WriteLine("Enter the Employee DOB(dd/mm/yyyy");
dob = Console.ReadLine();
Console.WriteLine("Enter the Employee BasicSalary");
basic = Convert.ToInt32(Console.ReadLine());
Salary Emp = new Salary(empno, Empname, dob, basic);
Emp.calculate();
Console.ReadLine();
}
}
Output:

Enter the Employee Number


12
Enter the Employee Name
Sandesh
Enter the Employee DOB(dd/mm/yyyy
23/3/1990
Enter the Employee BasicSalary
15000
Employee Number : 12
Employee Name : Sandesh
Employee DOB : 23/3/1990
Employee Basic Salary :15000
DA : 6000
HRA : 1500
PF : 2700
PT : 100
Gross Salary : 22500
Net Salary : 19700
******************************************************************
Program 7:Write a program in C# to find addition and multiplication operation on
two complex number using operator overloading.
******************************************************************

using System;

class Complex
{
public int real;
public int img;
public Complex()
{
real = 0;
img = 0;
}

public Complex(int real, int img)


{
this.real = real;
this.img = img;
}

public static Complex operator +(Complex Ob1, Complex Ob2)


{
Complex temp = new Complex();
temp.real = Ob1.real + Ob2.real;
temp.img = Ob1.img + Ob2.img;

return temp;
}

public static Complex operator *(Complex Ob1, Complex Ob2)


{
Complex temp = new Complex();
/* if (a,b)(c,d) then formula of multiplication
is (ac-bd,ad+bc) */
temp.real = (Ob1.real*Ob2.real)-(Ob1.img*Ob2.img);
temp.img = (Ob1.real*Ob2.img)+(Ob1.img*Ob2.real);

return temp;
}

public void PrintComplexNumber()


{
Console.WriteLine("{0} + {1}i", real, img);
}
}

class Program
{
static void Main(string[] args)
{
Complex C1 = new Complex(4, 2);
Complex C2 = new Complex(2, 3);
Complex C3,C4;

C3 = C1 + C2;
C4 = C1 * C2;

Console.Write("C1 : ");
C1.PrintComplexNumber();

Console.Write("C2 : ");
C2.PrintComplexNumber();
Console.WriteLine("The addition of two complex numbers");
Console.Write("C3 : ");
C3.PrintComplexNumber();
Console.WriteLine("The multiplication of two complex numbers");
Console.Write("C4 : ");
C4.PrintComplexNumber();
Console.ReadLine();

}
}
Output:

C1 : 4 + 2i

C2 : 2 + 3i

The addition of two complex numbers

C3 : 6 + 5i

The multiplication of two complex numbers

C4 : 2 + 16i

You might also like