0% found this document useful (0 votes)
124 views3 pages

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

The C program takes a set of numbers as input and assigns weights to each number based on certain conditions: 5 if the number is a perfect cube, 4 if the number is divisible by 4 and 6, and 3 if the number is a prime number. It then prints the original array with weights, sorts the array based on weights, and prints the sorted array.

Uploaded by

Manoj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views3 pages

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

The C program takes a set of numbers as input and assigns weights to each number based on certain conditions: 5 if the number is a perfect cube, 4 if the number is divisible by 4 and 6, and 3 if the number is a prime number. It then prints the original array with weights, sorts the array based on weights, and prints the sorted array.

Uploaded by

Manoj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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