Time spent here:

Saturday, 18 June 2016

FACTOR FUN.......!!!!1

Problem statement:
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

Code:
#include <stdio.h>
int main()
{
  int i,j,list[5],count[5],c=0;
  for(i=0;i<5;i++)
    {
        scanf("%d",&list[i]);
        for(j=1;j<list[i]/2;j++)
            {
                if(list[i]%j==0)
                  c++;
            }
            count[i]=c;
            c=0;
       
    }
    for(i=0;i<5;i++)
     {
         for(j=0;j<5;j++)
         {
             if(count[j]<count[j+1])
             {
                 int temp=count[j];
                 count[j]=count[j+1];
                 count[j+1]=temp;
                 temp=list[j];
                 list[j]=list[j+1];
                 list[j+1]=temp;
             }
         }
     }
     for(i=0;i<5;i++)
       printf("%d ",list[i]);
    return 0;

}

No comments:

Post a Comment