Friday, June 26, 2020

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


PROGRAM:

#include<stdio.h>
int main()
{
    int a[100],b[100],i,j,n,c=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        c=0;
        for(j=1;j<=a[i];j++)
        {
            if((a[i]%j)==0)
            {
                c++;
               
            }
        }
        b[i]=c;
        }
   
for(i=0;i<n;i++)
{
    int t,t1;
    for(j=i+1;j<n;j++)
    {
        if(b[i]<b[j])
        {
            t=b[i];
            b[i]=b[j];
            b[j]=t;
           
            t1=a[i];
            a[i]=a[j];
            a[j]=t1;
        }
       
    }
}
for(i=0;i<n;i++)
{
    printf(" %d",a[i]);
}
return 0;
}
OUTPUT:
5                                                                                                                     
8 2 3 12 16                                                                                                           
 12 16 8 2 3

No comments:

Post a Comment