0% found this document useful (0 votes)
7 views2 pages

C# Programming Exercises and Solutions

The document contains a list of programming tasks in C# that cover a variety of concepts including mathematical calculations, string manipulations, data structures, and object-oriented programming. Each task specifies a different requirement, such as calculating factorials, checking for prime numbers, performing matrix operations, and implementing inheritance. The tasks range from basic operations to more complex implementations involving classes and interfaces.

Uploaded by

jesaboc231
Copyright
© All Rights Reserved
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% found this document useful (0 votes)
7 views2 pages

C# Programming Exercises and Solutions

The document contains a list of programming tasks in C# that cover a variety of concepts including mathematical calculations, string manipulations, data structures, and object-oriented programming. Each task specifies a different requirement, such as calculating factorials, checking for prime numbers, performing matrix operations, and implementing inheritance. The tasks range from basic operations to more complex implementations involving classes and interfaces.

Uploaded by

jesaboc231
Copyright
© All Rights Reserved
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

Q.1] Write a C# program to calculate the factorial of a given number.

Q.2] Write a C# program to check whether a given number is prime or not.


Q.3] Write a C# program to display all the prime numbers between 1 to 100.
Q.4] Write a C# program to Display all perfect numbers between 1 to 100.
Q.5] Write a C# program to sort an array in ascending and descending order.
Q.6] Write a C# program to calculate the sum of first and last digits of a given number.
Q.7] Write a C# program to check whether the given string is a palindrome or not.
Q.8] Write a C# program to accept one string & a character, find the occurrences of that
character in String using function.
Q.9] Write a C# program to check whether given number is Armstrong or not.
Q.10] Write a C# program to find minimum & maximum element from an array.
Q.11] Write a C# program to accept M x N matrix and perform the following operations:
1. Addition
2. Transpose
Q.12] Write a C# program to accept MXN matrix from a user and calculate sum of diagonal
elements of a matrix.
Q.13] Write a C# program to implement a stack with push and pop operations. Find the top
element of the stack and check if the stack is empty or not.
Q.14] Write a C# program to find the top and bottom element from a given stack. [10M]
Q.15] Write a C# program to accept two numbers from a user, display following menus and
perform the following operations on them using function:
1. Addition
2. Subtraction
3. Multiplication
4. Division
Q.16] Write a C# program to accept the following information of student from a user:
Student Id:
Student Name:
Course Name:
Date of Birth:
The application should display the information of student once the data is entered.
Q.17] Write a C# program to implement multilevel inheritance from the following figure. Accept
and display data for one student.

.
Q.18] Write a C# program to define a class Person with attributes name and birthdate. Drive the
classes Student and Instructor from Person. The Student class has an attribute course and the
Instructor class has an attribute salary. Write the class definitions, constructor and the member
function to display all information.
Q.19] Write a C# program to implement the following multiple inheritance using interface.
Q.20] Write a program to swap three integer and three float numbers using the concept of
Function overloading.
Q.21] Write a C# program to accept two 3 X 3 matrix from a user and perform matrix
multiplication.
Q.22] Write a C# program using a constructor and destructor to display student information
(Name, Age & course).
Q.23] Define an abstract class ArithmeticOperation with an abstract method
PerformOperation().Create two derived classes Addition and Multiplication to
implement the PerformOperation ()method to Addition & Multiplication.

Common questions

Powered by AI

C# programs like matrix operations leverage inheritance to extend base functionalities into more specialized classes, while abstraction allows hiding of implementation details, focusing on essential operations like addition and multiplication. This separation of concerns improves code maintainability by facilitating updates and reuse without altering the core logic .

Function overloading in C# provides flexibility by allowing methods to be defined with the same name but different parameter types, enabling code to handle multiple data types seamlessly. This increases usability as it simplifies method calls for arithmetic operations by allowing the same method to work with integers and floats, improving code clarity and reducing errors .

Abstract classes and interfaces in C# allow arithmetic operations to be defined as contracts, which must be fulfilled by derived classes. This enforces a consistent API for operation methods, promoting design clarity and adherence to SOLID principles, and allows easy extension with new operations without altering existing code .

Polymorphism allows C# programs to process matrix multiplications using base class pointers or interfaces, making the code more adaptable to future changes. By enabling different matrix classes to handle multiplication differently, it enhances extensibility, allowing easy integration of new matrix types without modifying existing code .

Utilizing classes for stack implementation in C# encapsulates the complexity of stack operations, providing a reusable and tested way to manage a collection. Unlike primitive data types, stack classes allow for robust operations like push, pop, peeking at the top element, and checking for empty state, while ensuring type safety and maintaining efficiency .

Challenges in implementing multilevel inheritance include complexity due to deep hierarchy and increased difficulty in debugging and maintaining code. These challenges can be addressed by using interfaces to flatten hierarchy complexity, employing design patterns, and ensuring coherent and minimal class hierarchies to enhance clarity .

Recursive functions provide clearer and more concise code for factorial calculations by breaking down the problem into simpler sub-problems. However, they may lead to stack overflow for very large inputs, unlike iterative approaches, which are more memory-efficient yet may result in less intuitive code .

The benefits of using OOP for a student information system in C# include data encapsulation, which protects student data, inheritance, which allows code reuse, and polymorphism, which enhances system flexibility. However, drawbacks may include increased complexity for simple tasks and potential performance overhead due to abstraction layers .

Constructors and destructors help manage the lifecycle of a C# application by initializing and cleaning up resources, respectively. In student information systems, constructors ensure objects start in a well-defined state, and destructors assist in freeing resources like database connections, thus preventing resource leaks and ensuring efficient memory management .

Palindrome detection in C# requires reversing a string and comparing it to the original, enhancing skills in string manipulation such as indexing, slicing, and utilizing C# libraries for comparison. This process illustrates efficient data handling and promotes understanding of case sensitivity issues which are crucial for robust data comparison .

You might also like