Given a set of numbers like <10, 36, 54,89,12> we want to find sum of weights based on the following conditions
1. 5 if a perfect square
2. 4 if multiple of 4 and divisible by 6
3. 3 if even number
And sort the numbers based on the weight and print it as follows<10,its_weight>,<36,its weight><89,its weight>
Should display the numbers based on increasing order.
PROGRAM:
#include<stdio.h>
#include<math.h>
int main()
{
int a[100],b[100],i,j,n,c[100];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
b[i]=0;
int sq;
sq=sqrt(a[i]);
if(sq*sq==a[i])
{
b[i]=b[i]+5;
}
if((a[i]%4==0)&&(a[i]%6==0))
{
b[i]=b[i]+4;
}
if(a[i]%2==0)
{
b[i]=b[i]+3;
}
}
int t,t1;
for(i=0;i<n;i++)
{
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\t",a[i]);
}
return 0;
}
OUTPUT:
5
10 36 54 89 12
36 12 54 10 89
No comments:
Post a Comment