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

Programing in C

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

Programing in C

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

Sri Manakula Vinayagar Engineering College Programing In C

PROGRAMING IN C
INDEX

EX DATE EXPERIMENT NAME PG. MARK SIGNATURE


PT. NO
NO

1. Write a C program to find the Area of the triangle

2. Develop a C program to read a three digit number and


produce output like1 hundreds 7 tens 2 units For an input
of 172.

3. Write a C program to check whether a given character is


vowel or not using Switch – Case statement

4. Write a C program to Print the numbers from 1 to 10 along


with their squares

5. Demonstrate do—While loop in C to find the sum of ‘n’


numbers

6. Find the factorial of a given number using Functions in C

7. Write a C program to check whether a given string is


palindrome or not?

8. Write a C program to check whether a value is prime or


not?

9. Develop a C program to swap two numbers using call by


value and call by reference

10. Construct a C program to find the smallest and largest


element in an array

-1-
Sri Manakula Vinayagar Engineering College Programing In C

EX DATE EXPERIMENT NAME PG. MARK SIGNATURE


PT. NO
NO

11. Implement matrix multiplication using C program

12. Write a C program to perform various string handling


functions like strlen, strcpy, strcat, strcmp

13. Develop a C program to remove all characters in a string


except alphabets

14. Write a C program to find the sum of an integer array


using pointers

15. Write a C program to find the Maximum element in an


integer array using pointers

16. Construct a C program to display Employee details using


Structures

17. Write a C program to display the contents of a file on the


monitor screen

18. Write a File by getting the input from the keyboard and
retrieve the contents of the file using file operation
commands

19. Write a C program to create two files with a set of values.


Merge the two file contents to form a single file

20. Write a C program to pass the parameter using command


line arguments

-2-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:1 WRITE A C PROGRAM TO FIND THE AREA OF THE TRIANGLE

AIM
To write a C program to find the Area of the triangle

ALGORITHM
Step1: Start
Step2: Read the values for b, h, area.
Step3: Initialize b=5 and h=13
Step4: area = (b*h) / 2
Step5: Write the value of area
Step6: stop

FLOW CHART
Start

Read b, h, area

Initialize b=5, h=13

area = (b*h) / 2

Write area

Stop

-3-
Sri Manakula Vinayagar Engineering College Programing In C

PSEUDO CODE

1. Begin
2. declare b, h, area as float
3. set b = 5
4. set h = 13
5. set area = (b * h) / 2
6. print "area of triangle is: ", area
7. End

SOURCE CODE
#include<stdio.h>
int main()
{
float b ,h, area;
b= 5;
h= 13;
area = (b*h) / 2 ;
printf("\n\n Area of Triangle is: %f",area);
return (0);
}

OUTPUT

RESULT

Hence the output of Area of the triangle is verified successfully

-4-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:2 DEVELOP A C PROGRAM TO READ A THREE DIGIT NUMBER AND PRODUCE OUTPUT LIKE
1 HUNDREDS 7 TENS 2 UNITS FOR AN INPUT OF 172.

AIM
To develop a c program to read a three digit number and produce output like
1 hundreds 7 tens 2 units for an input of 172.

ALGORITHM
Step1: Start
Step2: Read the values of n
Step3: Calculate the hundreds digit as ‘hundreds = n / 100’.
Step4: Calculate the tens digit as ‘tens = (n / 10) % 10’.
Step5: Calculate the units digit as ‘units = n % 10’.
Step6: write hundreds, tens, units
Step7: stop

FLOW CHART
Start

Read n

hundreds = n / 100

tens = (n / 10) % 10

units = n % 10

write hundreds, tens, units

Stop

-5-
Sri Manakula Vinayagar Engineering College Programing In C

PSEUDO CODE

1. BEGIN
2. DECLARE n, hundreds, tens, units AS INTEGER
3. PRINT "Enter a three-digit number: "
4. READ n
5. hundreds = n / 100
6. tens = (n / 10) MOD 10
7. units = n MOD 10
8. PRINT hundreds, "hundreds", tens, "tens", units, "units"
9. END

SOURCE CODE
#include <stdio.h>
int main()
{
int n, hundreds, tens, units;
printf("Enter a three-digit number: ");
scanf("%d", &n);
hundreds = n / 100;
tens = (n / 10) % 10;
units = n% 10;
printf("%d hundreds %d tens %d units\n", hundreds, tens, units);
return 0;
}

OUTPUT

RESULT

Hence the output of a c program to read a three digit number and produce output like
1 hundreds 7 tens 2 units for an input of 172 is verified successfully

-6-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:3 WRITE A C PROGRAM TO CHECK WHETHER A GIVEN CHARACTER IS VOWEL OR


NOT USING SWITCH – CASE STATEMENT

AIM
To Write a C program to check whether a given character is vowel or not using Switch – Case
statement

ALGORITHM
Step1: Start
Step2: Read the character and store it in ‘ch’
Step3: Convert ‘ch’ to lowercase using ‘tolower’ function.
Step4: Using a ‘switch’ statement, check the value of ‘ch’
Step4a: If ch is 'a', 'e', 'i', 'o', or 'u' ,write "ch is a vowel."
Step4b: If ch is not 'a', 'e', 'i', 'o', or 'u' ,write "ch is a not vowel."
Step5: stop

FLOW CHART
Start

Read char ch

ch = tolower(ch)

True switch (ch) False


If ch is 'a', 'e',
'i', 'o', or 'u'

write ‘ch’ is a vowel write ‘ch’ is a not vowel

Stop

-7-
Sri Manakula Vinayagar Engineering College Programing In C

PSEUDO CODE

1. BEGIN
2. DECLARE ch AS CHARACTER
3. PRINT "Enter a character: "
4. READ ch
5. ch = tolower(ch)
6. SWITCH ch
CASE 'a':
CASE 'e':
CASE 'i':
CASE 'o':
CASE 'u':
a. PRINT ch, " is a vowel."
b. BREAK
DEFAULT:
c. PRINT ch, " is not a vowel."
7. END SWITCH
8. END

SOURCE CODE
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
ch = tolower(ch);
switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is not a vowel.\n", ch);
}

return 0;
}

-8-
Sri Manakula Vinayagar Engineering College Programing In C

OUTPUT 1

OUTPUT 2

RESULT

Hence the output of a C program to check whether a given character is vowel or not using
Switch – Case statement is verified successfully

-9-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:4 WRITE A C PROGRAM TO PRINT THE NUMBERS FROM 1 TO 10 ALONG WITH THEIR
SQUARES

AIM
To Write a C program to print the numbers from 1 to 10 along with their squares

ALGORITHM
Step1: Start
Step2: Declare an integer variable i
Step3: For i from 1 to 10 (inclusive)
Step3a: Print the value of i
Step3b: Calculate and print the square of i by (i * i)
Step4: End the loop
Step5: stop

FLOW CHART
Start

Declare int i

True For i from False


1 to 10

Calculate and print the


square of i by (i * i)

Stop

-10-
Sri Manakula Vinayagar Engineering College Programing In C

PSEUDO CODE

1. Initialize i as an integer
2. Print "Number Square"
3. For i = 1 to 10
4. Print i, i * i
End For
5. Exit program

SOURCE CODE

#include <stdio.h>

int main()
{
int i;
printf("Number\tSquare\n");
for (i = 1; i <= 10; i++)
{
printf("%d\t\t%d\n", i, i * i);
}

return 0;
}

OUTPUT

RESULT

Hence the output of a C program to print the numbers from 1 to 10 along with their squares is
verified successfully

-11-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:5 DEMONSTRATE DO—WHILE LOOP IN C TO FIND THE SUM OF ‘N’ NUMBERS

AIM
To Write a C program to find the sum of ‘n’ numbers using do—while loop

ALGORITHM
Step1: Start
Step2: Declare integer variables n, i, and sum
Step3: Read the value of n from the user
Step4: Set i to 1 and Set sum to 0
Step5: Repeat the following steps while i is less than or equal to n
Step5a: Add the value of i to sum
Step5b: Increment the value of i by 1
Step6: write sum
Step7: stop

FLOW CHART
Start

Read n

Set i to 1 and Set sum to 0

Add the value of i to sum


Increment the value of i by 1

True while i from


1 to n

False

write sum

Stop

-12-
Sri Manakula Vinayagar Engineering College Programing In C

PSEUDO CODE

1. Start
2. Declare integer variables n, i, and sum
3. Print "Enter the value of n: "
4. Read the value of n from the user
5. Set i to 1
6. Set sum to 0
7. Repeat the following steps while i is less than or equal to n:
a. Add the value of i to sum
b. Increment the value of i by 1
8. Print "The sum of the first", n, "natural numbers is:", sum
9. End the program

SOURCE CODE

#include <stdio.h>
int main()
{
int n, i = 1, sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
do
{
sum += i;
i++;
} while (i <= n);
printf("The sum of the first %d natural numbers is: %d\n", n, sum);
return 0;
}

OUTPUT 1

OUTPUT 2

RESULT

Hence the output of a C program to find the sum of ‘n’ numbers is verified successfully

-13-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:6 FIND THE FACTORIAL OF A GIVEN NUMBER USING FUNCTIONS IN C

AIM
To find the factorial of the given number using functions.

ALGORITHM
Step 1:Start
Step 2:Include the necessary header file stdio.h.
Step 3:Define a recursive function factorial to calculate the factorial of a number:
Step 3a:If num is 0 or 1, return 1 (base case).
Step 3b: Otherwise, return num multiplied by the factorial of num - 1 (recursive case).
Step 4:In the main() function:
a.Declare an integer variable number to store user input.
b.Display "Enter a number: " to prompt for user input.
c.Read and store the user's input in the number variable using scanf().
Step 5:Check if number is negative:
a.If true, display "Factorial is not defined for negative numbers."
b.If false, proceed to the next step.
Step 6:Calculate the factorial of number by calling the factorial() function and store the result in result.
Step 7:Return the result
Step 8:End.

FLOW CHART

-14-
Sri Manakula Vinayagar Engineering College Programing In C

PSEUDO CODE

1. Include the necessary header file stdio.h


2. Function factorial(num):
If num is equal to 0 or 1:
Return 1 // Base case: Factorial of 0 and 1 is 1
Else:
Return num multiplied by factorial(num - 1) // Recursive case
3. Function main():
Declare an integer variable number
Display "Enter a number: " to prompt for user input
Read and store the user's input in number using scanf()
4. If number is less than 0:
Display "Factorial is not defined for negative numbers."
Else:
Declare an integer variable result
5. Calculate result by calling factorial(number)
6. Display "Factorial of number is result."
7. End

SOURCE CODE

#include <stdio.h>
int factorial(int num) {
if (num == 0 || num == 1) {
return 1;
} else {
return num * factorial(num - 1);
}
}

int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);

if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
int result = factorial(number);
printf("Factorial of %d is %d\n", number, result);
}

return 0;
}

-15-
Sri Manakula Vinayagar Engineering College Programing In C

OUTPUT

RESULT

Hence the output of the factorial of the given number is verified successfully.

-16-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:7 WRITE A C PROGRAM TO CHECK WHETHER A GIVEN STRING


IS PALINDROME OR NOT?

AIM
To write a program to check whether the given string is palindrome or not.

ALGORITHM
Step 1: Start
Step 2: Include the necessary header files: stdio.h and string.h.
Step 3: Declare a character array str to store the input string.
Step 4: Prompt the user to enter a string and store it in str using scanf().
Step 5: Calculate the length of the string and store it in length.
Step 6: nitialize a variable isPalindrome to 1, assuming it's a palindrome initially.
Step 7: Use two pointers, i (starting from the beginning) and j (starting from the end), to compare Step
characters from both ends of the string.
Step 8: In a loop:
•If str[i] is not equal to str[j], set isPalindrome to 0 (not a palindrome) and break out of the loop.
Step 9: Check the value of isPalindrome:
•If it's 1, display that the input string is a palindrome.
•If it's 0, display that the input string is not a palindrome.
Step 10: End.

FLOW CHART

-17-
Sri Manakula Vinayagar Engineering College Programing In C

PSEUDO CODE

1. Start
2. Include the necessary header files: stdio.h and string.h
3. Declare a character array str to store the input string
4. Prompt the user to enter a string and store it in str using scanf()
5. Calculate the length of the string and store it in length
6. Initialize a variable isPalindrome to 1, assuming it's a palindrome initially
7. Use two pointers, i (starting from the beginning) and j (starting from the end), to compare characters
from both ends of the string
8. In a loop:
If str[i] is not equal to str[j], set isPalindrome to 0 (not a palindrome) and break out of the loop
9. Check the value of isPalindrome:
If it's 1, display that the input string is a palindrome
If it's 0, display that the input string is not a palindrome
10. End

SOURCE CODE

#include <stdio.h>
#include <string.h>

int main() {
char str[100];
int i, j;

printf("Enter a string: ");


scanf("%s", str);

int length = strlen(str);


int isPalindrome = 1;

for (i = 0, j = length - 1; i < j; i++, j--) {


if (str[i] != str[j]) {
isPalindrome = 0;
break;
}
}

if (isPalindrome) {
printf("%s is a palindrome.\n", str);
} else {
printf("%s is not a palindrome.\n", str);
}

return 0;

OUTPUT

-18-
Sri Manakula Vinayagar Engineering College Programing In C

RESULT

Hence the given string is checked whether it is palindrome or not and the output is verified
successfully.

-19-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:8 WRITE A C PROGRAM TO CHECK WHETHER A VALUE IS PRIME OR NOT?

AIM
To write a program to check whether a value is prime or not.

ALGORITHM
Step 1: Start
Step 1: Start
Step 2: Declare an integer number to store the input value.
Step 3: Declare an integer isPrime and set it to 1, assuming the number is prime initially.
Step 4: Prompt the user to enter a positive integer and store it in number using scanf.
Step 5: Check if number is less than or equal to 1:
•If true, set isPrime to 0 (not prime).
Step 6: If number is greater than 1, iterate through i from 2 to the square root of number:
•Check if number is divisible by i:
•If true, set isPrime to 0 and break out of the loop.
Step 7: Check the value of isPrime:
•If it's 1, display that the input number is prime.
•If it's 0, display that the input number is not prime.
Step 8: End.

FLOW CHART

-20-
Sri Manakula Vinayagar Engineering College Programing In C

PSEUDO CODE

1. Start
2. Declare an integer number to store the input value
3. Declare an integer isPrime and set it to 1, assuming the number is prime initially
4. Prompt the user to enter a positive integer and store it in number using scanf
5. Check if number is less than or equal to 1:
If true, set isPrime to 0 (not prime)
6. If number is greater than 1, iterate through i from 2 to the square root of number:
Check if number is divisible by i:
If true, set isPrime to 0 and break out of the loop
7. Check the value of isPrime:
If it's 1, display that the input number is prime
If it's 0, display that the input number is not prime
8. End

SOURCE CODE

#include <stdio.h>

int main() {
int number, i, isPrime = 1;

printf("Enter a positive integer: ");


scanf("%d", &number);
if (number <= 1) {
isPrime = 0;
} else {
for (i = 2; i * i <= number; i++) {
if (number % i == 0) {
isPrime = 0;
break;
}
}
}

if (isPrime) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}

return 0;
}

-21-
Sri Manakula Vinayagar Engineering College Programing In C

OUTPUT

RESULT

Hence the given number is checked whether it is prime or not and the output is verified successfully.

-22-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:9 DEVELOP A C PROGRAM TO SWAP TWO NUMBERS USING CALL BY VALUE AND
CALL BY REFERENCE.

AIM
To write a program to swap two numbers using call by value and call by reference.

ALGORITHM

Step 1: Start
Step 2: Declare two integer variables num1 and num2.
Step 3: Prompt the user to enter two numbers and store them in num1 and num2 using scanf.
Step 4: Print "Before swapping:" followed by the values of num1 and num2.
Step 5: Define a function swapByValue that takes two integer parameters num1 and num2:
•Create a temporary variable temp and set it to num1.
•Set num1 to num2.
•Set num2 to temp.
•Print "Inside swapByValue function:" followed by the swapped values of num1 and num2.
Step 6: Call swapByValue with num1 and num2 as arguments to swap the values using call by value.
Step 7: Print "After swapByValue call:" followed by the current values of num1 and num2.
Step 8: Define a function swapByReference that takes two pointers to integers num1 and num2:
•Create a temporary variable temp and set it to the value pointed to by num1.
•Set the value pointed to by num1 to the value pointed to by num2.
•Set the value pointed to by num2 to temp.
•Print "Inside swapByReference function:" followed by the swapped values pointed to by num1 and
num2.
Step 9: Call swapByReference with the addresses of num1 and num2 as arguments to swap the values
using call by reference.
Step 10: Print "After swapByReference call:" followed by the final values of num1 and num2.
Step 11: End.

FLOW CHART

-23-
Sri Manakula Vinayagar Engineering College Programing In C

PSEUDO CODE

1. Start
2. Declare two integer variables num1 and num2
3. Prompt the user to enter two numbers and store them in num1 and num2 using scanf
4. Print "Before swapping:" followed by the values of num1 and num2
5. Define a function swapByValue that takes two integer parameters num1 and num2:
Create a temporary variable temp and set it to num1
Set num1 to num2
Set num2 to temp
Print "Inside swapByValue function:" followed by the swapped values of num1 and num2
6. Call swapByValue with num1 and num2 as arguments to swap the values using call by value
7. Print "After swapByValue call:" followed by the current values of num1 and num2
8. Define a function swapByReference that takes two pointers to integers num1 and num2:
Create a temporary variable temp and set it to the value pointed to by num1
Set the value pointed to by num1 to the value pointed to by num2
Set the value pointed to by num2 to temp
Print "Inside swapByReference function:" followed by the swapped values pointed to by num1
and num2
9. Call swapByReference with the addresses of num1 and num2 as arguments to swap the values
using call by reference
10. Print "After swapByReference call:" followed by the final values of num1 and num2
11. End

SOURCE CODE

#include <stdio.h>

void swapByValue(int num1, int num2) {


int temp;
temp = num1;
num1 = num2;
num2 = temp;
printf("Inside swapByValue function:\n");
printf("Swapped values: num1 = %d, num2 = %d\n", num1, num2);
}

void swapByReference(int *num1, int *num2) {


int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
printf("Inside swapByReference function:\n");
printf("Swapped values: num1 = %d, num2 = %d\n", *num1, *num2);
}

int main() {
int num1, num2;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

printf("Before swapping:\n");
printf("num1 = %d, num2 = %d\n", num1, num2);

-24-
Sri Manakula Vinayagar Engineering College Programing In C

swapByValue(num1, num2);

printf("After swapByValue call:\n");


printf("num1 = %d, num2 = %d\n", num1, num2);

swapByReference(&num1, &num2);

printf("After swapByReference call:\n");


printf("num1 = %d, num2 = %d\n", num1, num2);

return 0;
}

OUTPUT

RESULT

Hence the output to swap two numbers using call by reference and call by value is verified
successfully.

-25-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:10 CONSTRUCT A C PROGRAM TO FIND THE SMALLEST AND LARGEST ELEMENT IN AN


ARRAY

AIM
To write a program to find the smallest and largest element in an array.

ALGORITHM

Step 1: Start
Step 1: Start
Step 2: Declare an integer variable n to store the size of the array.
Step 3: Prompt the user to enter the size of the array and store it in n using scanf.
Step 4: Check if n is less than or equal to 0:
•If true, print "Invalid array size" and exit the program with an error code.
Step 5: Declare an integer array arr of size n.
Step 6: Prompt the user to enter n elements of the array and store them in arr using a loop and scanf.
Step 7: Initialize integer variables smallest and largest with the first element of the array (arr[0]).
Step 8: Iterate through the array from the second element to the last:
•If the current element is smaller than smallest, update smallest with the current element.
•If the current element is larger than largest, update largest with the current element.
Step 9: Print "Smallest element:" followed by the value of smallest.
Step 10: Print "Largest element:" followed by the value of largest.
Step 11: End.

-26-
Sri Manakula Vinayagar Engineering College Programing In C

FLOW CHART

PSEUDO CODE

1. Start
2. Declare an integer variable n to store the size of the array
3. Prompt the user to enter the size of the array and store it in n using scanf
4. Check if n is less than or equal to 0:
If true, print "Invalid array size" and exit the program with an error code
5. Declare an integer array arr of size n
6. Prompt the user to enter n elements of the array:
for i from 0 to n-1:
Read an integer from the user and store it in arr[i] using scanf
7. Initialize integer variables smallest and largest with the first element of the array (arr[0])
8. Iterate through the array from the second element to the last:
for i from 1 to n-1:

-27-
Sri Manakula Vinayagar Engineering College Programing In C

If arr[i] is smaller than smallest, update smallest with arr[i]


If arr[i] is larger than largest, update largest with arr[i]
9. Print "Smallest element:" followed by the value of smallest
10. Print "Largest element:" followed by the value of largest
11. End

SOURCE CODE

#include <stdio.h>

int main() {
int n;

printf("Enter the size of the array: ");


scanf("%d", &n);

if (n <= 0) {
printf("Invalid array size.\n");
return 1;
}

int arr[n];

printf("Enter %d elements:\n", n);


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

int smallest = arr[0];


int largest = arr[0];

for (int i = 1; i < n; i++) {


if (arr[i] < smallest) {
smallest = arr[i];
}
if (arr[i] > largest) {
largest = arr[i];
}
}

printf("Smallest element: %d\n", smallest);


printf("Largest element: %d\n", largest);

return 0;
}

-28-
Sri Manakula Vinayagar Engineering College Programing In C

OUTPUT

RESULT

Hence the output to find the largest and smallest number in an array is verified and successfully.

-29-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:11 IMPLEMENT MATRIX MULTIPLICATION USING C PROGRAM

AIM
To write a program to implement matrix multiplication.
ALGORITHM

Step 1: Start
Step 2: Declare integer variables m, n, p, q, i, j, and k to store matrix dimensions and loop counters.
Step 3: Prompt the user to enter the number of rows and columns for the first matrix (m and n) and the
second matrix (p and q) using printf and scanf.
Step 4: Check if matrix multiplication is possible by verifying that the number of columns in the first
matrix (n) is equal to the number of rows in the second matrix (p):
•If not equal, print "Matrix multiplication is not possible. Column of the first matrix must be equal to
the row of the second matrix." and exit with an error code.
Step 5: Declare integer arrays firstMatrix[m][n], secondMatrix[p][q], and resultMatrix[m][q] to store
the matrices.
Step 6: Prompt the user to enter elements of the first matrix and the second matrix using nested loops
and printf and scanf.
Step 7: Initialize all elements of resultMatrix to 0 using nested loops.
Step 8: Perform matrix multiplication using nested loops:
•For each element in resultMatrix at position [i][j], calculate the sum of products of corresponding
elements from the first and second matrices using a third nested loop (k).
Step 9: Display the resultant matrix (resultMatrix) after multiplication using nested loops and printf.
Step 10: End.

-30-
Sri Manakula Vinayagar Engineering College Programing In C

FLOW CHART

PSEUDO CODE

1. Start
2. Declare integer variables m, n, p, q, i, j, k
3. Prompt the user to enter the number of rows and columns for the first matrix (m, n) using printf
and scanf
4. Prompt the user to enter the number of rows and columns for the second matrix (p, q) using printf
and scanf
5. Check if matrix multiplication is possible:
If n is not equal to p:
Print "Matrix multiplication is not possible. Column of the first matrix must be equal to the
row of the second matrix."

-31-
Sri Manakula Vinayagar Engineering College Programing In C

Exit the program with an error code


6. Declare integer arrays firstMatrix[m][n], secondMatrix[p][q], and resultMatrix[m][q]
7. Prompt the user to enter elements of the first matrix using nested loops and printf and scanf
8. Prompt the user to enter elements of the second matrix using nested loops and printf and scanf
9. Initialize all elements of resultMatrix to 0 using nested loops
10. Perform matrix multiplication using nested loops:
For each element in resultMatrix at position [i][j]:
Calculate the sum of products of corresponding elements from the first and second matrices
using a third nested loop (k)
11. Display the resultant matrix (resultMatrix) after multiplication using nested loops and printf
12. End.

SOURCE CODE

#include <stdio.h>

int main() {
int m, n, p, q, i, j, k;

printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &m, &n);

printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &p, &q);

if (n != p) {
printf("Matrix multiplication is not possible. Column of first matrix must be equal to the row of
the second matrix.\n");
return 1;
}

int firstMatrix[m][n], secondMatrix[p][q], resultMatrix[m][q];

printf("Enter elements of the first matrix:\n");


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

printf("Enter elements of the second matrix:\n");


for (i = 0; i < p; i++) {
for (j = 0; j < q; j++) {
scanf("%d", &secondMatrix[i][j]);
}
}

for (i = 0; i < m; i++) {


for (j = 0; j < q; j++) {
resultMatrix[i][j] = 0;
}
}

for (i = 0; i < m; i++) {

-32-
Sri Manakula Vinayagar Engineering College Programing In C

for (j = 0; j < q; j++) {


for (k = 0; k < n; k++) {
resultMatrix[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}

printf("Resultant matrix after multiplication:\n");


for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
printf("%d ", resultMatrix[i][j]);
}
printf("\n");
}

return 0;
}

OUTPUT

RESULT

Hence the output for the matrix multiplication is verified successfully.

-33-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:12 WRITE A C PROGRAM TO PERFORM VARIOUS STRING HANDLING FUNCTIONS LIKE


strlen, strcpy, strcat, strcmp

AIM
To write a program to perform string handling functions like strlen,strcpy,strcat,strcmp.

ALGORITHM

Step 1: Start
Step 2: Declare character arrays str1[100] and str2[100] to store input strings.
Step 3: Input the first string using printf and fgets.
Step 4: Input the second string using printf and fgets.
Step 5: Calculate and display the length of the first string using strlen and printf.
Step 6: Copy the contents of the second string into the first string using strcpy and display the result
using printf.
Step 7: Concatenate the contents of the second string to the first string using strcat and display the result
using printf.
Step 8: Compare the two strings using strcmp:
•If the result is 0, print "Both strings are equal."
•If the result is less than 0, print "The first string is lexicographically smaller than the second string."
•If the result is greater than 0, print "The first string is lexicographically larger than the second string."
Step 9: End.

FLOW CHART

PSEUDO CODE

1. Start
2. Declare character arrays str1[100] and str2[100] to store input strings.
3. Input the first string using printf and fgets.
4. Input the second string using printf and fgets.
5. Calculate and display the length of the first string using strlen and printf.
6. Copy the contents of the second string into the first string using strcpy and display the result
using printf.
7. Concatenate the contents of the second string to the first string using strcat and display the result
using printf.
8. Compare the two strings using strcmp:
- If the result is 0, print "Both strings are equal."
- If the result is less than 0, print "The first string is lexicographically smaller than the second
string."
- If the result is greater than 0, print "The first string is lexicographically larger than the second
string."

9. End.

-34-
Sri Manakula Vinayagar Engineering College Programing In C

SOURCE CODE

#include <stdio.h>
#include <string.h>

int main() {
char str1[100], str2[100];

printf("Enter the first string: ");


fgets(str1, sizeof(str1), stdin);

printf("Enter the second string: ");


fgets(str2, sizeof(str2), stdin);

printf("Length of the first string: %zu\n", strlen(str1));

strcpy(str1, str2);
printf("After copying, the first string is now: %s", str1);

strcat(str1, str2);
printf("After concatenating, the first string is now: %s", strcat(str1,str2));

int cmp = strcmp(str1, str2);


if (cmp == 0) {
printf("Both strings are equal.\n");
} else if (cmp < 0) {
printf("The first string is lexicographically smaller than the second string.\n");
} else {
printf("The first string is lexicographically larger than the second string.\n");
}

return 0;
}

OUTPUT

RESULT

Hence the output to perform various string handling functions like strlen, strcpy, strcat, strcmp
is verified successfully.

-35-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:13 DEVELOP A C PROGRAM TO REMOVE ALL CHARACTERS IN A STRING EXCEPT


ALPHABETS

AIM
To write a program to remove all characters in a srtring except alphabets.

ALGORITHM

Step 1: Start
Step 2: Declare character arrays inputString[100] and outputString[100] to store the input and output
strings.
Step 3: Input the string using printf and scanf.
Step 4: initialize two variables i and j to 0 for iteration and output string indexing.
Step 5: Iterate through the inputString character by character using a loop:
•Check if the character is an alphabet (uppercase or lowercase) using ASCII values.
•If it's an alphabet, add it to the outputString and increment j.
Step 6: Null-terminate the outputString at index j.
Step 7: Display the outputString containing only alphabets.
Step 8: End.

FLOW CHART

-36-
Sri Manakula Vinayagar Engineering College Programing In C

PSEUDO CODE

1. Start
2. Declare character arrays inputString[100] and outputString[100] to store the input and output strings.
3. Input the string using printf and scanf.
4. Initialize two variables i and j to 0 for iteration and output string indexing.
5. Iterate through the inputString character by character using a loop:
- Check if the character is an alphabet (uppercase or lowercase) using ASCII values.
- If it's an alphabet, add it to the outputString and increment j.
6. Null-terminate the outputString at index j.
7. Display the outputString containing only alphabets.
8. End.

SOURCE CODE

#include <stdio.h>

int main() {
char inputString[100], outputString[100];

printf("Enter a string: ");


scanf("%s", inputString);

int i, j = 0;

for (i = 0; inputString[i] != '\0'; i++) {


if ((inputString[i] >= 'A' && inputString[i] <= 'Z') || (inputString[i] >= 'a' && inputString[i] <=
'z')) {
outputString[j++] = inputString[i];
}
}

outputString[j] = '\0';

printf("String with only alphabets: %s\n", outputString);

return 0;
}

OUTPUT

RESULT

Hence the output to remove all characters in a string except alphabets is verified
successfully

-37-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:14 WRITE A C PROGRAM TO FIND THE SUM OF AN INTEGER ARRAY USING POINTERS

AIM
To write a program to find the sum of an integer array using pointer

ALGORITHM

Step 1: Start
Step 2: Declare integer variables n (for the array size) and sum (to store the sum of array elements).
Step 3: Prompt the user to enter the size of the array (n) and check if it's valid.
Step 4: Declare an integer array arr of size n to store the elements of the array.
Step 5: Prompt the user to enter n elements into the array arr.
Step 6: Declare an integer pointer ptr and initialize it with the base address of the array arr.
Step 7: Initialize sum to 0.
Step 8: Use a loop to iterate through the elements of the array:
•Add the value pointed to by ptr to sum.
•Move the pointer ptr to the next element.
Step 9: Display the sum as the sum of the array elements.
Step 10: End.

FLOW CHART

-38-
Sri Manakula Vinayagar Engineering College Programing In C

PSEUDO CODE

1. Start
2. Declare integer variables n (for the array size) and sum (to store the sum of array elements).
3. Prompt the user to enter the size of the array (n) and check if it's valid.
4. Declare an integer array arr of size n to store the elements of the array.
5. Prompt the user to enter n elements into the array arr.
6. Declare an integer pointer ptr and initialize it with the base address of the array arr.
7. Initialize sum to 0.
8. Use a loop to iterate through the elements of the array:
- Add the value pointed to by ptr to sum.
- Move the pointer ptr to the next element.
9. Display sum as the sum of the array elements.
10. End.

SOURCE CODE

#include <stdio.h>

int main() {
int n, sum = 0;

printf("Enter the size of the array: ");


scanf("%d", &n);

if (n <= 0) {
printf("Invalid array size.\n");
return 1;
}

int arr[n];

printf("Enter %d elements:\n", n);


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

int *ptr = arr;

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


sum += *ptr;
ptr++;
}

printf("Sum of the array elements: %d\n", sum);

return 0;
}

-39-
Sri Manakula Vinayagar Engineering College Programing In C

OUTPUT

RESULT

Hence the output to find the sum of an integer array using pointers is verified
successfully.

-40-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:15 WRITE A PROGRAM TO FIND THE MAXIMUM ELEMENT IN AN INTEGER ARRAY


USING POINTERS

AIM
To write a program to find the maximum element in an integer array using pointers.

ALGORITHM

Step 1: Start
Step 2: nitialize an integer variable n to store the size of the array.
Step 3: Prompt the user to enter the size of the array and read the value into n.
Step 4: Check if n is less than or equal to 0, indicating an invalid array size. If so, print an error message
and exit the program with an error code.
Step 5: Declare an integer array arr of size n to store the elements.
Step 6: Prompt the user to enter n elements and read them into the arr array using a for loop.
Step 7: Declare an integer pointer ptr and initialize it with the base address of the arr array.
Step 8: Initialize an integer variable max with the value pointed to by ptr, which is the first element of
the array.
Step 9: Use a for loop to iterate through the array elements starting from the second element.
•Inside the loop, compare the value pointed to by ptr with the current max value. If the value
pointed to by ptr is greater than max, update the max variable with the new maximum value.
Step 10: After the loop, print the value of max, which represents the maximum element in the array.
Step 11: Stop
FLOW CHART

-41-
Sri Manakula Vinayagar Engineering College Programing In C

PSEUDO CODE

1. Start
2. Initialize an integer variable n
3. Prompt the user to enter the size of the array and read n
4. If n is less than or equal to 0, print "Invalid array size" and exit with an error code
5. Declare an integer array arr of size n
6. Prompt the user to enter n elements and read them into arr
7. Declare an integer pointer ptr and initialize it with the address of the first element of arr
8. Initialize an integer variable max with the value pointed to by ptr
9. Iterate from 1 to n-1 using a loop index i
a. If the value pointed to by ptr is greater than max, update max with the new value
b. Move ptr to the next element in the array
10. Print "Maximum element in the array: max"
11. Stop

SOURCE CODE

#include <stdio.h>

int main() {
int n;

printf("Enter the size of the array: ");


scanf("%d", &n);

if (n <= 0) {
printf("Invalid array size.\n");
return 1;
}

int arr[n];

printf("Enter %d elements:\n", n);


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

int *ptr = arr;


int max = *ptr;

for (int i = 1; i < n; i++) {


if (*ptr > max) {
max = *ptr;
}
ptr++;
}

printf("Maximum element in the array: %d\n", max);

return 0;
}

-42-
Sri Manakula Vinayagar Engineering College Programing In C

OUTPUT

RESULT

Hence the output to find the Maximum element in an integer array using pointers
is verified successfully.

-43-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:16 CONSTRUCT A C PROGRAM TO DISPLAY EMPLOYEE DETAILS USING STRUCTURES

AIM
To Construct a C program to display Employee details using Structures

ALGORITHM

Step 1: Start
Step 2: Define a structure Employee to store employee details such as name, employee ID, salary, etc.
Step 3: Create an array of Employee structures to store multiple employee records.
Step 4: Initialize the employee records with data.
Step 5: Create a loop to iterate through the array of employee structures.
a. Display the details of each employee, including name, employee ID, and salary.
Step 5: End

FLOW CHART

PSEUDO CODE
1. Define a structure Employee
Fields:
name (string)
empID (integer)
salary (float)
2. Create an array of Employee structures: employees[MAX_EMPLOYEES]
3. Initialize the employee records with data
For each i in range from 0 to MAX_EMPLOYEES - 1
Read employee name from the user
Read employee ID from the user
Read employee salary from the user
End For
4. Display "Employee Details:"
5.For each i in range from 0 to MAX_EMPLOYEES - 1
Display "Employee Name: " + employees[i].name
Display "Employee ID: " + employees[i].empID
Display "Employee Salary: " + employees[i].salary
6. End For

-44-
Sri Manakula Vinayagar Engineering College Programing In C

SOURCE CODE

#include <stdio.h>
struct Employee {
char name[50];
int empID;
float salary;
};

int main()
{

const int MAX_EMPLOYEES = 5;


struct Employee employees[MAX_EMPLOYEES];
printf("Enter Employee Details:\n");
for (int i = 0; i < MAX_EMPLOYEES; i++) {
printf("Employee %d:\n", i + 1);
printf("Name: ");
scanf("%s", employees[i].name);
printf("Employee ID: ");
scanf("%d", &employees[i].empID);
printf("Salary: ");
scanf("%f", &employees[i].salary);
}

printf("\nEmployee Details:\n");
for (int i = 0; i < MAX_EMPLOYEES; i++) {
printf("Employee %d:\n", i + 1);
printf("Name: %s\n", employees[i].name);
printf("Employee ID: %d\n", employees[i].empID);
printf("Salary: %.2f\n", employees[i].salary);
}

return 0;
}

-45-
Sri Manakula Vinayagar Engineering College Programing In C

OUTPUT

RESULT

Hence output of a C program to display Employee details using Structures


is verified successfully

-46-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:17 WRITE A C PROGRAM TO DISPLAY THE CONTENTS OF A FILE ON MONITOR SCREEN

AIM
To write a c program to display the contents of a file on monitor screen

ALGORITHM

Step 1: Start
Step 2: Declare a file pointer variable.
Step 3: Open the file for reading using the fopen function. Check if the file was opened
Step 4: Create a loop to read each line from the file using the fgets function until the end of the file is
reached.
Step 5Inside the loop, display the read line using printf
Step 6: Close the file using the fclose function.
Step 7: End

FLOW CHART

PSEUDO CODE

1. Start
2. Declare a file pointer variable: FILE *file_ptr
3. Open the file for reading:
a.file_ptr = fopen("filename.txt", "r")
b. If file_ptr is NULL, display "Error opening the file." and exit.
4. Repeat until end of file (EOF) is reached:
a. Read a line from the file using fgets
b. If the read line is not NULL, display the line using printf
5. Close the file:
a. fclose(file_ptr)
6.End

-47-
Sri Manakula Vinayagar Engineering College Programing In C

SOURCE CODE

#include <stdio.h>

int main() {
FILE *file_ptr;
char line[100];

// Open the file for reading


file_ptr = fopen("filename.txt", "r");

// Check if the file was opened successfully


if (file_ptr == NULL) {
printf("Error opening the file.\n");
return 1; // Exit with an error code
}

// Read and display each line from the file


while (fgets(line, sizeof(line), file_ptr) != NULL) {
printf("%s", line);
}

// Close the file


fclose(file_ptr);

return 0; // Exit successfully


}

OUTPUT

RESULT

Hence output of a c program to display the contents of a file on monitor screen is verified successfully

-48-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:18 WRITE A FILE BY GETTING THE INPUT FROM THE KEYBOARD AND RETRIEVE THE
CONTENTS OF THE FILE USING FILE OPERATION COMMANDS

AIM

To write a c program a file by getting the input from the keyboard and retrieve the contents of the
file using file operation commands

ALGORITHM

Step 1: Start

Step 2: Declare a file pointer variable.

Step 3: Open the file for writing using the fopen function with write mode ("w"). Check if the file was
opened successfully; if not, display an error message and exit.
Step 4: Create a loop to get input from the user until they decide to stop (e.g., by typing "exit"). a.
Inside the loop, read a line of text from the keyboard using the fgets function. b. Write the input line to
the file using the fputs function.

Step 5: Close the file using the fclose function.

Step 6: Reopen the file for reading using the fopen function with read mode ("r"). Check if the file
was opened successfully; if not, display an error message and exit.

Step 7: Create a loop to read each line from the file using the fgets function until the end of the file is
reached. a. Inside the loop, display the read line using printf.

Step 8: Close the file using the fclose function.


Step 9: End

PSEUDO CODE
1. Start the program.

2. Create a file pointer variable.

3. Open a file in write mode (for writing text) using fopen().

4. Check if the file was opened successfully.

- If not, display an error message and exit.

5. If the file was opened successfully, prompt the user to input text from the keyboard.

6. Write the user's input to the file using fprintf().


7. Close the file using fclose().

-49-
Sri Manakula Vinayagar Engineering College Programing In C

8. Open the same file in read mode (for reading text) using fopen().

9. Check if the file was opened successfully.

- If not, display an error message and exit.

10. If the file was opened successfully, read the content of the file character by character and display it
on the screen.

11. Close the file using fclose().

12. End the program.

SOURCE CODE
#include <stdio.h>
int main() {
FILE *file;
char filename[] = "my_file.txt";
char ch;
// Open the file for writing
file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening the file.\n");
return 1;
}
// Input text from the user and write to the file
printf("Enter text (Ctrl+D to end input):\n");
while ((ch = getchar()) != EOF) {
fputc(ch, file);
}
// Close the file
fclose(file);

// Open the file for reading


file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening the file for reading.\n");
return 1;
}

-50-
Sri Manakula Vinayagar Engineering College Programing In C

// Read and display the content of the file


printf("\nContents of the file:\n");
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}

// Close the file


fclose(file);

return 0;
}

OUTPUT

RESULT
Hence a c program for a file by getting the input from the keyboard and retrieve the contents of
the file using file operation commands verified successfully

-51-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:19 WRITE A C PROGRAM TO CREATE TWO FILES WITH A SET OF VALUES. MERGE
THE TWO FILE CONTENTS TO FORM A SINGLE FILE

AIM

Write a C program to create two files with a set of values. Merge the two file contents to form
a single file

ALGORITHM

Step 1: Start the program.


Step 2: Create two file pointer variables, one for each input file and one for the output file.
Step 3: Open the first input file in read mode.
Step 4: Check if the first input file was opened successfully.
a.If not, display an error message and exit.
b.Open the second input file in read mode.
Step 5: Check if the second input file was opened successfully.
Step 6: If not, display an error message and exit.
a.Open the output file in write mode (to create a new merged file)
Step 7: Check if the output file was opened successfully.
a.If not, display an error message and exit.
Step 8: Read the contents of the first input file character by character and write to the output file.
Step 9: Read the contents of the second input file character by character and append to the output
file.
Step 10: Close all three files.
Step 11: End the program

PSEUDO CODE
1. Start the program.
2. Create two file pointer variables: inputFile1, inputFile2, and outputFile.
3. Open inputFile1 in read mode.
4. Check if inputFile1 was opened successfully.
- If not, display an error message and exit.
5. Open inputFile2 in read mode.
6. Check if inputFile2 was opened successfully.
- If not, display an error message and exit.
7. Open outputFile in write mode.
8. Check if outputFile was opened successfully.
- If not, display an error message and exit.
9. Read the contents of inputFile1 character by character and write to outputFile.
10. Read the contents of inputFile2 character by character and append to outputFile.
11. Close inputFile1, inputFile2, and outputFile.
12. End the program.

-52-
Sri Manakula Vinayagar Engineering College Programing In C

SOURCE CODE
#include <stdio.h>

int main() {

FILE *inputFile1, *inputFile2, *outputFile;

char ch;

// Open the first input file for reading

inputFile1 = fopen("file1.txt", "r");

if (inputFile1 == NULL) {

printf("Error opening file1.txt\n");

return 1;

// Open the second input file for reading

inputFile2 = fopen("file2.txt", "r");

if (inputFile2 == NULL) {

printf("Error opening file2.txt\n");

return 1;

// Open the output file for writing

outputFile = fopen("merged_file.txt", "w");

if (outputFile == NULL) {
printf("Error creating merged_file.txt\n");

return 1;

// Merge the contents of the first input file into the output file

while ((ch = fgetc(inputFile1)) != EOF) {

fputc(ch, outputFile);

// Merge the contents of the second input file into the output file

while ((ch = fgetc(inputFile2)) != EOF) {

fputc(ch, outputFile);

-53-
Sri Manakula Vinayagar Engineering College Programing In C

// Close all three files

fclose(inputFile1);

fclose(inputFile2);

fclose(outputFile);

printf("Files merged successfully.\n");

return 0;

OUTPUT

RESULT
Hence a C program to create two files with a set of values. Merge the two file contents to
form a single file is verified successfully

-54-
Sri Manakula Vinayagar Engineering College Programing In C

Ex no:19 WRITE A C PROGRAM TO PASS THE PARAMETER USING COMMAND LINE


ARGUMENTS

AIM

To Write a C program to pass the parameter using command line arguments

ALGORITHM

Step 1:Start the program.


Step 2:Declare variables to store command line arguments.
Step 3:Check if the number of command line arguments is at least 2 (including the program
name).
a.If not, display an error message and exit.
Step 4:Iterate through the command line arguments starting from index 1 (index 0 is the program
name
Step 5:Print each command line argument.
Step 6:End the program.

PSEUDOCODE
1. Start the program.
2. Declare variables to store command line arguments.
3. Check if the number of command line arguments is less than 2 (including the program name).
- If true, display an error message and exit.
4. Iterate through command line arguments starting from index 1 (index 0 is the program name).
- Print each command line argument.
5. End the program.

-55-
Sri Manakula Vinayagar Engineering College Programing In C

SOURCE CODE

#include <stdio.h>

int main(int argc, char *argv[]) {


// Check if the number of command line arguments is less than 2
if (argc < 2) {
printf("Usage: %s <arg1> <arg2> ... <argN>\n", argv[0]);
return 1;
}

// Iterate through and print command line arguments


printf("Command line arguments:\n");
for (int i = 1; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}

return 0;
}

OUTPUT

RESULT:

Hence a C program to pass the parameter using command line arguments is verified
sucessfully

-56-

You might also like