PSU C Programming Lab Manual - B20EN0308 - ECE
PSU C Programming Lab Manual - B20EN0308 - ECE
Communication Engineering
Program : B.Tech. in ECE
Problem Solving Using C Programming Lab
LABORATORY MANUAL
B20EN0308
III Semester
2021-25
Rukmini Educational
Charitable Trust www.reva.edu.in
Vision of the University
‘’REVA University aspires to become an innovative university by developing excellent human
resources with leadership qualities, ethical and moral values, research culture and innovative skills
through higher education of global standards”
The Program Educational Objectives of B. Tech in B.Tech in ECE/ ECM Engineering are as
follows:
PEO -1: To have successful professional careers in industry, government, academia, and military as
innovative engineers.
PEO -2: To successfully solve engineering problems associated with the lifecycle of B.Tech in
ECE/ ECM Engineering Systems either leading a team or as a team member.
PEO -3: To continue to learn and advance their careers through activities such as participation in
professional organizations, attainment of professional certification for lifelong
Learning and seeking higher education.
PEO -4: To be active members ready to serve the society locally and internationally and will undertake
entrepreneurship for the growth of economy and to generate employment.
Program Outcomes (POs)
On successful completion of the program, the graduates of B. Tech. in ECE/ ECM Engineering
program will be able to:
Contents
Total 1 2 2 - 28 50% 50
%
COURSE OVERVIEW:
C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to
develop the UNIX operating system at Bell Labs. C programming is a general-purpose, procedural
programming language used to develop software like operating systems, databases, compilers, and
so on. The main features of C language include low-level access to memory, a simple set of
keywords, and clean style. Many later languages have borrowed syntax/features directly or indirectly
from C language. Like syntax of Java, PHP, JavaScript, and many other languages are mainly based
on C language.
COURSE OBJECTIVES:
The objectives of this course are:
1. Provide exposure to problem-solving through C procedural programming
2. Explore the structure and syntax of the C programming language
3. Illustrate the applications of data types, operators, arrays, and control flow statements in problem-
solving.
4. Provide insight into concepts like pointers, structures, unions and records.
Solve data processing applications using appropriate data types, operators 1,2,3,5,10
CO2 1,2,3
and control flow statements.
1,2,3,5,10
Write C programs using derived data types like arrays and strings to
CO3 1,2,3
operate on block of data.
Solve complex problems using procedure-oriented (modular) 1,2,3,5,10
CO4 1,2,3
programming approach
Design and develop computer programs using the concept of 1,2,3,5,10
CO5 1,2,3
pointers, structures, unions and records.
C06 Demonstrate the creation of file and file operations in C-language 1,2,3,5,10 1,2,3
CO1
CO2
CO3
CO4
CO5
CO6
PSO2
PO10
PO11
PO12
PSO3
CO#/
PO1
PO2
PO3
PO4
PO5
PO6
PO7
PO8
PO9
POs
CO1 3 3 2 1 2 1 2 2 1
3 3 2 1 3 1 3 2 1
CO2
CO3 3 3 2 1 3 1 3 3 2
CO4 3 3 3 1 3 1 3 3 2
CO5 3 3 3 1 2 1 3 2 3
CO6 3 3 3 1 2 1 3 2 3
Note:1-Low,2-Medium,3-High
Click on Save as, and save file with your SRN with appropriate extension other than C
drive
Click on Compile Button (under Execute) to compile your source code. If there are any
errors in your program, then a window at the bottom will specify the warnings, after
program is compiled, click on Run button (Below Compile).
As soon as you Run the program, output window opens momentarily and then closes.
When you run the program, output window will show the string as below.
LAB EXERCISE #1
Program: Write a Program to calculate and display the volume of a CUBE having its
height (h=10cm), width (w=12cm) and depth (8cm).
Algorithm:
1. Start
2. Define variables: h(int), w(int), d(int), vol(int)
3. Assign value to variables: h = 10, w=12, d=8
4. Calculate the volume as: vol = h*w*d
5. Display the volume (vol)
6. Stop
Flowchart:
START
Define h, w, d, Vol
Print Vol
END
Code:
#include<stdio.h>
int main ()
{
//start the program
int h, w, d, vol; //variables declaration
h=10;
w=12;
d=8; //assign value to variables
vol=h*w*d; //calculation using mathematical formula
printf("The Volume of the cube is: %d",vol); //display the volume
return 0;
//end the main program
}
Output:
The Volume of the cube is: 960
LAB EXERCISE #2
Program: Write a program to take input of name, roll no and marks obtained
by a student in 4 subjects of 100 marks each and display the name,
Roll no with percentage score secured.
Algorithm:
1. Start
2. Define variables: name, Roll no, sub1, sub2, sub3, sub4, sum, score
3. Take input from keyboard for all the input variables
4. Calculate the sum of marks of 4 subjects and also calculate the percentage score
as:
Sum=sub1+sub2+sub3+sub4;
score = (sum/400) * 100
5. Display the name, roll number and percentage score.
6. Stop
Flowchart:
START
Sum=sub1+sub2+sub3+sub4
score=(sum/400)*100
END
Code:
#include<stdio.h>
int main()
{
char name[20];
int rollno;
float sub1,sub2,sub3,sub4,sum,score;
printf("Enter name of student: ");
scanf(“%s”,&name);
printf("\n Enter Roll Number: ");
scanf("%d", &rollno);
printf ("\n Enter Marks in 4 Subjects:\n");
scanf("%f%f%f%f", &sub1, &sub2, &sub3, &sub4);
sum=sub1+sub2+sub3+sub4;
score = (sum/500)*100;
printf("\n Name of student: %s", name);
printf("\n Roll Number: %d", rollno);
printf ("\n Percentage score secured: %2.2f%c", score,'%');
return 0;
}
Output:
Enter name of student: Ajit Singh
Roll Number: 25
Enter Marks in 4 Subjects:
50
75
85
62
Name of student: Ajit Singh
Roll Number: 25
Percentage score secured: 68.00
LAB EXERCISE #3
Program: 3a) Write a program to print whether a given number is even or odd.
3b) Write a program to print even numbers from 1 to 10.
Algorithm:
1. Start
2. [ Take Input ] Read: Number
3. Check: If Number%2 == 0 Then
4. N is an Even Number.
5. Else
6. N is an Odd Number.
7. Step 4: stop
Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
if(num%2==0)
printf("\n %d is even", num);
else printf("\n %d is odd", num);
return 0;
}
Output:
Enter the number: 6
6 is even
Code:
#include <stdio.h>
int main()
{
int i;
printf("Even numbers between 1 to 10 (inclusive):\n");
for (i = 1; i <= 10; i++)
{
if(i%2 == 0)
{
printf("%d ", i);
}
}
return 0;
}
Output:
Even numbers between1 to 10 (inclusive):
2 4 6 9 8 10
LAB EXERCISE #4
Algorithm:
1. Start
2. Declare a variable
3. Initialize the variable.
4. Use a for loop that iterates from 2 to N
5. Declare the count and initialize it to 0.
6. If the number is divisible by any of the numbers in between the loop then increment the count.
7. If the count is not equal to 0 then, it is not a prime number.
8. If the count is equal to 0, then it is a prime number.
9. Stop.
Code:
#include <stdio.h>
int main ()
{
int n, i, flag = 0;
printf ("Enter a positive integer: ");
scanf("%d", &n);
{
// condition for non-prime
if (n % i == 0)
{
flag = 1;
break;
}
}
if (n == 1)
{
printf("1 is neither prime nor composite.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Output:
Enter a positive integer: 29
29 is a prime number.
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Output:
Enter a number: 5
Factorial of 5 is: 120
LAB EXERCISE #5
Algorithm:
1. Start
2. Declare character type variable ch
3. Read ch from User
4. // Checking both lower and upper case vowel
IF (ch == 'a' || ch == 'A' ||
ch == 'e' || ch == 'E' ||
ch == 'i' || ch == 'I' ||
ch == 'o' || ch == 'O' ||
ch == 'u' || ch == 'U' )
5. Print "Vowel"
6. ELSE
7. Print "Consonant"
8. Stop
Flowchart:
5a. Write a program to find whether a character is consonant or vowel using switch
statement.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
printf("Enter any Alphabet\n");
scanf("%c",&ch);
switch(ch)
{
//check lower case vowel letters
case 'a':
printf("%c is a vowel",ch);
break;
case 'e':
printf("%c is a vowel",ch);
break;
case 'i':
printf("%c is a vowel",ch);
break;
case 'o':
printf("%c is a vowel",ch);
break;
case 'u':
printf("%c is a vowel",ch);
break;
case 'A':
printf("%c is a vowel",ch);
break;
case 'E':
printf("%c is a vowel",ch);
break;
case 'I':
printf("%c is a vowel",ch);
break;
case 'O':
printf("%c is a vowel",ch);
break;
case 'U':
printf("%c is a vowel",ch);
break;
default:
printf("%c is a consonant",ch);
break;
}
return 0;
}
Output:
case 1
a is a vowel
case 2
5b.Write a program to print the sum of numbers from 1 to 10 using for loop
Code:
#include <stdio.h>
int main()
{
int j, sum = 0;
Output :
The first 10 natural number is:
1 2 3 4 5 6 7 8 9 10
The Sum is : 55
LAB EXERCISE #6
Output:
LAB EXERCISE #7
Code:
#include<stdio.h>
int fact(int);
int main()
{
int x,n;
printf(" Enter the Number to Find Factorial :");
scanf("%d",&n);
x=fact(n);
printf(" Factorial of %d is %d",n,x);
return 0;
}
int fact(int n)
{
if(n==0)
return(1);
return(n*fact(n-1));
}
Output :
Enter the Number to Find Factorial :5
Factorial of 5 is 120
7b. Write a program to add, subtract, multiply and divide two integers using user-defined type
function with return type.
Code:
#include<stdio.h>
int main()
{
int num1, num2, res;
printf("Enter any two number: ");
scanf("%d%d", &num1, &num2);
res = num1+num2;
printf("Addition = %d", res);
res = num1-num2;
printf("Subtraction = %d", res);
res = num1*num2;
printf("Multiplication = %d", res);
res = num1/num2;
printf("Division = %d", res);
return 0;
}
Output :
Enter any two number: 5 7
Addition : 12
Subtraction: -2
Multiplication: 35
Division: 0
7c). Write a program to swap two integers using call by value and call by
reference methods of passing arguments to a function.
int main(){
int x = 10;
int y = 11;
printf("Values before swap: x = %d, y = %d\n", x,y);
swap(x,y);
printf("Values after swap: x = %d, y = %d", x,y);
}
Output :
Values before swap: x = 10, y = 11
Values after swap: x = 10, y = 11
Code:
#include <stdio.h>
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main(){
int x = 10;
int y = 11;
printf("Values before swap: x = %d, y = %d\n", x,y);
swap(&x,&y);
printf("Values after swap: x = %d, y = %d", x,y);
}
Output :
Values before swap: x = 10, y = 11
Values after swap: x = 11, y = 10
LAB EXERCISE #8
Code:
#include <stdio.h>
/*structure declaration*/
struct employee
{
char name[30];
int empId; float salary;
};
int main()
{ /*declare and initialization of structure variable*/
struct employee emp={"Mike",1120,76909.00f};
Output
Name: Mike
Id: 1120
Salary: 76909.000000
Code:
#include <stdio.h>
// union declaration
union pack {
char a;
int b;
double c;
};
int main()
{
pack p; //union object/variable declaration
// assign value to each member one by one other it will replace last value
p.a = 'A';
printf("\nValue of a:%c", p.a);
p.b = 10;
printf("\nValue of b:%d", p.b);
p.c = 12345.6790;
printf("\nValue of c:%f", p.c);
return 0;
}
Output:
Occupied size by union pack: 8
Value of a:A
Value of b:10
Value of c:12345.679000
Value of a:�, b:-377957122, c:12345.679000
LAB EXERCISE #9
Program: 9a). write a c program to find biggest among three numbers using
pointers
9b). write a c program to swap the values of two variables using
pointer
9(c). Write a program to swap to array using pointers.
Code:
#include <stdio.h>
int main()
{
int num1, num2, num3;
int *p1, *p2, *p3;
return 0;
}
Output:
Enter the first number: 10
Enter the second number: 5
Enter the third number:
15 is the largest number
9b) write a c program to swap the values of two variables using pointer
Code:
int a, b, temp ;
int *p1, *p2 ;
printf(" Enter the first number : ") ;
scanf("%d ", & a) ;
printf("\n Enter the second number : ") ;
scanf("%d ", & b) ;
printf("\n Two Number before swapping :%d, %d ", *p1, *p2) ;
temp = *p1 ;
*p1 = *p2 ;
*p2 = temp ;
Output:
Enter the first number: 10
Enter the second number: 15
Two Number before swapping: 10, 15
Two Number after swapping: 15, 10
Code:
#include <stdio.h>
/* Function declarations */
void inputArray(int *arr, int size);
void printArray(int *arr, int size);
void swapArray(int *sourceArr, int *destArr, int size);
int main()
{
int sourceArr[MAX_SIZE];
int destArr[MAX_SIZE];
int size;
/*
* Print elements of both arrays before swapping
*/
printf("\n\nSource array before swapping: ");
printArray(sourceArr, size);
/*
* Print elements of both arrays after swapping
*/
printf("\n\nSource array after swapping: ");
printArray(sourceArr, size);
return 0;
}
/**
* Function used to read input from user in an array.
*
* @arr Pointer to array to store input
* @size Size of the array
*/
void inputArray(int *arr, int size)
{
// Pointer to last element of array.
int *arrEnd = (arr + (size - 1));
/**
/**
* Function to swap elements of two arrays.
*
* @sourceArr Pointer to source array to swap.
* @destArr Pointer to destination array to swap.
* @size Size of array.
*/
void swapArray(int * sourceArr, int * destArr, int size)
{
// Pointer to last element of source array
int * sourceArrEnd = (sourceArr + (size - 1));
/*
* Swap individual element of both arrays
*/
while(sourceArr <= sourceArrEnd && destArr <= destArrEnd)
{
*sourceArr ^= *destArr;
*destArr ^= *sourceArr;
*sourceArr ^= *destArr;
Output:
Enter size of array: 10
Enter 10 elements in source array: 1 2 3 4 5 6 7 8 9 10
Enter 10 elements in destination array: 10 20 30 40 50 60 70 80 90 100
Source array after swapping: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
Destination array after swapping: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Program: 10(a). Write a program to create a file called ‘record’ and store
Information about a person, in-terms of his name, age,
and salary.
10(b). Write a program to illustrate how a file stored on the disk
is read.
Code:
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
fptr = fopen("emp.rec", "w");
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);
fclose(fptr);
getch();
}
Output:
Enter the name
Enter the age
Enter the salary
10(b). Write a program to illustrate how a file stored on the disk is read.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
FILE *fptr;
char filename[15];
char ch;
clrscr();
if (fptr == NULL)
{
printf("Cannot open file\n");
exit(0);
}
ch = fgetc(fptr);
fclose(fptr);
getch();
}
Output
Enter the filename to be opened
emp.rec
Name = Prabhu
Age = 25
Salary = 25000.00
Follow us on
/REVAUniversity