C# LAB

41
.NET Laboratory[10MCA57] Page no: Program1: Write a program in C# to check whether a number is Palindrome or not. using System; using System.Collections.Generic; using System.Text; namespace q8 { class Program { static void Main(string[] args) { int n,m, i=0; Console.WriteLine(" enter the number"); m= n = int.Parse(Console.ReadLine()); while (n > 0) { int rem = n % 10; i = i * 10 + rem; n /= 10; } if (i == m) Console.WriteLine("{0} is palindrom no ",m); else Console.WriteLine("{0} is not palindrom no ",m); Console.ReadLine(); } } } Output: NAME: NAVEEN N USN:1OX12MCA57

Transcript of C# LAB

.NET Laboratory[10MCA57] Page no:Program1: Write a program in C# to check whether a number is Palindrome or not.

using System;using System.Collections.Generic;using System.Text;

namespace q8{ class Program { static void Main(string[] args) { int n,m, i=0; Console.WriteLine(" enter the number"); m= n = int.Parse(Console.ReadLine()); while (n > 0) { int rem = n % 10; i = i * 10 + rem; n /= 10; } if (i == m) Console.WriteLine("{0} is palindrom no ",m); else Console.WriteLine("{0} is not palindrom no ",m); Console.ReadLine(); } }}

Output:

.NET Laboratory[10MCA57] Page no:

NAME: NAVEEN N USN:1OX12MCA57Program 2a: Write a program in C# to demonstrate Command Line Arguments Processing. Steps to set command line arguments:Step 1: go to project menu -> select project properties optionStep 2: In project property window select debug tab.Step 3: In Debug tab set the argument list in Command line argument text box which we seeing in following window:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace q2{ class Program { static void Main(string[] args) { if (args.Length > 0) foreach (string var in args) Console.Write("\t " + var); else Console.WriteLine("argument is not passed !"); Console.Read(); } }}Output:

Program 2b: Write a Program in C# to find the sum of arguments passed.using System;using System.Collections.Generic;using System.Text;

namespace ConsoleApplication8{ class Program { static void Main(string[] args) { int sum=0; if (args.Length > 0) { Console.WriteLine(); for (int i =0;i 0) { Console.WriteLine("roots are real and distinct \n"); Console.WriteLine("roots : {0}", -b + Math.Sqrt(d) / (2 * a)); Console.WriteLine("roots : {0}", -b - Math.Sqrt(d) / (2 * 1)); } else if (d == 0) { Console.WriteLine("roots are equal \n"); Console.WriteLine("roots :{0}", -b / (2 * a)); } else Console.WriteLine("roots are imaginary \n"); } Console.ReadKey(); } }}

Output:

Program 4: Write a Program in C# to demonstrate Boxing and UnBoxing. using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace q4{ class Program { static void Main(string[] args) { int a = 10; object x ; Console.WriteLine("value of a={0}", a); x = a; Console.WriteLine("value of object x={0}", x); x = 100; a =(int) x; Console.WriteLine("value of object x={0}", x); Console.WriteLine("value of a={0}", a); Console.Read(); } }}Output:

Program 5: Write a Program in C# to implement Stack Operations.using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace stack_operation{ class Program { static void Main(string[] args) { stack_operation s1 = new stack_operation(); int ch; while (true) { Console.WriteLine("\n 1.Push \n 2.pop \n 3.display \n 4.exit \n"); Console.WriteLine("enter yout choice \n"); ch = int.Parse(Console.ReadLine()); switch (ch) { case 1: s1.push(); break; case 2: s1.pop(); break; case 3: s1.display(); break; case 4: System.Environment.Exit(0); break; default: Console.WriteLine("\n enter correct option \n"); break; } } } public class stack_operation { int top; int[] stk = new int[20]; public stack_operation() { top=-1; } public void push() { Console.WriteLine("enter new element to push \n"); int ele=int.Parse(Console.ReadLine()); if(top==4) Console.WriteLine("stack overflow \n"); else { top=top+1; stk[top]=ele; } }public void pop() { if(top==-1) Console.WriteLine("stack underflow \n"); else { Console.WriteLine("popped element is "+stk[top]); top=top-1; } } public void display() { if(top==-1) Console.WriteLine("no element is in stack"); else { Console.WriteLine("elements in stack are \n"); for(int i=0;i