Sort numbers based on the no.of factors

Program:

#include<stdio.h>
struct number{
    int num;
    int fact;
};
int main()
{
    int n;
    printf("Enter the total numbers \n");
    scanf("%d",&n);
    struct number num[n];
    int i,j;
    for(i=0;i<n;i++)
    {
        printf("Enter the %d number \n",i);
        scanf("%d",&num[i].num);
    }
    for(i=0;i<n;i++)
    {
        int count=0;
        for(j=2;j<=num[i].num;j++)
        {
            if(num[i].num%j==0)
            {
                count++;
            }
        }
        num[i].fact=count;
    }
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(num[i].fact > num[j].fact)
            {
                int temp = num[i].fact;
                num[i].fact = num[j].fact;
                num[j].fact = temp;
               
                temp = num[i].num;
                num[i].num = num[j].num;
                num[j].num = temp;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        printf("%d   -----    %d \n",num[i].num,num[i].fact);
    }

Output:

Enter the total numbers                                                                                                                          
5                                                                                                                                                
Enter the 0 number                                                                                                                               
1                                                                                                                                                
Enter the 1 number                                                                                                                               
10                                                                                                                                               
Enter the 2 number                                                                                                                               
20                                                                                                                                               
Enter the 3 number                                                                                                                               
40                                                                                                                                               
Enter the 4 number                                                                                                                               
100                                                                                                                                              
1   -----    0                                                                                                                                   
10   -----    3                                                                                                                                  
20   -----    5                                                                                                                                  
40   -----    7                                                                                                                                  
100   -----    8    

Comments

Popular posts from this blog

c++ program to print string along the diagonals of the matrix.

Kollywood Game

C program to print the following Pattern