C# static class, method and constructor
C# PROGRAMMING EXAMPLES |
1. Write a C# program that takes two numbers and display the
product of two numbers.
class Program
{
static void
Main(string[ ] args)
{
int a,b,pro;
Console.WriteLine ("input first number=");
a =
int.Parse (Console.ReadLine ( ));
Console.WriteLine ("input second number=");
b =
int.Parse (Console.ReadLine ( ));
pro=a*b;
Console.Write
("product of two number= {0}",pro);
Console.ReadKey ( );
}
}
2. Write a C# program that takes three numbers to calculate and
print the average of the numbers
class Program
{
static void
Main(string[ ] args)
{
int a,b,c,sum;
double
average;
Console.WriteLine ("input first number=");
a =
int.Parse (Console.ReadLine ( ));
Console.WriteLine ("input second number=");
b =
int.Parse (Console.ReadLine ( ));
Console.WriteLine ("input third number=");
c =
int.Parse (Console.ReadLine ( ));
int sum
= num1 + num2 + num3;
average
= sum / 3.0;
Console.Write ("Average of three numbers = {0}", average);
Console.ReadKey ( );
}
}
3. Write a C# program to compute the area of a hexagon.
Area of a hexagon = (6 *
s^2)/(4*tan(Ï€/6))
Where s is the length of a side.
class Program
{
static void
Main(string[] args)
{
int s;
double
area;
Console.WriteLine ("input the length of a side of hexagon=");
s =
int.Parse (Console.ReadLine ( ));
area =
(6 * Math.Pow(s, 2)) / (4 * Math.Tan(Math.PI / 6));
Console.Write ("Area of hexagon = {0}", area);
Console.ReadKey ( );
}
}
4. Write a C# program to compute the area of a polygon.
Area of a polygon = (n*s^2)/(4*tan(Ï€/n))
Where n is n-sided polygon and s is the length of a
side.
class Program
{
static void
Main(string[] args)
{
int s,n;
double
area;
Console.WriteLine
("input the number of side of polygon=");
n = int.Parse
(Console.ReadLine ( ));
Console.WriteLine
("input the length of a side of polygon=");
s = int.Parse
(Console.ReadLine ( ));
area =
(n * Math.Pow(s, 2)) / (4 * Math.Tan(Math.PI / n));
Console.Write
("Area of hexagon = {0}", area);
Console.ReadKey
( );
}
}
5. Write a C# program to compute the distance between two points
on the surface of earth.
Distance between the two points [ (x1,y1) &
(x2,y2)]
d = radius * arccos(sin(x1) * sin(x2) + cos(x1) *
cos(x2) * cos(y1 - y2))
Radius of the earth r = 6371.01 Kilometers
namespace distance
{
class Program
{
static void
Main(string[] args)
{
int x1, x2, y1,, y2;
double
d,r=6371.01;
Console.WriteLine("input x1=");
x1 =
int.Parse(Console.ReadLine( ));
Console.WriteLine("input x2=");
x2 =
int.Parse(Console.ReadLine( ));
Console.WriteLine("input y1=");
y1 =
int.Parse(Console.ReadLine( ));
Console.WriteLine("input
y2=");
y2 =
int.Parse(Console.ReadLine( ));
d = r *
Math.Acos(Math.Sin(x1) * Math.Sin(x2) + Math.Cos(x1) * Math.Cos(x2) *
Math.Cos(y1
- y2));
Console.Write("distance between two point ={0}km", d);
Console.ReadKey( );
}
}
6. Write a C# program that accepts an integer (n) and computes
the value of n+nn+nnn.
class Program
{
static void
Main(string[] args)
{
int n;
Console.Write("input
an integer number=");
n =
int.Parse(Console.ReadLine( ));
a= n + Math.Pow(n, 2) + Math.Pow(n, 3);
Console.Write("result={0}",a);
Console.ReadKey( );
}
}
7. Write a C# program to accept a number and check the number is
even or not. Prints 1 if the number is even or 0 if the number is odd.
class Program
{
static void
Main(string[] args)
{
int n;
Console.Write("Enter
integer number=");
int n =
int.Parse(Console.ReadLine( ));
if (n %
2 == 0)
Console.Write("1");
else
Console.Write("0");
Console.ReadKey( );
}
}
8. Write a C# program that accepts three integers from the user
and return true if the second number is greater than first number and third
number is greater than second number. If "abc" is true second number
does not need to be greater than first number.
public class Evaluate
{
public bool
Great(int x, int y, int z)
{
if (y
> x && z > y)
return
true;
else
return
false;
}
}
class Program
{
static void
Main(string[] args)
{
Int a,b,c;
Console.Write("enter first integer=");
a = int.Parse(Console.ReadLine(
));
Console.Write("enter second integer=");
b = int.Parse(Console.ReadLine(
));
Console.Write("enter third integer=");
c = int.Parse(Console.ReadLine(
));
Evaluate
obj = new Evaluate( );
Console.Write(obj.Great(a, b, c));
Console.ReadKey( );
}
}
9. Write a C# program that accepts three integers from the user
and return true if two or more of them (integers) have the same rightmost
digit. The integers are non-negative.
public class Evaluate
{
public bool
Great(int x, int y, int z)
{
int d,
e, f;
d = x %
10;
e = y %
10;
f = z %
10;
if (d==e
|| d==f || e==f)
return
true;
else
return
false;
}
}
class Program
{
static void
Main(string[] args)
{
int a,b,c;
Console.Write("enter first
integer=");
a = int.Parse(Console.ReadLine( ));
Console.Write("enter second integer=");
b = int.Parse(Console.ReadLine(
));
Console.Write("enter third
integer=");
c = int.Parse(Console.ReadLine(
));
Evaluate
obj = new Evaluate( );
Console.Write(obj.Great(a, b, c));
Console.ReadKey( );
}
}
10. Write a C# program to find the number of integers within the
range of two specified numbers and that are divisible by another number.
class Program
{
static void
Main(string[] args)
{
int a,b,c;
Console.Write("Enter range begin
integer number =");
a = int.Parse(Console.ReadLine(
));
Console.Write("Enter range ending
integer number =");
b = int.Parse(Console.ReadLine(
));
Console.Write("Enter divisor number =");
c = int.Parse(Console.ReadLine(
));
for( i=a;
i<=b; i++)
{
if
(i % c == 0)
Console.WriteLine("
{0}", i);
}
Console.ReadKey( );
}
}
11. Write a C# program that accepts two integer values from the
user and return the larger values. However if the two values are the same,
return 0 and return the smaller value if the two values have the same remainder
when divided by 6.
public class Evaluate
{
public int
Great(int y, int z)
{
if (y ==
z)
return
0;
else
{
if (y
% 6 == z % 6)
{
if
(y < z)
return y;
else
return z;
}
else if (y > z)
return
y;
else
return
z;
}
}
}
class Program
{
static void
Main(string[] args)
{
int a,b;
Console.Write("Enter first integer =");
a = int.Parse(Console.ReadLine(
));
Console.Write("Enter second integer =");
b = int.Parse(Console.ReadLine(
));
Evaluate
obj = new Evaluate( );
Console.Write(obj.Great(a, b));
Console.ReadKey( );
}
}
12. Write a C# program that accepts two integer values between 25
to 75 and return true if there is a common digit in both numbers.
public class Evaluate
{
public bool
Comon(int y, int z)
{
int a,
b, c, d;
a = y %
10;
b = y /
10;
c = z %
10;
d = z /
10;
if (a ==
c || a == d || b == c || b == d)
return
true;
else
return
false;
}
}
class Program
{
static void Main(string[] args)
{
int a,b;
Console.Write("Enter 1st integer =");
a = int.Parse(Console.ReadLine(
));
Console.Write("Enter 2nd integer =");
c = int.Parse(Console.ReadLine(
));
Evaluate obj = new Evaluate( );
if ((a
>= 25 && a <= 75) && (b >= 25 && b <= 75))
{
Console.Write(obj.Comon(a,
b));
}
Console.ReadKey( );
}
}
13. Write a C# program to calculate the modules of two numbers
without using any built in modulus operator.
class Program
{
static void
Main(string[] args)
{
int a,b,c,d, modules ;
Console.Write("Enter dividend number=");
a = int.Parse(Console.ReadLine(
));
Console.Write("Enter divisor number =");
b = int.Parse(Console.ReadLine(
));
c = a / b;
d = c * b;
modules = a - d;
Console.Write("desired result ={0}",
modules);
Console.ReadKey( );
}
}
}
great job sir
ReplyDelete