Write A Program in C# To Check Whether A Number Is Palindrome or Not
The document contains source code for 7 C# programs assigned as exercises in a .NET laboratory course. Each program is presented with its problem statement, source code, and an output section. The programs include: 1) Checking if a number is a palindrome, 2) Processing command line arguments, 3) Finding roots of a quadratic equation, 4) Demonstrating boxing and unboxing, 5) Implementing stack operations, 6) Demonstrating operator overloading, and 7) Finding the second largest element in an array. For each program, the source code implements the given task.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
109 views41 pages
Write A Program in C# To Check Whether A Number Is Palindrome or Not
The document contains source code for 7 C# programs assigned as exercises in a .NET laboratory course. Each program is presented with its problem statement, source code, and an output section. The programs include: 1) Checking if a number is a palindrome, 2) Processing command line arguments, 3) Finding roots of a quadratic equation, 4) Demonstrating boxing and unboxing, 5) Implementing stack operations, 6) Demonstrating operator overloading, and 7) Finding the second largest element in an array. For each program, the source code implements the given task.
1. WRITE A PROGRAM IN C# TO CHECK WHETHER A NUMBER IS PALINDROME OR NOT.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Palindrome { class Program { static void Main(string[] args) {
int num,temp; int digit; int reverse = 0; Console.WriteLine("Enter a number"); num = int.Parse(Console.ReadLine()); temp=num; while(num!=0) { digit = num % 10; reverse = reverse * 10 + digit; num=num /= 10; } Console.WriteLine("The reverse of the number is: {0}",reverse); if (temp == reverse) { Console.WriteLine("This number is a palindrome!"); Console.ReadLine(); } else { Console.WriteLine("This number is not a palindrome"); Console.ReadLine(); } } } }
3. WRITE A PROGRAM IN C# TO FIND THE ROOTS OF QUADRATIC EQUATION.
SOURCE CODE:
using System; namespace QuadRoots { class Program {
public static void Main() { float a, b, c; double disc, deno, x1, x2;
Console.WriteLine("ENTER THE VALUES OF A,B,C..."); a = float.Parse(Console.ReadLine()); b = float.Parse(Console.ReadLine()); c = float.Parse(Console.ReadLine());
if (a == 0) { x1 = -c / b; Console.WriteLine("The roots are Linear:", x1); }
else { disc = (b * b) - (4 * a * c); deno = 2 * a; if (disc > 0) { Console.WriteLine("THE ROOTS ARE REAL AND DISTINCT ROOTS"); x1 = (-b / deno) + (Math.Sqrt(disc) / deno); x2 = (-b / deno) - (Math.Sqrt(disc) / deno); Console.WriteLine("THE ROOTS ARE... " + x1 + " and " + x2); }
4. WRITE A PROGRAM IN C# TO DEMONSTRATE BOXING AND UNBOXING.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Boxing { class Program { static void Main(string[] args) { int m = 10; object a = m; // boxing try {
Console.WriteLine("Value of m is:" + a); object n = 20; int b = (int)n; // attempt to unbox Console.WriteLine("Value of n is:" + b); System.Console.WriteLine("Unboxing OK."); Console.ReadLine();
5. WRITE A PROGRAM IN C# TO IMPLEMENT STACK OPERATIONS
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Stack {
class Program { static void Main(string[] args) { int top = -1; int[] s = new int[10]; Console.WriteLine("Enter The Size of The Stack"); int MAX = int.Parse(Console.ReadLine()); while (true) { Console.WriteLine("1.Push"); Console.WriteLine("2.Pop"); Console.WriteLine("3.Display"); Console.WriteLine("4.Exit");
Console.WriteLine("Enter your choice :"); int ch = int.Parse(Console.ReadLine());
switch (ch) { case 1: if (top > MAX - 1) Console.WriteLine("... Stack Overflow ..."); else { Console.WriteLine("Enter the item :"); int n = int.Parse(Console.ReadLine()); s[++top] = n; }
6. WRITE A PROGRAM TO DEMONSTRATE OPERATOR OVERLOADING.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Overload { public struct addOpp { private double a; public addOpp(double a) { this.a = a;
} public override string ToString() { return string.Format("{0}",a);
}
static public addOpp operator +(addOpp lhs, addOpp rhs) { return new addOpp(lhs.a + rhs.a);
} }
class Program { static void Main(string[] args) { Console.WriteLine("Enter Two Numbers"); addOpp c1 = new addOpp(double.Parse(Console.ReadLine())); addOpp c2 = new addOpp(double.Parse(Console.ReadLine())); addOpp c3 = c1 + c2; Console.WriteLine("First Value is {0}", c1); Console.WriteLine("Second Value is {0}", c2); Console.WriteLine("Addition is {0}", c3); Console.ReadLine();
7. WRITE A PROGRAM IN C# TO FIND THE SECOND LARGEST ELEMENT IN A SINGLE DIMENSIONAL ARRAY.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Text; namespace secondLarge { class Program { static void Main(string[] args) { int[] a = new int[10]; int i, j; Console.WriteLine("Enter the No. of Elements"); int n = int.Parse(Console.ReadLine()); Console.WriteLine("Enter the Elements"); for(i=0;i<n;i++) { a[i]=int.Parse(Console.ReadLine()); } for(i=0;i<n;i++) for (j = 0; j < n - 1; j++) { if(a[j]<a[j+1]) { int temp = a[j]; a[j]=a[j+1]; a[j+1]=temp;
} } i = 1; while (a[i] == a[0]) i++; if (i >= n) { Console.WriteLine("Second Largest Element Does Not Exist"); Console.ReadLine(); } else { Console.WriteLine("Second Largest Element is "+a[i]); Console.ReadLine(); }
8. WRITE A PROGRAM IN C# TO MULTIPLY TO MATRICES USING RECTANGULAR ARRAYS
SOURCE CODE:
using System; class MatrixMultiplication { int[,] a; int[,] b; int[,] c; public int m1, n1, m2, n2; public void ReadMatrix() { Console.WriteLine("\n Size of Matrix 1:"); Console.Write("\n Enter the number of rows in Matrix 1 :"); m1 = int.Parse(Console.ReadLine()); Console.Write("\n Enter the number of columns in Matrix 1 :"); n1 = int.Parse(Console.ReadLine()); a = new int[m1, n1]; Console.WriteLine("\n Size of Matrix 2 :"); Console.Write("\n Enter the number of rows in Matrix 2 :"); m2 = int.Parse(Console.ReadLine()); Console.Write("\n Enter the number of columns in Matrix 2 :"); n2 = int.Parse(Console.ReadLine()); b = new int[m2, n2]; if (n1 != m2) { Console.WriteLine("columns of A & Rows of B matrix are not equal"); Console.ReadLine(); Environment.Exit(0); } else { Console.WriteLine("\n Enter the elements of Matrix 1:"); for (int i = 0; i < m1; i++) { for (int j = 0; j < n1; j++) { a[i, j] = int.Parse(Console.ReadLine()); } } Console.WriteLine("\n Enter the elements of Matrix 2:"); for (int i = 0; i < m2; i++) { for (int j = 0; j < n2; j++) { b[i, j] = int.Parse(Console.ReadLine()); } } } }
public void PrintMatrix() { Console.WriteLine("\n Matrix 1:");
for (int i = 0; i < m1; i++) { .Net Laboratory [10MCA57]
9. FIND THE SUM OF ALL THE ELEMENTS PRESENT IN A JAGGED ARRAY OF 3 INNER ARRAYS
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace JagArray { class Program { static void Main(string[] args) { int[][] jarr = new int[3][]; int s = 0; for (int i = 0; i < 3; i++) { Console.WriteLine("Enter the Size of the Array" +(i + 1)); int n = int.Parse(Console.ReadLine()); jarr[i] = new int[n]; Console.WriteLine("Enter the Values of Array " +(i + 1)); for (int j = 0; j < n; j++) { jarr[i][j] = int.Parse(Console.ReadLine()); s = s + jarr[i][j]; } n = n + 0;
10. WRITE A PROGRAM TO REVERSE A GIVEN STRING USING C#.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ReverseStr { class Program { static void Main(string[] args) { Console.WriteLine("Enter the String :"); string a = Console.ReadLine(); int len = a.Length; Console.Write("The Reverse of String is :"); for (int i = len - 1; i >= 0; i--) { Console.Write(a[i]); } Console.WriteLine(); Console.ReadLine(); } } }
11. USING TRY, CATCH AND FINALLY BLOCKS WRITE A PROGRAM IN C# TO DEMONSTRATE ERROR HANDLING.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace TryCatch { class Program { static void Main(string[] args) { int[] a = new int[3]; int n = args.Length; try { if (n == 0) { int d = 10 / (n); } if (n == 1) { a[4] =6; } }
catch (IndexOutOfRangeException e) { Console.WriteLine("Exception"+e); } catch (DivideByZeroException e) { Console.WriteLine("DivideByZeroException"+e); } finally { Console.WriteLine("finally block :: End of Program");
12. DESIGN A SIMPLE CALCULATOR USING SWITCH STATEMENT IN C#.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Text;
namespace SimpleCalc { class Program { static void Main(string[] args) {
float a, b; int ch;
Console.Write("Enter The First No.: "); a = float.Parse(Console.ReadLine()); Console.Write("\nEnter the Second No.: "); b = float.Parse(Console.ReadLine()); while (true) { Console.WriteLine("===============================");
Console.WriteLine("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Moduler Division\n6.Square\n7.Square Root\n8.Exit"); Console.WriteLine("==============================="); Console.Write("Enter your Choice : "); ch = int.Parse(Console.ReadLine()); switch (ch) { case 1: Console.WriteLine("Addition :" + a + "+" + b + "=" + (a + b)); break; case 2: Console.WriteLine("Subtraction :" + a + "-" + b + "=" + (a - b)); break; case 3: Console.WriteLine("Multiplication :" + a + "*" + b + "=" + (a * b)); break; case 4: Console.WriteLine("Division :" + a + "/" + b + "=" + (a / b)); break; case 5: Console.WriteLine("Moduler Division:"+a+"%" + b + "=" + (a % b)); break; case 6: Console.WriteLine("Square(" + a + ") =" + (a * a)); break; case 7: Console.WriteLine("SquareRoot(" + a + ") =" + Math.Sqrt(a)); break; default: Console.WriteLine("Invalid Input"); Environment.Exit(0); break;
14. IMPLEMENT LINKED LISTS IN C# USING THE EXISTING COLLECTIONS NAME SPACE.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace LinkList { class Program { static LinkedList<int> ll = new LinkedList<int>(); static LinkedListNode<int> node; static void Main(string[] args) {
Console.WriteLine(" LINKED LIST DEMO"); int ch, x; Console.WriteLine("Linked List Operations");
Console.WriteLine("1.AddFirst\n2.AddLast\n3.RemoveFirst\n4.RemoveLast\n5.Remove Specified\n6.Display\n7..Exit"); while (true) { Console.Write("Enter your Choice : "); ch = int.Parse(Console.ReadLine()); switch (ch) {
case 1: Console.Write("Enter the Element to AddFirst : ");
x = int.Parse(Console.ReadLine()); ll.AddFirst(x); display(); break; case 2: Console.WriteLine("Enter the Element to AddLast : "); x = int.Parse(Console.ReadLine()); ll.AddLast(x); display(); break; case 3: if (ll.Count == 0) { Console.WriteLine("Nothing to Delete...!!!"); break; } else { Console.WriteLine("First Element Removed Successfully"); ll.RemoveFirst(); display();
break; } case 4: if (ll.Count == 0) { Console.WriteLine("Nothing to Delete...!!!"); break; } else { Console.WriteLine("Last Element Removed Successfully"); ll.RemoveLast(); display();
break; } case 5: if (ll.Count == 0) { Console.WriteLine("Nothing to Delete...!!!"); break; } else { Console.WriteLine("Enter the Element to Remove"); x = int.Parse(Console.ReadLine()); bool b=ll.Remove(x);
17. WRITE A PROGRAM TO ILLUSTRATE THE USE OF DIFFERENT PROPERTIES IN C#.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Properties { class point { int getx, gety; public int x { get { return getx; } set { getx = value; } } public int y { get { return gety; } set { gety = value; } } } class Program { static void Main(string[] args) {
point start = new point(); point end = new point(); start.x = 10; start.y = 20; end.x = 100; end.y = 200; Console.Write("\npoint 1 :" + start.x + " " + end.x); Console.Write("\npoint 2 :" + start.y + " " + end.y); Console.ReadLine(); } } }
18. DEMONSTRATE ARRAYS OF INTERFACE TYPES WITH A C# PROGRAM.
SOURCE CODE:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace IntrDemo {
public interface Shape { void area();
} public class Circle : Shape { public void area() { Console.WriteLine("*** Calculating Area of Circle ***"); Console.Write("Enter the Radius:"); float r = float.Parse(Console.ReadLine()); Console.WriteLine("Area of Circle = " + (3.142 * r * r)); } } public class Square : Shape { public void area() { Console.WriteLine("*** Calculating Area of Square ***"); Console.Write("Enter the Length:"); float side = float.Parse(Console.ReadLine()); Console.WriteLine("Area of Square = " + (side * side)); } }
class Program { static void Main(string[] args) { Console.WriteLine("*** Arrays of Inerface Demo ***"); Shape[] s = new Shape[2]; s[0] = new Circle(); s[1] = new Square();
for (int i = 0; i < s.Length; i++) { s[i].area(); Console.ReadLine(); } }