PPL Lab File
PPL Lab File
Language
Lab File
Submitted by :
Name – Rohit Hooda
Sap Id – 500110652
Batch – 44
Under the guidance of :
Dr. Ajay Singh
EXPERIMENT -1: Linux Commands and C programming
Objective: To familiarize Linux Commands and demonstrate a clear understanding
of the C-programming environment.
9.cd <name>-Used to change the directory. It is also used to convert user directory
to root directory.
Source code:-
#include<stdio.h>
int main()
{
printf("My Name is Rohit Hooda\n");
return 0;
}
Output:-
Sol- Algorithm
Step1-Start
Step2-Declare variables num1,num2 & sum, division, product
And difference
Step3-Read the values for num1 and num2.
Step4-add, subtract, multiply, divide num1 and num2.
Step5-Display add, subtract, multiply, divide and exit.
Source code
Output
Flow Chart
Start
Read X, Y
Display Diff.
Display Display Display
“Division “Sum” “Mult.”
”
End
Q2- Find the given number is even or not?
Sol- Algorithm
Step1-Start
Step2-Read: Numbers (taking input)
Step3-Check: If number%2==0 Then Print N is an even number
Else Print N is not an even number
Step4-Exit
Source code
Output
Flowchart
Start
Input
Number
Yes No
If
Number%2==0
Display Display
End
Q3- Find the biggest of three numbers?
Sol- Algorithm
Step1-Start the program.
Step2-Declare variable num1, num2 & num3.
Step3-If num1>num2 go to step4 otherwise go to step5.
Step4-If num1>num3 set largest value=num1 otherwise largest value=num3.
Step5-if num2>num3 set largest value=num2 otherwise largest value=num3.
Step6-End
Source code
Output
Flowchart
Start
Is
No Num1>n Yes
um2
Is Is
Num2> Num1
num3 >Num
No
Print
No No
“C is the largest
000 Number”
Print Print
Yes Yes
“B is the largest “A is the largest
Number” number”
End
Q4-Multiply two numbers without using arithmetic multiplication
Operator (*)?
Sol- Algorithm
Step1-Start
Step2-read two numbers.
Step3-Print the first number and scan it and print the second
Number and scan it.
Step4-now print the product using %d function.
Step5-and return the first number.
Step6-Exit
Source code
Output
Flowchart
Start
Input num1,num2,product
Int i=1
Yes No
Check
i<=num2
?
Product=product+num1
Print
product
End
Source code
Output
Sol- Algorithm
Step1-Start the program.
Step2-declare four variables base1, base2, height & area
Using float function.
Step3-then provide the two bases and height of the trapezoid
To the compiler.
Step4-compiler will compile the result and provide the area of
given trapezoid.
Source code
Output
SOL- Algorithm
Step1-Start the program
Step2-declare three variables d1, d2 & area using float func.
Step3-now provide the two diagonal of the Rhombus to the
Compiler.
Step4-compiler will compile the result and give the area of
Rhombus.
Source code
Output
Sol- Algorithm
Step1-Start the program
Step2-declare two variables r, area and assign PI as 3.14
Step3-provide the radius of the sphere to the compiler.
Step4- the compiler will compile the result and give the area
Of sphere.
Source code
Output
(v) Ellipse(with major and minor radius)
Solution- Algorithm:
Step1-Start the program.
Step2-declare three variables a, b with int and area with float
Function.
Step3- now provide the two radius a and b to the compiler
Step4-the compiler will compile the result and give area of
Ellipse.
Source code
Output
Q2-Given two numbers. Demonstrate the swapping of the values?
(i) Using a third variable
Sol- Algorithm
Step1-Start the program
Step2-now assign three variables a, b & temp using int function.
Step3-now when programming assign a as b and b as temp
where temp is the third variable used in swapping.
Step4-now provide numbers a and b to the compiler and
Run it.
Step5-compiler will show the swapped values of a and b
Source code
Output
(ii) Without using third variable
Sol- Algorithm
Step1-Start the program
Step2-assign the values to two variables a as 10 and b as 20
Using int function.
Step3-now complete the program and run it.
Step4-compiler will compile and show the result.
Source code
Output
Source code
Output
Sol- Algorithm
Step1-Start the program.
Step2-assign 3 variables as y, m, d using int function.
Step3-write a full code format to display total days into year
, month & days.
Step4-now the run the program and assign total days.
Step5-compiler will compile and write days into year months
And days.
Source code
Output
Output
Source code
Output
Q3-Check if the given number is divisible by 2 and 3 or not?
Sol-
Source code
Output
Source code
Output
Source code
Output
Q2-Multiply two numbers without using arithmetic binary operators using for
loop?
Sol-
Source code
Output
Output
Q4- Given value of ‘n’, find the sum of the series 1+ 1/2 + 1/3 + …. 1/n?
Sol-
Source code
Output
Sol-
Source code
Output
Q2- Function main() gets two numbers from the user and calls three functions in
the given order:
a. “int triangle_area(int base, int height)” returns the area of the right-angled
triangle to main().
b. “void swap(int *, int*)” swaps the two numbers using bitwise operator and
displays them.
c. “float* remainder (int a, int b)” returns the remainder of a/b to main().
Sol-
Source code
Output
Source code
/**
* C program to find sum of array elements using recursion
*/
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int N, i, sumofarray;
return 0;
}
/**
* Recursively find the sum of elements in an array.
*/
int sum(int arr[], int start, int len)
{
/* Recursion base condition */
if(start >= len)
return 0;
5
3) Convert uppercase string to lowercase using for loop.
Source Code
1. #include <stdio.h>
2. #include <conio.h>
3. int main ()
4. {
5. char upr, lwr; // declare variables
6. int ascii;
7.
8. // convert in lower case
9. printf (" Enter the Upper Case Character: ");
10. scanf (" %c", &upr);
11. ascii = upr + 32;
12. printf (" %c character in Lower case is: %c", upr, ascii);
13.
14. // convert in upper case
15. printf (" \n Enter the Lower Case Character: ");
16. scanf (" %c", &lwr);
17. ascii = lwr - 32;
18. printf (" %c character in the Upper case is: %c", lwr, ascii);
19.
20. return 0;
21. }
Output
Enter the Upper Case Character: A
A character in Lower case is: a
Enter the Lower Case Character: z
z character in the Upper case is: Z
EXPERIMENT-8: 2D Arrays & Searching
Objective: To understand the concept of 2D Arrays and searching techniques.
Output:
Enter the order of the matrix
22
Enter the co-efficients of the matrix
23 45
80 97
Sum of the 0 row is = 68
Sum of the 1 row is = 177
Sum of the 0 column is = 103
Sum of the 1 column is = 142
2)Count how many even numbers are there in a given integer 2D array. [Hint:
Linear Search]
Source Code
#include <conio.h>
int main()
{
int a[1000],i,n,even=0,odd=0;
printf("Enter size of the array : ");
scanf("%d",&n);
Source Code
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}
Output
EXPERIMENT-9: Structure and Union
Objective: To understand the concept of structure and union.
List of Lab Activities:
Write algorithm and C program, compile, execute and test the code with Linux C
compiler with suitable test cases.
1. Design a structure ‘product’ to store the details of the product purchased like
product name, price per unit, number of quantities purchased, and amount
spent. Get the name, price per unit, and number of quantities of the product
purchased. Calculate the amount spent on the product and then display all
the details of the procured product using structure pointers
Source Code
Output
2. Design a structure ‘student_record’ to store student details like name, SAP
ID, enrollment number, date of registration and date of birth. The element
date of registration is defined using another structure ‘date’ to store date
details like day, month, and year. Get data of ‘n’ students and then print the
entered values[Hint: Use concept of Nested structures and Array of
Structures.
Source Code
Output
3. Design a union ‘product’ to store the details of the product purchased like
product name, price per unit, number of quantities purchased, and amount
spent. Get the name, price per unit, and number of quantities of the product
purchased. Calculate the amount spent on the product and then display all
the details of the procured product using union pointers.
Source Code
Output
EXPERIMENT-10: Dynamic memory allocation
Output
2. Create a file with a list of ‘n’ integers. Read the numbers from the file into
an array and sort them in ascending order.
Output