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.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void read_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&a[i]);
}
}
void print_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:%d\n",i+1,a[i]);
}
}
void main()
{
int arr[MAX], divi[MAX];
int i, j, n, num, count;
count=0;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements of the array\n");
read_array(arr, n);
printf("Enter the number by which divisibility is to be checked\t:");
scanf("%d", &num);
j=0;
for(i=0;i<n;i++)
{
if(arr[i]%num==0)
{
count++;
divi[j] = arr[i];
j++;
}
}
printf("\nThere are total of %d numbers which are divisible by %d",count,num);
printf("\nNumbers are\n");
print_array(divi,j);
getch();
}
Output
Enter the number of elements in the array :8
Enter the elements of the array
Element 1 :12
Element 2 :25
Element 3 :56
Element 4 :58
Element 5 :47
Element 6 :89
Element 7 :36
Element 8 :45
Enter the number by which divisibility is to be checked :2
There are total of 4 numbers which are divisible by 2
Numbers are
Element 1 :12
Element 2 :56
Element 3 :58
Element 4 :36