In this tutorial, you will learn how to write C program to compare two arrays and check that they are equal in size or not.
Below are the approach which we will be follow to achieve our goal:
- Our approach is to check two given arrays are equal in size or not is first we will find the size of both array.
- And then compare the size. If the size are equal then it will print “size of both arrays are equal” and if the size is not equal then it will print “size of arrays are not equal”.
How our program will behave?
As we have already seen above our logic to check the size of given two arrays is equal or not.
In this program we have already two arrays. After the execution of the program it will print the output as per array size after the calculation.
Program to check the size of given two arrays are equal or not?
#include<stdio.h>
int main() {
int arr1[] = {1, 2, 5, 3, 4, 5};
int arr2[] = {2, 3, 1, 9, 5};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(arr2) / sizeof(arr2[0]);
printf("=== C Program to Check If Two Arrays Are of Equal Size ===\n");
if (size1 == size2) {
printf("Size of both arrays is equal.\n");
} else {
printf("Size of arrays is not equal.\n");
}
return 0;
}
Output:
=== C Program to Check If Two Arrays Are of Equal Size ===
Size of arrays is not equal.
Explanation of the above
- In the above program we have two arrays arr1 and arr2 of type integer.
- Elements in arr1 are 1, 2, 5, 3, 4, and 5.
- Elements in arr2 are 2, 3, 1, 9, and 5.
- Now we have calculated the size of both array separately.
- size1 variable has hold the size of array arr1 and size2 variable has hold the size of array arr2.
- If the size of both array is equal which we have checked using if statement then print “size of both arrays are equal” otherwise print “size of arrays are not equal”.
This was the all logic to check whether a size of two arrays are equal or not.
What did you think?
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…