Home
Aptitude
Categories


Home » C programs » C One Dimensional Array 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

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

Program:

/*
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;
	
	//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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.