0% found this document useful (0 votes)
326 views48 pages

PPL Lab File

This document contains a lab file submitted by Rohit Hooda (student ID 500110652) for their programming language lab under the guidance of Dr. Ajay Singh. The file details 22 Linux commands and experiments in C programming covering getting system information, file manipulation, directories, basic I/O, control flow, and swapping values with and without a third variable. The lab file provides the objectives, algorithms, source code, outputs, and flowcharts for each experiment.

Uploaded by

D E V E S H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
326 views48 pages

PPL Lab File

This document contains a lab file submitted by Rohit Hooda (student ID 500110652) for their programming language lab under the guidance of Dr. Ajay Singh. The file details 22 Linux commands and experiments in C programming covering getting system information, file manipulation, directories, basic I/O, control flow, and swapping values with and without a third variable. The lab file provides the objectives, algorithms, source code, outputs, and flowcharts for each experiment.

Uploaded by

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

Principles of Programming

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.

1.uname- Used to get the user name.

2.uname -r -Give little bit information about user.

3.uname -a-Give all the available information about user.

4.pwd-Used to check the current present directory.

5.touch <filename>- used to create document.

6.ls -used to list all the available directories.


7.mkdir <name>-Used to create directory.

8.rmdir <name>-Used to remove directory.

9.cd <name>-Used to change the directory. It is also used to convert user directory
to root directory.

10.cd ..- Used to convert user directory to root directory.

11.ls -l <name>-Used to check the permissions Given to <name> directory.

12.cumod <command> <name>-Used to change the permissions of the directory.

13.history -Used to Check all previous commands.


14.clear -Used to clear the screen.

15.ls -a-Used to read the hidden directory.

16.cat>file-Used to insert content in the file.

17.rm <file name>-Used to remove the file.

18.rm -r <filename>-Used to remove directory.

17.more <filename>- Used to display all content present in file.

18.head <filename>-Used to display top 10 lines present in the file.


19.tail <filename>-Used to display last 10 lines of file.

20.cp <file1name> <file2name>-Used to copy the content of file1 to file2.make


the file if file2 is not present.file 1 content remain same.

21.cp -r <dir1> <dir2>-Used to copy th content of directory1 to directory2. Dir2


is created if not present.file1 content remain same.

22.mv <file/,dir1><file2/dir2>-Move file1 to file2 if file2 is an file.

Q1 – Print your name and show the output.?


Sol-

Source code:-
#include<stdio.h>
int main()
{
printf("My Name is Rohit Hooda\n");
return 0;
}

Output:-

PS C:\Users\an626\c program\miscalleneous file> gcc print.c


PS C:\Users\an626\c program\miscalleneous file> ./a.exe
My Name is Rohit Hooda

Experiment 2:-Basic of problem solving and Program Control Flow


Q1 – Given to numbers. calculate sum, difference, multiplication and Division?

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

Division=X/Y Sum=X+Y Mult=X*Y Diff=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

“Even Number” “Not an Even NUMBER”

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

Read the three numbers as


num1, num2, num3

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

Experiment 3- Programming Sequential Logic


Q1-Obtain the required input and compute the area of following
Shapes?
(i) Parallelogram (with base and height)
Sol- Algorithm
Step1-Start the program.
Step2-declare three variables base, area and altitude using float
function.
Step3-then provide the base and height of parallelogram to the
compiler.
Step4-compiler will compile the result and provide area of given
Parallelogram.

Source code

Output

(ii) Trapezoid ( with height, long base and short base)

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

(iii) Rhombus (with height and side)

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

(iv) Sphere (with radius)

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

Q3-Convert the temperature from Celsius to Fahrenheit and kelvin?


Sol- Algorithm
Step1-Start the program.
Step2-assign three variables Celsius, farar, kelvin using float
Function.
Step3-write the formulas to change Celsius into fahr & kelvin.
Step4- now run it and assign the value of Celsius.
Step5- compiler will compile and change given temperature
Into Fahrenheit and kelvin.

Source code

Output

Q4-Print the given days in years-month-day format?

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

Experiment 4-Conditional Branching


Q1-Find the greatest among three numbers?
Sol-
Source code

Output

Q2-Check Weather an given year is leap year or not?


Sol-

Source code

Output
Q3-Check if the given number is divisible by 2 and 3 or not?
Sol-

Source code

Output

Q4-Check whether a given character is vowel or consonant using switch


statement?
Sol-

Source code
Output

Experiment 5-Working with Loop/ Iterations


Q1-Program to generate Armstrong number between 1 and n?
Sol-

Source code
Output

Q2-Multiply two numbers without using arithmetic binary operators using for
loop?
Sol-

Source code
Output

Q3-Find the sum of digit in an given number using while loop?

Sol- Source code

Output

Q4- Given value of ‘n’, find the sum of the series 1+ 1/2 + 1/3 + …. 1/n?
Sol-

Source code
Output

Q5- Practice the given pattern using nested for loop?

Sol-
Source code

Output

Experiment 6-Functions, Recursion and Pointers


Q1- Function main() gets a number and calls the following three functions
a. “void armstrong(int)” checks if the given number is an Armstrong number or
not.
b. “void coprime(int) reverses the given number and checks if the given number
and reversed number are coprime.
c. “int factorial(int) computes the factorial of the given number using recursion and
returns to main().
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

Experiment 7- 1D Arrays & Strings


Q1- Find sum of all array elements using recursion.
Sol-

Source code
/**
* C program to find sum of array elements using recursion
*/

#include <stdio.h>
#define MAX_SIZE 100

/* Function declaration to find sum of array */


int sum(int arr[], int start, int len);

int main()
{
int arr[MAX_SIZE];
int N, i, sumofarray;

/* Input size and elements in array */


printf("Enter size of the array: ");
scanf("%d", &N);
printf("Enter elements in the array: ");
for(i=0; i<N; i++)
{
scanf("%d", &arr[i]);
}

sumofarray = sum(arr, 0, N);


printf("Sum of array elements: %d", 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;

return (arr[start] + sum(arr, start + 1, len));


}
Output
Enter size of the array: 10
Enter elements in the array: 1 2 3 4 5 6 7 8 9 10
Sum of array elements: 55
elements in the array: 1 2 3 4 5 6 7 8 9 10
Sum of array e
2 )Create an array ‘a1’ with ‘n’ elements. Insert an element in ith position of ‘a1’
and also delete an element from jth position of ‘a1’.
Source Code
#include<stdio.h>
//Function to insert an element in an array.
//arr[] = array, elements = number of elements present in the array
//keyToBeInserted = element to be inserted in the array
// size of the array
int insertElement(int arr[], int elements, int keyToBeInserted, int size)
{
// Check if the capacity of the array is already full
if (elements >= size)
return elements;
//If not then the element is inserted at the last index
//and the new array size is returned
arr[elements] = keyToBeInserted;  
return (elements + 1);

// Main Function
int main()
{
int array[20] = { 31, 27, 3, 54, 67, 31 };
int size = sizeof(array) / sizeof(array[0]);
int elements = 6;
int i, keyToBeInserted = 32;
printf("n Before Insertion: ");
for (i = 0; i < elements; i++)
printf("%d  ", array[i]);
// Calling the function to insert the element in the array
elements = insertElement(array, elements, keyToBeInserted, size);
printf("n After Insertion: ");
for (i = 0; i < elements; i++)
printf("%d  ",array[i]);
return 0; }
Output:

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.

List of Lab Activities:


Write algorithm and C program, compile, execute and test the code with Linux C
compiler with suitable test cases.
1. Find the sum of rows and columns of matrix of given order.
Source Code
2. #include <stdio.h>
3. void main ()
4. {
5.  
6. static int array[10][10];
7. int i, j, m, n, sum = 0;
8.  
9. printf("Enter the order of the matrix\n");
10. scanf("%d %d", &m, &n);
11.  
12. printf("Enter the co-efficients of the matrix\n");
13. for (i = 0; i < m; ++i)
14. {
15. for (j = 0; j < n; ++j)
16. {
17. scanf("%d", &array[i][j]);
18. }
19. }
20.  
21. for (i = 0; i < m; ++i)
22. {
23. for (j = 0; j < n; ++j)
24. {
25. sum = sum + array[i][j] ;
26. }
27.  
28. printf("Sum of the %d row is = %d\n", i, sum);
29. sum = 0;
30.  
31. }
32. sum = 0;
33. for (j = 0; j < n; ++j)
34. {
35. for (i = 0; i < m; ++i)
36. {
37. sum = sum + array[i][j];
38. }
39.  
40. printf("Sum of the %d column is = %d\n", j, sum);
41. sum = 0;
42.  
43. }
44.  
45. }

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

    printf("Enter elements in array : ");


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

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


    {
          if(a[i]%2==0)
          even++;
          else
          odd++;
          
    }
     printf("even numbers in array: %d",even);
     printf("\n odd numbers in array: %d",odd);
 
 
    return 0;
}
Output
Enter size of the array: 5
Enter elements in array: 1
2
3
4
5
even numbers in an array: 2
odd numbers in an array: 3
3) Store ‘n’ integers in an 1D array in ascending or descending order. Search for a
number with binary search technique.

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

Objective: To understand the concept of dynamic memory allocation


List of Lab Activities:
1. Design a structure ‘subject’ to store the details of the subject (subject name,
subject code, etc.). Usingstructure pointer allocate dynamic memory to
structure, and obtain details of ‘n’ subjects with for loop.[Hint: Try to
incorporate this in Activity 9.2 with nested structures]
Output
EXPERIMENT-11: File Handling and Sorting
Objective: To understand the concept of Files, Operations on Files, and working
with different sorting algorithms.
List of Lab Activities:
1. Merge the contents of two files to another file

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

You might also like