UNIT 3.0
UNIT 3.0
UNIT 3.0
numbers.
#include <stdio.h>
int main() {
int n, num, highest, lowest, sum;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid number of elements.\n");
return 1;
}
printf("Enter number 1: ");
scanf("%d", &num);
highest = lowest = num;
for (int i = 1; i < n; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &num);
return 0;
2) Build a program to accept n numbers in array and display the addition of all even numbers
and multiplication of all odd numbers.
#include <stdio.h>
int main() {
int n, num;
int sum_even = 0;
int product_odd = 1;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the numbers:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &num);
if (num % 2 == 0) {
sum_even += num;
} else {
product_odd *= num;
}
}
printf("Sum of all even numbers: %d\n", sum_even);
printf("Product of all odd numbers: %d\n", product_odd);
return 0;
}
3) Build a program to sort numbers of a one-d array in descending order using Bubble sorot
#include <stdio.h>
int main() {
int n, num;
int sum_even = 0;
int product_odd = 1;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the numbers:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &num);
if (num % 2 == 0) {
sum_even += num;
} else {
product_odd *= num;
}
}
printf("Sum of all even numbers: %d\n", sum_even);
printf("Product of all odd numbers: %d\n", product_odd);
return 0;
}
4) Build a program to reverse only first n elements of an array.
#include <stdio.h>
int main() {
int arr[100], n, num_elements;
printf("Enter the number of elements in the array: ");
scanf("%d", &num_elements);
printf("Enter the elements of the array:\n");
for (int i = 0; i < num_elements; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the number of elements to reverse: ");
scanf("%d", &n);
// Ensure n is within the bounds of the array
if (n > num_elements) {
n = num_elements;
}
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
printf("Array after reversing the first %d elements:\n", n);
for (int i = 0; i < num_elements; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
5) Build a program to display the elements which are exists multiple times in a given 1D array
#include <stdio.h>
int main() {
int arr[100], n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Elements that exist multiple times:\n");
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
printf("%d\n", arr[i]);
break; // Break to avoid printing the same element multiple times
}
}
}
return 0;
#include <stdio.h>
int main() {
int n, matrix[100][100];
int is_symmetric = 1;
printf("Enter the size of the matrix (n): ");
scanf("%d", &n);
printf("Enter the elements of the %d x %d matrix:\n", n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (matrix[i][j] != matrix[j][i]) {
is_symmetric = 0;
break;
}
}
if (!is_symmetric) break;
}
if (is_symmetric) {
printf("The given matrix is symmetric.\n");
} else {
printf("The given matrix is not symmetric.\n");
}
return 0;
}
7) Build a program to count the occurrences of a number in the given matrix.
#include <stdio.h>
int main() {
int n, num, count = 0;
int matrix[100][100];
printf("Enter the size of the matrix (n): ");
scanf("%d", &n);
printf("Enter the elements of the %d x %d matrix:\n", n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("Enter the number to count: ");
scanf("%d", &num);
// Count the occurrences of the number in the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == num) {
count++;
}
}
}
printf("The number %d occurs %d times in the matrix.\n", num, count);
return 0;
}
int main() {
int rows, cols;
int matrix1[100][100], matrix2[100][100], sum[100][100];
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d %d", &rows, &cols);
printf("Enter the elements of the first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter the elements of the second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix2[i][j]);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf("The sum of the two matrices is:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
10) Build a program to perform multiplication of two matrices
12) Make use concept of 1D array to give its syntax for declaration and initialization with an
example program.
Syntax for declaration :-
dataType arrayName[arraySize];
syntax for Initialization:
arrayName[index] = value;
(or)
dataType arrayName[arraySize] = {value1, value2, ..., valueN};
example program
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
// Calculate the sum of all elements in the array
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += numbers[i];
}
// Print the elements of the array
printf("The elements of the array are:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
// Print the sum of the elements
printf("The sum of the elements is: %d\n", sum);
return 0;
}
13) Make use concept of 2D array to give its syntax for declaration and initialization with an
example program
Syntax for declaration :-
dataType arrayName[rows][columns];
syntax for Initialization:
arrayName[rows][columns] = {{value1, value2, ...}, {value3, value4, ...}, ...}
example program:-
#include <stdio.h>
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum += matrix[i][j];
}
}
printf("The elements of the 2D array are:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Print the sum of the elements
printf("The sum of the elements is: %d\n", sum);
return 0;
}
14) Identify the use of pointer and how to dereference a pointer. Identify the working pointer in
arithmetic operations.
Pointers- Pointers in C are powerful features that allow you to directly interact with memory
addresses. They are used for dynamic memory allocation, arrays, functions, and structures.
Here's a brief overview and example to help illustrate how to use and dereference pointers,
as well as how pointer arithmetic works.
Pointer Declaration: dataType *pointerName;
Example : int *ptr;
Dereferencing a Pointer: Dereferencing a pointer means accessing the value stored at the
memory address pointed to by the pointer.
Example :int dereferencedValue = *ptr;
Example program:
#include <stdio.h>
int main() {
int value = 5;
int *ptr = &value; // Pointer to an integer
// Pointer arithmetic
int arr[5] = {10, 20, 30, 40, 50};
int *arrPtr = arr; // Pointing to the first element of the array
return 0;
}
15) Identify how to work with arrays by using pointers. Give an example program.
Explanation:
o int arr[] = {10, 20, 30, 40, 50}; declares and initializes an array.
o int *ptr = arr; initializes a pointer to point to the first element of the array.
o Using a for loop and pointer arithmetic, *(ptr + i) is used to access and print each
element of the array.
4. Modifying Elements:
o The elements of the array are modified using pointer arithmetic. Each element is
doubled by the expression *(ptr + i) = *(ptr + i) * 2;.
Example code:
#include <stdio.h>
int main() {
printf("\n");
*(ptr + i) = *(ptr + i) * 2;
printf("\n");
return 0;