0% found this document useful (0 votes)
45 views35 pages

C Programming Lab Experiments List

The document contains a comprehensive list of C programming lab experiments and programs divided into two parts. Part A includes basic programs for arithmetic operations, finding prime numbers, generating Fibonacci series, and more. Part B covers advanced topics such as matrix operations, recursion, and file handling.
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)
45 views35 pages

C Programming Lab Experiments List

The document contains a comprehensive list of C programming lab experiments and programs divided into two parts. Part A includes basic programs for arithmetic operations, finding prime numbers, generating Fibonacci series, and more. Part B covers advanced topics such as matrix operations, recursion, and file handling.
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

C Programming Lab List of Experiments/Programs

Part A:
1. C program for given two numbers to perform arithmetic operations using switch statement.
2. C program to find biggest of three number using nested if statement.
3. C program to find sum of the S=1²+2²+3²+ …. +n² indirect method using looping statement
4. C program to find sum of the S=1-2+3-4+5. +n series by indirect method using looping statement
5. C program to find sum of the S=1+1/x+1/x²series up to 4 decimal places of accuracy.
6. C program to check whether the given number is prime or not.
7. C program to print and count prime numbers from 2 to n.
8. C program to generate Fibonacci series up to n numbers
9. C program to check whether the given number is factorial of a number or not
10. C program to convert binary number to decimal number.
11. C program to convert decimal number to binary number.
12. C program to find the roots of the quadratic equation using else if statement.
13. C program to find the reverse of the given number. Also sum & count the number of digits and check whether
the given number is palindrome or not palindrome
14. C program to find largest and smallest of n numbers
15. C program to find second largest and second smallest of n numbers

Part B:
1. C program for sorting given set of numbers using bubble sort technique.
2. C program to search given number using linear search technique
3. C program to accept two square matrix and find sum of two matrices.
4. C program to print difference of two matrices.
5. C program to accept two matrices of order m*n and p*q to find product of two matrices using function.
6. C program to check whether given number is Fibonacci or not.
7. C program to accept m*n matrix. To find trace and norm of square matrix and to print principle diagonal
elements.
8. C program to accept m*n matrix to find sum of upper diagonal and lower diagonal elements.
9. C program to find factorial of a number using recursive function
10. C program to find NCR and NPR using function.
11. C program to find LCM and GCD of two numbers.
12. C program to display transpose of given m*n matrix using function.
13. C program to swap two numbers using function and pointers.
14. C program to accept employee information and display the same using structure.
15. C program to create simple marks card assuming appropriate condition
16. C program to read and write information of an employee using a file.
1. C program for given two numbers to perform arithmetic operations using switch statement.
#include <stdio.h>
int main() {
int num1, num2, choice; // Declare variables
float result; // Variable to store result

printf("Enter first number: "); // Prompt for first number


scanf("%d", &num1); // Read first number

printf("Enter second number: "); // Prompt for second number


scanf("%d", &num2); // Read second number

// Display operation menu


printf("\nChoose operation:\n");
printf("1. Addition (+)\n");
printf("2. Subtraction (-)\n");
printf("3. Multiplication (*)\n");
printf("4. Division (/)\n");
printf("5. Modulus (%%)\n");

printf("Enter your choice (1-5): "); // Prompt for operation choice


scanf("%d", &choice); // Read user choice

// Perform operation based on user choice


switch (choice) {
case 1:
result = num1 + num2; // Addition
printf("Result: %.2f\n", result);
break;
case 2:
result = num1 - num2; // Subtraction
printf("Result: %.2f\n", result);
break;
case 3:
result = num1 * num2; // Multiplication
printf("Result: %.2f\n", result);
break;
case 4:
if (num2 != 0) { // Check division by zero
result = (float)num1 / num2; // Division
printf("Result: %.2f\n", result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
case 5:
if (num2 != 0) { // Check modulus by zero
printf("Result: %d\n", num1 % num2); // Modulus
} else {
printf("Error: Modulus by zero is not allowed.\n");
}
break;
default:
printf("Invalid choice.\n"); // Handle invalid input
}

return 0; // Return 0 to indicate successful execution


}

2. C program to find biggest of three number using nested if statement.


#include <stdio.h>
int main() {
int a, b, c; // Declare three integer variables

printf("Enter first number: "); // Prompt user for first number


scanf("%d", &a); // Read first number

printf("Enter second number: "); // Prompt user for second number


scanf("%d", &b); // Read second number
printf("Enter third number: "); // Prompt user for third number
scanf("%d", &c); // Read third number

// Use nested if statements to find the biggest number


if (a > b) {
if (a > c) {
printf("Biggest number is: %d\n", a); // a is greatest
} else {
printf("Biggest number is: %d\n", c); // c is greatest
}
} else {
if (b > c) {
printf("Biggest number is: %d\n", b); // b is greatest
} else {
printf("Biggest number is: %d\n", c); // c is greatest
}
}
return 0; // Return 0 to indicate successful execution
}

3. C program to find sum of the S=1²+2²+3²+ …. +n² indirect method using looping statement
#include <stdio.h>
int main() {
int n, i; // Declare variables
int sum = 0; // Initialize sum to 0

printf("Enter the value of n: "); // Prompt user to enter n


scanf("%d", &n); // Read value of n

// Loop from 1 to n to calculate sum of squares


for (i = 1; i <= n; i++) {
sum += i * i; // Add square of current number to sum
}

printf("Sum of squares from 1 to %d is: %d\n", n, sum); // Display result


return 0; // Return 0 to indicate successful execution
}

4. C program to find sum of the S=1-2+3-4+5. +n series by indirect method using looping statement
#include <stdio.h>
int main() {
int n, i; // Declare variables
int sum = 0; // Initialize sum to 0

printf("Enter the value of n: "); // Prompt user to enter n


scanf("%d", &n); // Read value of n

// Loop from 1 to n to calculate the alternating sum


for (i = 1; i <= n; i++) {
if (i % 2 == 0) {
sum -= i; // Subtract even numbers
} else {
sum += i; // Add odd numbers
}
}

printf("Sum of the series is: %d\n", sum); // Display result

return 0; // Return 0 to indicate successful execution


}

5. C program to find sum of the S=1+1/x+1/x²series up to 4 decimal places of accuracy


#include <stdio.h>
#include <math.h>
int main() {
float x; // Declare variable for x
float sum = 0.0; // Initialize sum to 0
int i, n; // Loop counters
printf("Enter the value of x: "); // Prompt user to enter x
scanf("%f", &x); // Read value of x

printf("Enter the number of terms: "); // Prompt for number of terms


scanf("%d", &n); // Read number of terms

// Loop to calculate the sum of the series


for (i = 0; i < n; i++) {
sum += 1.0 / pow(x, i); // Add term 1/x^i to sum
}

printf("Sum of the series: %.4f\n", sum); // Print sum with 4 decimal places

return 0; // Return 0 to indicate successful execution


}

6. C program to check whether the given number is prime or not.


#include <stdio.h>
int main() {
int num, i, isPrime = 1; // Declare variables

printf("Enter a number: "); // Prompt user to enter a number


scanf("%d", &num); // Read the number

if (num <= 1) {
isPrime = 0; // Numbers less than or equal to 1 are not prime
} else {
// Loop to check for factors from 2 to num/2
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0; // If divisible, it's not prime
break; // Exit loop early
}
}
}
// Display result based on isPrime flag
if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}

return 0; // Return 0 to indicate successful execution


}

7. C program to print and count prime numbers from 2 to n.


#include <stdio.h>

int main() {
int n, i, j, isPrime, count = 0; // Declare variables

printf("Enter the value of n: "); // Prompt user to enter n


scanf("%d", &n); // Read value of n

printf("Prime numbers from 2 to %d are:\n", n); // Display heading

// Loop through numbers from 2 to n


for (i = 2; i <= n; i++) {
isPrime = 1; // Assume number is prime

// Check if i is divisible by any number from 2 to i/2


for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = 0; // i is not prime
break; // Exit inner loop
}
}

// If number is prime, print it and increase count


if (isPrime) {
printf("%d ", i); // Print prime number
count++; // Increment count
}
}

printf("\nTotal prime numbers between 2 and %d: %d\n", n, count); // Display count

return 0; // Return 0 to indicate successful execution


}

8. C program to generate Fibonacci series up to n numbers


#include <stdio.h>

int main() {
int n, i; // Declare variables
int first = 0; // First term of Fibonacci series
int second = 1; // Second term of Fibonacci series
int next; // Variable to store next term

printf("Enter the number of terms: "); // Prompt user to enter n


scanf("%d", &n); // Read value of n

printf("Fibonacci series up to %d terms:\n", n); // Display heading

// Loop to generate Fibonacci series


for (i = 1; i <= n; i++) {
printf("%d ", first); // Print current term

next = first + second; // Calculate next term


first = second; // Update first term
second = next; // Update second term
}

printf("\n"); // Newline for neat output

return 0; // Return 0 to indicate successful execution


}
9. C program to check whether the given number is factorial of a number or not
#include <stdio.h>

int main() {
int num, i = 1, fact = 1; // Declare variables

printf("Enter a number: "); // Prompt user to enter a number


scanf("%d", &num); // Read the number

// Loop to find if number is a factorial of some integer


while (fact < num) {
i++; // Increment i
fact *= i; // Multiply fact by i
}

// Check if calculated factorial equals the input number


if (fact == num) {
printf("%d is the factorial of %d.\n", num, i);
} else {
printf("%d is not a factorial of any number.\n", num);
}

return 0; // Return 0 to indicate successful execution


}

10. C program to convert binary number to decimal number.


#include <stdio.h>
#include <math.h>

int main() {
long long binary; // Variable to store binary number
int decimal = 0, i = 0, remainder;

printf("Enter a binary number: "); // Prompt user to enter binary number


scanf("%lld", &binary); // Read binary number

// Convert binary to decimal


while (binary != 0) {
remainder = binary % 10; // Get last digit
decimal += remainder * (int)pow(2, i); // Add to decimal value
binary /= 10; // Remove last digit
i++; // Increment position
}

printf("Decimal equivalent: %d\n", decimal); // Print decimal number

return 0; // Return 0 to indicate successful execution


}

11. C program to convert decimal number to binary number.


#include <stdio.h>
int main() {
int decimal, remainder, i = 0; // Declare variables
int binary[32]; // Array to store binary digits

printf("Enter a decimal number: "); // Prompt user to enter decimal number


scanf("%d", &decimal); // Read decimal number

// Convert decimal to binary


while (decimal != 0) {
binary[i] = decimal % 2; // Store remainder (0 or 1)
decimal /= 2; // Divide number by 2
i++; // Move to next position
}

printf("Binary equivalent: "); // Display heading

// Print binary digits in reverse order


for (i = i - 1; i >= 0; i--) {
printf("%d", binary[i]); // Print each binary digit
}

printf("\n"); // Newline for neat output


return 0; // Return 0 to indicate successful execution
}

12. C program to find the roots of the quadratic equation using else if statement.
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c; // Coefficients of the quadratic equation
float discriminant, root1, root2; // Variables for calculation

printf("Enter coefficients a, b and c: "); // Prompt user to enter coefficients


scanf("%f %f %f", &a, &b, &c); // Read coefficients

discriminant = b * b - 4 * a * c; // Calculate discriminant

// Check the nature of the roots using else if


if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a); // Calculate first root
root2 = (-b - sqrt(discriminant)) / (2 * a); // Calculate second root
printf("Roots are real and different.\n");
printf("Root 1 = %.2f\n", root1);
printf("Root 2 = %.2f\n", root2);
} else if (discriminant == 0) {
root1 = -b / (2 * a); // Both roots are same
printf("Roots are real and equal.\n");
printf("Root = %.2f\n", root1);
} else {
float realPart = -b / (2 * a); // Real part of complex root
float imagPart = sqrt(-discriminant) / (2 * a); // Imaginary part
printf("Roots are complex and imaginary.\n");
printf("Root 1 = %.2f + %.2fi\n", realPart, imagPart);
printf("Root 2 = %.2f - %.2fi\n", realPart, imagPart);
}

return 0; // Return 0 to indicate successful execution


}

13. C program to find the reverse of the given number. Also sum & count the number of digits and check
whether the given number is palindrome or not palindrome
#include <stdio.h>
int main() {
int num, originalNum, reverse = 0, digit, sum = 0, count = 0; // Declare variables

printf("Enter a number: "); // Prompt user to enter a number


scanf("%d", &num); // Read the number

originalNum = num; // Store original number for later comparison

// Loop to reverse number, count digits, and sum digits


while (num != 0) {
digit = num % 10; // Get the last digit
reverse = reverse * 10 + digit; // Build the reversed number
sum += digit; // Add digit to sum
count++; // Increment digit count
num /= 10; // Remove last digit
}

// Print the results


printf("Reversed number: %d\n", reverse);
printf("Sum of digits: %d\n", sum);
printf("Number of digits: %d\n", count);

// Check if the number is a palindrome


if (originalNum == reverse) {
printf("The number is a palindrome.\n");
} else {
printf("The number is not a palindrome.\n");
}

return 0; // Return 0 to indicate successful execution


}
14. C program to find largest and smallest of n numbers
#include <stdio.h>
int main() {
int n, i, num; // Declare variables
int largest, smallest; // Variables to store largest and smallest

printf("Enter the number of elements: "); // Prompt user to enter n


scanf("%d", &n); // Read value of n

printf("Enter number 1: "); // Prompt for first number


scanf("%d", &num); // Read first number

largest = smallest = num; // Initialize largest and smallest

// Loop to read and compare the remaining numbers


for (i = 2; i <= n; i++) {
printf("Enter number %d: ", i); // Prompt for next number
scanf("%d", &num); // Read number

if (num > largest) {


largest = num; // Update largest if current number is greater
}
if (num < smallest) {
smallest = num; // Update smallest if current number is smaller
}
}

// Display results
printf("Largest number: %d\n", largest);
printf("Smallest number: %d\n", smallest);

return 0; // Return 0 to indicate successful execution


}

15. C program to find second largest and second smallest of n numbers


#include <stdio.h>
int main() {
int n, i, num; // Declare variables
int firstMax, secondMax; // To store largest and second largest
int firstMin, secondMin; // To store smallest and second smallest

printf("Enter the number of elements: "); // Prompt user to enter n


scanf("%d", &n); // Read n

if (n < 2) {
printf("At least two numbers are required.\n"); // Minimum 2 numbers needed
return 1; // Exit the program
}

int arr[n]; // Declare array to store numbers

// Read all numbers from user


for (i = 0; i < n; i++) {
printf("Enter number %d: ", i + 1); // Prompt for each number
scanf("%d", &arr[i]); // Read number
}

// Initialize values
firstMax = secondMax = firstMin = secondMin = arr[0];

// Loop through array to find firstMax and firstMin


for (i = 1; i < n; i++) {
if (arr[i] > firstMax) {
secondMax = firstMax;
firstMax = arr[i];
} else if (arr[i] > secondMax && arr[i] != firstMax) {
secondMax = arr[i];
}

if (arr[i] < firstMin) {


secondMin = firstMin;
firstMin = arr[i];
} else if (arr[i] < secondMin && arr[i] != firstMin) {
secondMin = arr[i];
}
}

// Check if secondMax and secondMin were updated


if (firstMax == secondMax || firstMin == secondMin) {
printf("Cannot determine second largest or second smallest (duplicate values).\n");
} else {
printf("Second largest number: %d\n", secondMax);
printf("Second smallest number: %d\n", secondMin);
}

return 0; // Return 0 to indicate successful execution


}

1. C program for sorting given set of numbers using bubble sort technique.
#include <stdio.h>

int main() {
int n, i, j, temp; // Declare variables

printf("Enter the number of elements: "); // Prompt user to enter number of elements
scanf("%d", &n); // Read value of n

int arr[n]; // Declare array of size n

// Read elements into the array


for (i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1); // Prompt for each element
scanf("%d", &arr[i]); // Read element
}

// Bubble sort algorithm


for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

// Display sorted array


printf("Sorted array in ascending order:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]); // Print each element
}

printf("\n"); // Newline for neat output

return 0; // Return 0 to indicate successful execution


}

2. C program to search given number using linear search technique


#include <stdio.h>

int main() {
int n, i, key, found = 0; // Declare variables

printf("Enter the number of elements: "); // Prompt user to enter number of elements
scanf("%d", &n); // Read value of n

int arr[n]; // Declare array of size n

// Read elements into the array


for (i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1); // Prompt for each element
scanf("%d", &arr[i]); // Read element
}

printf("Enter the number to search: "); // Prompt for the key to search
scanf("%d", &key); // Read key

// Perform linear search


for (i = 0; i < n; i++) {
if (arr[i] == key) {
found = 1; // Set found to true if key is found
break; // Exit loop once found
}
}

// Display result
if (found) {
printf("%d found at position %d.\n", key, i + 1); // Show position (1-based)
} else {
printf("%d not found in the array.\n", key); // Key not found
}

return 0; // Return 0 to indicate successful execution


}

3. C program to accept two square matrix and find sum of two matrices.
#include <stdio.h>
int main() {
int n, i, j; // Declare variables

printf("Enter the size of the square matrix (n x n): "); // Prompt for matrix size
scanf("%d", &n); // Read size

int mat1[n][n], mat2[n][n], sum[n][n]; // Declare matrices

// Read elements for first matrix


printf("Enter elements of first matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("mat1[%d][%d]: ", i, j); // Prompt for element
scanf("%d", &mat1[i][j]); // Read element
}
}

// Read elements for second matrix


printf("Enter elements of second matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("mat2[%d][%d]: ", i, j); // Prompt for element
scanf("%d", &mat2[i][j]); // Read element
}
}

// Compute sum of matrices


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
sum[i][j] = mat1[i][j] + mat2[i][j]; // Add corresponding elements
}
}

// Display resulting matrix


printf("Sum of the two matrices:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", sum[i][j]); // Print sum matrix element
}
printf("\n"); // Newline after each row
}

return 0; // Return 0 to indicate successful execution


}

4. C program to print difference of two matrices.


#include <stdio.h>
int main() {
int n, i, j; // Declare variables

printf("Enter the size of the square matrix (n x n): "); // Prompt for matrix size
scanf("%d", &n); // Read size

int mat1[n][n], mat2[n][n], diff[n][n]; // Declare matrices

// Read elements for first matrix


printf("Enter elements of first matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("mat1[%d][%d]: ", i, j); // Prompt for element
scanf("%d", &mat1[i][j]); // Read element
}
}

// Read elements for second matrix


printf("Enter elements of second matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("mat2[%d][%d]: ", i, j); // Prompt for element
scanf("%d", &mat2[i][j]); // Read element
}
}

// Compute difference of matrices


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
diff[i][j] = mat1[i][j] - mat2[i][j]; // Subtract corresponding elements
}
}

// Display resulting matrix


printf("Difference of the two matrices (mat1 - mat2):\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", diff[i][j]); // Print difference matrix element
}
printf("\n"); // Newline after each row
}

return 0; // Return 0 to indicate successful execution


}

5. C program to accept two matrices of order m*n and p*q to find product of two matrices using function
#include <stdio.h>

// Function to multiply two matrices


void multiplyMatrices(int mat1[][10], int mat2[][10], int result[][10], int m, int n, int p, int q) {
int i, j, k;

// Initialize result matrix to zero


for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
result[i][j] = 0;
}
}

// Perform matrix multiplication


for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
for (k = 0; k < n; k++) {
result[i][j] += mat1[i][k] * mat2[k][j]; // Multiply and accumulate
}
}
}
}

int main() {
int m, n, p, q, i, j;
int mat1[10][10], mat2[10][10], result[10][10]; // Declare matrices

// Input order of first matrix


printf("Enter the order of first matrix (m n): ");
scanf("%d %d", &m, &n);

// Input order of second matrix


printf("Enter the order of second matrix (p q): ");
scanf("%d %d", &p, &q);

// Check if multiplication is possible


if (n != p) {
printf("Matrix multiplication not possible. Columns of first matrix must equal rows of second matrix.\n");
return 1; // Exit if invalid
}

// Input elements of first matrix


printf("Enter elements of first matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("mat1[%d][%d]: ", i, j);
scanf("%d", &mat1[i][j]);
}
}

// Input elements of second matrix


printf("Enter elements of second matrix:\n");
for (i = 0; i < p; i++) {
for (j = 0; j < q; j++) {
printf("mat2[%d][%d]: ", i, j);
scanf("%d", &mat2[i][j]);
}
}

// Call function to multiply matrices


multiplyMatrices(mat1, mat2, result, m, n, p, q);

// Display resulting matrix


printf("Product of the two matrices:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
printf("%d\t", result[i][j]);
}
printf("\n");
}

return 0; // Return 0 to indicate successful execution


}

6. C program to check whether given number is Fibonacci or not


#include <stdio.h>

int main() {
int num, a = 0, b = 1, c = 0, found = 0; // Declare variables

printf("Enter a number: "); // Prompt user to enter number


scanf("%d", &num); // Read number

if (num == 0 || num == 1) {
found = 1; // 0 and 1 are Fibonacci numbers
} else {
// Generate Fibonacci numbers until c >= num
while (c < num) {
c = a + b; // Next Fibonacci number
a = b;
b = c;
}
if (c == num) {
found = 1; // Number is in Fibonacci series
}
}
// Print result
if (found) {
printf("%d is a Fibonacci number.\n", num);
} else {
printf("%d is not a Fibonacci number.\n", num);
}

return 0; // Return 0 to indicate successful execution


}

7. C program to accept m*n matrix. To find trace and norm of square matrix and to print principle diagonal
elements.
#include <stdio.h>
#include <math.h>

int main() {
int m, n, i, j;
int matrix[10][10]; // Matrix storage
int trace = 0; // Sum of principal diagonal elements
double norm = 0.0; // Norm of matrix (sqrt of sum of squares)

printf("Enter the order of the matrix (m n): ");


scanf("%d %d", &m, &n);

if (m != n) {
printf("Matrix is not square. Trace and norm calculation requires a square matrix.\n");
return 1; // Exit if matrix is not square
}

// Accept matrix elements


printf("Enter elements of the matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("matrix[%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}

// Calculate trace, norm and print principal diagonal elements


printf("Principal diagonal elements: ");
for (i = 0; i < m; i++) {
printf("%d ", matrix[i][i]); // Print diagonal element
trace += matrix[i][i]; // Add to trace
}
printf("\n");

// Calculate norm (sqrt of sum of squares of all elements)


for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
norm += matrix[i][j] * matrix[i][j];
}
}
norm = sqrt(norm);

// Display trace and norm


printf("Trace of the matrix: %d\n", trace);
printf("Norm of the matrix: %.4f\n", norm);

return 0; // Successful execution


}

8. C program to accept m*n matrix to find sum of upper diagonal and lower diagonal elements.
#include <stdio.h>
int main() {
int m, n, i, j;
int matrix[10][10]; // Matrix storage
int upperSum = 0; // Sum of upper diagonal elements
int lowerSum = 0; // Sum of lower diagonal elements

printf("Enter the order of the matrix (m n): ");


scanf("%d %d", &m, &n);

if (m != n) {
printf("Matrix must be square to find upper and lower diagonal sums.\n");
return 1; // Exit if not square
}

// Accept matrix elements


printf("Enter elements of the matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("matrix[%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}

// Calculate sums of upper and lower diagonals


for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (j > i) {
upperSum += matrix[i][j]; // Elements above main diagonal
} else if (i > j) {
lowerSum += matrix[i][j]; // Elements below main diagonal
}
}
}

// Display sums
printf("Sum of upper diagonal elements: %d\n", upperSum);
printf("Sum of lower diagonal elements: %d\n", lowerSum);

return 0; // Successful execution


}

9. C program to find factorial of a number using recursive function


#include <stdio.h>
// Recursive function to find factorial
int factorial(int n) {
if (n == 0 || n == 1) {
return 1; // Base case: factorial of 0 or 1 is 1
}
return n * factorial(n - 1); // Recursive call
}

int main() {
int num;

printf("Enter a positive integer: ");


scanf("%d", &num);

if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d is %d\n", num, factorial(num));
}

return 0;
}

10. C program to find NCR and NPR using function.


#include <stdio.h>

// Function to calculate factorial


unsigned long long factorial(int n) {
unsigned long long fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}

// Function to calculate nCr


unsigned long long nCr(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}

// Function to calculate nPr


unsigned long long nPr(int n, int r) {
return factorial(n) / factorial(n - r);
}

int main() {
int n, r;

printf("Enter values for n and r (n >= r): ");


scanf("%d %d", &n, &r);

if (n < r || n < 0 || r < 0) {


printf("Invalid input! Ensure that n >= r and both are non-negative.\n");
return 1;
}

printf("nCr = %llu\n", nCr(n, r));


printf("nPr = %llu\n", nPr(n, r));

return 0;
}

11. C program to find LCM and GCD of two numbers.


#include <stdio.h>

// Function to calculate GCD using Euclidean algorithm


int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

// Function to calculate LCM using GCD


int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}

int main() {
int num1, num2;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

if (num1 <= 0 || num2 <= 0) {


printf("Please enter positive integers.\n");
return 1;
}

printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));


printf("LCM of %d and %d is %d\n", num1, num2, lcm(num1, num2));

return 0;
}

12. C program to display transpose of given m*n matrix using function.


#include <stdio.h>

// Function to display transpose of a matrix


void transpose(int matrix[][10], int m, int n) {
int i, j;
printf("Transpose of the matrix:\n");
for (j = 0; j < n; j++) { // Columns become rows
for (i = 0; i < m; i++) { // Rows become columns
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int m, n, i, j;
int matrix[10][10]; // Declare matrix

printf("Enter the number of rows (m): ");


scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);

// Accept matrix elements


printf("Enter elements of the matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("matrix[%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}

// Call function to display transpose


transpose(matrix, m, n);

return 0;
}

13. C program to swap two numbers using function and pointers.


#include <stdio.h>
// Function to swap two numbers using pointers
void swap(int *a, int *b) {
int temp = *a; // Store value of a
*a = *b; // Assign value of b to a
*b = temp; // Assign temp to b
}
int main() {
int x, y;

printf("Enter two numbers: ");


scanf("%d %d", &x, &y);

printf("Before swap: x = %d, y = %d\n", x, y);

swap(&x, &y); // Call swap function with addresses of x and y

printf("After swap: x = %d, y = %d\n", x, y);

return 0;
}

14. C program to accept employee information and display the same using structure.
#include <stdio.h>

// Define structure for employee


struct Employee {
int id;
char name[50];
int age;
float salary;
};

int main() {
struct Employee emp; // Declare employee variable

// Accept employee details


printf("Enter Employee ID: ");
scanf("%d", &[Link]);

printf("Enter Employee Name: ");


scanf(" %[^\n]", [Link]); // Read string with spaces
printf("Enter Employee Age: ");
scanf("%d", &[Link]);

printf("Enter Employee Salary: ");


scanf("%f", &[Link]);

// Display employee details


printf("\nEmployee Information:\n");
printf("ID: %d\n", [Link]);
printf("Name: %s\n", [Link]);
printf("Age: %d\n", [Link]);
printf("Salary: %.2f\n", [Link]);

return 0;
}

15. C program to create simple marks card assuming appropriate condition


#include <stdio.h>

// Function to calculate total marks


int calculateTotal(int marks[], int subjects) {
int total = 0;
for (int i = 0; i < subjects; i++) {
total += marks[i];
}
return total;
}

// Function to calculate percentage


float calculatePercentage(int total, int maxMarks) {
return ((float)total / maxMarks) * 100;
}

// Function to determine grade based on percentage


char calculateGrade(float percentage) {
if (percentage >= 90)
return 'A';
else if (percentage >= 75)
return 'B';
else if (percentage >= 60)
return 'C';
else if (percentage >= 40)
return 'D';
else
return 'F';
}

int main() {
int subjects = 5;
int marks[5];
int maxMarks = subjects * 100; // Assuming max marks per subject is 100
char name[50];
int rollNo;

// Accept student details


printf("Enter student name: ");
scanf(" %[^\n]", name);

printf("Enter roll number: ");


scanf("%d", &rollNo);

// Accept marks for each subject


printf("Enter marks for %d subjects (out of 100):\n", subjects);
for (int i = 0; i < subjects; i++) {
printf("Subject %d: ", i + 1);
scanf("%d", &marks[i]);
if (marks[i] < 0 || marks[i] > 100) {
printf("Invalid marks! Please enter marks between 0 and 100.\n");
i--; // Re-enter marks for this subject
}
}
// Calculate total, percentage, and grade
int total = calculateTotal(marks, subjects);
float percentage = calculatePercentage(total, maxMarks);
char grade = calculateGrade(percentage);

// Print mark card


printf("\n------- MARKS CARD -------\n");
printf("Name : %s\n", name);
printf("Roll No. : %d\n", rollNo);
printf("Subjects : %d\n", subjects);
printf("Total Marks: %d / %d\n", total, maxMarks);
printf("Percentage : %.2f%%\n", percentage);
printf("Grade : %c\n", grade);
printf("--------------------------\n");

return 0;
}

16. C program to read and write information of an employee using a file.


#include <stdio.h>
#include <stdlib.h>

// Define structure for employee


struct Employee {
int id;
char name[50];
int age;
float salary;
};

int main() {
struct Employee emp;
FILE *file;

// Accept employee details


printf("Enter Employee ID: ");
scanf("%d", &[Link]);

printf("Enter Employee Name: ");


scanf(" %[^\n]", [Link]);

printf("Enter Employee Age: ");


scanf("%d", &[Link]);

printf("Enter Employee Salary: ");


scanf("%f", &[Link]);

// Open file in write mode


file = fopen("[Link]", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}

// Write employee data to file


fprintf(file, "Employee ID: %d\n", [Link]);
fprintf(file, "Employee Name: %s\n", [Link]);
fprintf(file, "Employee Age: %d\n", [Link]);
fprintf(file, "Employee Salary: %.2f\n", [Link]);

fclose(file); // Close file

// Open file in read mode to display contents


file = fopen("[Link]", "r");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}

char ch;
printf("\nEmployee information from file:\n");
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}

fclose(file); // Close file

return 0;
}
***********************************************************************************************

You might also like