You are on page 1of 3

To Write a C program to given a set of numbers like<10, 36, 54, 89, 12, 27> find sum of

weights based on the following conditions.

 5 if it is a perfect cube.
 4 if it is a multiple of 4 and divisible by 6.
 3 if it is a prime number.

#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int num, array[100],warray[100];
int i,j,count,flag,t,t1;
clrscr();
printf("****** SUM OF WEIGHTS*******\n");
printf("Enter the number: ");
scanf("%d",&num);
printf("Enter the array Elements : ");
for(i=0;i<num;i++)
{
scanf("%d",&array[i]);
}
for(i=0;i<num;i++)
{
warray[i]=0;
count=0;
//To check a number is Cube root or not
for(j=0;j<array[i];j++)
{
if(j*j*j ==array[i])
{
count=1;
break;
}
}
if (count ==1)
{
warray[i]=warray[i]+5;
}
//To check a number is divisible by 4 and 6
if(array[i] %4 ==0 && array[i] %6==0)
{
warray[i]=warray[i]+4;

}
//To check a number is prime or not
flag=0;
for (j=2;j< array[i]; j++)
{
if (array[i] % j ==0)
{
flag=1;
break;
}
}
if (flag==0)
{
warray[i]=warray[i]+3;
}
}
printf("Array Element\t\tWeights\n");
for (i=0;i<num;i++)
{
printf("%d\t\t%d\n",array[i],warray[i]);
}
//Sorting
for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
{
if(warray[i]>warray[j])
{
t=warray[i];
warray[i]=warray[j];
warray[j]=t;

t1=array[i];
array[i]=array[j];
array[j]=t1;
}
}
}
printf("Sorted array\n ");
for (i=0;i<num;i++)
{
printf("%d\t\t%d\n",array[i],warray[i]);
}
getch();
}
Output

You might also like