VISVESVARAYATECHNOLOGICALUNIVERSITY
Jnana Sangama, Belagavi-590018
LABORATORYMANUAL
INTRODUCTIONTOCPROGRAMMING LABORATORY- 1BPLC105E/205E
Prepared by,
Mrs. Archana B K
DEPARTMENTOFELECTRONICS&COMMUNICATIONENGINEERING
DR.HNNATIONALCOLLEGEOFENGINEERING
Bengaluru-560070
2024-2025
1
Dr. H N National College of Engineering
Approved by All India Council for Technical Education
(AICTE), Govt. of India and affiliated to Visvesvaraya
Technological University (VTU)
36BCross, Jayanagar 7th block, Bengaluru–560070
DEPARTMENT OF ELECTRONICS AND COMMUNICATION
ENGINEERING
INTRODUCTION TO C PROGRAMMING LABORATORY
BESCK104E/204E–1/2SEMESTERB.E.
[AS PER OUTCOME BASED EDUCATION (OBE) AND CHOICE BASED CREDIT
SYSTEM (CBCS) 2022 SCHEME]
AcademicYear–2024-2025 LAB
MANUAL
Prepared by:
Mrs. Archana B K
Department of ECE
Institute Vision and Mission
Our Vision
To become a premier education institute in the country, for providing futuristic knowledge and
profound skill in Engineering and Management, to produce global active brains that will provide
smart solutions in engineering for sustainable society.
Our Mission
• To impart the technical and managerial knowledge components, over an adorable academic
ambiance and enhanced learning over benchmarked syllabus through outcome-based education
systems.
• To develop professional skills in the research over creative conducive atmosphere of
interdisciplinaryresearchandinnovations.ToenhancecollaborativeandIPRskill,throughanactive
MOU with global organizations and clustering in emerging areas.
1
Program Outcomes (POs)
Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and leader
in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
2
Course Outcomes:
At the end of the course student will be able to:
1. Explain the fundamental structure of a C program and primitive constructs.
2. Apply decision-making and iterative control structures to solve simple
computational problems.
3. Develop programs using arrays and string operations to solve real-world problems.
4. Construct user-defined functions to modularize the solution to the given problems.
5. Build programs using structures and pointers for complex data representation and
access.
3
List of Experiments
Sl.No Name of the Experiment
1 Develop 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 book, as requested by the user, is available
or not.
4
Assessment Details (both CIE and SEE)
The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End
Exam (SEE) is 50%. The minimum passing mark for the CIE is 40% of the maximum
marks (20 marks out of 50). The minimum passing mark for the SEE is 35% of the
maximum marks (18 marks out of 50). A student shall be deemed to have satisfied
the academic requirements and earned the credits allotted to each subject/ course if
the student secures not less than 35% (18 Marks out of 50) in the semester-end
examination (SEE), and a minimum of 40% (40 marks out of 100) in the sum total of
the CIE (Continuous Internal Evaluation) and SEE (Semester End Examination)
taken together.
Continuous Internal Evaluation (CIE):
The CIE marks for the theory component of the IC shall be 30 marks and for the
laboratory component 20 Marks.
CIE for the practical component of the IC
• On completion of every experiment/program in the laboratory, the students shall be
evaluated and marks shall be awarded on the same day. The 15 marks are for
conducting the experiment and preparation of the laboratory record, the other 05
marks shall be for the test conducted at the end of the semester.
• The CIE marks awarded in the case of the Practical component shall be based on the
continuous evaluation of the laboratory report. Each experiment report can be
evaluated for 10 marks. Marks of all experiments’ write-ups are added and scaled
down to 15 marks.
• The laboratory test (duration 03 hours) at the end of the 15th week of the semester
/after completion of all the experiments (whichever is early) shall be conducted for 50
marks and scaled down to 05 marks. Scaled-down marks of write-up evaluations and
tests added will be CIE marks for the laboratory component of IC/IPCC for 20 marks.
• The minimum marks to be secured in CIE to appear for SEE shall be 12 (40% of
maximum marks) in the theory component and 08 (40% of maximum marks) in the
practical component. The laboratory component of the IC/IPCC shall be for CIE
only. However, in SEE, the questions from the laboratory component shall be
included. The maximum of 05 questions is to be set from the practical component of
IC/IPCC, the total marks of all questions should not be more than 25 marks. The
theory component of the IC shall be for both CIE and SEE.
5
Semester End Examination (SEE):
SEE for IC
Theory SEE will be conducted by University as per the scheduled time table, with
common question papers for the course (duration 03 hours)
1. The question paper will have ten questions. Each question is set for 20 marks.
2. There will be 2 questions from each module. Each of the two questions under a
module (with a maximum of 3 sub-questions), should have a mix of topics under that
module.
3. The students have to answer 5 full questions, selecting one full question from each
module. The theory portion of the Integrated Course shall be for both CIE and SEE,
whereas the practical portion will have a CIE component only. Questions mentioned
in the SEE paper shall include questions from the practical component).
Passing standard:
• The minimum marks to be secured in CIE to appear for SEE shall be 12 (40% of
maximum marks-30) in the theory component and 08 (40% of maximum marks -20)
in the practical component. The laboratory component of the IPCC shall be for CIE
only. However, in SEE, the questions from the laboratory component shall be
included. The maximum of 04/05 questions to be set from the practical component of
IPCC, the total marks of all questions should not be more than 30 marks.
• SEE will be conducted for 100 marks and students shall secure 35% of the
maximum marks to qualify for the SEE. Marks secured will be scaled down to 50.
6
Table of Contents
Sl.No Name of the Experiment Page. No
1 Develop a program to calculate the temperature converter
8
from degree to Fahrenheit.
2 Develop a program to find the roots of quadratic equations. 9
3 Develop a program to find whether a given number is prime
11
or not.
4 Develop a program to find key elements in an array using
12
linear search.
5 Given age and gender of a person, develop a program to
14
categorise senior citizen (male & female).
6 Generate Floyd’s triangle for given rows. 15
7 Develop a program to find the transpose of a matrix. 16
8 Develop a program to concatenate two strings, find length of a
18
string and copy one string to other using string operations.
9 Develop a modular program to find GCD and LCM of given
19
numbers.
10 Develop a program to declare the structure of employees and
display the employee records with higher salary among two 20
employees.
11 Develop a program to add two numbers using the pointers to
21
the variables.
12 Develop a program to find the sum of digits of a given
22
number.
13 Develop a program to perform Matrix Multiplication. 23
14 Develop a program to create an array of structures to store
book details and check whether a specific book, as requested 25
by the user, is available or not.
7
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:
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?
Exercise Problems:
1. Write a program to convert Fahrenheit to Celsius.
2. Modify the program to convert Kelvin to Celsius.
8
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) { // Real & distinct
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) { // Real & equal
root1 = -b / (2*a);
printf("Roots are real and equal: %.2f\n", root1);
}
else { // Complex
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:
9
Viva Questions:
1. What is the quadratic formula?
2. What does the discriminant signify?
3. How do you handle complex roots in C?
Exercise Problems:
1. Write a program to print only real roots if they exist.
2. Extend to handle cubic equations (bonus).
10
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; // isPrime = 1 means prime by default
printf("Enter a positive integer: "); // Ask user for number
scanf("%d", &num);
if (num <= 1) { // Numbers less than or equal to 1 are not prime
isPrime = 0;
} else {
for (i = 2; i <= num/2; i++) { // Check divisibility
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:
Viva Questions:
1. Define a prime number.
2. How does using sqrt(n) help?
3. How to handle large inputs?
Exercise Problems:
1. Print all primes up to N using Sieve of Eratosthenes.
2. Optimize primality test for large numbers.
11
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; // Array and search variables
printf("Enter number of elements: ");
scanf("%d", &n); // Input size of array
printf("Enter array elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]); // Read array elements
printf("Enter key element to search: ");
scanf("%d", &key); // Input element to be searched
for (i = 0; i < n; i++) {
if (arr[i] == key) { // If match found
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:
12
Viva Questions:
1. What is linear search?
2. What is its time complexity?
3. When is linear search preferred?
Exercise Problems:
1. Implement binary search (sorted array).
2. Modify to return all indices of occurrences.
13
Program 5: Given age and gender of a person, develop a program to categorise
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); // Space before %c ignores newline from previous input
if (age >= 60) { // Check senior citizen
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:
Viva Questions:
1. What is the commonly used age threshold for senior citizen?
2. How to safely read a char input?
3. How to validate inputs?
Exercise Problems:
1. Categorize into child/adult/senior.
2. Read gender string (Male/Female/Other) and handle.
14
Program 6: Generate Floyd’s triangle for given rows.
#include <stdio.h>
int main() {
int rows, i, j, num = 1; // rows = no. of rows, num = counter
printf("Enter number of rows: ");
scanf("%d", &rows); // Input number of rows
for (i = 1; i <= rows; i++) { // Outer loop → row control
for (j = 1; j <= i; j++) { // Inner loop → print numbers in each row
printf("%d ", num); // Print current number
num++; // Increase number for next print
}
printf("\n"); // Move to next line after each row
}
return 0;
}
Output:
Viva Questions:
1. What is Floyd's triangle?
2. How does the nested loop work?
3. How to modify to print pattern with characters?
Exercise Problems:
1. Print Pascal's triangle (n rows).
2. Print Floyd's triangle with row-wise reset (1..i each row).
15
Program 7: Develop a program to find the transpose of a matrix.
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10]; // Declare 2D arrays for matrix and its transpose
int r, c, i, j; // Variables for rows, columns, and loop counters
// 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); // Helpful prompt for beginners
for (j = 0; j < c; j++) {
scanf("%d", &a[i][j]); // Read element at row i, column 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]); // Print matrix elements
}
printf("\n"); // New line after each row
}
// 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]; // Assign element to transposed position
}
}
// 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]); // Print transposed element
}
printf("\n"); // New line after each row
}
return 0;
}
16
Output:
Viva Questions:
1. Define transpose of a matrix.
2. What are dimensions of transpose?
3. Can transpose be done in-place for square matrices?
Exercise Problems:
1. Check if matrix is symmetric.
2. Find trace of a square matrix.
17
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> // For string handling functions
int main() {
char str1[50], str2[50], result[100];
printf("Enter first string: ");
scanf("%s", str1); // Input first string
printf("Enter second string: ");
scanf("%s", str2); // Input second string
// Concatenate strings
strcpy(result, str1); // Copy str1 into result
strcat(result, str2); // Append str2 to result
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:
Viva Questions:
1. What is the null terminator in C strings?
2. Difference between strcpy and strncpy?
3. Why should buffers be large enough to hold concatenation?
Exercise Problems:
1. Reverse a string.
2. Count vowels and consonants in a string.
18
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; // Remainder becomes new divisor
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); // Input numbers
printf("GCD = %d\n", gcd(num1, num2));
printf("LCM = %d\n", lcm(num1, num2));
return 0;
}
Output:
Viva Questions:
1. Explain Euclid's algorithm for GCD.
2. How is LCM computed using GCD?
3. What about negative inputs?
Exercise Problems:
1. Compute GCD/LCM for an array of numbers.
2. Implement iterative GCD.
19
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; // Declare two employee variables
printf("Enter details of Employee 1 (Name ID Salary): ");
scanf("%s %d %f", e1.name, &e1.id, &e1.salary);
printf("Enter details of Employee 2 (Name ID Salary): ");
scanf("%s %d %f", e2.name, &e2.id, &e2.salary);
// Compare salaries
if (e1.salary > e2.salary)
printf("Employee with higher salary: %s (%.2f)\n", e1.name, e1.salary);
else if (e2.salary > e1.salary)
printf("Employee with higher salary: %s (%.2f)\n", e2.name, e2.salary);
else
printf("Both employees have equal salary\n");
return 0;
}
Output:
Viva Questions:
1. What is a structure? How is it useful?
2. How to read strings with spaces into struct fields?
3. Difference between struct and typedef struct?
Exercise Problems:
1. Store N employees and find the highest paid.
2. Sort employees by salary.
20
Program 11: Develop a program to add two numbers using the pointers to the
variables.
#include <stdio.h>
int main() {
int a, b, sum; // Variables to store numbers and their sum
int *p1, *p2; // Pointer variables
printf("Enter two integers: ");
scanf("%d %d", &a, &b); // Read input values
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); // Display result
return 0;
}
Output:
Viva Questions:
1. What is a pointer and how is it declared?
2. What does the unary & operator do?
3. What is dereferencing?
Exercise Problems:
1. Multiply two numbers using pointers.
2. Swap two variables using pointers.
21
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:
Viva Questions:
1. How do you extract digits from a number?
2. How to handle negative numbers?
3. What is modulo operator?
Exercise Problems:
1. Compute product of digits.
2. Reverse digits and print the reversed number.
22
Program 13: Develop a program to perform Matrix Multiplication.
#include <stdio.h>
int main() {
int a[10][10], b[10][10], product[10][10]; // Matrices and their product
int r1, c1, r2, c2; // Dimensions of matrices
int i, j, k; // Loop counters
// 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; // Exit program
}
// 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;
}
}
23
// Step 7: Multiply matrices
// product[i][j] = sum of (a[i][k] * b[k][j]) for k = 0 to c1-1
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:
Viva Questions:
1. What are the conditions for matrix multiplication?
2. What is the size of the result matrix?
3. Explain the triple loop.
Exercise Problems:
1. Implement matrix addition and subtraction.
2. Optimize using pointer arithmetic.
24
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;
}
25
Output:
Viva Questions:
1. What is an array of structures?
2. How to perform string comparisons?
3. How to allow titles with spaces?
Exercise Problems:
1. Search by author or id.
2. Make the database dynamic using malloc.
26