×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to create a new array from a given array with the elements divisible by a specific number

Here, we are implementing a c program that will create a new array from a given array with the elements divisible by a specific number in an array.
Submitted by IncludeHelp, on December 04, 2018

Problem statement

Given an array arr1 and the number b, we have to create a new array arr2 by the elements of arr1 elements which are divisible by b.

Example

Input:
Enter array elements:
10
15
20
25
30
35
40
45
50
55

Number: 10
Output:

Elements of arr2: 10 20 30 40 50

C program to create a new array from a given array with the elements divisible by a specific number

/*
C program to create a new array from a given array
with the elements divisible by a specific number
*/

#include <stdio.h>
#define MAX 10

int main() {
  int arr1[MAX], arr2[MAX];
  int i, j;
  int b = 10;
  int n = 10;

  // initializing j with 0
  j = 0;

  printf("Enter array elements:\n");
  for (i = 0; i < n; i++) {
    scanf("%d", &arr1[i]);
    if (arr1[i] % b == 0) arr2[j++] = arr1[i];
  }

  printf("Elements of arr2: ");
  // run loop from 0 to j
  //(total number of new elements from arr1)
  for (i = 0; i < j; i++) printf("%d ", arr2[i]);

  return 0;
}

Output

Enter array elements:
10
15
20
25
30
35
40
45
50
55
Elements of arr2: 10 20 30 40 50

C One-Dimensional Array Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.