Lab Manual BPC
Lab Manual BPC
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Editors
Vandana Birle
Assistant Professor,
Medicaps University, Pigdamber
Rau, Indore
[email protected]
Kush Bhushanwar
Assistant Professor,
Medicaps University, Pigdamber
Rau, Indore
[email protected]
2023
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
PREFACE
This manual was created primarily for first-year students enrolling in their first programming
course, but anyone interested in learning C is encouraged to study it as well. Its goal is to
enhance lectures in the classroom by emphasizing C programming. According to the
sequence of class discussion, topics are ordered.
Here, only a portion of the ANSI C language is covered. It is specifically assumed that the
student will be utilizing the Windows operating system and the Borland Turbo C/C++ or
Code Blocks, or another compiler, to program. Coding guidelines must be followed.
An emphasis on bitwise operations is another feature that is uncommon in beginning C
literature. An introduction to bitwise operations appropriate for embedded systems
programming was necessary for the course for which this textbook was initially created
because it was a prerequisite for one on embedded systems.
May you gain a lot of information through studying this guidebook, and may you apply what
you learn for the benefit of all people.
Vandana Birle
Kush Bhushanwar
Vidhya Samad Barpha
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
ACKNOWLEDGEMENT
We want to thank everyone who has supported and assisted us while we were writing this
book with the utmost gratitude. We appreciate our head of department's continued assistance
throughout the project, which began with initial counsel and encouragement and ultimately
resulted in the book's concluding chapter.
We would especially want to thank our coworkers who shared their experiences and
intriguing ideas with us, which enabled us to finish the book.
Without Medi-Caps University's unwavering support and attention, who gave us the
inspiration and prodding to follow our own paths, we would not have been able to finish the
book.
Finally, we would like to express our gratitude to our friends for their encouragement and
expressions of admiration for our work.
Vandana Birle
Kush Bhushanwar
Vidhya Samad Barpha
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
CONTENT
Sr. Title of Chapter Page
No. Author Name(s) No.
A. Basics of language:
Create a software that prints Hello World on the output screen.
1. 1
Kush Bhushanwar, Vandana Birle
Create a software that performs arithmetic operations on two numbers.
2. 2
Shyam Patel, Anurag Golwalkar
Create a software to calculate the sum of each digit in any three-digit number.
3. 4
Bharat Pahadiya, Shyam Patel
Use various data types to illustrate the sizes of the inputs that scanf receives.
4. 6
Rahul S. Pawar, Kush Bhushanwar
Create a software that output the average of three numbers to two decimal places.
5. 8
Sagar Pandya, Chanda Chouhan
B. Control Statements:
Create a computer software to determine whether a number is even or odd.
6. 10
Vandana Birle, Jyoti Kukade
Create a software that will flip an integer.
7. 11
Jyoti Kukade, Chanda Chouhan
Create a software that can swap any two numbers both with and without the use of
8. the third variable. 12
Rahul S. Pawar, Sagar Pandya
Create a computer software that prints a student's grade based on a percentage:
i. mark less than 50 then display F Grade
ii. mark >=50 and <60 then display D Grade
iii. mark >=60 and <70 then display C Grade
9. 14
iv. mark >=70 and <80 then display B Grade
v. mark >=80 and <90 then display A Grade
vi. mark >=90 then display A+ Grade
Kush Bhushanwar, Trapti Mishra
Create a switch case program for addition, subtraction, multiplication, and division.
10. 17
Vidhya Samad Barpha, Prashant Panse
Create a software to determine whether a given number is an Armstrong or not.
11. 19
Prashant Panse, Deepa Pandit
Create a computer program to determine whether a given year is a leap year.
12. 21
Anurag Golwalkar, Bharat Pahadiya
Create a program to determine whether a given number is a palindrome or not.
13. 23
Kush Bhushanwar, Vandana Birle
Create a software to print any number of tables.
14. 25
Shyam Patel, Anurag Golwalkar
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
a) b) c)
21. 37
d) e)
Kush Bhushanwar, Trapti Mishra
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
D. Functions
Create a software of string functions.
30. 60
Vidhya Samad Barpha, Prashant Panse
Create a function to find sum of two numbers.
31. 66
Prashant panse, Deepa Pandit
Create a recursive function to calculate factorial of any number.
32. 68
Kush Bhushanwar, Vandana Birle
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-1
Create a program that prints Hello World on the output screen.
Medi-Caps University
E-mail: [email protected]
[email protected]
What is C?
Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in
1972. Despite being an ancient language, it is extremely popular. Since the UNIX operating
system was created using C, the two are closely related.
Program:
#include <stdio.h>
#include<conio.h>
int main() {
printf("Hello World!!"); // printf() is used to display the string inside quotation
return 0;
}
Output:
Page 1
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-2
Create a software that performs arithmetic operations on two numbers.
Medi-Caps University
E-mail: [email protected]
[email protected]
Arithmetic Operators:
The symbols that are used to carry out mathematical operations on operands are known as the
C arithmetic operators. The fundamental arithmetic operations, such as addition, subtraction,
multiplication, etc., are provided by a total of 9 arithmetic operators in C.
Program:
#include <stdio.h>
#include <conio.h>
int main()
{
int n1, n2;
int add, sub, mult, mod;
float div;
//Input any two numbers from user
printf("Enter any two numbers : ");
scanf("%d%d", &n1, &n2);
//Perform all arithmetic operations
add = n1 + n2;
Page 2
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
sub = n1 - n2;
mult = n1 * n2;
div = (float)n1 / n2;
mod = n1 % n2;
//Print result of all arithmetic operations
printf("ADDITION = %d\n", add);
printf("DIFFERENCE = %d\n", sub);
printf("PRODUCT = %d\n", mult);
printf("QUOTIENT = %f\n", div);
printf("MODULUS = %d", mod);
return 0;
}
Output:
Page 3
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-3
Create a software to calculate the sum of each digit in any three-digit number.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include <stdio.h>
#include <conio.h>
int main()
{
int n, sum;
//Print result
printf("The sum of digits of %d is %d\n", n, sum);
Page 4
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
return 0;
}
Output:
Page 5
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-4
Use various data types to illustrate the sizes of the inputs that scanf receives.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include<stdio.h>
int main()
{
int num1, num2;
float fraction;
char character;
Page 6
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Output:
Page 7
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-5
Create a software that output the average of three numbers to two decimal places.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include <stdio.h>
int main(){
int a, b, c, sum;
float avg;
// Calculating sum
sum = a + b + c;
// Displaying output
Page 8
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Output:
Page 9
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-6
Create a computer software to determine whether a number is even or odd.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
return 0;
}
Page 10
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Output:
Page 11
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-7
Create a software that will flip an integer.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include <stdio.h>
int main() {
int n, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", reverse);
return 0;
}
Page 12
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Output:
Page 13
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-8
Create a software that can swap any two numbers both with and without the use of the
third variable.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
Swapping using third Variable:
#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
// value of first is assigned to temp
temp = first;
// value of second is assigned to first
first = second;
// value of temp (initial value of first) is assigned to second
second = temp;
// %.2lf displays number up to 2 decimal points
Page 14
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Output:
Page 15
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
}
Output:
Page 16
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-9
Create a computer software that prints a student's grade based on a percentage:
i. mark less than 50 then display F Grade
ii. mark >=50 and <60 then display D Grade
iii. mark >=60 and <70 then display C Grade
iv. mark >=70 and <80 then display B Grade
v. mark >=80 and <90 then display A Grade
vi. mark >=90 then display A+ Grade
Medi-Caps University
E-mail: [email protected]
trapti.mishra @medicaps.ac.in
Page 17
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Program:
#include<stdio.h>
void main()
{
int marks;
printf("Enter your marks ");
scanf("%d",&marks);
if(marks<0 || marks>100)
{
printf("Wrong Entry");
}
else if(marks<50)
{
printf("Grade F");
Page 18
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
}
else if(marks>=50 && marks<60)
{
printf("Grade D");
}
else if(marks>=60 && marks<70)
{
printf("Grade C");
}
else if(marks>=70 && marks<80)
{
printf("Grade B");
}
else if(marks>=80 && marks<90)
{
printf("Grade A");
}
else
{
printf("Grade A+");
}
}
Output:
Page 19
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Page 20
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-10
Create a switch case program for addition, subtraction, multiplication, and division.
Medi-Caps University
E-mail: [email protected]
[email protected]
The switch statement allows us to execute one code block among many alternatives.
You can do the same thing with the if...else..if ladder. However, the syntax of
the switch statement is much easier to read and write.
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
default:
// default statements
}
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
int op;
Page 21
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Page 22
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Output:
Page 23
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-11
Create a software to determine whether a given number is an Armstrong or not.
Medi-Caps University
E-mail: [email protected]
[email protected]
Armstrong number is a number that is equal to the sum of cubes of its digits. For example
0, 1, 153, 370, 371, 407 and 1634 are the Armstrong numbers.
Example:
153 = (1*1*1) + (5*5*5) + (3*3*3)
where:
(1 * 1 * 1) = 1
(5 * 5 * 5) = 125
(3 * 3 * 3) = 27
So:
1 + 125 + 27 = 153
Program:
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
Page 24
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
else
printf("not armstrong number");
return 0;
}
Output:
Page 25
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-12
Create a computer program to determine whether a given year is a leap year.
Medi-Caps University
E-mail: [email protected]
[email protected]
Generally, a year has 365 days in a year, but a leap year has 366 days which comes after four
year. Below are some points related to leap year:
o A leap year is a year, which is different than a normal year having 366 days instead of
365.
o A leap year comes once in four years, in which February month has 29 days. With this
additional day in February, a year becomes a Leap year.
o Some leap years examples are - 1600, 1988, 1992, 1996, and 2000.
o Although 1700, 1800, and 1900 are century years, not leap years.
Below conditions are used to check that year is a leap year or not.
Program:
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
Page 26
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
return 0;
}
Output:
Page 27
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Page 28
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-13
Create a program to determine whether a given number is a palindrome or not.
Medi-Caps University
E-mail: [email protected]
[email protected]
A palindrome number is a number that is same after reverse. For example 121, 34543, 343,
131, 48984 are the palindrome numbers.
Program:
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}
Page 29
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Output:
Page 30
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-14
Create a software to print any number of tables.
Medi-Caps University
E-mail: [email protected]
[email protected]
for Loop:
In programming, a loop is used to repeat a block of code until the specified condition is
met.
The syntax of the for loop is:
Program:
#include <stdio.h>
int main() {
int n;
printf("Enter an integer: ");
Page 31
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
scanf("%d", &n);
Output:
Page 32
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-15
Create software that calculates the factorial of any number.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
Page 33
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
return 0;
}
Output:
Page 34
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-16
To print a sequence of the alphabet, create a program.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include <stdio.h>
int main() {
char c;
for (c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
return 0;
}
Output:
Page 35
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-17
Create a script to print the Fibonacci sequence.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include <stdio.h>
int main() {
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
Page 36
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Output:
Page 37
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-18
Programmatically verify whether a given integer is perfect.
Medi-Caps University
E-mail: [email protected]
[email protected]
In mathematics, a perfect number is a positive integer that is equal to the sum of its positive
divisors, excluding the number itself.
For example, 6 is a positive number that is completely divisible by 1, 2, and 3. We know that
the number is also divisible by itself but we will include it in the addition of divisors. When
we add these divisors (1 + 2 + 3 = 6), it produces 6, which is equal to the number that we
have considered. So, we can say that 6 is a perfect number.
Program:
#include<stdio.h>
int main()
scanf("%d", &num);
Page 38
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
if(num % i == 0)
Sum = Sum + i;
i++;
if(Sum == num)
else
return 0;
Output:
Page 39
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-19
Make a computer program that verifies whether a given number is prime.
Medi-Caps University
E-mail: [email protected]
[email protected]
A prime number is a positive integer that is divisible only by 1 and itself. For example: 2,
3, 5, 7, 11, 13, 17.
Program:
#include <stdio.h>
int main() {
int n, i, flag = 0;
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
Page 40
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
if (n % i == 0) {
flag = 1;
break;
if (flag == 0)
else
return 0;
Output:
Page 41
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Page 42
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-20
To print a succession of numbers from 1 to 100 without using a loop, write a program.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include <stdio.h>
int main()
{
int i = 0;
begin:
i = i + 1;
printf("%d ", i);
if (i < 100)
goto begin;
return 0;
}
Output:
Page 43
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Page 44
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-21
Create a software to print the following pattern:
a) b) c)
d) e)
Medi-Caps University
E-mail: [email protected]
trapti.mishra @medicaps.ac.in
Program:
a)
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d",&n);
Page 45
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
b)
#include <stdio.h>
int main()
{
int n,m=1;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i-1;j++)
{
printf(" ");
}
for(int k=1;k<=m;k++)
{
printf("*");
Page 46
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
}
printf("\n");
m++;
}
return 0;
}
Output:
c)
#include <stdio.h>
int main()
int n,m=1;
scanf("%d",&n);
for(int i=n;i>=1;i--)
for(int j=1;j<=i;j++)
printf("*");
Page 47
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
printf("\n");
return 0;
Output:
d)
#include <stdio.h>
int main()
{
int n,m;
printf("Enter the number of rows: ");
scanf("%d",&n);
m=n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
printf(" ");
}
for(int k=1;k<=m;k++)
{
printf("*");
Page 48
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
}
m--;
printf("\n");
}
return 0;
}
Output:
e)
#include <stdio.h>
int main()
{
int n,m;
printf("Enter the number of rows: ");
scanf("%d",&n);
m=n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m-1;j++)
{
printf(" ");
Page 49
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
}
for(int k=1;k<=2*i-1;k++)
{
printf("*");
}
m--;
printf("\n");
}
return 0;
}
Output:
Page 50
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-22
Create a software to find maximum & minimum number from array.
Medi-Caps University
E-mail: [email protected]
[email protected]
An array is a variable that can store multiple values. For example, if you want to store 100
integers, you can create an array for it.
int data[100];
Program:
#include <stdio.h>
void main()
{
int arr1[100];
int i, mx, mn, n;
printf("\n\nFind maximum and minimum element in an array :\n");
printf("--------------------------------------------------\n");
Page 51
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Page 52
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Page 53
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-23
Create a software to check how many numbers is prime & not prime in a list.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include <stdio.h>
int main() {
int i, c, j, A[5];
printf("Please enter any 5 numbers:\n");
for(i=0; i<=4; i++)
{
scanf("%d", &A[i]);
}
printf("Prime numbers: ");
for(j = 0; j < 5; j++)
{
c=0;
for(i=1; i<A[j]; i++)
{
if (A[j]%i == 0)
Page 54
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
{
c++;
}
}
if (c==1)
{
printf("\n%d", A[j]);
}
}
return 0;
}
Output:
Page 55
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-24
Create a software to check how many digits at each index of array.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include <stdio.h>
int main() {
Page 56
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
temp = A[i];
c=0;
while(A[i] != 0)
{
A[i]=A[i]/10;
c++;
}
printf("Digit in %d is %d\n", temp, c);
}
return 0;
}
Output:
Page 57
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-25
Create a software to check (search) given number is present or not present in list.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include<stdio.h>
int main()
{
int i,n,m,flag=0; int a[10];
Page 58
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Output:
Page 59
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-26
Create a software to arrange (sort) array elements in ascending or descending order.
Medi-Caps University
E-mail: [email protected]
[email protected]
Proram:
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {5, 2, 8, 7, 1};
int temp = 0;
Page 60
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
printf("\n");
//Displaying elements of array after sorting
printf("Elements of array sorted in descending order: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Page 61
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-27
Create a software to print a 2*2 matrix.
Medi-Caps University
E-mail: [email protected]
[email protected]
Two-Dimensional Array:
An array of arrays is one way to define the two-dimensional array. Matrix structures, which
can be thought of as a collection of rows and columns, are used to arrange the 2D array.
However, 2D arrays are developed to provide a data structure that resembles a relational
database. It makes it simple to keep a large amount of data at once and transfer it to as many
functions as needed.
The syntax to declare the 2D array is given below.
data_type array_name[rows][columns];
Program:
#include <stdio.h>
int main()
{
// variables
int row;
int column;
// declare array
int arr[row][column];
Page 62
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
return 0;
}
Output:
Page 63
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Page 64
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-28
Create a software to find sum of two matrix.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
Page 65
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
return 0;
}
Output:
Page 66
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-29
Create a software to find multiplication of two matrix.
Medi-Caps University
E-mail: [email protected]
trapti.mishra @medicaps.ac.in
Program:
#include<stdio.h>
int main() {
int m, n, p, q, i, j, k;
int a[10][10], b[10][10], res[10][10];
if (n != p) {
printf("Matrix is incompatible for multiplication\n");
} else {
printf("Enter the elements of Matrix-A:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j]);
}
}
Page 67
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Page 68
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Page 69
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-30
Create a software of string functions.
Medi-Caps University
E-mail: [email protected]
[email protected]
When the compiler encounters a sequence of characters enclosed in the double quotation
marks, it appends a null character \0 at the end by default.
char s[5];
Page 70
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Page 71
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
C strlen()
The strlen() function takes a string as an argument and returns its length. The returned value
is of type size_t (an unsigned integer type).
It is defined in the <string.h> header file.
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Medicaps";
char b[20]={'P','r','o','g','r','a','m','\0'};
Output:
C strcpy()
Page 72
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
● The strcpy() function copies the string pointed by source (including the null character)
to the destination.
● The strcpy() function also returns the copied string.
Program:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello Studetns";
char str2[20];
puts(str2); // C programming
return 0;
}
Output:
C strcmp()
The strcmp() compares two strings character by character. If the strings are equal, the
function returns 0.
Program:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
Page 73
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
return 0;
}
Output:
C strcat()
The strcat() function concatenates the destination string and the source string, and the result is
stored in the destination string.
Program:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "This is ", str2[] = "Medi-Caps University";
puts(str1);
puts(str2);
return 0;
}
Output:
Page 74
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Page 75
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-31
Create a function to find sum of two numbers.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include<stdio.h>
Page 76
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Output:
Page 77
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
Chapter-32
Create a recursive function to calculate factorial of any number.
Medi-Caps University
E-mail: [email protected]
[email protected]
Program:
#include<stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
int main()
{
int number;
long fact;
printf("Enter a number: ");
Page 78
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}
Output:
Page 79
MEDI-CAPS UNIVERSITY, INDORE
LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27
References:
[1] C Programming: A Modern Approach (2nd Edition) - K. N. King (2008). A good book for
learning C.
[2] Programming in C (4th Edition) - Stephen Kochan (2014). A good general introduction
and tutorial.
[3] C Primer Plus (5th Edition) - Stephen Prata (2004)
[4] A Book on C - Al Kelley/Ira Pohl (1998).
[5] The C Book (Free Online) - Mike Banahan, Declan Brady, and Mark Doran (1991).
About Author
Page 80