0% found this document useful (0 votes)
25 views29 pages

Practical File

The document is a programming lab practical file for a student named Sujal Singh, detailing various C programming experiments. It includes a list of experiments focused on fundamental programming concepts such as printing text, finding the greatest number, calculating factorials, and performing arithmetic operations. Each experiment contains an aim, description, algorithm, flowchart, program code, and input/output examples.

Uploaded by

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

Practical File

The document is a programming lab practical file for a student named Sujal Singh, detailing various C programming experiments. It includes a list of experiments focused on fundamental programming concepts such as printing text, finding the greatest number, calculating factorials, and performing arithmetic operations. Each experiment contains an aim, description, algorithm, flowchart, program code, and input/output examples.

Uploaded by

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

Programming Lab Practical File

Name Sujal Singh

Roll Number

Branch IIOT–B1–23

School University School of Automation & Robotics

University Guru Gobind Singh Indraprastha University


Index

No. Name of Experiment Remarks

0 Write a program to print “Hello, World”.

Write a C program to find the greatest number among three numbers


1
provided by the user using if else.
Write a C program to find the sum of individual digits of a positive
2
integer using while.

3 Write a C program to find the roots of a quadratic equation.

Write a C program to perform arithmetic operations using switch


4
case statement.
Write a C program to find the factorial of a given integer using
5(a)
non-recursive function.
Write a C program to find the factorial of a given integer using
5(b)
recursive function.
Write a C program to find GCD of two integers by using a recursive
6
function.
Write a C program to find the largest and smallest number in a list of
7
integers.

8 Write a C program to sort an array in ascending order.

9 Write a C program to multiply two matrices.

10 Write a C program to check whether a matrix is symmetric or not.


Experiment–0
Aim:
Write a C program to print “Hello, World!”

Description:
Program to print “Hello, World”.

Algorithm:
1: Start
2: print “Hello, World!”
3: Stop
Flowchart:

Start

print “Hello, World!”

Stop

Program:
1 #include <stdio.h>
2
3 int main() {
4 printf("Hello, World!" ); // prints "Hello, World!"
5 return 0;
6 }
Input & Output:
1 Hello, World!

1
Experiment–1
Aim:
Write a C program to find the greatest number among three numbers provided by the user
using if else.

Description:
Program that finds the largest number among three numbers input by the user.

Algorithm:
1: Start
2: read a, b, c
3: if a ≥ b and a ≥ c then
4: largest ← a
5: else if b ≥ a and b ≥ c then
6: largest ← b
7: else
8: largest ← c
9: print largest
10: Stop
Flowchart:

Start

read a, b, c

yes
a ≥ b and a ≥ c largest = a

no

yes
largest = b b ≥ a and b ≥ c

no

largest = c

print largest

Stop

2
Program:
1 #include <stdio.h>
2
3 int main() {
4 int a, b, c, largest;
5
6 // Read first number
7 printf("Enter first number: " );
8 scanf("%d" , &a);
9
10 // Read second number
11 printf("Enter second number: " );
12 scanf("%d" , &b);
13
14 // Read third number
15 printf("Enter third number: " );
16 scanf("%d" , &c);
17
18 if (a >= b && a >= c) {
19 // a is larger than both b and c
20 // (it might also be equal to one of them or even both)
21 largest = a;
22 } else if (b >= a && b >= c) {
23 // b is larger than both a and c
24 // (it might also be equal to one of them or even both)
25 largest = b;
26 } else {
27 // Since a and b aren't the largest, c must be.
28 largest = c;
29 }
30
31 // Output the largest number among the three.
32 printf("%d is the largest number." , largest);
33 return 0;
34 }
Input & Output:
1 Enter first number: 1
2 Enter second number: 2
3 Enter third number: 3
4 3 is the largest number.

3
Experiment–2
Aim:
Write a C program to find the sum of individual digits of a positive integer using while.

Description:
Program that uses a while loop to find the sum of the individual digits in a number input by
the user.

Algorithm:
1: Start
2: rem, sum ← 0
3: read num
4: while num ̸= 0 do
5: rem ← num mod 10
6: sum ← sum + rem
7: num ← num
10
8: print sum
9: Stop
Flowchart:

Start

rem, sum = 0

read num

no yes
print sum num ̸= 0 rem = num mod 10

Stop sum = sum + rem

num
num = 10

4
Program:
1 #include <stdio.h>
2
3 int main() {
4 int num, rem, sum = 0;
5
6 // Read num
7 printf("Enter a number: " );
8 scanf("%d" , &num);
9
10 // Keep removing the one's place from num and add the removed digit to
,→ the running sum.
11 while (num != 0) {
12 rem = num % 10;
13 sum += rem;
14 num /= 10;
15 }
16
17 // Output sum of digits
18 printf("Sum of digits = %d" , sum);
19 return 0;
20 }
Input & Output:
1 Enter a number: 1234
2 Sum of digits = 10

5
Experiment–3
Aim:
Write a C program to find the roots of a quadratic equation.

Description:
Program that finds the roots of a quadratic equation by using the quadratic formula:

−b ± b2 − 4ac
x=
2a
Algorithm:
1: Start
2: read a, b, c
3: discriminant ← b2 − 4ac
4: if discriminant > 0 then√
5: root1, root2 ← −b± discriminant
2a
6: else if discriminant = 0 then
7: root1, root2 ← −b
2a
8: else
9: No real roots exist
10: print root1, root2
11: Stop
Flowchart:

Start

read a, b, c

disriminant ← b2 − 4ac

=0 >0 √
−b −b± discriminant
root1, root2 ← 2a discriminant root1, root2 ← 2a

<0

No real roots exist

print calculated roots

Stop

6
Program:
1 #include <stdio.h>
2 #include <math.h>
3
4 int main() {
5 float a, b, c, discriminant, root1, root2;
6
7 // Read a
8 printf("Enter xˆ2 coefficient (a): " );
9 scanf("%f" , &a);
10
11 // Read b
12 printf("Enter x coefficient (b): " );
13 scanf("%f" , &b);
14
15 // Read c
16 printf("Enter constant (c): " );
17 scanf("%f" , &c);
18
19 // Calculate the discriminant for the given equation
20 discriminant = (b * b) - (4 * a * c);
21
22 // Calculate roots by using the quadratic formula
23 if (discriminant > 0) {
24 root1 = (b + sqrt(discriminant) / (-2 * a));
25 root2 = (b - sqrt(discriminant) / (-2 * a));
26
27 printf("Two distinct real roots exist: \n %f \n %f" , root1, root2);
28 } else if (discriminant == 0) {
29 root1 = b / (-2 * a);
30 printf("One distinct real root exists: \n %f" , root1);
31 } else {
32 printf("No real roots exist." );
33 }
34
35 return 0;
36 }
Input & Output:
1 Enter xˆ2 coefficient (a): 1
2 Enter x coefficient (b): 2
3 Enter constant (c): 3
4 No real roots exist.

7
Experiment–4
Aim:
Write a C program to perform arithmetic operations using switch case statement

Description:
Program that uses switch case statements to perform an arithmetic operation on two numbers
input by the user.

Algorithm:
1: Start
2: read a, b, operation
3: if operation is add then
4: print a + b
5: else if operation is subtract then
6: print a − b
7: else if operation is multiply then
8: print a × b
9: else if operation is divide then
10: print ab
11: else
12: print “Invalid Operation”
13: Stop
Flowchart:

Start

read a, b, operation

switch operation

no no no no
case 1 case 2 case 3 case 4 Invalid Operation

yes yes yes yes

a
print a + b print a − b print a × b print b

Stop

8
Program:
1 #include <stdio.h>
2
3 int main() {
4 int a, b;
5 int operation;
6
7 // Read a
8 printf("Enter first number: " );
9 scanf("%d" , &a);
10
11 // Read b
12 printf("Enter second number: " );
13 scanf("%d" , &b);
14
15 // Read operation
16 printf("Enter operation add(1), subtract(2), multiply(3), divide(4):
,→ " );
17 scanf("%d" , &operation);
18
19 // Perform the operation input by the user on a and b
20 switch (operation) {
21 case 1:
22 printf("%d + %d = %d" , a, b, a + b);
23 break;
24 case 2:
25 printf("%d - %d = %d" , a, b, a - b);
26 break;
27 case 3:
28 printf("%d x %d = %d" , a, b, a * b);
29 break;
30 case 4:
31 printf("%d / %d = %f" , a, b, (float) a / b);
32 break;
33 default:
34 printf("Invalid operation." );
35 }
36
37 return 0;
38 }
Input & Output:
1 Enter first number: 2
2 Enter second number: 2
3 Enter operation add(1), subtract(2), multiply(3), divide(4): 1
4 2 + 2 = 4

9
Experiment–5(a)
Aim:
Write a C program to find the factorial of a given integer using non-recursive function.

Description:
Program that finds the factorial of a number input by the user without using a recursive
function.

Algorithm:
1: Start
2: fact ← 1
3: read n
4: while n ̸= 0 do
5: fact = fact × n
6: n=n−1
7: print n
8: Stop
Flowchart:

Start

fact = 1

read n

no yes
print n n ̸= 0 fact = fact × n

Stop n=n−1

10
Program:
1 #include <stdio.h>
2
3 int main() {
4 // Initialize fact to 1 since 0! = 1
5 int fact = 1, n;
6
7 // Read n
8 printf("Enter number: " );
9 scanf("%d" , &n);
10
11 // Keep multiplying fact by the current value of n and subtract 1 from
,→ n on each iteration.
12 while (n != 0) {
13 fact *= n;
14 n -= 1;
15 }
16
17 // Output factorial
18 printf("Factorial is %d" , fact);
19 return 0;
20 }
Input & Output:
1 Enter number: 5
2 Factorial is 120

11
Experiment–5(b)
Aim:
Write a C program to find the factorial of a given integer using recursive function.

Description:
Program that finds the factorial of a number input by the user by using a recursive function.

Algorithm:
1: Start
2: function factorial(n)
3: if n = 0 then
4: return 1
5: else
6: return n × factorial(n − 1)
7: read n
8: print factorial(n)
9: Stop
Flowchart:

Start factorial(n)

read n yes
n=0 return 1

no
print factorial(n)
return n × factorial(n − 1)

Stop

12
Program:
1 #include <stdio.h>
2
3 // Recursive implementation of the factorial function,
4 // similar to how it's mathematically stated in terms of itself.
5 int factorial(int n) {
6 if (n == 0) {
7 return 1;
8 }
9
10 return n * factorial(n - 1);
11 }
12
13 int main() {
14 int n;
15
16 // Read n
17 printf("Enter number: " );
18 scanf("%d" , &n);
19
20 // Output factorial
21 printf("Factorial of %d is %d" , n, factorial(n));
22
23 return 0;
24 }
Input & Output:
1 Enter number: 5
2 Factorial of 5 is 120

13
Experiment–6
Aim:
Write a C program to find GCD of two integers by using a recursive function.

Description:
Program that finds the greatest common denominator of two integers input by the user using
a recursive function.

Algorithm:
1: Start
2: function gcd(a, b)
3: if b ̸= 0 then
4: return gcd(b, a mod b)
5: else
6: return a
7: read a, b
8: print gcd(a, b)
9: Stop
Flowchart:

Start gcd(a, b)

read a, b no
b ̸= 0 return a

yes
print gcd(a, b)
return gcd(b, a mod b)

Stop

14
Program:
1 #include <stdio.h>
2
3 // Recursive function that calculates the greatest common denominator for
,→ 2 positive integers.
4 int gcd(int a, int b) {
5 if (b != 0) {
6 return gcd(b, a % b);
7 }
8 return a;
9 }
10
11 int main() {
12 int a, b;
13
14 // Read a
15 printf("Enter first number: " );
16 scanf("%d" , &a);
17
18 // Read b
19 printf("Enter second number: " );
20 scanf("%d" , &b);
21
22 // Output the greatest common denominator of a and b
23 printf("GCD(%d, %d) = %d" , a, b, gcd(a, b));
24
25 return 0;
26 }
Input & Output:
1 Enter first number: 5
2 Enter second number: 10
3 GCD(5, 10) = 5

15
Experiment–7
Aim:
Write a C program to find the largest and smallest number in a list of integers.

Description:
Program to find the largest and smallest number in an array

Algorithm:
1: Start
2: read arr
3: smallest, largest ← arr[0]
4: smallest
5: for ∀ j ∈ {0, . . . , size} do
6: if arrj < smallest then
7: smallest ← arrj
8: else if arrj > largest then
9: largest ← arrj
10: print smallest, largest
11: Stop
Flowchart:

Start

read arr

largest ← arrj

arrj > largest

0 < j < size


∀ j ∈ {0, . . . , size} arrj

arrj < smallest

smallest ← arrj

j ≥ size

print smallest, largest

Stop

16
Program:
1 #include <stdio.h>
2
3 int main() {
4 int size = 5, arr[size];
5
6 // Read array
7 for (int i = 0; i < size; i++) {
8 // Another approach would be to merge the second loop right here
9 printf("Enter number %i: " , i+1);
10 scanf("%d" , &arr[i]);
11 }
12
13 int smallest, largest;
14 smallest = largest = arr[0];
15
16 // Find largest and smallest
17 for (int j = 0; j < size; j++) {
18 if (arr[j] < smallest) {
19 smallest = arr[j];
20 }
21
22 if (arr[j] > largest) {
23 largest = arr[j];
24 }
25 }
26
27 // Output largest and smallest
28 printf("Smallest: %d \n Largest: %d" , smallest, largest);
29
30 return 0;
31 }
Input & Output:
1 Enter number 1: 1
2 Enter number 2: 2
3 Enter number 3: 3
4 Enter number 4: 4
5 Enter number 5: 5
6 Smallest: 1
7 Largest: 5

17
Experiment–8
Aim:
Write a C program to find the largest and smallest number in a list of integers.

Description:
Program that sorts an array input by the user in ascending order by using bubble sort.

Algorithm:
1: Start
2: read arr
3: for ∀ i ∈ {0, . . . , size − 1} do
4: for ∀ j ∈ {0, . . . , size − i − 1} do
5: if arrj > arrj+1 then
6: arrj , arrj+1 ← arrj+1 , arrj
7: print arr
8: Stop
Flowchart:

Start

read arr no

0 < i < size − 1 0 < j < size − i − 1


∀ i ∈ {0, . . . , size − 1} ∀ j ∈ {0, . . . , size − i − 1} arrj > arrj+1

yes
j ≥ size − i − 1
i ≥ size − 1
arrj , arrj+1 ← arrj+1 , arrj

print arr

Stop

18
Program:
1 #include <stdio.h>
2
3 int main() {
4 int size = 5, arr[size], temp;
5
6 // Read array
7 for (int i = 0; i < size; i++) {
8 printf("Enter number %i: " , i+1);
9 scanf("%d" , &arr[i]);
10 }
11
12 // Sort the array with bubble sort
13 for (int i = 0; i < (size - 1); i++) {
14 // Since the last element after each iteration completed in the
,→ first loop ensures the greatest number bubbles
15 // up to the highest index the second loop need not traverse the
,→ whole array.
16 for (int j = 0; j < (size - i - 1); j++) {
17 // Swap the current and the next number if they're out of
,→ order.
18 if (arr[j] > arr[j + 1]) {
19 temp = arr[j];
20 arr[j] = arr[j + 1];
21 arr[j + 1] = temp;
22 }
23 }
24 }
25
26 // Output the sorted array
27 printf("[" );
28 for (int k = 0; k < (size - 1); k++) {
29 printf(" '%d' " , arr[k]);
30 }
31 if (size > 0) {
32 printf(" '%d' " , arr[size-1]);
33 }
34 printf("]" );
35
36 return 0;
37 }
Input & Output:
1 Enter number 1: 5
2 Enter number 2: 4
3 Enter number 3: 3
4 Enter number 4: 2
5 Enter number 5: 1
6 [ '1' '2' '3' '4' '5' ]

19
Experiment–9
Aim:
Write a C program to multiply two matrices.

Description:
If A is an m × n matrix and B is an n × p matrix, such that:

··· b11 b12 · · · b1p


   
a11 a12 a1n
 a21

a22 ··· a2n 
  b21 b22 · · · b2p 
 
A=
 .. .. .. ,
..  B =  ..
 .. . . . 
 . . . .   . . . .. 
am1 am2 · · · amn bn1 bn2 · · · bnp

Then the matrix product C = AB is defined to be a m × p matrix:

···
 
c11 c12 c1p
 c21

c22 ··· c2p 

C = AB = 
 .. .. ... .. 
 . . . 

cm1 cm2 · · · cmp

Where,
n
X
cij = ai1 b1j + ai2 b2j + · · · + ain bnj = aik bkj , for i = 1, . . . , m and j = 1, . . . , p
k=1

Example:
 
" # 0 1 " # " #
0 1 2 (0 × 0 + 1 × 2 + 2 × 4) (0 × 1 + 1 × 3 + 2 × 5) 10 13
× 2 3 = =
 
3 4 5 (3 × 0 + 4 × 2 + 5 × 4) (3 × 1 + 4 × 3 + 5 × 5) 28 40
4 5

Algorithm:
1: Start
2: read A, B, m, n, p
3: for ∀ i ∈ {0, . . . , m} do
4: for ∀ j ∈ {0, . . . , p} do
5: sum ← 0;
6: for ∀ k ∈ {0, . . . , n} do
7: sum ← sum + aik bkj
8: abij ← sum
9: print AB
10: Stop

20
Flowchart:

Start

read A, B, m, n, p

i≥m
∀ i ∈ {0, . . . , m} print AB

Stop
j≥p 0<i<m

∀ j ∈ {0, . . . , p}

0<j<p

sum ← 0

k≥n
∀ k ∈ {0, . . . , n} abij ← sum

0<k<n

sum ← sum + aik bkj

21
Program:
1 #include <stdio.h>
2
3 void inputMatrix(char name, int arr[][10], int rows, int columns) {
4 for (int i = 0; i < rows; i++) {
5 for (int j = 0; j < columns; j++) {
6 printf("Enter %c_%d%d: " , name, i + 1, j + 1);
7 scanf("%d" , &(arr[i][j]));
8 }
9 }
10 }
11
12 void printMatrix(int arr[][10], int rows, int columns) {
13 for (int i = 0; i < rows; i++) {
14 printf("| " );
15 for (int j = 0; j < columns; j++) {
16 printf("%d " , arr[i][j]);
17 }
18 }
19 }
20
21 void main() {
22 int m, n, p, A[10][10], B[10][10], AB[10][10], sum;
23
24 // Read number of rows and columns for first matrix
25 printf("Enter number of rows x columns for matrix A (<10): " );
26 scanf("%dx%d" , &m, &n);
27
28 // The number of rows must be equal to the number of columns in the
,→ first matrix
29 printf("Enter number of columns for matrix B (<10): " );
30 scanf("%d" , &p);
31
32 // Read values for both matrices
33 inputMatrix('A' , A, m, n); matrixReader('B' , B, n, p);
34
35 // Calculate matrix multiplication [O(nˆ3)]
36 for (int i = 0; i < m; i++) {
37 for (int j = 0; j < p; j++) {
38 sum = 0;
39 for (int k = 0; k < n; k++) {sum += A[i][k] * B[k][j];}
40 AB[i][j] = sum;
41 }
42 }
43
44 // Print matrix multiplication
45 printf(" \n The matrix product AB =" );
46 matrixPrinter(AB, m, p);
47 }

22
Input & Output:
1 Enter number of rows for matrix A (<10): 2x3
2 Enter number of columns for matrix B (<10): 2
3
4 Enter A_11: 0
5 Enter A_12: 1
6 Enter A_13: 2
7 Enter A_21: 3
8 Enter A_22: 4
9 Enter A_23: 5
10
11 Enter B_11: 0
12 Enter B_12: 1
13 Enter B_21: 2
14 Enter B_22: 3
15 Enter B_31: 4
16 Enter B_32: 5
17
18 The matrix product AB =
19 | 10 13 |
20 | 28 40 |

23
Experiment–10
Aim:
Write a C program to check whether a matrix is symmetric or not.

Description:

A symmetric matrix is a square matrix that is equal to its transpose. Formally described as:

A is symmetric ⇐⇒ A = AT

Or,

A is symmetric ⇐⇒ ∀ i, j, aji = aij

For example, the following 3 × 3 matrix is symmetric:


 
1 2 3
A = 2 6 4


3 4 5

Since A = AT

Algorithm:
1: Start
2: read A, size
3: for ∀ i ∈ {0, . . . , size} do
4: for ∀ j ∈ {0, . . . , size} do
5: if aij ̸= aji then
6: print Matrix isn’t symmetric
7: goto Stop
8: print Matrix is symmetric
9: Stop

24
Flowchart:

Start

read A, size

i ≥ size
∀ i ∈ {0, . . . , size} print Matrix is symmetric

j ≥ size 0 < i < size

∀ j ∈ {0, . . . , size}
Stop

no
0 < j < size

aij ̸= aji

yes

print Matrix is asymmetric

25
Program:
1 #include <stdio.h>
2
3 void inputMatrix(char name, int arr[][10], int rows, int columns) {
4 printf(" \n " );
5 for (int i = 0; i < rows; i++) {
6 for (int j = 0; j < columns; j++) {
7 printf("Enter %c_%d%d: " , name, i + 1, j + 1);
8 scanf("%d" , &(arr[i][j]));
9 }
10 }
11 }
12 void printMatrix(int arr[][10], int rows, int columns) {
13 printf(" \n " );
14 for (int i = 0; i < rows; i++) {
15 printf("| " );
16 for (int j = 0; j < columns; j++) {
17 printf("%d " , arr[i][j]);
18 }
19 printf("| \n " );
20 }
21 }
22 int main() {
23 int size, matrix[10][10];
24
25 // Read number of rows and columns
26 printf("Enter size of square matrix (<10): " );
27 scanf("%d" , &size);
28 // Read values for both matrices
29 inputMatrix('a' , matrix, size, size);
30
31 // Check symmetricality
32 for (int i = 0; i < size; i++){
33 for (int j = 0; j < size; j++) {
34 if (matrix[i][j] != matrix[j][i]) {
35 // Asymmetric elements found, abort.
36 printf(" \n The given given matrix, A =" );
37 printMatrix(matrix, size, size);
38 printf("is not symmetric as a_%d%d does not equal a_%d%d" ,
,→ i + 1, j + 1, j + 1, i + 1);
39 return 0;
40 }
41 }
42 }
43
44 // This step will only be reached if no asymmetric elements are found
45 printf(" \n The given given matrix, A =" );
46 printMatrix(matrix, size, size);
47 printf("is symmetric." );
48 return 0;
49 }

26
Input & Output (Symmetric):
1 Enter size of square matrix (<10): 3
2
3 Enter a_11: 1
4 Enter a_12: 2
5 Enter a_13: 3
6 Enter a_21: 2
7 Enter a_22: 6
8 Enter a_23: 4
9 Enter a_31: 3
10 Enter a_32: 4
11 Enter a_33: 5
12
13 The given given matrix, A =
14 | 1 2 3 |
15 | 2 6 4 |
16 | 3 4 5 |
17 is symmetric.
Input & Output (Asymmetric):
1 Enter size of square matrix (<10): 3
2
3 Enter a_11: 1
4 Enter a_12: 2
5 Enter a_13: 3
6 Enter a_21: 4
7 Enter a_22: 5
8 Enter a_23: 6
9 Enter a_31: 7
10 Enter a_32: 8
11 Enter a_33: 9
12
13 The given given matrix, A =
14 | 1 2 3 |
15 | 4 5 6 |
16 | 7 8 9 |
17 is not symmetric as a_12 does not equal a_21

27

You might also like