Lab Manual C AIDS - 2
Lab Manual C AIDS - 2
LAB MANUAL
OF
Programming in C
(MCACCA1203)
1
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
TABLE OF CONTENTS
2 Instructions 4
3 Syllabus 5
4 Marks Scheme 6
5 Lab Plan 7
6 Lab objective 8
8 Resources 10
9 Experiments 11-47
2
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
LAB RULES
Responsibilities of Users
Users are expected to follow some obvious rules of conduct:
Always:
▪ Wait for the previous class to leave before the next class enters.
▪ Turn off the machine before leaving the lab unless a member of lab staff has
respond) or the room in general (e.g. cooling, heating, lighting) please report it
to lab staff immediately. Do not attempt to fix the problem yourself.
Never:
3
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
▪ Do not adjust the heat or air conditioners. If you feel the temperature is not
properly set, inform lab staff; we will attempt to maintain a balance that is
healthy for people and machines.
▪ Do not remove printers and machines from the network without being explicitly
▪ Don't monopolize equipment. If you're going to be away from your machine for
more than 10 or 15 minutes, log out before leaving. This is both for the security
of your account, and to ensure that others are able to use the lab resources while
you are not.
▪ Don’t use internet, internet chat of any kind in your regular lab schedule.
▪ No food or drink is allowed in the lab or near any of the equipment. Aside from
the fact that it leaves a mess and attracts pests, spilling anything on a keyboard
or other piece of computer equipment could cause permanent, irreparable, and
4
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
costly damage. (and in fact has) If you need to eat or drink, take a break and do
so in the canteen.
▪ Don’t bring any external material in the lab, except your lab record, copy and
books.
▪ Don’t bring the mobile phones in the lab. If necessary then keep them in silence
mode.
While labs are a natural place for conversations of all types, kindly keep the
volume turned down.
If you are having problems or questions, please go to either the faculty, lab in-charge or
the lab supporting staff. They will help you. We need your full support and cooperation
for smooth functioning of the lab.
5
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
INSTRUCTIONS
All the students are supposed to prepare the theory regarding the next experiment.
Students are supposed to bring the practical file and the lab copy.
Previous programs should be written in the practical file.
All the students must follow the instructions, failing which he/she may not be allowed in
the lab.
6
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Given the values of the variables x, y and z, write a program to rotate their values such that x
has the value of y, y has the value of z, and z has the value of x
Write a program that reads a floating point number and then displays the right-most digit of
the integral part of the number.
Program to find largest and smallest number from four given number.
Write a C program in which enter any number by the user and perform the operation of Sum of
digits of entered number.
Write a program to convert years into Minute, Hours, Days, Months, Seconds using switch ()
statements
7
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Write a C program in which a function prime that returns 1 if its argument is a prime and
return zero otherwise.
Write a C program in which enter 10 elements by the user and perform the operation of
sorting in ascending order
Write a program to determine the length of the string and find its equivalent ASCII codes.
Write a program to delete all the occurrences of the vowels in a given text. Assume that the
text length will be of one line
Write a program to maintain the library record for 100 books with book name, author’s
name, and edition, year of publishing and price of the book
MARKS SCHEME
8
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
TOTAL 100 %
Total Marks – 10
Performance
Attendance Discipline Record Total
& Viva
1 1 5 3 10
9
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
LAB PLAN
Exp. 1 1 1
Exp. 2 1 2
Exp.3 1 3
Exp.4 2 4
Exp.5 1 5
Exp.6 1 6
Exp.7 1 7
Exp.8 2 8
Exp.9 1 9
Exp.10 1 10
Exp.11 1 11
Exp.11 1 11
Exp.12 1 12
Ex 13 1 13
Ex 14 1 14
Ex 15 1 15
Ex 16 1 16
Exp.17 1 17
10
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Ex. 18 1 18
Ex. 19 1 19
Ex. 20 1 20
Ex. 21 1 21
Lab Objective
11
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
A. LIST OF EXPERIMENTS:
1 Given the values of the variables x, y and z, write a program to rotate their values such that x
has the value of y, y has the value of z, and z has the value of x
2 Write a program that reads a floating point number and then displays the right-most digit of
the integral part of the number.
4 Program to find largest and smallest number from four given number.
6 Write a C program in which enter any number by the user and perform the operation of Sum of
digits of entered number.
10 Write a program to convert years into Minute, Hours, Days, Months, Seconds using switch ()
statements
12
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
15 Write a C program in which a function prime that returns 1 if its argument is a prime and
return zero otherwise.
17 Write a C program in which enter 10 elements by the user and perform the operation of
sorting in ascending order
19 Write a program to determine the length of the string and find its equivalent ASCII codes.
20 Write a program to delete all the occurrences of the vowels in a given text. Assume that the
text length will be of one line
21
Write a program to maintain the library record for 100 books with book name, author’s
name, and edition, year of publishing and price of the book
13
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Hardware :
1.Computer&
2.Peripheral devices
Software:
1. Turbo C ++
Text Books:
Let Us C by Yashvant Kanetkar
Reference Books:
Programming in ANSI C by E. Balagurusamy
Reference Websites:
1. https://www.programiz.com/c-programming
2. https://www.geeksforgeeks.org/c-programming-language/
14
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Experiments
15
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Experiment No.:1
Name of Experiment: Given the values of the variables x, y and z, write a program to
rotate their values such that x has the value of y, y has the value of z, and z has the value of x.
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y,z, temp;
clrscr();
printf("Enter first number \n");
scanf("%d", &x);
printf("Enter second number \n\n");
scanf("%d", &y);
printf("Enter third number \n\n");
scanf("%d", &z);
printf("Before swapping \n\t value of x: %d\n\t value of y: %d
\n\t value of z: %d\n\n ", x,y,z);
temp=x;
x=y;
y=z;
z=temp;
printf("After swapping \n\t value of x: %d\n\t value of y: %d \n\t
value of z: %d\n", x,y,z);
getch();
Output:
16
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Viva Questions:
Q. 4 What functions we use in C to take input and output? Name those two functions.
Experiment 2
Name of Experiment: Write a program that reads a floating point number and then displays
the right-most digit of the integral part of the number.
Code:
17
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
#include <stdio.h>
#include<conio.h>
int main() {
float fnum;
clrscr();
scanf("%f", &fnum);
inum = (int)fnum;
rm= inum%10;
printf("The right most digit of the floating point number is:%d \n", rm);
getch();
return 0;
Output:
18
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Viva Questions:
Experiment 3
Name of Experiment: Write a C program to calculate the sum of digits of given number.
Code:
19
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
#include <stdio.h>
#include<conio.h>
int main()
{
int n, t, sum = 0, remainder;
clrscr();
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
getch()
return 0;
}
Viva Questions:
Experiment 4
Name of Experiment: Write a C program to find largest and smallest number from four
given number.
Aim: find the largest among four numbers
Code:
#include<stdio.h>
#include<conio.h>
int main(){
clrscr();
21
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
if(num1>num2&&num1>num3&&num1>num4){
else if(num2>num3&&num2>num4){
else if(num3>num4){
else{
22
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
getch();
return 0;
Output:
Viva Questions:
Experiment 5
Name of Experiment: Write a C program to find whether the year is a leap year or not.
Aim: Find leap year
Code:
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
return 0;
24
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Output:
Viva Question:
Q.4 Write the same logic of leap year with the help of operators.
Experiment 6
Name of Experiment: Write a C program in which enter any number by the user and
perform the operation of Sum of digits of entered number.
Aim: To take a number as input from the user and perform its sum.
Code:
#include <stdio.h>
int main()
25
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
remainder = t % 10;
t = t / 10;
return 0;
Output:
Viva Questions:
26
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Q.4 What statement you can use to terminate the infinite loop?
Experiment 7
Name of Experiment: Write a C Program to convert Decimal number to Binary number.
Aim: Convert decimal number into binary
Code:
#include <stdio.h>
void decToBinary(int n)
{
// array to store binary number
int binaryNum[32];
int n;
printf("Enter any decimal number: ");
scanf("%d",&n);
//int n = 17;
decToBinary(n);
return 0;
}
Viva Questions:
(a) 123
(b) 27
Q.2 What is the difference between a decimal and binary number system?
Experiment 8
Name of Experiment: Find the sum of this series up to n terms 1+2+3+4+5+6+……….
Aim: find the sum of n terms
Code:
#include<stdio.h>
28
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
int main() {
int n,i;
int sum=0;
printf("Enter the n i.e. max values of series: ");
scanf("%d",&n);
sum = (n * (n + 1)) / 2;
printf("Sum of the series: ");
for (i =1;i <= n;i++) {
if (i!=n)
printf("%d + ",i); else
printf("%d = %d ",i,sum);
}
return 0;
}
Output:
Viva Question:
Experiment 9
29
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
#include <stdio.h>
#include <math.h>
int main()
originalNum = num;
n = 0;
result = 0;
while (originalNum != 0)
originalNum /= 10;
n++;
30
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
originalNum = num;
while (originalNum != 0)
originalNum /= 10;
if (result == num) {
return 0;
Viva Questions:
31
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Viva Questions:
Q.1 What is an armstrong number?
Q.2 Write the count of armstrong numbers between 1-500.
Q.3 Why math library is used in the above program?
Experiment 10
Name of Experiment: Write a C menu driven program
Aim: menu driven program
Code:
32
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
#include <stdio.h>
int main() {
while(1){
scanf("%d", &choice);
// If-else ladder
if(choice == 1){
printf("BYE!!!\n");
33
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
break;
else{
return 0;
Viva Question:
34
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Experiment 11
Name of Experiment: Write a program to generate the various pattern of numbers
Aim: To generate inverted half pyramid and full pyramid Pattern of numbers:
int main()
{
int n;
// User Input
printf("Enter the number of rows: ");
scanf("%d", &n);
35
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Code:
#include <stdio.h>
int main()
{
int rows = 5;
36
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Viva Questions:
Experiment 12
Name of Experiment: Write a program to convert years into Minute, Hours, Days, Months,
Seconds using switch () statements.
Aim: Program to convert year into Minute, Hours, Days, Months, Seconds using switch ()
statements.
Code:
Experiment 13
Name of Experiment: Write a C Program to print the reverse of an integer number.
Aim: To print the reverse of a number
int main() {
37
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
return 0;
}
Output:
Experiment 14
Name of Experiment: Write a C program to perform the factorial of given number
Aim: To find the factorial of a given number without using recursion.
Code:
#include<stdio.h>
int findFactorial(int);
int main(){
int i,factorial,num;
38
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
factorial = findFactorial(num);
printf("Factorial of %d is: %d",num,factorial);
return 0;
}
for(i=1;i<=num;i++)
f=f*i;
return f;
}
Output:
Experiment 15
Name of Experiment: Write a C program in which a function prime that returns 1 if its
argument is a prime and return zero otherwise.
Aim: To write a program in which a function prime that returns 1 if its argument is a prime
and returns zero otherwise.
Code:
#include<stdio.h>
int prime(int);
int main()
{
int n,p;
39
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
return 0;
}
int prime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
Output:
Experiment 16
Name of Experiment: Write a C program to calculate factorial of a number using recursion
Aim: To find the factorial of a given number using recursion
Code:
#include<stdio.h>
40
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Output:
Experiment 17
Name of Experiment: Write a C program in which enter 10 elements by the user and
perform the operation of sorting in ascending order.
Aim: C Program to enter 10 elements by user and perform sorting operation in ascending
order.
Code:
#include<stdio.h>
int main(){
int element[10],i,j,temp;
printf("enter 10 integer numbers:");
for(i=0;i<10;i++){
scanf("%d",&element[i]);
41
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
}
for(i=0;i<10;i++){
for(j=i+1;j<10;j++){
if(element[i]>element[j]){
temp=element[i]; //swapping element[i] with element[j]
element[i]=element[j];
element[j]=temp;
}
}
}
printf("Elements are now in ascending order:");
for(i=0;i<10;i++)
printf("%d\n",element[i]);
return 0;
}
Output
Experiment 18
42
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Output:
44
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
Experiment 19
Name of Experiment: Write a program to determine the length of the string and find its
equivalent ASCII codes.
Aim: C program to find length of string and find its equivalent ASCII code
Code:
#include<stdio.h>
int main() {
char string[10] = "Apoo";
int i=0, j;
45
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
while(string[i]!='\0') {
printf("ASCII Value of %c=%d\n", string[i], string[i]);
i++;
}
return 0;
}
Output:
Experiment 20
Name of Experiment: Write a C program to delete all the occurrences of the vowels in a
given text. Assume that the text length will be of one line.
Aim: C program to delete all the occurrences of the vowels in a given text
Code:
#include<stdio.h>
#include<string.h>
void main(){
char str[20];
46
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
int len, i, j;
Output
Experiment 21
Name of Experiment: Write a C program to maintain the library record for 100 books with
book name, author’s name, and edition, year of publishing and price of the book.
Aim: C program to maintain the library record for 100 books
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
47
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
#include<string.h>
struct library
{
char bk_name[30];
char author[30];
int edition;
float price;
};
int main()
{
struct library l[100];
char ar_nm[30],bk_nm[30];
int i,j, keepcount;
i=j=keepcount = 0;
while(j!=6)
{
printf("\n\n1. Add book information\n2. Display book information\n");
printf("3. List all books of given author\n");
printf("4. List the title of specified book\n");
printf("5. List the count of books in the library\n");
printf("6. Exit");
switch (j)
{
/* Add book */
case 1:
break;
case 2:
printf("you have entered the following information\n");
for(i=0; i<keepcount; i++)
{
printf ("book name = %s",l[i].bk_name);
case 3:
printf ("Enter author name : ");
scanf ("%s",ar_nm);
for (i=0; i<keepcount; i++)
{
if (strcmp(ar_nm, l[i].author) == 0)
printf ("%s %s %d %f",l[i].bk_name,l[i].author,l[i].edition,l[i].price);
}
break;
case 4:
printf ("Enter book name : ");
scanf ("%s",bk_nm);
for (i=0; i<keepcount; i++)
{
49
Lab Manual – MCACCA1103 –Programming in C la Poornima University, Academic Year 2023-24
if (strcmp(bk_nm, l[i].bk_name) == 0)
printf ("%s \t %s \t %d \t %f",l[i].bk_name,l[i].author,l[i].edition,l[i].price);
}
break;
case 5:
printf("\n No of books in library : %d", keepcount);
break;
case 6:
exit (0);
}
}
return 0;
Output:
50