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

C lab manual

The document is a lab manual for the Programming in C course (CS3271) at Saranathan College of Engineering for the academic year 2023-24. It outlines the course objectives, a list of experiments, and expected outcomes for students, focusing on various C programming constructs and applications. The manual includes detailed examples and algorithms for multiple programming exercises related to input/output, decision-making, loops, arrays, strings, functions, pointers, structures, and file processing.

Uploaded by

tannam1071980
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C lab manual

The document is a lab manual for the Programming in C course (CS3271) at Saranathan College of Engineering for the academic year 2023-24. It outlines the course objectives, a list of experiments, and expected outcomes for students, focusing on various C programming constructs and applications. The manual includes detailed examples and algorithms for multiple programming exercises related to input/output, decision-making, loops, arrays, strings, functions, pointers, structures, and file processing.

Uploaded by

tannam1071980
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

1

SARANATHAN COLLEGE OF ENGINEERING


TIRUCHIRAPPALLI – 620012.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CS3271
PROGRAMMING IN C LABORATORY

LAB MANUAL

ACADEMIC YEAR
2023-24 (EVEN)

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


2

CS3271 PROGRMMING IN C LABORATORY LTPC


0042
COURSE OBJECTIVES:

• To familiarize with C programming constructs.


• To develop programs in C using basic constructs.
• To develop programs in C using arrays.
• To develop applications in C using strings, pointers, functions.
• To develop applications in C using structures.
• To develop applications in C using file processing.

LIST OF EXPERIMENTS

1. I/O statements, operators, expressions


2. Decision-making constructs: if-else, goto, switch-case, break-continue
3. Loops: for, while, do-while
4. Arrays: 1D and 2D, Multi-dimensional arrays, traversal.
5. Strings: Operations
6. Functions: call, return, passing parameters by (value, reference), passing arrays to function.
7. Recursion
8. Pointers: Pointers to functions, Arrays, Strings, Pointers to Pointers, Array of Pointers
9. Structures: Nested Structures, Pointers to Structures, Arrays of Structures and Unions.
10. Files: reading and writing, File pointers, file operations, random access, processor directives.

COURSE OUTCOMES: upon completion of the course, the students will be able to

CO1: Demonstrate knowledge on C programming constructs.


CO2: Develop programs in C using basic constructs.
CO3: Develop programs in C using arrays.
CO4: Develop applications in C using strings, pointers, functions.
CO5: Develop applications in C using structures.
CO6: Develop applications in C using file processing.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


3

EX.NO: 1 I/O Statements, Operators, And Expressions

1. Write a C program for getting the five subject marks from the user, and then calculate total
marks obtained and average of input passed and print the results.
2. Write a C program to calculate area of a circle and cylinder as well as circumference of a
circle and volume of a cone.
3. Write a C program to display the ASCII value of an entered character.
4. Write a C program to check whether given number is even or odd using ternary operator.
5. Write a C program to evaluate the arithmetic expression ((a + b / c * d - e) * (f - g)). Read
the values a, b, c, d, e, f, g from the standard input device
6. Write a C program to print the sizeof () for all data types.
7. Write a C program to swap two numbers using temporary variable and without using
temporary variable

EX. NO: 2 Decision-making Constructs


(if-else, goto, switch-case, break-continue)

1. Write a C program to find a given year is leap or not using if-else


2. Write a C program to check whether the number is odd or even
3. Write a C program to check whether the student is pass or fail.
4. Write a C program to find the biggest among given three integers using nested if-else
5. Write a C program to generate numbers between 20 and 100 which are divisible by 2 and
not divisible by 3 and 5.
6. Write a C program to illustrate the functionality of continue and break Statement
7. Write a C program to perform the basic calculator operations, namely, addition,
subtraction, multiplication, division and square of a number using (Switch Case)

EX. NO: 3 Loops: for, while, do-while

1. Write a C program to print first 25 positive integers.


2. Write a C program to find the factorial of a given number.
3. Write a C program to reverse a given number
4. Write a C program to check whether a number is Palindrome or not.
5. Write a C program to display odd and even numbers within a given range using while loop.

EX.NO: 4 Arrays: 1D and 2D, Multi-dimensional arrays, traversal

1. Write a C program to insert 5 elements into an array and print the elements of the array.
2. Write a C program to find whether a search element is present in the array or not.
3. Write a C program to sort the elements in the array using selection sort.
4. Write a C Program to perform Matrix Addition
5. Write a C Program to multiply two Matrices
6. Write a C program for transpose of a matrix
N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C
4

EX: NO: 1 I/O Statements, Operators and Expressions

1a. Calculate total and average marks

Write a C program for getting the five subject marks from the user, and then calculate the total
marks obtained, average of input passed and print the results.

Aim:
To write a program for getting 5 subjects mark from user and print its total,
average and result.

Algorithm:
Step 1: Start the program.
Step 2: Declare the variables mark, result, total=0, i.
Step 3: Perform the below expression; total=total + mark and avg = (total)/5.
Step 4: Print pass, if avg greater than 50, otherwise print fail.
Step 5: Stop the program.

Program:
#include <stdio.h>
int main()
{
float eng, phy, chem, math, comp;
float total, average, percentage;
printf("Enter marks of five subjects: \n");
scanf("%f%f%f%f%f", &eng, &phy, &chem, &math, &comp);
total = eng + phy + chem + math + comp;
average = total / 5.0;
percentage = (total / 500.0) * 100;
printf("Total marks = %.2f\n", total);
printf("Average marks = %.2f\n", average);
printf("Percentage = %.2f", percentage);
return 0;
}

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


5

Output:
Enter marks of five subjects:
34 43 54 67 78
Total marks = 276.00
Average marks = 55.20
Percentage = 55.20

Result:
Thus the C program to calculate the total marks obtained, average of input passed and
print the results has been successfully executed and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


6

1b. Calculate area, Circumference and volume

Write a C program to calculate area of a circle and cylinder as well as circumference of a circle
and volume of a cone.

Aim:
To write a C program to calculate the area of circle and cylinder, circumference of
a circle and volume of a cone.

Algorithm:
Step 1: Start the program.
Step2: Declare the variables c_radi, cyl_radi, cyl_h, cone_radi, cone_h, area_c,
circum_c, area_cy and vol_cone.
Step 3: Perform the below expressions:
area_c=(pi*c_radi*c_radi)
circum_c=(2*pi*c_radi)
area_cy=((2*pi*cyl_radi*cyl_h)+(2*pi*cyl_radi*cyl_radi))
vol_cone=((pi*cone_radi*cone_radi*cone_h)/(3)).
Step 4: Display the output to the user.
Step 5: Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#define pi 3.14
void main()
{
int c_radi, cyl_radi, cyl_h, cone_radi, cone_h;
float area_c, circum_c, area_cy, vol_cone;
clrscr();
printf("\n Enter radius of circle");
scanf("%d",&c_radi);
printf("\n Enter radius of cylinder");
scanf("%d",&cyl_radi);
printf("\n Enter height of cylinder:");
scanf("%d",&cyl_h);
printf("\n Enter radius of cone:");
scanf("%d",&cone_h);
printf("\n Enter height of cone:");
scanf("%d",&cone_h);
area_c=(pi*c_radi*c_radi);
circum_c=(2*pi*c_radi);

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


7

area_cy=((2*pi*cyl_radi*cyl_h)+(2*pi*cyl_radi*cyl_radi));
vol_cone=((pi*cone_radi*cone_radi*cone_h)/(3));
printf("\n t\The area of a circle is:%f",area_c);
printf("\n The circumference of a circle is:%f",circum_c);
printf("\n The area of a cylinder is:%f",area_cy);
printf("\n The volume of a cone is:%f",vol_cone);
getch();
}

Output:
Enter radius of circle 5
Enter radius of cylinder 5
Enter height of cylinder: 6
Enter radius of cone: 5
Enter height of cone: 6
The area of a circle is: 78.54
The circumference of a circle is: 31.42
The area of a cylinder is: 345.58
The volume of a cone is: 157.08

Result:
Thus the C program to calculate the area of circle and cylinder, circumference of a circle
and volume of a cone was successfully executed and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


8

1c. Display an ASCII value of a Character

Write a C program to display the ASCII value of an Entered character.

Aim:
To write a C program to display the ASCII value of an Entered character.

Algorithm:

Step1: Start the program.


Step 2: Declare the character.
Step 3: Perform the below printing statement:
printf("\n The ASCII value is:%d",a);
Step 4: Stop the program.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
char a;
printf("Enter a letter:");
scanf("%c",&a);
printf("\n The ASCII value is:%d",a);
getch();
}

Output:

Enter a letter: G
The ASCII value is: 71

Result:

Thus the C program to display the ASCII value of an Entered character was
successfully executed and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


9

1d. Even or odd using Ternary operator

Write a C program to check whether given number is even or odd using ternary operator.

Aim:
To write a C program to check whether a given number is even or odd using
Ternary operator.

Algorithm:

Step1: Start the program.

Srep2: Declare the integer variables a and b.

Step3: Get the value of the number to be checked from the user as ‘a’.

Step4: Using ternary operator, evaluate the following expression:

b=a%2==0? 1 : 0.

step5: If b==1; print the number is even. Else print the number is odd.

Step6: Stop the program.

Program:

#include <stdio.h>

int main()

int a,b;

printf("Enter value of a: \n");

scanf("%d",&a);

b=a%2==0?1:0;

if (b==0)

printf("The number is Odd");

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


10

else

printf("The number is Even");

return 0;

Output:

Enter value of a: 32

The number is even

Result:

Thus the C program to display the ASCII value of an Entered character was
successfully executed and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


11

1e. Evaluating arithmetic expression

Write a C program to evaluate the arithmetic expression ((a + b / c * d - e) * (f - g)). Read the
values a, b, c, d, e, f and g from the standard input device.

Aim:
To write a C program to evaluate the arithmetic expression ((a + b / c * d - e) * (f -
g)) by taking input from a, b, c, d, e, f and g.

Algorithm:

Step 1: Start the program.


Step 2: Declare the integer variables a, b, c, d, e, f, g, result.
Step 3: Get the values of a, b, c, d, e, f, g from the user.
Step 4: Compute the following expression: result = ((a +b / c * d - e) * (f +g)).

Step 5: Display the value of result to the user.

Step 6: Stop the program.

Program:

#include<stdio.h>

#include<conio.h>

void main()

int a, b, c, d, e, f, g, result;

printf("Enter value of a :");

scanf("%d", &a);

printf("\n Enter value of b:");

scanf("%d", &b);

printf("\n Enter value of c:");

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


12

scanf("%d", &c);

printf("\n Enter value of d:");

scanf("%d", &d);

printf("\n Enter value of e:");

scanf("%d", &e);

printf("\n Enter value of f :");

scanf("%d", &f);

printf("\n Enter value of g:");

scanf("%d", &g);

result = ((a +b / c * d - e) * (f +g));

printf("\n After evaluation result is :%d",result);

getch();

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


13

Output:

Enter value of a: 1

Enter value of b: 6

Enter value of c: 3

Enter value of d: 4

Enter value of e: 3

Enter value of f: 2

Enter value of g: 1

After evaluation result is: 18

Result:
Thus the C program to evaluate the arithmetic expression ((a + b / c * d - e) * (f -g)) by
taking input from a, b, c, d, e, f and g was successfully executed and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


14

1f. Print sizeof () for all data types

Write a C program to print the sizeof () for all data types.

Aim:
To write a C program to print the sizeof () for all data types.

Algorithm:
Step 1: Start the program.
Step 2: Get four different variables from the user to display the data type.

Step 3: Using the built in function sizeof ().


Step 4: Display the size of each data type.
Step 5: Stop the program.

Program:

#include <stdio.h>
#include<conio.h>
int main()
{
int integerType;
char charType;
float floatType;
double doubletype;
printf("Size of int is: %ld”, sizeof(integerType));
printf("Size of char is: %ld”, sizeof(charType));
printf("Size of float is: %ld”, sizeof(floatType));
printf("Size of double is: %ld”, sizeof(doubleType));
return 0;
getch();
}

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


15

Output:
Size of int is: 4 Size
of char is: 1 Size of
float is: 4 Size of
double is: 8

Result:
Thus the C program to print the sizeof () for all data types was successfully
executed and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


16

1g. Swap two numbers using temporary variable and without using temporary
variable

Write a C program to swap two numbers using temporary variable and without using
temporary variable.

Aim:
To write a program to swap two numbers without using a temporary variable and
with using a temporary variable.

Algorithm:
(a) Without using a temporary variable:
Step 1: Start the program.
Step 2: Get the value of two numbers from the user, let it be a and b.
Step 3: Now perform these operations:
a=a-b
b=a+b
a=a+b
Step 4: Print the variables.
Step 5: Stop the program.

(b) With using a temporary variable:


Step 1: Start the program.
Step 2: Get three variables from the user, let it be first, second, temp.
Step 3: Now perform these operations:
temp=first
first = second
second=first
Step 4: Print the variables.
Step 5: Stop the program.

Program:

(a) Without using temporary variable


#include<stdio.h>
#include<conio.h>
int main()
{

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


17

int a, b;
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);
a = a - b;
b = a+b;
a = b - a;
printf("After swapping, a = %f\n", a);
printf("After swapping, b = %.d", b);
return 0;
getch();
}

(b) With using temporary variable


#include<stdio.h>
#include<conio.h>
int main()
{
int first, second, temp;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
temp = first;
first = second;
second =temp;
printf("\nAfter swapping, first number = %d\n", first);
printf("After swapping, second number = %d", second);
return 0;
getch();
}

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


18

Output:

(a) Without using a temporary variable

Enter a: 25
Enter b: 50
After swapping, a=50
After swapping, b=25

(b) With using a temporary variable:

Enter first number: 75


Enter second number:100
After swapping, first number=100
After swapping, second number=75

Result:
Thus the C program to swap two numbers with using temporary variable and
without using temporary variable was successfully executed and the output was verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


19

EX: NO: 2 Decision-making Constructs


(If-else, goto, switch-case, break-continue)

2a. Find the year leap year or not

Write a C program to find a given year is leap or not using if-else.

Aim:
To write a C program to check whether a given year is leap year or not using if-else.

Algorithm:
Step 1: Start the program.
Step 2: Take integer variable year
Step 3: Assign value to the variable
Step 4: Check if year is divisible by 4 but not 100, DISPLAY "leap year"
Step 5: Check if year is divisible by 400, DISPLAY "leap year"
Step 6: Otherwise, DISPLAY "not leap year"
Step 7: Stop the program.

Program:
#include<stdio.h>
int main()
{
int year;
printf(“Enter a year:\n”);scanf(“%d”, &year);
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0)) printf("%d is a leap
year", year);
else
printf("%d is not a leap year", year);

return 0;
}

Output:
Enter a year: 2016 2016 is a leap year Enter a year: 2018 2018 is not a leap year

Result:
Thus the C program to check whether a given year is leap year or not using if-else has
been executed successfully and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


20

2b. Find the number odd or even

Aim:
To write a C program to check whether a given number is odd or even using if-else.

Algorithm:

Step 1: Start the program.


Step 2: Input the number to be checked (let's call it num).
Step 3: If num modulo 2 equals 0, then goto step 4 else goto step 5
Step 4: Print "The number is even".
Step 5: Print "The number is odd".
Step 6: End the program.
Program:
#include <stdio.h> // Header file for input/output operations

int main() {
// Declare variable to store the number
int num;

// Input the number


printf("Enter a number: ");
scanf("%d", &num);

// Check if the number is odd or even using if-else


if (num % 2 == 0) {
printf("The number is even\n");
} else {
printf("The number is odd\n");
}

return 0; // Indicate successful termination of the program


}
Output:

Enter a number: 7
The number is odd
Enter a number: 12
The number is even

Result:
Thus the C program to check whether a given number is odd or even using if-else has been
executed successfully and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


21

2c. Find the student is Pass or Fail

Aim:
To write a C program to check the student is pass or fail using if-else.

Algorithm:

Step 1: Start the program.


Step 2: Input the mark to be checked (let's call it num).
Step 3: If num is greater than or equals to 35, then goto step 4 else goto step 5
Step 4: Print "The student is pass".
Step 5: Print "The student is fail".
Step 6: End the program.
Program:
#include <stdio.h> // Header file for input/output operations

int main() {
// Declare variable to store the number
int num;

// Input the number


printf("Enter a number: ");
scanf("%d", &num);

if (num>=35) {
printf("The student is pass ");
} else {
printf("The student is fail");
}

return 0; // Indicate successful termination of the program


}
Output:

Enter a number: 7
The number is odd
Enter a number: 12
The number is even

Result:
Thus the C program to check whether the student is pass or fail using if-else has been executed
successfully and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


22

2d. Biggest among given three integers using nested if-else

Write a C program to find the biggest among given three integers using nested if-else.

Aim:
To write a C program to find the biggest among given three integers using nested if-else.

Algorithm:
Step 1: Start the program.
Step 2: Initialize the variables a, b and c.
Step 3: Compare the variables using nested if-else.
Step 4: Print the largest integer number.
Step 5: Stop the program.

Program:
#include<stdio.h>
void main()
{
int a, b, c;
printf("Enter three numbers\n");
scanf("%d %d %d", &a, &b, &c); if(a > b)
{
if(a > c)
printf("a: %d is largest\n", a);else
printf("b: %d is largest\n", b);
}
else if(b > c)
printf("b: %d is largest\n", b);else
printf("c: %d is largest\n", c);
}

Output:
Enter three numbers
45
89
63
b: 89 is largest

Result:
Thus the C program to find the biggest among given three integers using nested if-else
has been executed successfully and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


23

2e. Generate numbers between 20 and 100 divisible by 2 and not divisible by 3 and 5

Write a C program to generate numbers between 20 and 100 which are divisible by 2 and not
divisible by 3 and 5.

Aim:
To write a C program to generate numbers between 20 and 100 which are divisible by 2
and not divisible by 3 and 5.

Algorithm:
Step 1: Start the program.
Step 2: Initialize the value of i to 20. And check the value of i less than or equal to 100.
If true go to step 3, otherwise go to step 6.
Step 3: Check whether i is divisible by 2 and not by 3 and 5. If yes print the value of i
else go to step 5.
Step 4: Increment the value of i by 1 and repeat step 3.
Step 5: Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
int main()
{
printf(“The values between 20 and 100 that are divisible by 2 and not by 3 and 5 are”);
for(int i=20; i<100; i++)
{
if((i%2==0)&&(i%3!=0)&&(i%5!=0))
{
printf(“\n %d”,i);
}
}
getch();
return 0;
}

Output:
The values between 20 and 100 that are divisible by 2 and not by 3 and 5 are
22
26
28
32
34
38
44
46
52

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


24

56
58
62
64
68
74
76
82
86
88
92
94
98

Result:
Thus the C program to generate numbers between 20 and 100 which are divisible by 2
and not divisible by 3 and 5.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


25

2f. Functionality of continue and break statement

Write a C program to illustrate the functionality of continues and break Statement.

Break Statement:
Aim:
To write a C program to illustrate the function of break statement.
Algorithm:
Step 1: Start the program.
Step 2: Initialize the input variable i.
Step 3: Increment the value of variable from 1 to 10 using for loop.
Step 4: If the value of i is 7, then break the condition.
Step 5: So the value of i prints from 1 to 6.
Step 6: Stop the program.

Program:
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=10;++i)
{
if(i==7)
break;
printf("%d",i);
}
return 0;
}
Output:
123456

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


26

Continue Statement:
Aim:
To write a C program to illustrate the function of continue statement.
Algorithm:
Step 1: Start the program.
Step 2: Initialize the input variable i.
Step 3: Increment the value of variable from 0 to 10 using for loop.
Step 4: If the value of i is 7, then break the condition.
Step 5: Print i value and 0 to 10 will be printed.
Step 6: Stop the program.

Program:
#include <stdio.h>
int main()
{
int i;
for(i=0;i<=10;++i)
{
if(i==7)
continue;
printf("%d",i);
}
return 0;
}
Output:
01234568910

Result:
Thus the C program to illustrate the function of break and continue statement was
executed successfully and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


27

2g. Basic calculator operations – addition, subtraction, multiplication, division and


square of a number using Switch case

Write a C program to perform the basic calculator operations, namely, addition, subtraction,
multiplication, division and square of a number using Switch case.

Aim:
To write a C program to perform the basic calculator operations, namely, addition,
subtraction, multiplication, division and square of a number using Switch case.

Algorithm:
Step 1: Start the program.
Step 2: Get two integer numbers from the user.
Step 3: Create a switch statement to get the calculator options add, subtract, multiply,
divide and square in cases.
Step 4: Ask the user to give their operation by selecting Case1: Sum, Case2: Subtract,
Case 3: Multiply, Case 4: Divide and Case 5: Square.
Step 5: If not given any of cases in Step 4, Print “Enter a valid operator”.
Step 6: Stop.

Program:
#include <stdio.h>
int main()
{
int a, b, c;
char operation;
printf(" Enter two numbers:");
scanf("%d%d", &a, &b)
printf("\n Press 1 to add %d and %d", a, b);
printf("\n Press 2 to subtract %d and %d", a, b);
printf("\n Press 3 to multiply %d and %d", a, b);
printf("\n Press 4 to divide %d and %d", a, b);
printf(“\n Press 5 to square %d”, a);
printf("\n Enter the operation: ");
scanf("%d", &c);
switch (c)
{
case 1:
printf("Sum: %d", a + b);
break;
case 2:
printf("Subtract :%d", a - b);
break;

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


28

case 3:
printf("Multiply :%d", a * b);
break;
case 4:
if (b == 0)
printf("\n Denominator cannot be zero");
else
printf("Divide :%d", a / b);
break;
case 5:
printf(“Square: %d”, a*a);break;
default:
printf("Enter a valid operator!");
}
return 0;

Output:
Enter two Numbers: 3 4
Press 1 to add 3 and 4
Press 2 to subtract 3 and 4
Press 3 to multiply 3 and 4
Press 4 to divide 3 and 4
Press 5 to square 3

Enter the operation: 1


Sum: 7

Enter the operation: 9


Enter a valid operator!

Result:
Thus the C program to perform the basic calculator operations, namely, addition,
subtraction, multiplication, division and square of a number using Switch case has been executed
successfully and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


29

Ex. No.:3 Loops: for, while and do-while

3a. Print first 25 positive integers

Write a C program to print first 25 positive integers.

Aim:
To write a C program to display the first 25 positive integers.

Algorithm:
Step 1: Start the program.
Step 2: Initialize the value to variable.
Step 3: In order to display the first 25 positive integers, iterate a loop usingfor.
Here i++ denotes the increment of numbers.
Step 4: Display the values in i.
Step 5: Stop the program.

Program:
#include<stdio.h>
void main()
{
int i;
printf("The first 25 positive integers are:\n ");
for (i = 1; i <=25; i++)
{
printf("%d ,", i);
}
}

Output:
The first 25 positive integers are:
1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,14 ,15 ,16 ,17 ,18 ,19 ,20 ,21 ,22 ,23
,24 ,25

Result:
Thus the C program to display the first 25 positive integers has been executed
successfully and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


30

3b. Factorial of a given number

Aim:
To write a C program to find the factorial of a given number
Algorithm:

Step 1. Start the program.

Step 2. Input the number (let's call it num) whose factorial is to be calculated.

Step 3. Initialize variables:


3.1. Set factorial = 1
3.2. Set counter = 1

Step 4. While counter is less than or equal to num, do the following:


4.1. Multiply factorial by counter and store the result in factorial.
4.2. Increment counter by 1.

Step 5. Print the factorial.

Step 6. End the program.


Program:
#include <stdio.h>

int main()
{
// Declare variables
int num, factorial = 1, counter = 1;

// Input the number


printf("Enter a number: ");
scanf("%d", &num);

// Calculate factorial using a while loop


while (counter <= num) {
factorial *= counter;
counter++;
}

// Print the factorial


printf("The factorial of %d is %d\n", num, factorial);

return 0;
}

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


31

Output:
Enter a number: 5
The factorial of 5 is 120

Result:
Thus the C program to find the factorial of the given number has been executed
successfully and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


32

3c. Reverse a given number

Write a C program to reverse a given number.

Aim:
To write a C program to reverse the given number.

Algorithm:
Step 1: Start the program.
Step 2: Initialize the value to variable 0 to rev, n, rem.
Step 3: Ask the user to enter the value of n.
Step 4: Check the value of n is not equal to zero if true goto step 5, otherwise go to
step 7.
Step 5: Perform the following expression to reverse the given number
rem=n%10
rev=rev*10+rem
n/=10
Step 6: Display the value of rev.
Step 7: Stop the program.

Program:
#include <stdio.h>
int main()
{
int n, rev = 0, rem;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}

Output:
Enter the integer: 1231
Reversed number: 1321

Result:
Thus the C program to reverse the given number has been executed successfully and
verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


33

3d. Check a number for Palindrome

Write a C program to check whether a number is Palindrome or not.

Aim:
To write a C program to check whether a number is Palindrome or not.

Algorithm:
Step 1: Start the program.
Step 2: Initialize the value to variable 0 to rev, a to n, rem.
Step 3: Ask the user to enter the value of n.
Step 4: Check the value of n is not equal to 0, if true goto step 5, otherwise goto 7.
Step 5: Perform the following expression to check the given integer is palindrome
or not.
rem=n%10
rev=rev*10+rem
n/=10
Step 6: If a equals to rev, display “The given number is Palindrome” otherwise display
“The given number is not Palindrome”.
Step 7: Stop the program.

Program:
#include <stdio.h>
int main()
{
int n, rev = 0, rem, a;
printf("Enter an integer: ");
scanf("%d", &n);
a = n;
while (n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
if (a == rev)
printf("%d is a Palindrome.", a);
else
printf("%d is not a Palindrome.", a);
return 0;
}

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


34

Output:
Enter an integer: 676
676 is a Palindrome.

Enter an integer: 123


123 is not a Palindrome.

Result:
Thus the C program to check whether the given number is Palindrome ornot has been executed
successfully and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


35

3e. Display odd and even numbers within a given range

Write a C program to display odd and even numbers using while loop within a given
range.
Aim:
To write a C program to display odd and even number using while loop in a given
range.
Algorithm:
Step 1: Start the program.
Step 2: Read the lower limit as i.

Step 3: Read the upper limit as n.


Step 4: Open a while loop with a condition i<=n.
Step 5: Check if i is divisible by 2 , add it to the even[] array. If not add it to odd[]
array.
Step 6: Display the odd numbers array.

Step 7: Display the even numbers array.

Step 8: Stop.

Program:
#include
<stdio.h>
int main()
{
int odd[100],even[100],i,n,j=0,k=0;
printf(“Enter lower limit :”);
scanf(“%d”,&i);
printf(“Enter upper limit :”);
scanf(“%d”,&n);
while(i<=n)
{
if(i%2==0)
{
even[j]
=i;j++;

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


36

}
else
{
odd[k]
=i;k++;
}
i++;
}
printf(“The even numbers in the given range are:\n”);
for(i=0;i<k;++i)
{
printf(“%d\t”,odd[i]);}
printf(“\n”);
printf(“The even numbers in the given range are:\n”);
for(i=0;i<j;++i)
{
printf(“%d\t”,even[i]);}
return 0;

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


37

Output:
Enter lower limit: 50
Enter upper limit: 80
The even numbers in the given range are:

51 53 55 57 59 61 63 65 67 69 71 73
75 77 79

The even numbers in the given range are:

50 52 54 56 58 60 62 64 66 68 70 72
74 76 78 80

Result:
Thus the C program to display even and odd numbers using while loopwithin a
given range has been executed successfully and verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


38

Ex. No: 4 Arrays: 1D and 2D, Multi – dimensional arrays,


traversal

4a. Printing elements of an array

Aim:
To write a C program to print the elements of an array.

Algorithm:
Step 1: Start the program.
Step 2: Initialize an array ‘a’ with a size of 5.
Step 3: Create a loop that runs 5 times to insert the elements.
Step 4: Prompt the user to enter an element and store it in a variable element.
Step 5: Insert element into the array at the current index.
Step 6: Print the elements of the array.
Step 7: Create a loop that iterates over each element in the array.
Step 8: Print each element.
Step 9: Stop.

Program:
#include<stdio.h>
int main()
{
int a[]={2,4,6,8};
for(inti=0;i<=a[i];i++)
{
printf(“%d”,a[i]);
}
return 0;
}

Output:
2
4
6
8

Result:
Thus the C program to print the elements of the array has been done successfully and
the output is verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


39

4b.Linear and Binary Search

Aim:
To write a C program to find whether a search element is present in the array or not

(LINEAR SEARCH)

Algorithm

Step 1. Start the program.

Step 2. Input the array of elements (let's call it arr) and the element to be searched (let's call it key).

Step 3. Initialize a variable (let's call it found) to keep track of whether the element is found, initially set
it to false.

Step 4. Iterate over each element in the array using a loop:


4.1. If the current element is equal to the key, set found to true and break out of the loop.
4.2. If the current element is not equal to the key, continue to the next element.

Step 5. If found is true, print "Element found at index i", where i is the index of the element in the array.
If found is false, print "Element not found".

Step 6. End the program.

Program:
#include <stdio.h>

int main() {
int arr[100], n, key;

// Input the number of elements in the array


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

// Input the array elements


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

// Input the element to be searched


printf("Enter the element to be searched: ");
scanf("%d", &key);

// Perform linear search


int found = 0, index;
for (index = 0; index < n; index++) {
if (arr[index] == key) {
found = 1;
break;
}

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


40

// Check if the element is found or not and print the result


if (found) {
printf("Element found at Position %d.\n", index+1);
} else {
printf("Element not found.\n");
}

return 0;
}

Output:

Enter the number of elements in the array: 5


Enter 5 elements:
3
6
9
12
15
Enter the element to be searched: 9
Element found at Position 3.

(BINARY SEARCH)

Algorithm

Algorithm: Binary Search

Step 1. Start the program.

Step 2. Input a sorted array of elements (let's call it arr) and the element to be searched (let's call it key).

Step 3. Initialize variables:


3.1. Set low = 0 (index of the first element in the array).
3.2. Set high = n - 1 (index of the last element in the array), where n is the number of elements in the
array.

Step 4. Repeat the following steps while low is less than or equal to high:
4.1. Compute mid as the average of low and high, i.e., mid = (low + high) / 2.
4.2. If the element at index mid is equal to the key, return mid (element found).
4.3. If the element at index mid is greater than the key, set high = mid - 1 (discard the right half).
4.4. If the element at index mid is less than the key, set low = mid + 1 (discard the left half).

Step 5. If the element is not found, return -1.

Step 6. End the program.

Program:
#include <stdio.h>

int main() {

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


41

int arr[100], n, key;

// Input the number of elements in the array


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

// Input the sorted array elements


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

// Input the element to be searched


printf("Enter the element to be searched: ");
scanf("%d", &key);

// Binary search
int low = 0, high = n - 1, found = 0, mid, index;

while (low <= high) {


mid = (low + high) / 2;

if (arr[mid] == key) {
found = 1;
index = mid;
break;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}

// Print the result


if (found) {
printf("Element found at Posotion %d.\n", index+1);
} else {
printf("Element not found.\n");
}

return 0;
}

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


42

Output:
Enter the number of elements in the array: 5
Enter 7 sorted elements:
1
3
5
7
9
11
13
Enter the element to be searched: 5
Element found at Position 3.

Result:
Thus the C program to find whether a search element is present in the array or not has
been done successfully andthe output is verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


43

4c.Selection sort

Aim:

To write a C program to sort the elements in the array using selection sort.
Algorithm

Step 1. Start the program.

Step 2. Input an array of elements (let's call it arr) to be sorted.

Step 3. Iterate through the array from the first element to the second last element:
3.1. Assume the current element as the minimum element (let's call it min).
3.2. Iterate through the remaining unsorted elements:
3.2.1. If any element is smaller than min, update min to that element.
3.3. Swap the current element with the minimum element (min) found in the unsorted portion.

Step 4. Repeat steps 3 until all elements are sorted.

Step 5. End the program.

Program:
#include <stdio.h>

int main() {
int arr[100], n;

// Input the number of elements in the array


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

// Input the array elements


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

// Selection sort
for (int i = 0; i < n - 1; i++) {
// Assume the current element as the minimum
int min_index = i;

// Find the index of the minimum element in the unsorted portion


for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}

// Swap the current element with the minimum element


int temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


44

// Print the sorted array


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

return 0;
}

Output:

Enter the number of elements in the array: 6


Enter 6 elements:
5
2
7
1
9
3
Sorted array:
123579

Result:
Thus the C program to sort the elements in the array using selection sort has been done
successfully andthe output is verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


45

4d. Matrix Addition

Write a C program to perform matrix addition

Aim:
To write a C program to perform matrix addition.

Algorithm:
Step 1: Start the program.
Step 2: Initialize an empty matrix C with the same dimensions asmatrices A and B.
Step 3: Iterate over each row i and column j of matrices A, B, and C.
Step 4: Calculate the sum of the corresponding elements from matrices A and B.
Step 5: store it in the corresponding position of matrix C (C[i][j] = A[i][j] + B[i][j]).
Step 6: After iterating over all the elements, matrix C will contain the result of the
addition.
Step 7: Stop.

Program:
#include<stdio.h>
int main()
{
inti=0,j=0;
int g ,h;
printf(“Enter row size”);
scanf(“%d”,&g);
printf(Enter column size”);
scanf(“%d”,&h);
int a[g][h];
int b[g][h];
printf(“Enter the First Matrix elements :”);
for (i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“a[%d] [%d]=”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“First Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d”,a[i][j]);
}
printf(“ ”);
printf(“Enter Second Matrix elements:” );

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


46

for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“b[%d][%d]= ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf( “Second Matrix: ”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d ”,b[i][j]);
}
printf(“ ”);
}
printf(“Addition of Two Matrix “);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d ”,a[i][j]+b[i][j]);
}
printf(“ ” );
}
return 0;
}

Output:

Enter row size:3


Enter column size:3
Enter First Matrix elements:
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
First Matrix:
1 2 3
4 5 6
7 8 9
Enter Second Matrix elements:
a[0][0]=1
a[0][1]=2

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


47

a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
Second Matrix:
1 2 3
4 5 6
7 8 9

Addition of Two Matrix:


2 4 9
8 10 12
14 16 18

Result:
Thus the C program to print the Addition of Two Matrix has been done successfully
and the output is verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


48

4e. Matrix multiplication

Write a C program to multiply two 3x3 matrices.

Aim:
To write a C program to perform matrix multiplication.

Algorithm:
Step 1: Start the program.
Step 2: Initialize an empty matrix C with the appropriate dimensions for the resulting
matrix.
Step 3: Iterate over each row i of matrix A.
Step 4: Iterate over each column j of matrix B.
Step 5: Iterate over each element k of the row i of matrix A and the column j of matrix
B.
Step 6: Multiply the corresponding elements from matrix A and matrix B, and
accumulate the result.
Step 7: Store the accumulated value in the corresponding position of matrix C (C[i][j]
= accumulated value).
Step 8: Repeat steps 2-6 until all rows and columns have been processed.
Step 9: After iterating over all the elements, matrix C will contain the result of the
matrix multiplication.
Step 10: Stop the program.

Program:
#include<stdio.h>
int main()
{
inti=0,j=0;
int g ,h;
printf(“enter row size”);
scanf(“%d”,&g);
printf(“enter column size”);
scanf(“%d”,&h);
int a[g][h];
int b[g][h];
printf(“Enter the First Matrix elements :”);
for (i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“a[%d] [%d]=i,j);
scanf(“%d”,&a[i][j]);
}
}
Printf(“First Matrix:”);
for(i=0;i<g;i++)
{

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


49

for(j=0;j<h;j++)
{
printf(“%d”,a[i][j]);
}
printf(“ ”);
printf(“Enter Second Matrix elements:” );
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“b[%d][%d]= ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf( “Second Matrix: ”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
Printf(“%d ”,b[i][j]);
}
printf(“ ”);
}
printf(“Multiplication of Two Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
int c=0;
for(int k=0;k<h;k++)
{
c=c+(a[i][j]*b[i][j]);
}
printf(“%d”,c);
printf(“ “);
}
printf(“ “);
}
return 0;
}

Output:
Enter row size:3
Enter column size:3
Enter First Matrix elements:
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


50

a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
First Matrix:
1 2 3
4 5 6
7 8 9
Enter Second Matrix elements:
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
Second Matrix:
1 2 3
4 5 6
7 8 9
Multiplication of two matrix:
30 36 42
66 81 96
102 126 150

Result:
Thus the C program to print the multiplication of Two Matrix has been done
successfully and the output is verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


51

4f. Transpose of a Matrix

Write a C program for transpose of a matrix.

Aim:
To write a C program to perform transpose of a matrix.

Algorithm:
Step 1: Start the program.
Step 2: Initialize an empty matrix C with dimensions equal to the number of columns
in the original matrix A and the number of rows in A.
Step 3: Iterate over each row i and column j of matrix A.
Step 4: Assign the element A[i][j] to the position C[j][i].
Step 5: After iterating over all the elements, matrix C will be the transpose of matrix
A.
Step 6: Stop the program.

Program:
#include<stdio.h>
int main()
{
inti=0,j=0;
int g ,h;
printf(“enter row size”);
scanf(“%d”,&g);
printf(“enter column size”);
scanf(“%d”,&h);
int a[g][h];
int c[g][h];
printf(“Enter the First Matrix elements :”);
for (i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“a[%d] [%d]=”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“First Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d”,a[i][j]);
}
printf(“ “);
}

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C


52

for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
c[i][j]=a[i][j];
}
}
printf(“Transpose of Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf( “%d”,c[i][j]);
}
printf(“ ”);
}
return 0;
}

Output:
Enter row size: 3
Enter column size: 3
Enter First Matrix elements:
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9

First Matrix:
1 2 3
4 5 6
7 8 9

Transpose of Matrix:
1 4 7
2 5 8
3 6 9

Result:
Thus the C program to print the transpose of a matrix has been done successfully and the
output is verified.

N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C

You might also like