PPS LAB Manual Revised
PPS LAB Manual Revised
Scheme: 2024-25
Proposed by :
Mr. Sachidanand Chaturvedi
1
Table of Contents
8 LAB 2 WAP that swaps values of two variables using a third 20-24
variable.
WAP that checks whether the two numbers entered by
the user are equal or not.
WAP to find the greatest of three numbers.
WAP that finds whether a given number is even or
odd.
9 LAB 3 WAP that tells whether a given year is a leap year or 25-29
not.
WAP that accepts marks of five subjects and finds
percentage and prints grades according to the
following criteria:
Between 90-100%-----Print ‘A’
80-90%-----------------Print ‘B’
60-80%-----------------Print ‘C’
2
Below 60%-------------Print ‘D’
12 LAB 6 WAP that simply takes elements of the array from the 42-49
user and finds the sum of these elements.
WAP that inputs two arrays and saves sum of
corresponding elements of these arrays in a third array
and prints them.
WAP to find the minimum and maximum element of
the array.
WAP to search an element in a array using Linear
Search.
WAP to add and multiply two matrices of order nxn.
WAP that finds the sum of diagonal elements of a mxn
matrix.
3
Vision of the Institute
To become Centre of Excellence in technical education and occupy prominent place
amongst top technical institutes of the nation.
4
Program Outcomes (POs)
Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis
of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant
to the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Individual and teamwork: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give
and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one‘s own work, as a member
and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
5
Course Details
Course Objectives
Course Outcomes
CO After completion of the course students will be able to:
BCS 151.1 / Write program for arithmetic and logical problems in C language.
251.1
BCS 151.1 / Write program using modular concepts, arrays, structure and looping in C
251.2 language.
BCS 151.1 / Implement the pointer and file handling concepts using C programs.
251.3
6
Syllabus
List of Experiments
S.N. Program CO
I WAP that accepts the marks of 5 subjects and finds the sum and percentage marks
obtained by the student.
II WAP that calculates the Simple Interest and Compound Interest. The Principal,
Amount, Rate of Interest and Time are entered through the keyboard.
LAB 1 CO1
III WAP to calculate the area and circumference of a circle.
IV WAP that accepts the temperature in Centigrade and converts into Fahrenheit
using the formula C/5=(F-32)/9.
II WAP that checks whether the two numbers entered by the user are equal or not.
LAB 2 CO1
III WAP to find the greatest of three numbers.
IV WAP that finds whether a given number is even or odd.
II WAP that accepts marks of five subjects and finds percentage and prints grades
according to the following criteria:
Between 90-100%-----Print ‘A’
80-90%-----------------Print ‘B’
LAB 3 60-80%-----------------Print ‘C’ CO1
Below 60%-------------Print ‘D’
III WAP that takes two operands and one operator from the user, perform the
operation, and prints the result by using switch statement.
LAB 4
III WAP to print sum of even and odd numbers from 1 to N numbers.
IV WAP to print the Fibonacci series.
I WAP to check whether the entered number is prime or not.
LAB 7 CO3
II WAP to sort the elements of the array in ascending order using Bubble Sort
technique and pointer.
I WAP to compare the contents of two files and determine whether they are same or
not.
LAB 8 CO3
II WAP to check whether a given word exists in a file or not. If yes then find the
number of times it occurs
8
CO-PO Mapping
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 3 2 - - 2 - - - - - - 3
CO2 3 2 3 - 2 - - - - - - 3
CO3 3 2 3 - 2 - - - - - - 3
9
Continuous Internal Lab Evaluation Process
CIE Marks in each practical/lab shall be the sum of marks prescribed for Lab tests
and continuous evaluation of experiments. Marks prescribed for lab tests shall be 20
and that for continuous evaluation of experiments 8.
The CIE marks awarded for lab tests in the practical Course/s shall be based on two
lab tests generally conducted during the semester. Both lab tests shall be conducted
for a maximum of 20 marks and average / best one is considered as final marks. This
final mark is proportionality scale to 30 marks.
The remaining 20 marks shall be awarded on the basis average marks obtained
for continuous evaluation during conduction of experiments.
Tentative schedule for each class test:
10
Lab Rubrics:
Weightage
Rubric Parameters
(Assessment Marks)
R1 Arrangement/Algorithm of 4
Program and Attendance of
student
R2 Observation/Execution of Experiment 2
R3 Catechism / Viva-Voce 2
Total 10
11
planning code/setup runs not provide all but not provide all errors but not
properly but not results. results. provide all
. All code/setup provide all results. results.
runs properly and
provide all results.
Rubric #R3: Viva-Voce
Level of Achievement
Excellent (90- Very Good (80- Good (60-79%) Average (40-59%) Poor (0-39%)
100%) 89%)
Demonstrates Demonstrates Has a fair idea of Has some idea of Confused in
good knowledge good knowledge both theory and theory but do not both theory
of both theory of either theory experiments know and
and experimental or experimental but confused in experimental experimental
Procedure. Procedure. experimental Procedure. procedure
procedure
12
Lab Evaluation Sheet
CIE-I
S.N. Date Name of Experiments Marks Obtained Remark and
A(4) O(2) C(2) R(2) Total(10) Signature of
faculty
CIE-II
LT-1
LT-2
Marks Obtained
CIE-I (Scaled to 20) CIE-II(Scaled to 30) Total Marks(50) Faculty Signature HOD signature
13
Program #1
Objective:
I WAP that accepts the marks of 5 subjects and finds the sum and percentage marks
obtained by the student.
II WAP that calculates the Simple Interest and Compound Interest. The Principal,
Amount, Rate of Interest and Time are entered through the keyboard.
I. WAP that accepts the marks of 5 subjects and finds the sum and percentage marks
obtained by the student.
Algorithm:
start
Read five subject marks and store them into 5 different variables.
Calculate the sum of all subjects and store in total = eng + phy + chem + math + comp.
Divide the sum of all subjects by the total number of subjects to find the average i.e.
average = total / 5.
Calculate the percentage using percentage = (total / 500) * 100.
Finally, print resultant values total, average, and percentage.
stop
Flow Chart:
14
Source Code:
#include <stdio.h>
int main()
{
float eng, phy, chem, math, comp;
float total, average, percentage;
printf("Enter marks of five subjects: :- ");
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);
15
return 0;
}
Input:
Enter marks of five subjects: :-
90
90
90
90
90
Output:
Total marks = 450.00
Average marks = 90.00
Percentage = 90.00
II.WAP that calculates the Simple Interest and Compound Interest. The Principal, Amount,
Rate of Interest and Time are entered through the keyboard.
Algorithm:
1. Begin.
2. Input principal amount (P), rate of interest (R), and time period (T) from the keyboard.
3. Calculate simple interest using the formula: Simple Interest (SI) = (P * R * T) / 100
4. Print the calculated simple interest.
5. Calculate amount (A) using the formula: Amount = P + SI
6. Input the number of times interest is compounded per time period (n) from the keyboard.
7. Calculate compound interest using the formula: Compound Interest (CI) = P * (1 + (R / (100
* n)))^(n * T) - P
8. Print the calculated compound interest.
9. End.
Flow Chart:
16
Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float p, t, r, si, ci;
clrscr();
printf("Enter principal amount (p): ");
scanf("%f", &p);
printf("Enter time in year (t): ");
scanf("%f", &t);
printf("Enter rate in percent (r): ");
scanf("%f", &r);
17
Enter principal amount (p): 5000 ↲
Input:
Output:
Simple Interest = 1800.000
Compound Interest = 1962.000
Algorithm:
Step 1: Start
Step 2: Read radius
Step 3: Calculate area, area = 3.14*radius*radius and cir = 3.14*radius*radius
Step 4: Print area , cir
Step 5: Stop
Flow Chart:
Source Code:
#include <stdio.h>
#define PI 3.14159
int main() {
double radius, area, circumference;
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
area = PI * radius * radius;
18
circumference = 2 * PI * radius;
printf("Area of the circle: %.2lf\n", area);
printf("Circumference of the circle: %.2lf\n", circumference);
return 0;
}
Input:
Enter the radius of the circle: 34
Output:
Area of the circle: 3631.68
Circumference of the circle: 213.63
IV. WAP that accepts the temperature in Centigrade and converts into Fahrenheit using the
formula C/5=(F-32)/9.
Algorithm:
1. start
2. Define temperature in Fahrenheit unit.
3. Apply in the formula C/5=(F-32)/9.
4. Print the temperature in Celsius.
5. stop
Flow Chart:
19
Source Code:
#include<stdio.h>
int main()
{
float Fahrenheit, Celsius;
Fahrenheit = 64;
Celsius = ((Fahrenheit-32)*5)/9;
printf("\n\n Temperature in Celsius is : %f",Celsius);
return (0);
}
Input:
Fahrenheit = 64;
Output:
Temperature in Celsius is: 17.7777777778
Program #2
Objective:
I WAP that swaps values of two variables using a third variable.
II WAP that checks whether the two numbers entered by the user are equal or not.
Algorithm:
Step-1 Start
Step-2 Input Two Numbers Say NUM1, NUM2
Step-3 Display Before Swap Values NUM1, NUM2
Step-4 TEMP = NUM1
Step-5 NUM1 = NUM2
Step-6 NUM2 = NUM1
Step-7 Display After Swap Values NUM1, NUM 2
Step-8 Stop Algorithm
Flow Chart:
20
Source Code:
include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
Input:
21
Enter first number: 1.20
Enter second number: 2.45
Output:
After swapping, first number = 2.45
After swapping, second number = 1.20
II. WAP that checks whether the two numbers entered by the user are equal or not.
Algorithm:
1. Start
2. Input the first number (num1)
3. Input the second number (num2)
4. If num1 is greater than num2, then set max_num to num1
5. Else, set max_num to num2
6. Output max_num as the larger number
7. End
Flow Chart:
Source Code:
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int int1, int2; // Declare two integer variables 'int1' and 'int2'.
Algorithm:
Start
Initialize the three numbers let say a,b,c
Check if a is greater than b.
If true, then check if a is greater than c.
o If true, then print ‘a’ as the greatest number.
o If false, then print ‘c’ as the greatest number.
If false, then check if bis greater than num3.
o If true, then print ‘b’ as the greatest number.
o If false, then print ‘c’ as the greatest number
end
Flow Chart:
Source Code:
#include <stdio.h>
int main()
{
int A, B, C;
23
scanf("%d %d %d", &A, &B, &C);
else
printf("%d is the largest number.", C);
return 0;
}
Input:
Enter the numbers A, B and C: 5 7 9
Output:
9 is largest number
Algorithm:
Step 1- Start the program.
Step 2- Read/input the number.
Step 3- if n%2==0 then the number is even.
Step 4- else number is odd.
Step 5- display the output.
Step 6- Stop the program.
Flow Chart:
24
Source Code:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
return 0;
}
Input:
Enter an integer: -7
Output:
-7 is odd.
Program #3
Objective:
I WAP that tells whether a given year is a leap year or not.
II WAP that accepts marks of five subjects and finds percentage and prints grades
according to the following criteria:
Between 90-100%-----Print ‘A’
80-90%-----------------Print ‘B’
60-80%-----------------Print ‘C’
Below 60%-------------Print ‘D’
III WAP that takes two operands and one operator from the user, perform the
operation, and prints the result by using switch statement.
Algorithm:
START
Step 1 → Take integer variable year
Step 2 → Assign value to the variable
25
Step 3 → Check if year is divisible by 4 but not 100, DISPLAY "leap year"
Step 4 → Check if year is divisible by 400, DISPLAY "leap year"
Step 5 → Otherwise, DISPLAY "not leap year"
STOP
Flow Chart:
Source Code:
#include <stdio.h>
int main() {
int inputYear;
return 0;
}
Input:
Enter a year: 2020
Output:
2020 is a leap year
II. WAP that accepts percentage and prints grades according to the following criteria:
26
Between 90-100%-----Print ‘A’
80-90%-----------------Print ‘B’
60-80%-----------------Print ‘C’
Below 60%-------------Print ‘D’
.
Algorithm:
Step 1: Start
Step 2: Enter the marks obtained as M
Step 3: IF M >= 90
Print "A"
Step 3: IF M >= 80 and M < 90
Print "B"
Step 4: IF M >= 60 and M < 80
Print "C"
ELSE
Print "D"
[END OF IF]
Step 6: End
Flow Chart:
Source Code:
#include<stdio.h>
void main()
{
int marks;
27
printf("Enter your marks ");
scanf("%d",&marks);
if(marks<0 || marks>100)
{
printf("Wrong Entry");
}
else if(marks<60)
{
printf("Grade D");
}
else if(marks>=60 && marks<80)
{
printf("Grade C");
}
else if(marks>=80 && marks<90)
{
printf("Grade B");
}
else if(marks>=90 && marks<=100)
{
printf("Grade A");
}
else if(marks>=80 && marks<90)
}
Input:
92
Output:
Grade A
III. WAP that takes two operands and one operator from the user, perform the operation, and
prints the result by using switch statement.
Algorithm:
1. Start
2. Declare 4 variables - num1, num2, result, opt. The variables num1 and num2 are for storing
the values or operands, for storing the calculation result, and opt to take the operator as
inputs.
3. Take inputs of the operands, operator.
4. Use switch case or conditional statements to check the operator.
5. Display the result according to the operator.
6. End
Flow Chart:
28
Source Code:
#include <stdio.h>
int main() {
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
29
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
Input:
Enter an operator (+, -, *, /): +
Enter two operands: 23 10
Output:
23.0 + 10.0 = 33.0
.
Program #4
Objective:
I WAP to print the sum of all numbers up to a given number.
II WAP to find the factorial of a given number.
III WAP to print sum of even and odd numbers from 1 to N numbers.
IV WAP to print the Fibonacci series.
Algorithm:
start
Include the required Header files using include keyword.
Initialize the required variables.
Create a variable sum = 0
Run for loop in iterations of (I) in between [a, b]
o In each iteration add I to sum value
o Print sum value
end
30
Flow Chart:
Source Code:
#include <stdio.h>
int main() {
int n, i, sum = 0;
Input:
Enter a positive integer: 100
Output:
Sum = 5050
31
Start program
Ask the user to enter an integer to find the factorial
Read the integer and assign it to a variable
From the value of the integer up to 1, multiply each digit and update the final value
The final value at the end of all the multiplication till 1 is the factorial
End program
Flow Chart:
Source Code:
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
return 0;
}
Input:
Enter an integer: 5
32
Output:
Factorial of 5 = 120
III. WAP to print sum of even and odd numbers from 1 to N numbers.
Algorithm:
1. Start
2. Take the number N upto which we have to find the sum as input.
3. Using for loop take the elements one by one from 1 to N.
4. Using if,else statements separate the element as even or odd.
5. Add the even and odd numbers separately and store it in different variables.
6. Print the sum separately and exit.
Flow Chart:
Source Code:
#include <stdio.h>
void main()
{
int i, num, odd_sum = 0, even_sum = 0;
33
printf("Enter the value of num\n");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
if (i % 2 == 0)
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
printf("Sum of all odd numbers = %d\n", odd_sum);
printf("Sum of all even numbers = %d\n", even_sum);
}
Input:
Enter the value of num
10
Output:
Sum of all odd numbers = 25
Sum of all even numbers = 30
Algorithm:
START
Step 1 → Take integer variable A, B, C
Step 2 → Set A = 0, B = 0
Step 3 → DISPLAY A, B
Step 4 → C = A + B
Step 5 → DISPLAY C
Step 6 → Set A = B, B = C
Step 7 → REPEAT from 4 - 6, for n times
STOP
Flow Chart:
34
Source Code:
#include <stdio.h>
int main() {
int i, n;
return 0;
}
Input:
Enter the number of terms: 10
Output:
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
Program #5
Objective:
I WAP to check whether the entered number is prime or not.
II WAP to find the sum of digits of the entered number.
Algorithm:
STEP 1: start, Take num as input.
STEP 2: Initialize a variable temp to 1.
STEP 3: Iterate a “for” loop from 2 to sqrt(num).
36
STEP 4: If num is divisible by loop iterator, then update temp value to 0.
STEP 5: If the temp is equal to 1,
Return “Num IS PRIME”.
Else,
Return “Num IS NOT PRIME”.
STEP 6: Stop
Flow Chart:
Source Code:
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
if (n % i == 0) {
flag = 1;
break;
}
37
}
return 0;
}
Input:
Enter a positive integer: 7
Output:
7 is not prime number
Algorithm:
o Step 1: start, Get number by user
o Step 2: Get the modulus/remainder of the number
o Step 3: sum the remainder of the number
o Step 4: Divide the number by 10
o Step 5: Repeat the step 2 while number is greater than 0.
o Step 6: Stop
Flow Chart:
Source Code:
#include<stdio.h>
int main()
{
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
return 0;
}
38
Input:
Enter a number:654
Output:
Sum is: 15
Algorithm:
1. Start
2. Take the input from the user
3. Perform remainder = number % 10
4. Print remainder
5. Perform number/10
6. Repeat steps 2 and 4 until the input number cannot be further divided.
7. Stop
Flow Chart:
Source Code:
#include<stdio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
39
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
return 0;
}
Input:
Enter a number: 123
Output:
Reversed Number: 321
Source Code:
#include <math.h>
#include <stdio.h>
int main()
{
int i, sum, num, count = 0;
printf("All Armstrong number between 1 and 100 are:\n");
40
No Input
Output:
123456789
V. WAP to convert binary number into decimal number and vice versa.
Source Code:
include <stdio.h>
#include <math.h>
int convert(long long);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
Input:
Enter a binary number: 1101
Output:
1101 in binary = 13 in decimal
41
Program #6
Objective:
I WAP that simply takes elements of the array from the user and finds the sum of
these elements.
II WAP that inputs two arrays and saves sum of corresponding elements of these
arrays in a third array and prints them.
III WAP to find the minimum and maximum element of the array.
IV WAP to search an element in a array using Linear Search.
I. WAP that simply takes elements of the array from the user and finds the sum of these
elements.
Source Code:
#include <stdio.h>
int main()
42
{
int a[100];
int i, n, sum = 0;
printf("\n\nFind sum of all elements of array:\n");
printf("--------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d", &n);
printf("Input %d elements in the array :\n", n);
for (i = 0; i < n; i++)
{
printf("element - %d : ", i);
scanf("%d", &a[i]);
}
Output:
Sum of all elements stored in the array is : 15
II. WAP that inputs two arrays and saves sum of corresponding elements of these arrays in
a third array and prints them.
Source Code:
#include <stdio.h>
void main()
{
int a[10], b[10], c[10], n, i;
43
printf("Enter the number of elements:\t");
scanf("%d", &n);
Input:
Enter the number of elements: 5
Enter 5 elements for array 1:
93
37
71
03
17
Enter 5 elements for array 2:
29
84
28
75
63
Output:
Sum of two array elements are:
122
121
99
78
80
44
III. WAP to find the minimum and maximum element of the array.
Source Code:
#include<stdio.h>
void check(int arr[], int n)
{
int small, large;
small = large = arr[0];
for(int i = 1; i < n ;i++)
{
if(arr[i] < small)
small = arr[i];
if(arr[i] > large)
large = arr[i];
}
printf("Smallest Number: %d\n",small);
printf("Largest Number: %d", large);
}
int main()
{
int n;
printf("Enter the size of array: ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements of array: ");
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
check(arr, n);
return 0;
}
Input:
Enter the size of array: 5
Enter the elements: 10 20 5 40 30
Output:
Smallest Number: 5
Largest Number: 40
45
IV. WAP to search an element in a array using Linear Search.
Source Code:
#include <stdio.h>
void main()
{
int num;
int i, key, element_found = 0;
printf("Enter number of elements you would like to take as input: ");
scanf("%d", &num);
int arr[num];
printf("\nEnter all the elements of your choice:");
for (i = 0; i < num; i++)
{
scanf("%d", &arr[i]);
}
printf("\nEnter the key element that you would like to be searched: ");
scanf("%d", &key);
/* Linear search starts */
for (i = 0; i < num ; i++)
{
if (key == arr[i] )
{
element_found = 1;
break;
}
}
if (element_found == 1)
printf("we got the element at index %d",i+1);
else
printf("we haven’t got element at any index in the array\n");
}
Input:
Enter number of elements you would like to take as input: 8
Enter all the elements of your choice:
10, 50, 30, 70, 80, 20, 90, 40
Enter the key element that you would like to be searched: 52
Output:
we haven’t got element at any index in the array
46
Source Code:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
47
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Input:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333
Output:
multiply of the matrix=
666
12 12 12
18 18 18
VI. WAP that finds the sum of diagonal elements of a mxn matrix.
Source Code:
#include <stdio.h>
int main()
{
int i, j, m = 3, n = 3, a = 0, sum = 0;
// input matrix
int matrix[3][3]
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
48
// printing the input matrix
printf("The matrix is \n");
Output:
The matrix is
123
456
789
49
Program #7
Objective:
I WAP to swap two elements using the concept of pointers.
II WAP to sort the elements of the array in ascending order using Bubble Sort
technique and pointer.
Source Code:
#include <stdio.h>
void swap(int *, int *);
int main()
{
50
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
Input:
a = 10;
b = 20;
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
II. WAP to sort the elements of the array in ascending order using Bubble Sort technique
and pointer.
Source Code:
#include <stdio.h>
51
if(*(array+m) > *(array+m+1))
{
swap((array+m), (array+m+1));
}
}
}
}
int main() {
int array[] = {64, 47, 25, 28, 22, 17, 90};
int n = sizeof(array)/sizeof(array[0]);
bubbleSort(array, n);
printf("Sorted array: \n");
printArray(array, n);
return 0;
}
Input:
64, 47, 25, 28, 22, 17, 90
Output:
Sorted array:
17 22 25 28 47 64 90
52
Program #8
Objective:
I WAP to compare the contents of two files and determine whether they are same or
not.
II WAP to check whether a given word exists in a file or not. If yes then find the
number of times it occurs
I. WAP to compare the contents of two files and determine whether they are same or not.
Source Code:
#include <stdio.h>
#include <stdlib.h>
53
int compareFile(FILE * fPtr1, FILE * fPtr2, int * line, int * col);
int main()
{
FILE * fPtr1;
FILE * fPtr2;
char path1[100];
char path2[100];
int diff;
int line, col;
printf("Enter path of first file: ");
scanf("%s", path1);
printf("Enter path of second file: ");
scanf("%s", path2);
fPtr1 = fopen(path1, "r");
fPtr2 = fopen(path2, "r");
if (fPtr1 == NULL || fPtr2 == NULL)
{
printf("\nUnable to open file.\n");
printf("Please check whether file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
diff = compareFile(fPtr1, fPtr2, &line, &col);
if (diff == 0)
{
printf("\nBoth files are equal.");
}
else
{
printf("\nFiles are not equal.\n");
printf("Line: %d, col: %d\n", line, col);
}
fclose(fPtr1);
fclose(fPtr2);
return 0;
}
54
char ch1, ch2;
*line = 1;
*col = 0;
do
{
ch1 = fgetc(fPtr1);
ch2 = fgetc(fPtr2);
if (ch1 == '\n')
{
*line += 1;
*col = 0;
}
if (ch1 != ch2)
return -1;
*col += 1;
Output:
Files are not equal
Line: 2 , col: 14
II. WAP to check whether a given word exists in a file or not. If yes then find the number of
times it occurs.
Source Code:
55
#include<stdio.h>
void main()
{
FILE* filePointer;
int wordExist=0;
int bufferLength = 255;
char search[100];
printf("Enter word to be search=");
scanf("%s",search);
char line[bufferLength];
filePointer = fopen("D:\\file.txt", "r");
while(fgets(line, bufferLength, filePointer))
{
char *ptr = strstr(line, search);
if (ptr != NULL)
{
wordExist=1;
break;
}
}
fclose(filePointer);
if (wordExist==1)
{
printf("Word exists.");
}
else
{
printf("Word doesn't exist.");
}
}
Input:
File contain lines:
how are you.
this is a boy
i am good
Output:
56
Lab Viva Questions with Answers
Q 1: What is an algorithm?
Ans: An algorithm is a step-by-step procedure or a well-defined set of instructions that is
designed to solve a specific problem or perform a particular task. It is a systematic approach
to problem-solving and provides a clear and unambiguous sequence of operations that can
be executed by a computer or a human to achieve the desired result.
Q 2: What are the characteristics of a good algorithm?
Ans: A good algorithm possesses the following characteristics:
1. Correctness: The algorithm should produce the correct output for all valid inputs.
2. Efficiency: It should be able to solve the problem with reasonable resource utilization,
such as time and memory.
3. Finiteness: The algorithm must terminate after a finite number of steps.
4. Input: The algorithm should take zero or more inputs, which are well-defined.
5. Output: It should produce at least one output, which is related to the input.
6. Unambiguous: Each step in the algorithm should be clear and unambiguous, leaving no
room for ambiguity or misinterpretation.
7. Determinism: The algorithm should behave predictably and consistently for a given input.
8. Generality: It should be applicable to a wide range of problem instances within its domain.
Q 3: What is the difference between an algorithm and a program?
Ans: An algorithm is a high-level, abstract description of the steps needed to solve a
problem, whereas a program is a concrete implementation of those steps in a specific
programming language to be executed by a computer. An algorithm is a set of instructions
that can be executed by a human or a computer, while a program is a sequence of coded
instructions written in a programming language that is executed by a computer.
Q 4: What are tokens in C?
Ans: The Tokens are identifiers or the smallest single unit in a program that is meaningful to
the compiler. In C we have the following tokens:
Keywords: Predefined or reserved words in the C programming language. Every keyword
is meant to perform a specific task in a program. C Programming language supports 32
keywords.
Identifiers: Identifiers are user-defined names that consist of an arbitrarily long sequence
of digits or letters with either a letter or the underscore (_) as a first Character. Identifier
names can’t be equal to any reserved keywords in the C programming language. There
are a set of rules which a programmer must follow in order to name an identifier in C.
Constants: Constants are normal variables that cannot be modified in the program once
they are defined. Constants refer to a fixed value. They are also referred to as literals.
Strings: Strings in C are an array of characters that end with a null character (‘\0). Null
character indicates the end of the string;
Special Symbols: Some special symbols in C have some special meaning and thus, they
cannot be used for any other purpose in the program. # = {} () , * ; [] are the special
symbols in C programming language.
Operators: Symbols that trigger an action when they are applied to any variable or any
57
other object. Unary, Binary, and ternary operators are used in the C Programming
language.
Ans: To store a negative integer, we need to follow the following steps. Calculate the two’s
complement of the same positive integer.
Eg: 1011 (-5)
Step-1 − One’s complement of 5: 1010
Step-2 − Add 1 to above, giving 1011, which is -5
Ans: The Parameters which are sent from main function to the subdivided function are called
as Actual Parameters and the parameters which are declared a the Subdivided function end
are called as Formal Parameters.
58
Q12. What is Preprocessor?
Ans: A Preprocessor Directive is considered as a built-in predefined function or macro that
acts as a directive to the compiler and it gets executed before the actual C Program is
executed.
59
Q17. What is /0 character?
Ans: The Symbol mentioned is called a Null Character. It is considered as the terminating
character used in strings to notify the end of the string to the compiler.
Q18. What is the main difference between the Compiler and the Interpreter?
Ans: Compiler is used in C Language and it translates the complete code into the Machine
Code in one shot. On the other hand, Interpreter is used in Java Programming Langauge and
other high-end programming languages. It is designed to compile code in line by line fashion.
60