G.
MADEGOWDA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
List of Experiments
Name of the Experiments
[Link] a program to calculate the temperature converter from degree to Fahrenheit.
2. Develop a program to find the roots of quadratic equations.
3. Develop a program to find whether a given number is prime or not.
4. Develop a program to find key elements in an array using linear search.
5. Given age and gender of a person, develop a program to categorise senior citizen (male &
female).
6. Generate Floyd’s triangle for given rows.
7. Develop a program to find the transpose of a matrix.
8. Develop a program to concatenate two strings, find length of a string and copy one string to
other using string operations.
9. Develop a modular program to find GCD and LCM of given numbers.
10. Develop a program to declare the structure of employees and display the employee records
with higher salary among two employees.
11. Develop a program to add two numbers using the pointers to the variables.
12. Develop a program to find the sum of digits of a given number.
13. Develop a program to perform Matrix Multiplication.
14. Develop a program to create an array of structures to store book details and check whether a
specific
Program1: Develop a program to calculate the temperature converter from
degree to Fahrenheit.
#include <stdio.h> // Standard input-output library
int main()
{ // Main function - program execution starts here
float celsius, fahrenheit; // Variables to store input and result
printf("Enter temperature in Celsius: "); // Ask user for input
scanf("%f", &celsius); // Read temperature in Celsius
fahrenheit = (celsius * 9 / 5) + 32; // Convert Celsius to Fahrenheit
printf("Temperature in Fahrenheit = %.2f\n", fahrenheit); // Display result
return 0; // Successful program termination
}
Output:
Program 2: Develop a program to find the roots of quadratic equations.
#include <stdio.h>
#include <math.h>
int main ()
{
float a, b, c, discriminant, root1, root2;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant = b*b - 4*a*c;
if (discriminant > 0.00001)
{
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("Roots are real and distinct: %.2f and %.2f\n", root1, root2);
}
else if (fabs(discriminant) < 0.00001)
{
root1 = -b / (2*a);
printf("Roots are real and equal: %.2f\n", root1);
}
else
{
float realPart = -b / (2*a);
float imagPart = sqrt(-discriminant) / (2*a);
printf("Roots are complex: %.2f + %.2fi and %.2f - %.2fi\n",
realPart, imagPart, realPart, imagPart);
}
return 0;
}
Output:
Program 3: Develop a program to find whether a given number is prime or
not.
#include <stdio.h>
int main()
{
int num, i, isPrime = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num <= 1)
{
isPrime = 0;
}
else
{
for (i = 2; i <= num/2; i++)
{
if (num % i == 0) {
isPrime = 0; // Found a factor → not prime
break;
}
}
}
if (isPrime)
printf("%d is a Prime Number\n", num);
else
printf("%d is Not a Prime Number\n", num);
return 0;
}
Output:
Program 4: Develop a program to find key elements in an array using linear
search.
#include <stdio.h>
int main()
{
int arr[50], n, key, i, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter key element to search: ");
scanf("%d", &key);
for (i = 0; i < n; i++) {
if (arr[i] == key)
{
printf("Element %d found at position %d\n", key, i+1);
found = 1;
break;
}
}
if (!found) // If not found
printf("Element %d not found in the array\n", key);
return 0;
}
Output:
Program 5: Given age and gender of a person, develop a program to
categorize senior citizen (male & female).
#include <stdio.h>
int main()
{
int age;
char gender;
printf("Enter age: ");
scanf("%d", &age);
printf("Enter gender (M/F): ");
scanf(" %c", &gender);
if (age >= 60)
{
if (gender == 'M' || gender == 'm')
printf("The person is a Male Senior Citizen\n");
else if (gender == 'F' || gender == 'f')
printf("The person is a Female Senior Citizen\n");
} else {
printf("The person is not a Senior Citizen\n");
}
return 0;
}
Output:
Program 6: Generate Floyd’s triangle for given rows.
#include <stdio.h>
int main()
{
int rows, i, j, num = 1;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}
Output:
Program 7: Develop a program to find the transpose of a matrix.
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10];
int r, c, i, j;
// Step 1: Input the size of the matrix
printf("Enter number of rows and columns: ");
scanf("%d %d", &r, &c);
// Step 2: Input elements of the matrix row by row
printf("Enter elements row by row:\n");
for (i = 0; i < r; i++) {
printf("Row %d: ", i + 1);
for (j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
// Step 3: Display the original matrix
printf("\nOriginal Matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
// Step 4: Compute transpose (swap rows and columns)
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
transpose[j][i] = a[i][j];
}
}
// Step 5: Display transpose of the matrix
printf("\nTranspose of the Matrix:\n");
for (i = 0; i < c; i++)
{
for (j = 0; j < r; j++)
{
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Program 8: Develop a program to concatenate two strings, find length of a
string and copy one string to other using string operations.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[50], str2[50], result[100];
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
// Concatenate strings
strcpy(result, str1);
strcat(result, str2);
printf("Concatenated string = %s\n", result);
// Find length of strings
printf("Length of first string = %d\n", (int)strlen(str1));
printf("Length of second string = %d\n", (int)strlen(str2));
// Copy string
strcpy(result, str1); // Copy str1 again into result
printf("Copied first string into result = %s\n", result);
return 0;
}
Output:
Program 9: Develop a modular program to find GCD and LCM of given
numbers.
#include <stdio.h>
// Function to find 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 * b) / gcd(a, b);
}
int main()
{
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("GCD = %d\n", gcd(num1, num2));
printf("LCM = %d\n", lcm(num1, num2));
return 0;
}
Output:
Program 10: Develop a program to declare the structure of employees and
display the employee records with higher salary among two employees.
#include <stdio.h>
// Define structure to store employee details
struct Employee
{
char name[30];
int id;
float salary;
};
int main()
{
struct Employee e1, e2;
printf("Enter details of Employee 1 (Name ID Salary): ");
scanf("%s %d %f", [Link], &[Link], &[Link]);
printf("Enter details of Employee 2 (Name ID Salary): ");
scanf("%s %d %f", [Link], &[Link], &[Link]);
// Compare salaries
if ([Link] > [Link])
printf("Employee with higher salary: %s (%.2f)\n", [Link], [Link]);
else if ([Link] > [Link])
printf("Employee with higher salary: %s (%.2f)\n", [Link], [Link]);
else
printf("Both employees have equal salary\n");
return 0;
}
Output:
Program 11: Develop a program to add two numbers using the pointers to the
variables.
#include <stdio.h>
int main()
{
int a, b, sum;
int *p1, *p2;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
p1 = &a; // Pointer p1 stores the address of a
p2 = &b; // Pointer p2 stores the address of b
sum = *p1 + *p2; // Access values using pointers and add
printf("Sum = %d\n", sum);
return 0;
}
Output:
Program 12: Develop a program to find the sum of digits of a given number.
#include <stdio.h>
int main()
{
int num, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
// Repeat until the number becomes 0
while (num != 0)
{
digit = num % 10; // Extract last digit
sum += digit; // Add digit to sum
num /= 10; // Remove last digit
}
printf("Sum of digits = %d\n", sum);
return 0;
}
Output:
Program 13: Develop a program to perform Matrix Multiplication.
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], product[10][10];
int r1, c1, r2, c2;
int i, j, k;
// Step 1: Input size of first matrix
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
// Step 2: Input size of second matrix
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
// Step 3: Check multiplication rule (columns of A == rows of B)
if (c1 != r2)
{
printf("Matrix multiplication not possible!\n");
return 0;
}
// Step 4: Input elements of first matrix
printf("Enter elements of first matrix (row by row):\n");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
scanf("%d", &a[i][j]);
}
}
// Step 5: Input elements of second matrix
printf("Enter elements of second matrix (row by row):\n");
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
scanf("%d", &b[i][j]);
}
}
// Step 6: Initialize product matrix with zeros
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
product[i][j] = 0;
}
}
// Step 7: Multiply matrices
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
for (k = 0; k < c1; k++)
{
product[i][j] += a[i][k] * b[k][j];
}
}
}
// Step 8: Display resultant matrix
printf("\nResultant Matrix (A × B):\n");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
printf("%d ", product[i][j]);
}
printf("\n"); // New line after each row
}
return 0;
}
Output:
.
Program 14: Develop a program to create an array of structures to store book
details and check whether a specific book, as requested by the user, is
available or not.
#include <stdio.h>
#include <string.h> // For string comparison
// Define structure for book details
struct Book {
char title[50];
char author[30];
int id;
};
int main()
{
struct Book books[5]; // Array to store multiple books
int n, i, found = 0;
char searchTitle[50];
printf("Enter number of books: ");
scanf("%d", &n);
// Read book details
for (i = 0; i < n; i++) {
printf("Enter details of book %d (Title Author ID): ", i+1);
scanf("%s %s %d", books[i].title, books[i].author, &books[i].id);
}
printf("Enter book title to search: ");
scanf("%s", searchTitle);
// Search for the book by title
for (i = 0; i < n; i++) {
if (strcmp(books[i].title, searchTitle) == 0) {
printf("Book Found! Title: %s, Author: %s, ID: %d\n",
books[i].title, books[i].author, books[i].id);
found = 1;
break;
}
}
if (!found)
printf("Book Not Available\n");
return 0;
}
Output:
Viva Questions:
1. What is the formula to convert Celsius to Fahrenheit?
2. Which data type is suitable for temperature values?
3. What happens if you input a negative Celsius value?
4. What is the quadratic formula?
5. What does the discriminant signify?
6. How do you handle complex roots in C?
7. Define a prime number.
8. How does using sqrt(n) help?
9. How to handle large inputs?
10. What is linear search?
11. What is its time complexity?
12. When is linear search preferred?
13. What is the commonly used age threshold for senior citizen?
14. How to safely read a char input?
15. How to validate inputs?
16. What is Floyd's triangle?
17. How does the nested loop work?
18. How to modify to print pattern with characters?
19. Define transpose of a matrix.
20. What are dimensions of transpose?
21. Can transpose be done in-place for square matrices?
22. Explain Euclid's algorithm for GCD.
23. How is LCM computed using GCD?
24. What is a pointer and how is it declared?
25. What does the unary & operator do?
26. What is dereferencing?
27. What about negative inputs?
28. What is the null terminator in C strings?
29. Difference between strcpy and strncpy?
30. Why should buffers be large enough to hold concatenation?
31. What are the conditions for matrix multiplication?
32. What is the size of the result matrix?
33. How do you extract digits from a number?
34. How to handle negative numbers?
35. What is modulo operator?
36. Explain the triple loop
37. What is a structure? How is it useful?
38. How to read strings with spaces into struct fields?
39. Difference between struct and typedef struct?
40. What is an array of structures?
41. How to perform string comparisons?
42. How to allow titles with spaces?