0% found this document useful (0 votes)
12 views33 pages

C Programming Lab Manual Guide

The lab manual for Fundamentals of Programming Languages at Savitribai Phule Pune University outlines the objectives and course outcomes for C programming, including understanding concepts, operators, control flow, and user-defined functions. It details the steps for program development, the structure of C programs, and provides exercises for practical application. Additionally, it includes instructions for preparing lab reports and examples of algorithms and code for various programming tasks.
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)
12 views33 pages

C Programming Lab Manual Guide

The lab manual for Fundamentals of Programming Languages at Savitribai Phule Pune University outlines the objectives and course outcomes for C programming, including understanding concepts, operators, control flow, and user-defined functions. It details the steps for program development, the structure of C programs, and provides exercises for practical application. Additionally, it includes instructions for preparing lab reports and examples of algorithms and code for various programming tasks.
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

LAB MANUAL

For :
Fundamentals of Programming Languages
Savitribai Phule Pune University
First Year of Engineering (2024 Pattern)

Prepared by

Mrs Jadhav Bhavana Sunil


Department of Computer Engineering

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 1
OBJECTIVES

1. To understand the fundamental Concepts of C Programming .


2. To acquire knowledge and Compare usage of Operators and Expressions in C Programming.
3. To apply Control Flow structures in C Programming for Problem solving
4. To design a solution using Arrays, Character and String Arrays in C programming
5. To design a develop solution for simple computational problems using User Defined
Functions and structures in C Programming

COURSE OUTCOME

On completion of the course, students will be able to:

CO1: To Design algorithms for simple computational problems.


CO2: To Use mathematical, Logical Operators and Expressions.
CO3: To apply Control Flow structures for decision making.
CO4: To design a solution using Arrays, Character and String Arrays.
CO5: To Design and apply user defined functions and structures

INTRODUCTION ABOUT LAB


Steps involved in program development:-

To develop the program in high level language and translate it into machine level
language following steps have to be practised.
1. Writing and editing the program.
2. Linking the program with the required library modules.
3. Compiling the program.
4. Executing the program.

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 2
Algorithm:-

It is a method of representing the step by step process for solving a problem. Each step
is called an instruction.
Characteristics of algorithm are:

 Finiteness:- It terminates with finite number of steps.

 Definiteness:- Each step of algorithm is exactly defined.

 Effectiveness:- All the operations used in the algorithm can be performed


exactly in a fixed duration of time.

 Input:- An algorithm must have an input before the execution of program


begins.

 Output:- An algorithm has one or more outputs after the execution of the
program.

Example of algorithm to find sum of two numbers:

Step1: BEGIN
Step2: READ a, b
Step3: ADD a and b and store in variable c
Step4: DISPLAY c
Step5: STOP

ABOUT C LANGUAGE
C is a programming language developed by Dennis Ritchie at AT&T‟s BELL
Laboratory of USA in 1972. Because of its reliability, C is very popular. C is highly
portable & it is well suited for structured programming. C program consists of
collection of functions.

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 3
HISTORY OF C

The milestones in C's development as a language are listed below:

1. BCPL - a user friendly OS providing powerful development tools


developed from BCPL c. 1967. Assembler was tedious, long and error
prone.
2. A new language “B” was attempted c. 1970 by Ken Thompson at Bell Labs.
3. UNIX was developed c. 1970 using “B” - DEC PDP-7 Assembly Language.
4. A totally new language “C”, a successor to “B” was developed c. 1971.
5. By 1973, UNIX OS was almost totally written in “C”.

Hardware Requirement: Desktop Computer / laptop computer.

Software Requirement: TURBO C in WINDOWS

TURBO C:
Open Turbo C from your Desktop or Programs menu. Select “File” from Menu
bar and select option “New” and Save C program with filename „.C‟ extension.
To do compiling – Select -> Compile from menu and click-> compile.
If the compilation is successful – you will see a “success” message. Else you will
see the number of errors.
To RUN the program – you may select ->Run from menu and click -> Run
Now you will see the output screen

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 4
.

STRUCTURE OF „C‟ PROGRAM :

C program is a collection of several instructions where each instruction is written as a


separate statement. The C program starts with a main function followed by the
opening braces which indicates the start of the function. Then follows the variable and
constant declarations which are followed by the statements that include input and
output statements.

C program may contain one or more sections as shown below:

DOCUMENTATION SECTION
LINK SECTION
DEFINITION SECTION

GLOBAL DECLARATION SECTION


Main() Function section
{
Declaration part
Executable part
}
SUBPROGRAM SECTION
User defined functions

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 5
Keywords

C has 32 keywords (reserved words with special meaning):

1. auto,
2. break,
3. case,
4. char,
5. const,
6. continue,
7. default,
8. do,
9. double,
10. else,
11. enum,
12. extern,
13. float,
14. for,
15. goto,
16. if,
17. int,
18. long,
19. register,
20. return,
21. short,
22. signed,
23. sizeof,
24. static,
25. struct,
26. switch,
27. typedef,
28. union,
29. unsigned,
30. void,
31. volatile, and
32. while.

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 6
Operators

C supports a rich set of operators, which are symbols used within an expression to
specify the manipulations to be performed while evaluating that expression. C has
the following operators:
arithmetic: +, -, *, /, %
assignment: =
augmented assignment: +=, - =, *=, /=, %=, &=, |=, ^=, <<=, >>=
bitwise logic: ~, &, |, ^
bitwise shifts: <<, >>
boolean logic: !, &&, ||
conditional evaluation: ? :
equality testing: = =, !=
calling functions: ( )
increment and decrement: ++ and - -
member selection: ., ->
object size: sizeof
order relations: <, <=, >, >=
reference and dereference: &, *, [ ]
sequencing: ,
subexpression grouping: ( )
type conversion: (typename)

SALIENT FEATURES OF C

C language has some characteristics that define the language and also have led to its
popularity as a programming language.

 Small size.
 Extensive use of function calls.
 Structured language.
 Low level (Bitwise) programming readily available.
 Pointer implementation - extensive use of pointers for memory, array,
structures and functions.
 It has high-level constructs.
 It can handle low-level activities.
 It produces efficient programs.
 It can be compiled on a variety of computers.

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 7
INSTRUCTIONS TO STUDENTS FOR PREPARING
PROGRAMMING IN C LAB REPORT

This Lab Manual is prepared to help the students with their practical understanding
and development of programming skills, and may be used as a base reference during
the lab/practical classes.

Students have to submit Lab Exercise report of previous lab into corresponding next
lab, and can be collected back after the instructor/course co-ordinator after it has been
checked and signed. At the end of the semester, students should compile all the Lab
Exercise reports into a single report and submit during the end semester sessional
examination.

“Sample of Lab report” is shown for LAB Exercise #1 in this manual. For the rest of
the labs, the reporting style as provided is to be followed. The lab report to be
submitted during the end semester Sessional Examination should include at least the
following topics:-
1. Top Cover page – pg. 9 (to be used while compiling all the Lab Exercise
reports into single report)
2. Index – pg. 10 (to be used while compiling all the Lab Exercise reports into
single report)
3. Cover page – pg. 11 (to be attached with every Lab Exercise)
4. Title of the program
5. Algorithm (optional)
6. Flowchart (optional)
7. Coding
8. Output (compilation, debugging & testing)

Students are to show the coding and output to the instructor/course co-ordinator for the
additional lab exercises given in each lab module.
Note: The lab exercises may not be completed in a single specific lab. Students are
encouraged to complete the programming questions given in the exercise prior to come
to the lab hour and do the lab for the given programs.

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 8
FUNDAMENTALS OF PROGRAMMING LANGUAGES LAB REPORT

SUBMITTED BY
Name:
Roll No.
Division:

Dattakala Group Of Institutions Faculty Of Engineering

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 9
INDEX

Exercises/Assignments Page No.


1 .Exercise – [Link] accept the number and Compute
a) Square of number,
b) check for prime,
c) factorial of number
2. Exercise – [Link] accept from user the number of Fibonacci numbers to be generated and
print the Fibonacci series.
3. Exercise – [Link] array do the following:
1. Find given element in array
4. Exercise – [Link] a C program for employee salary calculation given, Basic, H.R.A.
20 % of Basic and D.A. 150 % of Basic.
5. Exercise – [Link] accept a student's marks for five subjects, compute his/her result.
Student is passing if he/she scores marks equal to and above 40 in each course. If student
scores aggregate greater than 75%, then the grade is distinguished. If aggregate is 60>=
and <75 then the Grade of first division. If aggregate is 50>= and <60, then the grade is
second division. If aggregate is 40>= and <50, then the grade is third division.
[Link] – 6To accept two numbers from user and compute smallest divisor and
Greatest Common Divisor of these two numbers.
7.. Exercise – 7Write a C program that accepts a string from the user and performs the
following string operations-
i. Calculate length of string
ii. String reversal
iii. Equality check of two Strings
iii. Check palindrome ii. Check substring
8. Exercise – 8Create Structure EMPLOYEE for storing details (Name, Designation,
gender, Date of Joining and Salary), and store the data and update the data in structure.

LAB Exercise #1

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 10
Fundamentals of Programming Languages Exercise Report Submitted By:

Name:

RollNo:

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 11
LAB EXERCISE #1

Objective(s):
To be familiar with syntax and structure of C- programming.
To learn problem solving techniques using C

Program: To accept the number and Compute, a) Square of number, b) check for
prime, c) factorial of number
Algorithm:

Algorithm of Square of a number

1. Start

2. Read input number (num)

3. Calculate square = num * num

4. Display square

5. End

Flowchart:

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 12
Code:
/* C Program to Calculate Square of a Number */

#include<stdio.h>

int main()

int number, Square;

printf(" \n Please Enter any integer Value : ");

scanf("%d", &number);

Square = number * number;

printf("\n Square of a given number %d is = %d", number, Square);

return 0;

Output :

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 13
Algorithm:

Algorithm to Find Prime Number Program in C

STEP 1: Take num as input.

STEP 2: Initialize a variable temp to 0.

STEP 3: Iterate a “for” loop from 2 to num/2.

STEP 4: If num is divisible by loop iterator, then increment

temp.

STEP 5: If the temp is equal to 0,

Return “Num IS PRIME”.

Else,

Return “Num IS NOT PRIME”.

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 14
Flowchart

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 15
Code:

1. #include<stdio.h>
2. int main(){
3. int n,i,m=0,flag=0;
4. printf("Enter the number to check prime:");
5. scanf("%d",&n);
6. m=n/2;
7. for(i=2;i<=m;i++)
8. {
9. if(n%i==0)
10. {
11. printf("Number is not prime");
12. flag=1;
13. break;
14. }
15. }
16. if(flag==0)
17. printf("Number is prime");
18. return 0;
19. }

Output :

Algorithm:

Algorithm of factorial of a number

Step 1: Start
Step 2: Read a number n
Step 2: Initialize variables:
i = 1, fact = 1
Step 3: if i <= n go to step 4 otherwise go to step 7
Step 4: Calculate
fact = fact * i
Step 5: Increment the i by 1 (i=i+1) and go to step 3
Step 6: Print fact
Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 16
Step 7: Stop

Flowchart:

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 17
Code:
1. #include<stdio.h>
2. int main()
3. {
4. int i,fact=1,number;
5. printf("Enter a number: ");
6. scanf("%d",&number);
7. for(i=1;i<=number;i++){
8. fact=fact*i;
9. }
10. printf("Factorial of %d is: %d",number,fact);
11. return 0;
12. }

Output :
+

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 18
LAB EXERCISE #2

Objective(s):
To be familiar with different data types, Operators and Expressions in C.
Program: To accept from user the number of Fibonacci numbers to be generated and
print the Fibonacci series.
Algorithm:
Fibonacci Series Algorithm:
[Link]
[Link] variables i, a,b , show
[Link] the variables, a=0, b=1, and show =0
[Link] the number of terms of Fibonacci series to be printed
[Link] First two terms of series
[Link] loop for the following steps
-> show=a+b
-> a=b
-> b=show
-> increase value of i each time by 1
-> print the value of show
End

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 19
Flowchart:

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 20
Code: (Use comments wherever applicable)

1. #include<stdio.h>
2. int main()
3. {
4. int n1=0,n2=1,n3,i,number;
5. printf("Enter the number of elements:");
6. scanf("%d",&number);
7. printf("\n%d %d",n1,n2);//printing 0 and 1
8. for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
9. {
10. n3=n1+n2;
11. printf(" %d",n3);
12. n1=n2;
13. n2=n3;
14. }
15. return 0;

Output :

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 21
LAB EXERCISE #3

Objective(s):
To understand the programming knowledge using Decision Statements (if, if-else,
if-else-if ladder, switch and GOTO)

Program: Write a program to Find given element in array.

Code:
#include <stdio.h>

int main() {
int n, i, search, flag = 0;
int arr[100];

// Collecting the number of elements in the array


printf("Enter the number of elements in array: ");
scanf("%d", &n);

// Obtaining the elements


printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Getting the element to search


printf("Enter the element to search: ");
scanf("%d", &search);

// Searching the element


for(i = 0; i < n; i++) {
if(arr[i] == search) {
printf("Element found at position: %d\n", i);
flag = 1;
break;
}
}

if(flag == 0) {
printf("Element not found in the array.\n");
}

return 0;
}

Output

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 22
Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 23
LAB EXERCISE #4
Objective(s):
To understand the programming using Loop & nested loop Statements (for,
while, do-while)

Program: Write a C program for employee salary calculation given, Basic, H.R.A. 20 % of
Basic and D.A. 150 % of Basic.

Code:

#include <stdio.h>
int main()
{
//variable to store values
float basic, da, hra;

float net_salary;

//input required fields


printf("Enter Basic Salary ($): ");
scanf("%f",&basic);
printf("Enter HRA ($): ");
scanf("%f",&hra);

//calculate DA 12% of Basic Salary


da = (basic*150)/100;
//calculate net salary
net_salary = basic + da + hra;

//printing Net salary


printf("Net Salary is: $ %.02f\n",net_salary);

return 0;
}
Output:

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 24
Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 25
LAB EXERCISE #5

Objective(s):
To understand programming using different dimensions of Array.

Program: To accept a student's marks for five subjects, compute his/her result.
Student is passing if he/she scores marks equal to and above 40 in each course. If
student scores aggregate greater than 75%, then the grade is distinguished. If
aggregate is 60>= and = and = and

Code:
#include <stdio.h>

int main()
{
int phy, chem, bio, math, comp;
float per;

/* Input marks of five subjects from user */


printf("Enter five subjects marks: ");
scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp);

/* Calculate percentage */
per = (phy + chem + bio + math + comp) / 5.0;

printf("Percentage = %.2f\n", per);

/* Find grade according to the percentage */


if(per >= 90)

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 26
{
printf("Grade A");
}
else if(per >= 80)
{
printf("Grade B");
}
else if(per >= 70)
{
printf("Grade C");
}
else if(per >= 60)
{
printf("Grade D");
}
else if(per >= 40)
{
printf("Grade E");
}
else
{
printf("Grade F");
}

return 0;
}

Output:

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 27
Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 28
LAB EXERCISE #6

Objective(s):
To understand function programming, its types and function-call.

Program: To accept two numbers from user and compute smallest divisor and
Greatest Common Divisor of these two numbers..

Code:
#include <stdio.h>
int main()
{
int n1, n2, i, gcd;

printf("Enter two integers: ");


scanf("%d %d", &n1, &n2);

for(i=1; i <= n1 && i <= n2; ++i)


{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}

printf("G.C.D of %d and %d is %d", n1, n2, gcd);

return 0;
}

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 29
LAB EXERCISE #7

Objective(s):
To understand programming with Pointer, String and Function call by reference.

Program: Write a C program that accepts a string from the user and performs the following
string operations- i. Calculate length of string ii. String reversal iii. Equality check of two
Strings iii. Check palindrome ii. Check substring.

Code:
#include <stdio.h>
#include <string.h>

void main()
{
char str[100];

printf("Enter a string: ");


scanf("%s", str);

int len = strlen(str);

printf("The length of the string is: %d\n", len);


}

Output:

Exercise:
i Write a C program that accepts a string from the user and performs the Reverse string
operations.

ii. Write a C program that accepts a string from the user and performs the Equality check of two Strings.

iii. Write a C program that accepts a string from the user and performs the Equality check palindrome

iv Write a C program that accepts a string from the user and performs the substring.

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 30
LAB EXERCISE #8

Objective(s):
To understand programming with Structure.

Program 1: Create Structure EMPLOYEE for storing details (Name, Designation, gender,
Date of Joining and Salary), and store the data and update the data in structure.
Code:

#include <stdio.h>

// Define the structure "Employee"


struct Employee {
int employeeID;
char name[50];
char designation[30];
char gender[20];
int doj[3];;
float salary;
};

int main() {
// Declare variables to store details for three employees
struct Employee employee1;

// Input details for the first employee


printf("Input details for Employee 1:\n");
printf("Employee ID: ");
Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 31
scanf("%d", &[Link]);

printf("Name: ");
scanf("%s", [Link]);
printf("Designation: ");
scanf("%s", [Link]);
printf("gender: ");
scanf("%s", [Link]);
// Assuming names do not contain spaces
printf("Date of joining: ");
scanf("%d", [Link]);
printf("Salary: ");
scanf("%f", &[Link]);
return 0;
}

Output:

Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 32
Lab Manual for Programming in C Lab by Er. Suraj Deb Barma Page 33

You might also like