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

UNIT 3.0

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

UNIT 3.0

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

1) Build a program to accept n numbers and display the sum of the highest and lowest

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);

if (num > highest) highest = num;


if (num < lowest) lowest = num;
}
sum = highest + lowest;

printf("Highest number: %d\n", highest);


printf("Lowest number: %d\n", lowest);
printf("Sum of highest and lowest: %d\n", sum);

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;

6) Build a program to check whether given n*n matrix is symmetric or not

#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;
}

8) Build a program to reverse the rows of a matrix.


#include <stdio.h>
int main() {
int n;
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]);
}
}
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[n - 1 - i][j];
matrix[n - 1 - i][j] = temp;
}
}
printf("Matrix after reversing the rows:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}

9) Build a program to perform addition two matrices


#include <stdio.h>

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

#include<stdio.h> //logic for multiplication


int main() for(i=0; i<c1; i++)
{ {
int a[100][100], b[100][100], c[100][100]; for(j=0; j<r2; j++)
int r1,c1, r2,c2,i,j,k; {
printf("Enter the size of matrix a:"); c[i][j] = 0;
scanf("%d%d",&r1,&c1); for(k=0; k<c1; k++)
printf("\nEnter the size of matrix b:"); {
scanf("%d%d",&r2,&c2); c[i][j] = c[i][j]+(a[i][k] * b[k][j]);
if( c1 == r2 ) }
{ }
printf("\nMatrix multiplication is possible\n"); }
printf("\nEnter the elements into matrix a:\n");

for(i=0; i<r1; i++) printf("\nResultant matrix is :\n");


{ for(i=0; i<r1; i++)
for(j=0; j<c1; j++) {
{ for(j=0; j<c2; j++)
scanf("%d",&a[i][j]); {
} printf("%d ",c[i][j]);
} }
printf("\nEnter the elements into matrix b:\n"); printf("\n");
for(i=0; i<r2; i++) }
{ }
for(j=0; j<c2; j++) else
{ {
scanf("%d",&b[i][j]); printf("\nMatrix multiplication is not
possible");
}
}
}
}
11) Build a program to find the sum of secondary diagonal elements in a given square matrix
#include <stdio.h>
int main() {
int n;
int matrix[100][100];
int sum = 0;
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++) {
sum += matrix[i][n - 1 - i];
}
printf("The sum of the secondary diagonal elements is: %d\n", sum);
return 0;
}

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;

Pointer Initialization: int value = 5; ptr = &value;

Dereferencing a Pointer: Dereferencing a pointer means accessing the value stored at the
memory address pointed to by the pointer.
Example :int dereferencedValue = *ptr;

Pointer Arithmetic: Pointers can be incremented or decremented to point to the next or


previous memory location of the data type it points to:
Example: ptr++;

Example program:
#include <stdio.h>

int main() {
int value = 5;
int *ptr = &value; // Pointer to an integer

// Dereferencing the pointer


printf("The value is: %d\n", *ptr);

// Pointer arithmetic
int arr[5] = {10, 20, 30, 40, 50};
int *arrPtr = arr; // Pointing to the first element of the array

printf("Array elements using pointer arithmetic:\n");


for (int i = 0; i < 5; i++) {
printf("%d ", *(arrPtr + i)); // Dereference each element
}
printf("\n");

return 0;
}
15) Identify how to work with arrays by using pointers. Give an example program.

Explanation:

1. Array Declaration and Initialization:

o int arr[] = {10, 20, 30, 40, 50}; declares and initializes an array.

o int n = sizeof(arr) / sizeof(arr[0]); calculates the number of elements in the array.

2. Pointer to the Array:

o int *ptr = arr; initializes a pointer to point to the first element of the array.

3. Accessing and Printing Elements:

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;.

5. Printing Modified Elements:

o The modified array elements are printed to show the changes

Example code:

#include <stdio.h>

int main() {

int arr[] = {10, 20, 30, 40, 50};

int n = sizeof(arr) / sizeof(arr[0]);

int *ptr = arr;

printf("Array elements using pointers:\n");

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

printf("%d ", *(ptr + i));

printf("\n");

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

*(ptr + i) = *(ptr + i) * 2;

printf("Modified array elements:\n");

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

printf("%d ", arr[i]);


}

printf("\n");

return 0;

You might also like