CONTENT
SL. NO. TOPIC PAGE NO.
1. Write a c program to find out the biggest and lowest elements of an array. 1
2. Write a c program to arrange the numbers in an ordered form. 2
3. Write a c program to print the fibonacci series using arrays. 3
4. Write a c program to count the number of positive and negative numbers in 4
an array.
5. Write a c program to print the elements in reverse order in an array. 5
6. Write a c program to add two matrices and display the resultant matrix. 6
7. Write a c program to multiply two matrices and display the resultant matrix 7
8. Write a c program to compute the LCM and GCD of the two user given integer 8.
number using function.
9. Calculate the factorial of a given integer number using function. 9
10. Write a c program to print the fibonacci series using function. 10
CONTENT
11. Write a program in c to display the contents of the variables and their 11
addresses using pointer.
12. Write a program in c to multiply two numbers by using pointer. 12
13. Write a program in c to swap two numbers using pointer. 13
14. Write a program in c to find out the length of a user given string. 14
15. Write a program in c to convert a user given lower case string to upper case 15
string.
16. Write a program in c to reverse a given string. 16
17. Write a program in c to check whether a given string is palindrome or not. 17
18. Write a program in c to execute i. strlen(), ii. strcpy(), iii. strcmp(), iv. strcut(). 18
6. Write a c program to add two matrices and display the resultant matrix
INPUT-
#include <stdio.h>
int main() {
int rows, cols, i, j;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int mat1[rows][cols], mat2[rows][cols], result[rows][cols];
printf("Enter elements of first matrix:\n");
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
scanf("%d", &mat1[i][j]);
printf("Enter elements of second matrix:\n");
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
scanf("%d", &mat2[i][j]);
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
result[i][j] = mat1[i][j] + mat2[i][j];
printf("Resultant matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
printf("%d ", result[i][j]);
printf("\n");
}
return 0;
}
OUTPUT-
Enter the number of rows and columns: 2 2
Enter elements of first matrix:
22
13
Enter elements of second matrix:
11
23
Resultant matrix:
33
36
7. Write a c program to multiply two matrices and display the resultant matrix.
INPUT-
#include <stdio.h>
int main() {
int r1, c1, r2, c2, i, j, k;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
if (c1 != r2) {
printf("Matrix multiplication not possible.\n");
return 0;
}
int mat1[r1][c1], mat2[r2][c2], result[r1][c2];
printf("Enter first matrix:\n");
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
scanf("%d", &mat1[i][j]);
printf("Enter second matrix:\n");
for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
scanf("%d", &mat2[i][j]);
for (i = 0; i < r1; i++)
for (j = 0; j < c2; j++) {
result[i][j] = 0;
for (k = 0; k < c1; k++)
result[i][j] += mat1[i][k] * mat2[k][j];
}
printf("Resultant matrix:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++)
printf("%d ", result[i][j]);
printf("\n");
}
return 0;
}
OUTPUT-
Enter rows and columns of first matrix: 2 2
Enter rows and columns of second matrix: 2 2
Enter first matrix:
12
21
Enter second matrix:
23
32
Resultant matrix:
87
78
8. Write a c program to compute the LCM and GCD of the two user given integer number
using function.
INPUT-
#include <stdio.h>
// Function to calculate GCD
int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
// Function to calculate LCM
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("GCD: %d\n", gcd(num1, num2));
printf("LCM: %d\n", lcm(num1, num2));
return 0;
}
OUTPUT-
Enter two numbers: 6 32
GCD: 2
LCM: 96
9. Write a program in c to calculate the factorial of a given integer number using function.
INPUT-
#include <stdio.h>
// Function to calculate factorial
int factorial(int n) {
return (n == 0 || n == 1) ? 1 : n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial: %d\n", factorial(num));
return 0;
}
OUTPUT-
Enter a number: 6
Factorial: 720
10. Write a program in c to print the fibonacci series using function.
INPUT-
#include <stdio.h>
// Function to print Fibonacci series
void fibonacci(int n) {
int a = 0, b = 1, next;
for (int i = 0; i < n; i++) {
printf("%d ", a);
next = a + b;
a = b;
b = next;
}
}
int main() {
int num;
printf("Enter the number of terms: ");
scanf("%d", &num);
printf("Fibonacci series: ");
fibonacci(num);
return 0;
}
OUTPUT-
Enter the number of terms: 15
Fibonacci series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
11. Write a program in c to display the contents of the variables and their addresses
using pointer.
INPUT-
#include <stdio.h>
int main() {
int num = 10;
float f = 5.5;
char ch = 'A';
// Pointers to variables
int *ptr1 = #
float *ptr2 = &f;
char *ptr3 = &ch;
// Display values and addresses
printf("Value of num: %d, Address of num: %p\n", *ptr1, ptr1);
printf("Value of f: %.2f, Address of f: %p\n", *ptr2, ptr2);
printf("Value of ch: %c, Address of ch: %p\n", *ptr3, ptr3);
return 0;
}
OUTPUT-
Value of num: 10, Address of num: 0061FF10
Value of f: 5.50, Address of f: 0061FF0C
Value of ch: A, Address of ch: 0061FF0B
12. Write a program in c to multiply two numbers by using pointer.
INPUT-
#include <stdio.h>
int main() {
int num1, num2, result;
// Pointers to store addresses of num1 and num2
int *ptr1, *ptr2;
// Input the numbers
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Assign the addresses to pointers
ptr1 = &num1;
ptr2 = &num2;
// Multiply using pointers
result = (*ptr1) * (*ptr2);
// Output the result
printf("The multiplication result is: %d\n", result);
return 0;
}
OUTPUT-
Enter two numbers: 15 12
The multiplication result is: 180
13. Write a program in c to swap two numbers using pointer.
INPUT-
#include <stdio.h>
int main() {
int num1, num2;
// Input the numbers
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Swap using pointers
int *ptr1 = &num1, *ptr2 = &num2;
*ptr1 = *ptr1 + *ptr2;
*ptr2 = *ptr1 - *ptr2;
*ptr1 = *ptr1 - *ptr2;
// Output the swapped values
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
OUTPUT-
Enter two numbers: 45 62
After swapping: num1 = 62, num2 = 45
14. Write a program in c to find out the length of a user given string.
INPUT-
#include <stdio.h>
int main() {
char str[100];
// Input the string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Calculate and display the length of the string
int length = 0;
while (str[length] != '\0' && str[length] != '\n') {
length++;
}
printf("Length of the string: %d\n", length);
return 0;
}
OUTPUT-
Enter a string: programming
Length of the string: 11
15. Write a program in c to convert a user given lower case string to upper case string.
INPUT-
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
// Input the string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Convert to uppercase
for (int i = 0; str[i] != '\0' && str[i] != '\n'; i++) {
str[i] = toupper(str[i]);
}
// Output the uppercase string
printf("Uppercase string: %s\n", str);
return 0;
}
OUTPUT-
Enter a string: programming
Uppercase string: PROGRAMMING
16. Write a program in c to reverse a given string.
INPUT-
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
// Input the string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove the newline character from fgets
str[strcspn(str, "\n")] = '\0';
// Reverse and print the string
for (int i = strlen(str) - 1; i >= 0; i--)
printf("%c", str[i]);
printf("\n");
return 0;
}
OUTPUT-
Enter a string: programming
gnimmargorp
17. Write a program in c to check whether a given string is palindrome or not.
INPUT-
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int len, i;
// Input the string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove the newline character
str[strcspn(str, "\n")] = '\0';
len = strlen(str);
// Check for palindrome
int is_palindrome = 1;
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
is_palindrome = 0;
break;
}
}
if (is_palindrome)
printf("The string is a palindrome.\n");
else
printf("The string is not a palindrome.\n");
return 0;
}
OUTPUT-
Enter a string: mom
The string is a palindrome.
Enter a string: leopard
The string is not a palindrome.
18. Write a program in c to execute i. strlen(), ii. strcpy(), iii. strcmp(), iv. strcut().
INPUT-
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
// Input the strings
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0'; // Remove newline character
printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = '\0'; // Remove newline character
// strlen: Get the length of the first string
printf("Length of first string: %lu\n", strlen(str1));
// strcpy: Copy the first string to the second string
strcpy(str2, str1);
printf("After copying, second string: %s\n", str2);
// strcmp: Compare the first and second strings
int result = strcmp(str1, str2);
if (result == 0)
printf("Both strings are equal.\n");
else if (result > 0)
printf("First string is greater than second string.\n");
else
printf("First string is smaller than second string.\n");
// strcat: Concatenate the first string to the second string
strcat(str2, str1);
printf("After concatenation, second string: %s\n", str2);
return 0;
}
OUTPUT-
Enter the first string: programming
Enter the second string: hello
Length of first string: 11
After copying, second string: programming
Both strings are equal.
After concatenation, second string: programmingprogramming
5. Write a c program to print the element in reverse order in an array.
INPUT-
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Elements in reverse order: ");
for (i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT-
Enter the number of elements: 5
Enter 5 elements: 15 35 62 -98 -24
Number of positive numbers: 3
Number of negative numbers: 2
1. Write a c program to find out the biggest and lowest elements of an array.
INPUT-
#include <stdio.h>
int main() {
int n, i, max, min;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
max = min = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
printf("Biggest element: %d\n", max);
printf("Lowest element: %d\n", min);
return 0;
}
OUTPUT-
Enter the number of elements: 5
Enter 5 elements: 1 35 65 24 86
Biggest element: 86
Lowest element: 1
2. Write a c program to arrange the number in an ordered form.
INPUT-
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Bubble Sort
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Sorted elements in ascending order: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT-
Enter the number of elements: 5
Enter 5 elements: 12 65 48 96 87
Sorted elements in ascending order: 12 48 65 87 96
3. Write a c program to print the fibonacci series using arrays
INPUT-
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
int fib[n];
fib[0] = 0;
fib[1] = 1;
for (i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
printf("Fibonacci series: ");
for (i = 0; i < n; i++) {
printf("%d ", fib[i]);
}
return 0;
}
OUTPUT-
Enter the number of terms: 9
Fibonacci series: 0 1 1 2 3 5 8 13 21
4. Write a c program to count the number of positive and negative numbers into
an array.
INPUT-
#include <stdio.h>
int main() {
int n, i, positive = 0, negative = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
if (arr[i] > 0)
positive++;
else if (arr[i] < 0)
negative++;
}
printf("Number of positive numbers: %d\n", positive);
printf("Number of negative numbers: %d\n", negative);
return 0;
}
OUTPUT-
Enter 5 elements: 15 35 62 -98 -24
Number of positive numbers: 3
Number of negative numbers: 2