Cycle 2 Ge23121 - C Programming Lab Manual Ai & DS
Cycle 2 Ge23121 - C Programming Lab Manual Ai & DS
NO:4A 1D ARRAY
DATE:
AIM:
To write a C Program to find Sum and Average of Elements in a 1D Array.
ALGORITHM:
1.Start the program.
2.Get the input for number of elements in the array and store it in a variable n.
3.Create an array with n elements to store the user's input.
4.Initialize a variable sum to 0, which will store the total sum of the array elements.
5.Use a loop to ask the user to input each element of the array:
i. For each iteration of the loop (from 1 to n):
ii. Prompt the user to enter the value of the current element.
iii. Store the value entered by the user in the array at the current position.
iv. Add the value of the current element to the sum (sum = sum + current element).
6.Calculate the average by dividing the total sum by the number of elements (average =
sum/ n).
7.Display the sum of all the elements.
8.Display the average of the elements, formatted to two decimal places.
9.End the program.
PROGRAM:
#include<stdio.h>
int main()
{
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
int sum = 0;
float average;
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
sum += arr[i];
}
average = (float)sum / n;
printf("Sum of elements: %d\n", sum);
printf("Average of elements: %.2f\n", average);
return 0;
}
OUTPUT:
Enter the number of elements in the array: 5
Enter 5 elements:
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
Element 5: 5
Sum of elements: 15
Average of elements: 3.00
RESULT:
Thus the C Program to find Sum and Average of Elements in a 1D Array is written and executed successfully.
EX.NO:4B MAXIMUM AND MINIMUM NUMBER-1D ARRAY
DATE:
AIM:
To write a C program to input n integers into a 1D array and find the maximum and minimum elements.
ALGORITHM:
1. Start the program.
2. Ask the user to input the number of elements n they want to enter into the array.
3. Create an array of size n to store the elements.
4. Prompt the user to input n elements:
5. Initialize two variables: 1. Set max to the first element of the array. 2. Set min to the first element of the array.
6. Loop through the array starting from the second element to find the maximum and minimum values:
For each element:
■ If the element is greater than max, update max with this element.
■ If the element is smaller than min, update min with this element.
7. Display the max (maximum element) and min (minimum element) after the loop finishes.
8. End the program.
PROGRAM:
#include<stdio.h>
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
int max = arr[0];
int min = arr[0];
for (int i = 1; i < n; i++)
{ if (arr[i] > max)
{
max = arr[i];
} if (arr[i] < min)
{ min = arr[i];
}}
printf("Maximum element: %d\n", max);
printf("Minimum element: %d\n", min);
return 0;
}
OUTPUT:
Enter the number of elements: 5
Enter 5 elements:
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
Element 5: 5
Maximum element: 5
Minimum element: 1
RESULT:
Thus the C program to input n integers into a 1D array and find the maximum and minimum elements is written
and executed.
EX.NO:4C LINEAR SEARCH-1D ARRAY
DATE:
AIM:
To write a C program to Linear Search for a specific number in a 1D array and return its position (or -1 if not
found).
ALGORITHM:
1.Start the program.
2.Ask the user to input the number of elements in the array and store this value in a variable n.
3.Create an array of size n to store the elements.
4.Ask the user to input the n elements and store them in the array.
5.Ask the user to input the number they want to search for (target).
6.Initialize a loop to search for the target number in the array:
● For each element in the array: Compare the current element with the target number. If the element matches
the target, return the position of that element (in terms of array index).
7.If the target number is found, print the position of the target (1-based index).
8.If the target number is not found, print a message saying the number is not in the array.
9.End the program.
PROGRAM:
#include<stdio.h>
int linearSearch(int arr[], int n, int target)
{
for (int i = 0; i < n; i++)
{
if (arr[i] == target)
{
return i;
}}
return -1;
}
int main()
{
int n, target;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("Enter the number to search for: ");
scanf("%d", &target);
int result = linearSearch(arr, n, target);
if (result != -1)
{
printf("The number %d is found at position %d.\n", target, result + 1);
}
else
{
printf("The number %d is not found in the array.\n", target);
}
return 0;
}
OUTPUT:
Enter the number of elements in the array: 5
Enter 5 elements:
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
Element 5: 5
Enter the number to search for: 4
The number 4 is found at position 4.
RESULT:
Thus the C program to Linear Search for a specific number in a 1D array and return its position (or -1 if not
found) is written and executed successfully.
EX.NO:4D 2D ARRAY MATRICES
DATE:
AIM:
To write a C Program to add two matrices of size m×n (2D arrays).
ALGORITHM:
1. Start the program.
2. Ask the user to input the number of rows (m) and columns (n) for the matrices.
3. Create two 2D arrays A[m][n] and B[m][n] to store the elements of the two matrices.
4. Create a 2D array C[m][n] to store the result of the matrix addition.
5. Input the elements for matrix A:
6. Input the elements for matrix B:
7. Perform the matrix addition:
8. Display the resulting matrix C:
9. End the program.
PROGRAM:
#include<stdio.h>
int main()
{
int m, n;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int A[m][n], B[m][n], C[m][n];
printf("Enter elements of matrix A:\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("A[%d][%d]: ", i + 1, j + 1);
scanf("%d", &A[i][j]);
}}
printf("Enter elements of matrix B:\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("B[%d][%d]: ", i + 1, j + 1);
scanf("%d", &B[i][j]);
}}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
C[i][j] = A[i][j] + B[i][j];
}}
printf("Resultant matrix (C = A + B):\n");
for (int i = 0; i < m; i++)
{ for (int j = 0; j < n; j++)
{
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the number of rows (m): 3
Enter the number of columns (n): 3
Enter elements of matrix A:
A[1][1]: 1
A[1][2]: 2
A[1][3]: 3
A[2][1]: 4
A[2][2]: 5
A[2][3]: 6
A[3][1]: 7
A[3][2]: 8
A[3][3]: 9
Enter elements of matrix B:
B[1][1]: 9
B[1][2]: 8
B[1][3]: 7
B[2][1]: 6
B[2][2]: 5
B[2][3]: 4
B[3][1]: 3
B[3][2]: 2
B[3][3]: 1
Resultant matrix (C = A + B):
10 10 10
10 10 10
10 10 10
RESULT:
Thus the C Program to add two matrices of size m×n (2D arrays) is written and executed successfully.
EX.NO:4E TRANSPOSE OF THE MATRIX
DATE:
AIM:
To write a C program to commute the transpose of the matrix.
ALGORITHM:
1.Start the program.
2.Ask for the matrix dimensions (m for rows and n for columns).
3.Create two 2D arrays: one for the original matrix A and another for its transpose B.
4.Input the elements of matrix A from the user.
5.Compute the transpose: The transpose of a matrix A[m][n] becomes B[n][m] by swapping its rows and
columns.
6.Display the original matrix A.
7.Display the transposed matrix B.
8.End the program.
PROGRAM:
#include<stdio.h>
int main()
{
int m, n;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int A[m][n], B[n][m];
printf("Enter elements of matrix A:\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++) {
printf("A[%d][%d]: ", i + 1, j + 1);
scanf("%d", &A[i][j]);
}}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
B[j][i] = A[i][j];
}}
printf("\nOriginal matrix A:\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d ", A[i][j]);
}
printf("\n");
}
printf("\nTranspose of matrix A (B):\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("%d ", B[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the number of rows (m): 3
Enter the number of columns (n): 3
Enter elements of matrix A:
A[1][1]: 1
A[1][2]: 2
A[1][3]: 3
A[2][1]: 4
A[2][2]: 5
A[2][3]: 6
A[3][1]: 7
A[3][2]: 8
A[3][3]: 9
Original matrix A:
123
456
789
Transpose of matrix A (B):
147
258
369
RESULT:
Thus the C Program to compute the transpose of a matrix is written and executed successfully
EX.NO:5A PALINDROME
DATE:
AIM:
To write a C Program to check if a given string is a palindrome (ignoring case).
ALGORITHM:
3.Remove unwanted characters (like spaces, punctuation, etc.) and convert all characters to lowercase.
4.Compare characters: Start with the first and last characters of the string.Move towards the middle, comparing
characters.If any characters don't match, the string is not a palindrome.
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
if (isPalindrome(str)) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}
OUTPUT:
Enter a string: malayalam
The string is a palindrome.
Enter a string: tamilnadu
The string is not a palindrome.
RESULT:
Thus the C Program to check if a given string is a palindrome (ignoring case) is written and executed
successfully.
EX.NO:5B LENGTH OF A STRING
DATE:
AIM:
To write a C Program to find the length of a string without using the strlen() function.
ALGORITHM:
PROGRAM:
#include <stdio.h>
int findLength(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
int length = findLength(str);
printf("The length of the string is: %d\n", length);
return 0;
}
OUTPUT:
Enter a string: Tamilnadu
The length of the string is: 9
RESULT:
Thus the C Program to find the length of a string without using the strlen() functionis written and executed
successfully.
EX.NO:5C CONCATENATION OF A STRING
DATE:
AIM:
To write a C Program to concatenate two strings without using the strcat() function.
ALGORITHM:
PROGRAM:
#include <stdio.h>
result[i] = '\0';
}
int main() {
char str1[100], str2[100], result[200];
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
return 0;
}
OUTPUT:
Enter the first string: Tamil
Enter the second string: Nadu
The concatenated string is: TamilNadu
RESULT:
Thus the C Program to concatenate two strings without using the strcat() function is written and executed
successfully.
EX.NO:5D COMPARING TWO STRINGS
DATE:
AIM:
To write a C Program to compare two strings and determine if they are equal without using the strcmp() function.
ALGORITHM:
PROGRAM:
#include <stdio.h>
int compareStrings(char str1[], char str2[]) {
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
return 0;
}
i++;
}
if (str1[i] == '\0' && str2[i] == '\0') {
return 1;
} else {
return 0;
}
}
int main() {
char str1[100], str2[100];
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
return 0;
}
OUTPUT 1:
Enter the first string: Programming
Enter the second string: Python
The strings are not equal.
OUTPUT 2:
Enter the first string: Programming
Enter the second string: Programming
The strings are equal.
RESULT:
Thus the C Program to compare two strings and determine if they are equal without using the strcmp() function is
written and executed successfully.
EX.NO:5E SUBSTRING IN STRING
DATE:
AIM:
To write a C Program to check if a substring exists within a string and return its starting position.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <string.h>
int main() {
char str[100], substring[100];
printf("Enter the main string: ");
fgets(str, sizeof(str), stdin);
if (position != -1) {
printf("Substring found at position: %d\n", position);
} else {
printf("Substring not found.\n");
}
return 0;
}
OUTPUT:
Enter the main string: Programming
Enter the substring: gram
Substring found at position: 3
RESULT:
Thus the C Program to check if a substring exists within a string and return its starting position is written and
executed successfully.
EX.NO:5F COUNT NUMBER OF WORDS IN STRING
DATE:
AIM:
To write a C Program to count the number of words in a string. Words are separated by spaces.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <ctype.h>
int countWords(char str[]) {
int count = 0;
int inWord = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (!isspace(str[i]) &&inWord == 0) {
count++;
inWord = 1;
}
if (isspace(str[i])) {
inWord = 0;
}
}
return count;
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
int wordCount = countWords(str);
printf("The number of words in the string is: %d\n", wordCount);
return 0;
}
OUTPUT:
Enter a string: Welcome To C Programming
The number of words in the string is: 4
RESULT:
Thus the C Program to count the number of words in a string. Words are separated by spaces is written and
executed successfully.
EX.NO:5G FREQUENCY OF A STRING
DATE:
AIM:
To write a C Program to count the frequency of a specific character in a string.
ALGORITHM:
PROGRAM:
#include <stdio.h>
return count;
}
int main() {
char str[100], ch;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter the character to count: ");
scanf("%c", &ch);
int frequency = countCharFrequency(str, ch);
printf("The character '%c' appears %d times in the string.\n", ch, frequency);
return 0;
}
OUTPUT:
Enter a string: Programming
Enter the character to count: m
The character 'm' appears 2 times in the string.
RESULT:
Thus the C Program to count the frequency of a specific character in a string is written and executed
successfully.
EX.NO:6A FUNCTIONS
DATE:
AIM:
To write a C Program to create a function that takes two numbers as arguments, compares them, and returns the
larger number.
ALGORITHM:
1. Start the program.
2. Get Input as Two Numbers:
3. Compare the Two Numbers:
4. Compare the first number (num1) with the second number (num2).
a. If num1 is greater than num2, then the larger number is num1.
b. Otherwise, the larger number is num2.
5. Return the Larger Number:
6. Output the Result
7. Stop the program
PROGRAM:
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter the first number: ");
scanf("%d", &number1);
return 0;
}
OUTPUT:
Enter the first number: 25
Enter the second number: 56
The larger number is: 56
RESULT:
Thus the C Program to create a function that takes two numbers as arguments, compares them, and returns the
larger number is written and executed successfully.
EX.NO:6B FACTORIAL OF A NUMBER
DATE:
AIM:
To write a C Program to calculate the factorial of a number using recursion.
ALGORITHM:
\PROGRAM:
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num< 0) {
printf("Factorial is not defined for negative numbers.\n");
} else
{
int result = factorial(num);
printf("The factorial of %d is: %d\n", num, result);
}
return 0;
}
OUTPUT:
Enter a number: 5
The factorial of 5 is: 120
RESULT:
Thus the C Program to calculate the factorial of a number using recursion is written and executed successfully.
EX.NO:6C FUNCTION WITH ARRAY
DATE:
AIM:
To write a C Program to create a function that takes an array and its size as arguments and returns the sum of its
elements.
ALGORITHM:
1. Start
2. Get the input for number of elements in the array
3. Initialize the sum=0
4. Loop through the array and add each element to the sum.
5. Output the sum:Print the total sum of the array elements.
6. End the program.
PROGRAM:
#include <stdio.h>
int sumArray(int arr[], int size) {
int sum = 0;
for (int i = 0; i< size; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int size;
printf("Enter the number of elements in the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements:\n", size);
for (int i = 0; i< size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
int result = sumArray(arr, size);
return 0;
}
OUTPUT:
Enter the number of elements in the array: 5
Enter 5 elements:
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50
The sum of the array elements is: 150
RESULT:
Thus the C Program to create a function that takes an array and its size as arguments and returns the sum of its
elements is written and executed successfully.
EX.NO:6D GCD
DATE:
AIM:
To write a C program to calculate the greatest common divisor (GCD) of two numbers using recursion
ALGORITHM:
1. Start
2. Input two numbers from the user.
3. Calculate the GCD using the Euclidean algorithm:
4. If the second number is 0, the GCD is the first number.
5. Otherwise, recursively calculate the GCD of the second number and the remainder when the first number
is divided by the second.
6. Display the result to the user.
7. End the program.
PROGRAM:
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("The GCD of %d and %d is: %d\n", num1, num2, gcd(num1, num2));
return 0;
}
OUTPUT:
Enter two numbers: 56
98
The GCD of 56 and 98 is: 14
RESULT:
Thus the C program to calculate the greatest common divisor (GCD) of two numbers using recursion is written
and executed successfully.
EX.NO:6E CALL BY VALUE & CALL BY REFERENCE
DATE:
AIM:
To write a C Program to swap two values using Call by Value and Call by Reference.
ALGORITHM:
1. Start
2. Input: Read two integers x and y from the user.
3. Swap Using Call by Value:
4. Swap Using Call by Reference:
5. Display the final values of x and y after the swap by reference.
6. End
PROGRAM:
#include <stdio.h>
void swapByValue(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
int main() {
int x, y;
return 0;
}
OUTPUT:
Enter two integers: 5
10
Before swapping (Call by Value):
x = 5, y = 10
After swapping (Call by Value):
a = 10, b = 5
Before swapping (Call by Reference):
x = 5, y = 10
After swapping (Call by Reference):
a = 10, b = 5
Final values (Call by Reference):
x = 10, y = 5
RESULT:
Thus the C Program to swap two values using Call by Value and Call by Reference is written and executed
successfully.