0% found this document useful (0 votes)
48 views

Lab Manual BPC

Uploaded by

kaskhedikarn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Lab Manual BPC

Uploaded by

kaskhedikarn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

“BASIC PROGRAMMING WITH C”

Editors
Vandana Birle
Assistant Professor,
Medicaps University, Pigdamber
Rau, Indore
[email protected]

Kush Bhushanwar
Assistant Professor,
Medicaps University, Pigdamber
Rau, Indore
[email protected]

Vidhya Samad Barpha


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

Create software that calculates the factorial of any number.


15. 27
Bharat Pahadiya, Shyam Patel
To print a sequence of the alphabet, create a program.
16. 29
Rahul S. Pawar, Kush Bhushanwar
Create a script to print the Fibonacci sequence.
17. 30
Sagar Pandya, Chanda Chouhan
Programmatically verify whether a given integer is perfect.
18. 32
Vandana Birle, Jyoti Kukade
Make a computer program that verifies whether a given number is prime.
19. 34
Jyoti Kukade, Chanda Chouhan
To print a succession of numbers from 1 to 100 without using a loop, write a
20. program. 36
Rahul S. Pawar, Sagar Pandya
Create a software to print the following pattern:

a) b) c)
21. 37

d) e)
Kush Bhushanwar, Trapti Mishra

C. Arrays and strings:


Create a software to find maximum & minimum number from array.
22. 43
Vidhya Samad Barpha, Prashant Panse
Create a software to check how many numbers is prime & not prime in a list.
23. 45
Prashant panse, Deepa Pandit
Create a software to check how many digits at each index of array.
24. 47
Anurag Golwalkar, Bharat Pahadiya
Create a software to check (search) given number is present or not present in list.
25. 49
Sagar Pandya, Chanda Chouhan
Create a software to arrange (sort) array elements in ascending or descending order.
26. 51
Vandana Birle, Jyoti Kukade
Create a software to print a 2*2 matrix.
27. 53
Jyoti Kukade, Chanda Chouhan
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

Create a software to find sum of two matrix.


28. 55
Rahul S. Pawar, Sagar Pandya
Create a software to find multiplication of two matrix.
29. 57
Kush Bhushanwar, Trapti Mishra

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.

Kush Bhushanwar1, Vandana Birle2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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.

Shyam Patel1, Anurag Golwalkar2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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.

Bharat Pahadiya1, Shyam Patel2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

E-mail: [email protected]
[email protected]

Program:

#include <stdio.h>
#include <conio.h>

int main()
{
int n, sum;

//Input any three digit number from user


printf("Enter any three digit number : ");
scanf("%d", &n);
//Sum of digits
sum = (n/100) + (n/10)%10 + (n%10);

//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.

Rahul S. Pawar1, Kush Bhushanwar 2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

E-mail: [email protected]
[email protected]

Program:
#include<stdio.h>
int main()
{
int num1, num2;
float fraction;
char character;

// Taking Character as input from the user


printf("Enter a Character\n");
scanf("%c",&character);
printf("\n\nThe character that you have entered is %c", character);

printf("\n\nEnter two numbers number\n");


// Taking integer as input from user
scanf("%d%i", &num1, &num2);
printf("\n\nThe two numbers You have entered are %d and %i\n\n", num1, num2);

Page 6
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

// Taking float or fraction as input from the user


printf("\n\nEnter a Decimal number\n");
scanf("%f", &fraction);
printf("\n\nThe float or fraction that you have entered is %f", fraction);
return 0;
}

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.

Sagar Pandya1, Chanda Chouhan2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

E-mail: [email protected]
[email protected]

Program:
#include <stdio.h>
int main(){
int a, b, c, sum;
float avg;

// Asking for input


printf("Enter 3 numbers: \n");
scanf("%d %d %d", &a, &b, &c);

// Calculating sum
sum = a + b + c;

// Calculating average of 3 numbers


avg = (float) sum / 3;

// Displaying output

Page 8
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

printf("Sum = %d \n", sum);


printf("Average = %.2f", avg);
return 0;
}

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.

Vandana Birle 1, Jyoti Kukade2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

E-mail: [email protected]
[email protected]

Program:

#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

// true if num is perfectly divisible by 2


if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", 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.

Jyoti Kukade 1, Chanda Chouhan2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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.

Rahul S. Pawar1, Sagar Pandya2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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

printf("\nAfter swapping, first number = %.2lf\n", first);


printf("After swapping, second number = %.2lf", second);
return 0;
}

Output:

Swapping without using third variable:


#include<stdio.h>
int main()
{
int x, y;
printf("Input value for x & y: \n");
scanf("%d%d",&x,&y);
printf("Before swapping the value of x & y: %d %d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("\nAfter swapping the value of x & y: %d %d",x,y);
return 0;

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

Kush Bhushanwar1, Trapti Mishra2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

E-mail: [email protected]
trapti.mishra @medicaps.ac.in

How if statement works?


If the test expression is evaluated to true,
● statements inside the body of if are executed.
● statements inside the body of else are skipped from execution.
If the test expression is evaluated to false,
● statements inside the body of else are executed
● statements inside the body of if are skipped from execution.

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.

Vidhya Samad Barpha1, Prashant Panse2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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

printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");


printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
return 0;
}

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.

Prashant Panse 1, Deepa Pandit2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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.

Anurag Golwalkar1, Bharat Pahadiya2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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.

1. Year must be divisible by 4


2. Year is divisible by 400 and not divisible by 100.

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

// leap year if perfectly divisible by 400


if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}

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.

Kush Bhushanwar1, Vandana Birle2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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.

Shyam Patel1, Anurag Golwalkar2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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:

for (initializationStatement; testExpression; updateStatement)


{
// statements inside the body of loop
}
How for loop works?
● The initialization statement is executed only once.
● Then, the test expression is evaluated. If the test expression is evaluated to false,
the for loop is terminated.
● However, if the test expression is evaluated to true, statements inside the body of
the for loop are executed, and the update expression is updated.
● Again the test expression is evaluated.
This process goes on until the test expression is false. When the test expression is false,
the loop terminates.

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);

for (int i = 1; i <= 10; ++i) {


printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}

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.

Bharat Pahadiya1, Shyam Patel2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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);

// shows error if the user enters a negative integer


if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);

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.

Rahul S. Pawar1, Kush Bhushanwar 2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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.

Sagar Pandya1, Chanda Chouhan2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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.

Vandana Birle 1, Jyoti Kukade2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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()

int i = 1, num, Sum = 0;

printf(" Enter any number to check Perfect Number \n");

scanf("%d", &num);

while(i < 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)

printf("\n %d is Perfect Number", num);

else

printf("\n %d is not a Perfect Number", num);

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.

Jyoti Kukade 1, Chanda Chouhan2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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;

printf("Enter a positive integer: ");

scanf("%d", &n);

// 0 and 1 are not prime numbers

// change flag to 1 for non-prime number

if (n == 0 || n == 1)

flag = 1;

Page 40
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

for (i = 2; i <= n / 2; ++i) {

// if n is divisible by i, then n is not prime

// change flag to 1 for non-prime number

if (n % i == 0) {

flag = 1;

break;

// flag is 0 for prime numbers

if (flag == 0)

printf("%d is a prime number.", n);

else

printf("%d is not a prime number.", n);

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.

Rahul S. Pawar1, Sagar Pandya2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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)

Kush Bhushanwar1, Trapti Mishra2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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;

printf("Enter the number of rows: ");

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.

Vidhya Samad Barpha1, Prashant Panse2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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");

printf("Input the number of elements to be stored in the array :");


scanf("%d",&n);

Page 51
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
mx = arr1[0];
mn = arr1[0];
for(i=1; i<n; i++)
{
if(arr1[i]>mx)
{
mx = arr1[i];
}
if(arr1[i]<mn)
{
mn = arr1[i];
}
}
printf("Maximum element is : %d\n", mx);
printf("Minimum element is : %d\n\n", mn);
}
Output:

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.

Prashant Panse 1, Deepa Pandit2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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.

Anurag Golwalkar1, Bharat Pahadiya2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

E-mail: [email protected]
[email protected]

Program:
#include <stdio.h>

int main() {

int n,i,temp, A[100], c;

printf("Enter the size of Array: ");


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

for(i = 0; i < n; i++)


{

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.

Sagar Pandya1, Chanda Chouhan2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

E-mail: [email protected]
[email protected]

Program:

#include<stdio.h>
int main()
{
int i,n,m,flag=0; int a[10];

printf("How many elements you want to enter : ");


scanf("%d",&n);

printf("Enter %d element in the array :\n",n);


for (i=0; i<n; i++)
scanf("%d", &a[i]);

printf("Enter the element you want to search: ");


scanf("%d", &m);

Page 58
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

for (i=0; i<n; i++)


{
if(a[i]==m)
{
flag=1;
break;
}
}
if(flag==0)
printf("Not present");
else
printf("Present");
return 0;
}

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.

Vandana Birle 1, Jyoti Kukade2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

E-mail: [email protected]
[email protected]

Proram:
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {5, 2, 8, 7, 1};
int temp = 0;

//Calculate length of array arr


int length = sizeof(arr)/sizeof(arr[0]);

//Displaying elements of original array


printf("Elements of original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}

//Sort the array in descending order


for (int i = 0; i < length; i++) {
for (int j = i+1; j < length; j++) {
if(arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

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.

Jyoti Kukade 1, Chanda Chouhan2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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;

// take row and column size


printf("Ener row size: ");
scanf("%d", &row);
printf("Ener column size: ");
scanf("%d", &column);

// declare array
int arr[row][column];

Page 62
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

// take matrix elements as input


printf("Enter elements for %dx%d matrix:\n",
row, column);
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
printf("arr[%d][%d]: ",i,j);
scanf("%d", &arr[i][j]);
}
printf("\n");
}

// display matrix using for loop


printf("The %dx%d matrix elements are:\n",
row, column);
for(int i=0; i<row; i++) {
for(int j=0; j<column; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

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.

Rahul S. Pawar1, Sagar Pandya2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {

Page 65
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

sum[i][j] = a[i][j] + b[i][j];


}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

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.

Kush Bhushanwar1, Trapti Mishra2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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];

printf("Enter the order of first matrix\n");


scanf("%d%d", & m, & n);
printf("Enter the order of second matrix\n");
scanf("%d%d", & p, & q);

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]);
}
}

printf("Enter the elements of Matrix-B:\n");


for (i = 0; i < p; i++) {
for (j = 0; j < q; j++) {
scanf("%d", & b[i][j]);
}
}

Page 67
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

for (i = 0; i < m; i++) {


for (j = 0; j < q; j++) {
res[i][j] = 0;
for (k = 0; k < p; k++) {
res[i][j] += a[i][k] * b[k][j];
}
}
}

printf("The product of the two matrices is:-\n");

for (i = 0; i < m; i++) {


for (j = 0; j < q; j++) {
printf("%d\t", res[i][j]);
}
printf("\n");
}
}
return 0;
}
Output:

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.

Vidhya Samad Barpha1, Prashant Panse2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

E-mail: [email protected]
[email protected]

In C programming, a string is a sequence of characters terminated with a null character \0.


For example:

char c[ ] = "c string";

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.

Here's how you can declare strings:

char s[5];

You can initialize strings in a number of ways.

char c[] = "abcd";

Page 70
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

char c[50] = "abcd";

char c[] = {'a', 'b', 'c', 'd', '\0'};

char c[5] = {'a', 'b', 'c', 'd', '\0'};

Read String from the user


You can use the scanf() function to read a string. The scanf() function reads the sequence of
characters until it encounters whitespace (space, newline, tab, etc.).

Example 1: scanf() to read a string


#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Output:

Example 2: fgets() and puts()


#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin); // read string
printf("Name: ");

Page 71
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

puts(name); // display string


return 0;
}
Output:

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'};

// using the %zu format specifier to print size_t


printf("Length of string a = %zu \n",strlen(a));
printf("Length of string b = %zu \n",strlen(b));
return 0;
}

Output:

C strcpy()

Page 72
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

The function prototype of strcpy() is:

char* strcpy(char* destination, const char* source);

● 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];

// copying str1 to str2


strcpy(str2, str1);

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;

// comparing strings str1 and str2


result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);

Page 73
MEDI-CAPS UNIVERSITY, INDORE

LAB MANUAL
BASIC PROGRAMMING WITH C SEM: ODD
EN3ES27

// comparing strings str1 and str3


result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);

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";

// concatenates str1 and str2


// the resultant string is stored in str1.
strcat(str1, str2);

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.

Prashant Panse 1, Deepa Pandit2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

E-mail: [email protected]
[email protected]

Program:
#include<stdio.h>

int sum(int, int); //Function declaration


int main() {
int num1, num2, res;

printf("\nEnter the two numbers : ");


scanf("%d %d", &num1, &num2);

//Call Function Sum With Two Parameters


res = sum(num1, num2);

printf("\nAddition of two number is : %d", res);


return (0);
}
//Function Definition
int sum(int num1, int num2) {
int num3;
num3 = num1 + num2;
return (num3);
}

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.

Kush Bhushanwar1, Vandana Birle2

Department of Information Technology

Medi-Caps University

A.B. Road, Pigdamber Rau, Indore, India

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

Ms. Vandana Birle is currently working as an Assistant Professor in


department of Information Technology at Medi-Caps University,
Indore. She earned his B.E. (CSE) from JawaharLal Institute of
Technology in Borawan. and her M.Tech. (IT) fromMahakal Institute
Of technology Ujjain. The individual's research interests include the
fields of Network Security, Machine Learning, Artificial
Intelligence, Database Management System, AI.She has made
significant contributions to the field of academia, including training and research, during a
period exceeding Nine years.

Mr. Kush Bhushanwar is currently working as an Assistant Professor in


the department of Information Technology at Medi-Caps University,
Indore. He has completed B.E. in Information Technology & M.Tech. in
Computer Engineering. Currently pursuing Ph.D. in Computer Science &
Engineering. The individual's research interests include the fields Machine
Learning, IoT and Wireless Sensor Network. He has made significant
contributions to the field of academia, including training and research,
during a period exceeding 13 years.

Ms. Vidhya Samad Barpha is currently working as an Assistant


Professor in the department of Information Technology at Medi-Caps
University, Indore. She has completed B.E. in Information Technology
& M.E. in Computer Engineering. Currently pursuing Ph.D. in Computer
Science & Engineering. The individual's research interests include the
fields of Machine Learning, Compiler Design, Data Mining, and Data
Structures. She has made significant contributions to the field of
academia, including training and research, during a period exceeding 10
years.

Page 80

You might also like