0% found this document useful (0 votes)
20 views10 pages

Denn

The document contains code snippets demonstrating various C# programming concepts like: 1) Taking input from the user and performing calculations on the input like multiplication, finding prime numbers, Fibonacci series etc. 2) Defining classes and structures to store student details and reading/printing the details. 3) Implementing concepts like functions, loops, arrays, inheritance, constructors etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Denn

The document contains code snippets demonstrating various C# programming concepts like: 1) Taking input from the user and performing calculations on the input like multiplication, finding prime numbers, Fibonacci series etc. 2) Defining classes and structures to store student details and reading/printing the details. 3) Implementing concepts like functions, loops, arrays, inheritance, constructors etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

1) Product of two numbers

int n1, n2, n3, n4, ans;


Console.WriteLine("Enter Number 1:");
n1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Number 2:");
n2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Number 3:");
n3 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Number 4:");
n4 = Convert.ToInt32(Console.ReadLine());
ans = n1 * n2 * n3 * n4;
Console.WriteLine("Product Of Four Numbers: " + ans);
Console.ReadKey();

2)read details
public struct stud
{
public int id;
public string name;
public string course;
public string dob;
};
internal class Program
{
static void Main(string[] args)
{
stud[] s = new stud[2];
for (int i = 0; i < 2; i++)
{
Console.WriteLine("Enter Student id:");
s[i].id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Student name:");
s[i].name = Console.ReadLine();
Console.WriteLine("Enter Student course:");
s[i].course = Console.ReadLine();
Console.WriteLine("Enter Student dob:");
s[i].dob = Console.ReadLine();
}
for (int i = 0; i < 2; i++)
{
Console.WriteLine("ID:" + s[i].id);
Console.WriteLine("Name:" + s[i].name);
Console.WriteLine("Course:" + s[i].course);
Console.WriteLine("DOB:" + s[i].dob);
}
Console.ReadKey();
}
}

3)fibonacci
internal class Program
{
static void Main(string[] args)
{
int ni, f1, f2, fib;
Console.WriteLine("ENTER NO. OF ITERATIONS THAT YOU WANT");
ni = int.Parse(Console.ReadLine());
f1 = 0;
f2 = 1;
Console.WriteLine("\n" + f1);
Console.WriteLine("\n" + f2);
for (int i = 1; i <= ni; i++)
{
fib = f1 + f2;
f1 = f2;
f2 = fib;
Console.WriteLine("\n" + fib);
}
Console.ReadKey();
}
}

4)prime
internal class Program
{
static void Main(string[] args)
{
int i, no, j;
bool flag = true;
Console.Write("ENTER ANY NO:");
no = int.Parse(Console.ReadLine());
for (i = 2; i < no; i++)
{
for (j = 2; j <= no; j++)
{
if (i != j && i % j == 0)
{
flag = false;
break;
}
}
if (flag == true)
{
Console.WriteLine("\nThe prime numbers are:" + i);
}
flag = true;
}
Console.ReadKey();
}
}

5)vowel
internal class Program
{
static void Main(string[] args)
{
String s = "Hi GOOD Mornning";
Char[] letter = s.ToCharArray();
foreach (char c in letter)
{
string s1 = Vowel(c);
Console.WriteLine("{0}is{1}", c, s1);
}
Console.ReadKey();
}
public static string Vowel(char a)
{
switch (a)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'U':
case 'u':
return ("Vowel");
default:
return ("Not a Vowel");
}
}
}

6)foreach loop
internal class Program
{
static void Main(string[] args)
{
String[] item = { "CHRIS", "IPSII", "NIMII", "DHRUV", "SUMEDHA", "SAHIL"};
foreach (string i in item)
{
Console.WriteLine(i +"\n");
}
Console.ReadKey();
}
}

7) reverse and sum


internal class Program
{
static void Main(string[] args)
{
int no, r, rev, sum;
Console.WriteLine("ENTER ANY NO: ");
no = int.Parse(Console.ReadLine());
rev = 0;
sum = 0;
while (no != 0)
{
r = no % 10;
rev = (rev * 10) + r;
sum = sum + r;
no = no / 10;
}
Console.WriteLine("\n Reverse=" + rev);
Console.WriteLine("\n Sum=" + sum);
Console.ReadKey();
}
}

8)factorial
internal class Program
{
static void Main(string[] args)
{
int no, fact = 1;
Console.WriteLine("ENTER ANY NO:");
no = int.Parse(Console.ReadLine());
for (int i = 1; i <= no; i++)
{
fact = fact * i;
}
Console.WriteLine("\nFactorial of a number=" + fact);
Console.ReadKey();
}
}

9)money conversion
internal class Program
{
static void Main(string[] args)
{
string ans = " ";
do
{
Console.WriteLine("Enter currency in Rs");
int r = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Select ur choice to convert into 1.Dollars 2.Euro
3.pounds");
int n = Convert.ToInt32(Console.ReadLine());
double d = 0;
Console.WriteLine("Currency Converter");
switch (n)
{
case 1:
d = r / 68.5;
Console.WriteLine(r + "Rs.=" + d + "$");
break;
case 2:
d = r / 89.48;
Console.WriteLine(r + "Rs.=" + d + "pounds");
break;
case 3:
d = r / 79.64;
Console.WriteLine(r + "Rs.=" + d + "Euro");
break;
default:
Console.WriteLine("Invalid choice");
break;
}
Console.WriteLine("Do u want to continue???");
ans = Console.ReadLine();
}
while (ans != "n");
Console.ReadKey();
}
}

10)Quadratic
class QuadraticEquationSolver
{
static void Main()
{
Console.WriteLine("Quadratic Equation Solver");

// Get user input for coefficients


Console.Write("Enter coefficient a: ");
double a = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter coefficient b: ");


double b = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter coefficient c: ");


double c = Convert.ToDouble(Console.ReadLine());

// Calculate the discriminant


double discriminant = b * b - 4 * a * c;

// Check the discriminant for the nature of roots


if (discriminant > 0)
{
// Two distinct real roots
double root1 = (-b + Math.Sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.Sqrt(discriminant)) / (2 * a);
Console.WriteLine($"Roots are real and distinct: Root1 = {root1}, Root2
= {root2}");
}
else if (discriminant == 0)
{
// One real root (repeated)
double root = -b / (2 * a);
Console.WriteLine($"Roots are real and repeated: Root = {root}");
}
else
{
// Complex roots
double realPart = -b / (2 * a);
double imaginaryPart = Math.Sqrt(-discriminant) / (2 * a);
Console.WriteLine($"Roots are complex: Root1 = {realPart} +
{imaginaryPart}i, Root2 = {realPart} - {imaginaryPart}i");
}
Console.ReadLine(); }
}

11)temperature
static void Main(string[] args)
{
Console.WriteLine("Enter temperature in Fahrenheit ");
double F, C;
F = Convert.ToDouble(Console.ReadLine());
C = (F - 32) * 5 / 9;
Console.WriteLine("After conversion temperature in Celsius={0}", C);
Console.ReadKey();
}
}

12)function overloading
internal class Program1
{
internal class Program
{
public float Area(float x)
{
return (x * x);
}
public int Area(int l, int h)
{
return (l * h);
}
public double Area(double r)
{
return (3.14 * r * r);
}
public float Area(float len, float hyt)
{
return (0.5F * len * hyt);
}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine("Area of Square =" + p.Area(10));
Console.WriteLine("Area of Rectangle =" + p.Area(4, 5));
Console.WriteLine("Area of Circle =" + p.Area(4.0));
Console.WriteLine("Area of Triangle=" + p.Area(1.2F, 2.3F));
Console.ReadKey();
}
}
}

14)inheritance
internal class Program
{
class A
{
public int a;
public A(int x)
{
a = x;
}
public void ShowA()
{
Console.WriteLine("A Show A= " + a);
}
}
class B : A
{
int v;
public B(int z, int y) : base(z)
{
v = y;
}
public void ShowB()
{
Console.WriteLine("B Show B= " + v);
}
}
class C : A
{
int n;
public C(int x, int m) : base(x)
{
n = m;
}
public void ShowC()
{
Console.WriteLine("C Show C= " + n);
}
}
class D : B
{
int d;
public D(int x, int y, int z) : base(x, y)
{
d = z;
}
public void ShowD()
{
Console.WriteLine("D Show D= " + d);
}
}
static void Main(string[] args)
{
C c1 = new C(5, 9);
D d1 = new D(4, 2, 3);
c1.ShowA();
c1.ShowC();
d1.ShowA();
d1.ShowB();
d1.ShowD();
Console.ReadKey();
}
}

15)constructor
internal class Demo
{
class Program
{
int x, y;
public Program()
{
x = y = 0;
}
static Program()
{
Console.WriteLine("Static Constructor");
}
public Program(int a)
{
x = y = a;
}
public Program(int a, int b)
{
x = a;
y = b;
}
public Program(Program p)
{
x = p.x;
y = p.y;
}
public void Display()
{
Console.WriteLine(" " + x);
Console.WriteLine(" " + y);
}
static void Main(string[] args)
{
Program p1 = new Program();
Program p2 = new Program(9);
Program p3 = new Program(6, 3);
Program p4 = new Program(p3);
Console.WriteLine("Default constructor");
p1.Display();
Console.WriteLine("Single parameterized constructor");
p2.Display();
Console.WriteLine("Parameterized constructor");
p3.Display();
Console.WriteLine("Copy constructor");
p4.Display();
Console.ReadKey();
}
}
}

16)single inheritance
interface Addition
{
int Add();
}
interface Multiplication
{
int Mul();
}
class A : Addition, Multiplication
{
public int x, y;
public A(int a, int b)
{
x = a;
y = b;
Console.WriteLine("1ST NO:" + x);
Console.WriteLine("2ND NO:" + y);
}
public int Add()
{
return (x + y);
}
public int Mul()
{
return (x * y);
}
}
class MultipleInterface
{
static void Main(string[] args)
{
A a1 = new A(10, 20);
Addition obj = (Addition)a1;
Multiplication obj1 = (Multiplication)a1;
Console.WriteLine("Addition=" + obj.Add());
Console.WriteLine("Multiplication=" + obj1.Mul());
Console.ReadKey();
}
}

17)delegates events
public delegate void EventHandler(string a);
public class Operation
{
public event EventHandler xyz;
public void Action(string a)
{
if (xyz != null)
{
xyz(a);
Console.WriteLine(a);
}
else
{
Console.WriteLine("Not Registered");
}
}
}
class Program
{
public static void CatchEvent(string s)
{
Console.WriteLine("Method Calling");
}
static void Main(string[] args)
{
Operation o = new Operation();
o.xyz += new EventHandler(CatchEvent);
o.Action("Event Calling");
Console.ReadLine();
}
}

18)exception handling
class Program
{
static void Main(string[] args)
{
int a = 10, b = 5, c = 5;
int x, y;
try
{
x = a / (b - c);
}
catch (Exception e)
{
Console.WriteLine("Divide by zero");
}
y = a / (b + c);
int[] d = { 5, 10 };
int f = 5;
try
{
int g = d[2] / f - d[1];
}
catch (ArithmeticException e)
{
Console.WriteLine("Divide by zero");
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("Array index error");
}
try
{
Division();
}
catch (DivideByZeroException e)
{
Console.WriteLine("Caught exception inside main");
}
finally
{
Console.WriteLine("Inside main");
}
Console.ReadKey();
}
static int m = 10;
static int n = 0;
static void Division()
{
try
{
int k = m / n;
}
catch (ArgumentException e)
{
Console.WriteLine("Caught exception inside main");
}
finally
{
Console.WriteLine("Inside division");
}
}
}

You might also like