0% found this document useful (0 votes)
7 views1 page

C Programming Lab Exercises

The document outlines a series of C programming tasks, including programs for basic arithmetic operations, matrix multiplication, temperature conversion, file handling, number swapping, calculator simulation, square root calculation, pyramid construction, compound interest, matrix addition, area calculation using Heron's formula, palindrome checking, recursive factorial calculation, finding min and max in an array, expression evaluation, linear search, string length determination, and student marks processing using structures. Each task is presented with a specific programming challenge. The document serves as a comprehensive guide for practicing various C programming concepts.

Uploaded by

suni
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

C Programming Lab Exercises

The document outlines a series of C programming tasks, including programs for basic arithmetic operations, matrix multiplication, temperature conversion, file handling, number swapping, calculator simulation, square root calculation, pyramid construction, compound interest, matrix addition, area calculation using Heron's formula, palindrome checking, recursive factorial calculation, finding min and max in an array, expression evaluation, linear search, string length determination, and student marks processing using structures. Each task is presented with a specific programming challenge. The document serves as a comprehensive guide for practicing various C programming concepts.

Uploaded by

suni
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.a) Write a C program to Sum and average of 3 numbers.

b) Write a C program to Multiplication two matrices


2.a) Write a C program to Conversion of Fahrenheit to Celsius and vice versa
b) Write a C program to write and read text into a file
3.a)Write a C program to swap two numbers using call by reference.
b)Write a C program to simulate a calculator using switch case.
4. a) Write a C program to Finding the square root of a given number
b) Write a C program to Construct a pyramid of numbers.
5.a) Write a C program to Finding compound interest
b) Write a C program to Addition of two matrices
6 a) Write a C program to Area of a triangle using heron’s formulae
b) Write a C program to Checking a number palindrome.
7 a) Write a C program to recursive function to find the factorial of a number.
b) Write a C program to find the min and max of a 1-D integer array.
8) a)Evaluate the following expressions.
i. A+B*C+(D*E) + F*G ii. A/B*C-B+A*D/3 iii. A+++B---A iv. J= (i++) + (++i)
b) Write a C program to Perform linear search on1D array.
9) a)Write a C function to find the length of a string.
b) Write a C program to Take marks of 5 subjects in integers, and find the total, average in
float
10 a) Write a C program to find the total, average of n students using structures
b) Write a C program to Find the roots of the quadratic equation.

Common questions

Powered by AI

When writing a C program to add two matrices, factors to consider include ensuring the matrices are of the same dimensions, initializing a resultant matrix to store the sum, and iterating over the matrices to add corresponding elements at each index . Initialization is crucial to prevent accessing uninitialized memory, which could lead to unpredictable results or runtime errors.

Call by reference in C uses pointers to directly modify the original variables, which allows swapping two numbers without creating a copy of the data . This is more memory efficient than call by value, as it avoids duplicating the data for passing to the function, saving stack memory and potentially reducing execution time.

Key considerations for accurate conversion between Fahrenheit and Celsius include ensuring the use of the correct formulae, which for Fahrenheit to Celsius is: (Fahrenheit - 32) * 5/9, and for Celsius to Fahrenheit is: (Celsius * 9/5) + 32 . Floating-point arithmetic is crucial for precise calculations, as temperature values can be non-integer, requiring precision beyond integer arithmetic capabilities.

To perform a linear search on a 1D array in C, iterate through each element of the array sequentially and compare with the sought value until a match is found or the array ends . The time complexity of a linear search is O(n), which implies that the execution time grows linearly with the number of elements, making it inefficient for large data sets compared to more advanced search algorithms like binary search.

It is preferable to use a structure to calculate the total and average marks of students when organizing data that naturally groups related elements, such as a student's ID, name, and marks . Structures offer the advantage of encapsulating this data, allowing for cleaner code, reducing errors from inconsistent data handling, and enhancing maintainability by modularizing code related to student information management.

The main challenge of implementing a recursive function to find the factorial of a number in C is dealing with large input values, as recursion can lead to stack overflow if the depth becomes too great . Stack overflow can be mitigated by using an iterative approach for computing factorials or optimizing the recursion with tail calls. Additionally, limiting the maximum input size and checking conditions before recursive calls can help prevent stack overflow.

Designing a C program to find the area of a triangle using Heron's formula requires: 1) Reading three side lengths; 2) Verifying they can form a valid triangle; 3) Calculating the semi-perimeter (s = (a + b + c) / 2); 4) Using the formula: sqrt(s * (s - a) * (s - b) * (s - c)) to find the area . Mathematical considerations include ensuring the side lengths adhere to the triangle inequality theorem and handling precision in sqrt function for floating-point arithmetic.

A C program can simulate a calculator using switch-case by prompting the user for two operands and an operator, then using a switch statement to perform the corresponding arithmetic operation (addition, subtraction, multiplication, division) based on the operator input . The major limitation of this approach is its reliance on predefined operations; any unsupported operator or operation will require additional switch-case conditions, quickly making the program cumbersome and less flexible.

The standard approach to find the roots of a quadratic equation in a C program involves using the quadratic formula: (-b ± sqrt(b^2 - 4ac)) / (2a). When dealing with complex roots, identified by a negative discriminant, the program must manage imaginary numbers, often requiring an inclusion of complex number handling logic using separate real and imaginary parts, as C does not natively support complex types out-of-the-box.

To write a C program that multiplies two matrices, one must: 1) Verify if the number of columns in the first matrix equals the number of rows in the second; 2) Initialize a resultant matrix with zero values; 3) Use nested loops to iterate through rows of the first and columns of the second matrix, computing the sum of products for each cell . Understanding matrix dimensions is crucial because incorrect dimensions would lead to erroneous or undefined multiplication results.

You might also like