B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP

C Arrays

Arrays in C are used to store multiple values of the same type in a single variable. It is a collection of elements stored in contiguous memory locations.

C प्रोग्रामिंग में Arrays का use तब होता है जब हमें एक साथ कई values (same type की) एक variable में store करनी हों। यह data के continuous memory locations में elements का collection होता है।

Example 1: Accessing Array Elements
#include <stdio.h>

int main() {
  int numbers[5] = {10, 20, 30, 40, 50};
  for(int i = 0; i < 5; i++) {
    printf("%d\n", numbers[i]);
  }
  return 0;
}

Output:

10
20
30
40
50

Prints all elements of the array using a loop.

यह प्रोग्राम एक array के सभी elements को for loop से print करता है।

Example 2: Sum of Array Elements
#include <stdio.h>

int main() {
  int arr[4] = {1, 2, 3, 4};
  int sum = 0;
  for(int i = 0; i < 4; i++) {
    sum += arr[i];
  }
  printf("Sum = %d", sum);
  return 0;
}

Output: Sum = 10

Calculates and prints the sum of elements in an array.

यह प्रोग्राम array के सारे elements का sum निकालता है और print करता है।

Example 3: Find Maximum Element
#include <stdio.h>

int main() {
  int arr[5] = {12, 34, 56, 23, 45};
  int max = arr[0];
  for(int i = 1; i < 5; i++) {
    if(arr[i] > max) {
      max = arr[i];
    }
  }
  printf("Maximum = %d", max);
  return 0;
}

Output: Maximum = 56

Finds and prints the maximum element in the array.

यह प्रोग्राम array में सबसे बड़े element को ढूंढ़कर print करता है।

Example 4: Find Minimum Element
#include <stdio.h>

int main() {
  int arr[5] = {12, 34, 56, 23, 45};
  int min = arr[0];
  for(int i = 1; i < 5; i++) {
    if(arr[i] < min) {
      min = arr[i];
    }
  }
  printf("Minimum = %d", min);
  return 0;
}

Output: Minimum = 12

Finds and prints the minimum element in the array.

यह प्रोग्राम array में सबसे छोटे element को ढूंढ़कर print करता है।

Example 5: Reverse Array Elements
#include <stdio.h>

int main() {
  int arr[5] = {1, 2, 3, 4, 5};
  for(int i = 4; i >= 0; i--) {
    printf("%d ", arr[i]);
  }
  return 0;
}

Output: 5 4 3 2 1

Prints array elements in reverse order.

यह प्रोग्राम array के elements को उल्टे क्रम में print करता है।

Example 6: Copy One Array to Another
#include <stdio.h>

int main() {
  int source[3] = {10, 20, 30};
  int dest[3];
  for(int i = 0; i < 3; i++) {
    dest[i] = source[i];
  }
  for(int i = 0; i < 3; i++) {
    printf("%d ", dest[i]);
  }
  return 0;
}

Output: 10 20 30

Copies elements from one array to another and prints the destination array.

यह प्रोग्राम एक array के elements को दूसरे array में copy करता है और print करता है।

Example 7: Count Even Numbers in Array
#include <stdio.h>

int main() {
  int arr[6] = {1, 2, 3, 4, 5, 6};
  int count = 0;
  for(int i = 0; i < 6; i++) {
    if(arr[i] % 2 == 0) {
      count++;
    }
  }
  printf("Even numbers count = %d", count);
  return 0;
}

Output: Even numbers count = 3

Counts how many even numbers are present in the array.

यह प्रोग्राम array में कितने even numbers हैं, उन्हें count करता है।

Example 8: Linear Search in Array
#include <stdio.h>

int main() {
  int arr[5] = {5, 10, 15, 20, 25};
  int target = 15, found = 0;
  for(int i = 0; i < 5; i++) {
    if(arr[i] == target) {
      found = 1;
      break;
    }
  }
  if(found)
    printf("Element %d found", target);
  else
    printf("Element not found");
  return 0;
}

Output: Element 15 found

Searches for a specific element in the array using linear search.

यह प्रोग्राम array में एक खास element को खोजता है और बताता है कि मिला या नहीं।

Example 9: Modify Array Elements
#include <stdio.h>

int main() {
  int arr[3] = {1, 2, 3};
  for(int i = 0; i < 3; i++) {
    arr[i] = arr[i] * 2;
  }
  for(int i = 0; i < 3; i++) {
    printf("%d ", arr[i]);
  }
  return 0;
}

Output: 2 4 6

Multiplies each element by 2 and prints the modified array.

यह प्रोग्राम array के हर element को 2 से गुणा करता है और print करता है।

Example 10: Initialize Array with Zero
#include <stdio.h>

int main() {
  int arr[5] = {0}; // सभी elements zero से initialized
  for(int i = 0; i < 5; i++) {
    printf("%d ", arr[i]);
  }
  return 0;
}

Output: 0 0 0 0 0

Initializes all array elements to zero and prints them.

यह प्रोग्राम array के सारे elements को zero से initialize करता है और print करता है।