Csharp Lab Programs 1. Write A Program in C# To Check Whether A Number Is Palindrome or Not

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

BIT CSHARP LAB PROGRAMS

1BI08MCA28

1. Write a program in c# to check whether a number is palindrome or not. using System; using System.Collections.Generic; using System.Text; namespace ClassLibrary1 { public class Reverse { public static void Main(string[] arg) { int num, temp = 0; double rev = 0; Console.WriteLine("Enter a number :"); num = int.Parse(Console.ReadLine()); int num1 = num; do { temp = num % 10; rev = temp + rev * 10; num = num / 10; } while (num != 0); Console.WriteLine("The reversal number is : " + rev); Console.ReadLine(); if (rev == num1) Console.WriteLine("The Number is Palindrome"); else Console.WriteLine("The Number is Not a Palindrome"); Console.ReadLine(); } } }

BIT

1BI08MCA28

2. Write a program in c# to demonstrate command line argument processing. using System; using System.Collections.Generic; using System.Text; class CommandLine { public static void Main(String[] args) { int sum = 0, n, num; for (n = 0; n < args.Length; n++) { num = int.Parse(args[n]); sum = sum + num; } Console.WriteLine("The sum is:" + sum); } }

BIT

1BI08MCA28

3. Write a program in c# to find the roots of quadratic equation. using System; class roots { public static void Main() { int b, a, c; double d, r1, r2; Console.WriteLine("Enter the value of a,b,c:"); a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); c = int.Parse(Console.ReadLine()); d = Math.Sqrt(b * b - 4 * a * c); if (d == 0) { r1 = r2 = -b / (2 * a); Console.WriteLine("The roots are " + r1 + " and " + r2); Console.ReadLine(); } else if (d > 0) { r1 = (-b + d) / (2 * a); r2 = (-b - d) / (2 * a); Console.WriteLine("The roots are " + r1 + " and " + r2); Console.ReadLine(); } else { Console.WriteLine("Imaginary roots exist"); Console.ReadLine(); } } }

BIT

1BI08MCA28

4. Write a program to demonstrate boxing and unboxing. using System; using System.Collections.Generic; using System.Text; namespace BoxingUnboxing { class BoxUnBox { static void Main() { Console.WriteLine("\nBoxing :\n"); int i = 123; Console.WriteLine("Value Type (i) = (0)", i); Console.WriteLine("\nAssigning value type to object type (obj=i) "); object obj = i; // Implicit boxing Console.WriteLine("\nThe value-type (i) value = {0}", i); Console.WriteLine("\nThe object-type (obj) value = {0}", obj); Console.ReadLine(); Console.WriteLine("\nUnBoxing :\n"); obj = 456; Console.WriteLine("Object Type (obj) = {0}", obj); int j = (int)obj; Console.WriteLine("\nAssigning object type to value type (j=obj) "); Console.WriteLine("\nThe value-type (j) value = {0}", j); Console.WriteLine("\nThe object-type (obj) value = {0}", obj); Console.ReadLine(); } } }

BIT

1BI08MCA28

5.Write a program in c# to implement stack operations. using System; using System.Collections.Generic; using System.Text; namespace stack { class Program { static void Main() { int ch,n=0,i,top=-1; Console.Write("\nEnter the size :"); n = int.Parse(Console.ReadLine()); int[] a = new int[n]; do { Console.WriteLine("\nEnter choice"); Console.Write("\n1 for push \n2 for pop \n3 for Display\n4 for exit ch=int.Parse(Console.ReadLine()); switch(ch) { case 1: if(top==n-1) { Console.WriteLine("\nOVERFLOW"); break; } else { Console.Write("\nEnter the element for stack : "); int val = int.Parse(Console.ReadLine()); top++; a[top]=val; } break; case 2: if(top==-1) { Console.WriteLine("\nUNDERFLOW\n");

: ");

BIT break; } else { int valu=a[top]; top--; Console.WriteLine("The pop element is {0}", valu); } break; case 3: if (top == -1) { Console.WriteLine("\nNo Element in stack\n"); } else { Console.WriteLine("\nThe stack element are :"); for (i = top; i>=0 ; i--) { Console.WriteLine("{0} ", a[i]); } } break; case 4: break; default: Console.WriteLine("\nInvalid choice"); break; } }while(ch!=4); } } }

1BI08MCA28

BIT

1BI08MCA28

6.Program to demonstrate operator overloading using System; namespace OperatorOverloading { class Complex { double x; //real part double y; //imaginary part public void read() { Console.Write("\n Enter real part : "); x = double.Parse(Console.ReadLine()); Console.Write("\n Enter imaginary part : "); y = double.Parse(Console.ReadLine()); } public static Complex operator +(Complex c1, Complex c2) { Complex c = new Complex(); c.x = c1.x + c2.x; c.y = c1.y + c2.y; return (c); } public static Complex operator -(Complex c1, Complex c2) { Complex c = new Complex(); c.x = c1.x - c2.x; c.y = c1.y - c2.y; return (c); } public void Display() { Console.WriteLine("(" + x + ")" + " + i" + "(" + y + ")"); } } class OverloadingTest { public static void Main() { Complex a = new Complex(); Console.WriteLine("\n a (Complex number1) : "); a.read(); Complex b = new Complex();

BIT Console.WriteLine("\n\n b (Complex number2) : "); b.read(); Complex c = a + b; Complex d = b - a; Console.Write("\n\n a = "); a.Display(); Console.Write("\n\n b = "); b.Display(); Console.Write("\n\n c = a + b : "); c.Display(); Console.Write("\n\n d = b - a : "); d.Display(); Console.ReadLine(); } } }

1BI08MCA28

BIT

1BI08MCA28

7. Write a program to find first and second largest elements in a single dimensional array. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { int i, j; Console.WriteLine("\nEnter how many number :"); int n = int.Parse(Console.ReadLine()); int[] a = new int[n]; Console.WriteLine("Enter {0} Numbers to list :", n); for (i = 0; i < n; i++) a[i] = int.Parse(Console.ReadLine()); int sbig, big; if (a[0] > a[1]) { big = a[0]; sbig = a[1]; } else { sbig = a[0]; big = a[1]; } for (i = 2; i < n; i++) { if (a[i] > sbig) { if (a[i] > big) { sbig = big; big = a[i]; } else sbig = a[i]; }

BIT Console.WriteLine("\nThe list is "); for (i = 0; i < n; i++) Console.Write("{0} ", a[i]); Console.WriteLine("\n\nThe first large no in the list is {0}", big); Console.WriteLine("\n\nThe second large no in the list is {0}", sbig); Console.ReadLine(); } } } 10. write a prog to reverse the string. using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Console.WriteLine("Enter a String : "); string str = Console.ReadLine(); int len = str.Length; char[] arr = new char[len]; for (int i = 0; i < len; i++) { arr[i] = str[len - 1 - i]; } Console.WriteLine("Reverse of a string is = "); for (int i = 0; i < len; i++) { Console.Write(arr[i]); } Console.ReadLine(); } }

1BI08MCA28

BIT

1BI08MCA28

12. Simple calculator using switch case. using System; using System.Collections.Generic; using System.Text; namespace prasanna { class calculator { static void Main(string[] args) { int ch=0; do { Console.WriteLine("\nEnter your choice :"); Console.Write(" 1 for Addition \n 2 for subtraction \n 3 for multiplication \n 4 for division \n 5 for Exit: "); ch = int.Parse(Console.ReadLine()); switch (ch) { case 1: Console.WriteLine("Enter any two numbers :"); float a = float.Parse(Console.ReadLine()); float b = float.Parse(Console.ReadLine()); float c = a + b; Console.WriteLine("\nThe sum of {0} and {1} is : {2}\n", a, b, c); break; case 2: Console.WriteLine("Enter any two numbers :"); a = float.Parse(Console.ReadLine()); b = float.Parse(Console.ReadLine()); c = a - b; Console.WriteLine("\nThe subtraction of {0} and {1} is : {2}\n", a, b, c); break; case 3: Console.WriteLine("Enter any two numbers :"); a = float.Parse(Console.ReadLine()); b = float.Parse(Console.ReadLine()); c = a * b; Console.WriteLine("\nThe multiplication of {0} and {1} is : {2}\n", a, b, c); break; case 4:

BIT Console.WriteLine("Enter any two numbers :"); a = float.Parse(Console.ReadLine()); b = float.Parse(Console.ReadLine()); if (b != 0) { c = a / b; Console.WriteLine("\nThe sum of {0} and {1} is : {2}\n", a, b, c); } else Console.WriteLine("Division is not posiible "); break; case 5: break; default: Console.WriteLine("Invalid Choice... "); break; } } while (ch != 5); } } }

1BI08MCA28

You might also like