Wednesday, 22 June 2016

To find the factors of the numbers given in an array and to sort the numbers in descending order according to the factors present in it. Input: Given array : 8, 2, 3, 12, 16 Output: 12, 16, 8, 2, 3

 #include<stdio.h>

int factor(int aa){

int k,count=0;

for(k=1;k<=aa;k++){

if(aa%k==0)

count++;

}

return count;

}

int main(){

int a[100],f[100]={0},i,j,n,temp,temp1;

scanf("%d",&n);

for(i=0;i<n;i++){

scanf("%d",&a[i]);

f[i]=factor(a[i]);

}

for(i=0;i<n­1;i++){

for(j=0;j<n­1­i;j++){

if(f[j] > f[j+1]) {

temp1=a[j];

a[j]=a[j+1];

a[j+1]=temp1;

temp = f[j];

f[j] = f[j+1];

f[j+1] = temp;

}

}

}

for(i=n­1;i>=0;i­­){

printf("%d ",a[i]);

}

return 0;

}


No comments:

Post a Comment