Weight Generation

Program 6: GENERATE WEIGHT FOR SET OF NUMBERS

Intro Text

In this program, we learn by completing the given requirement. This is a weird requirement where we will use some function which can be a separate exercise for your previous batch.

Requirement

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

Sort the numbers based on the weight in the increasing order as shown below <10,its weight>,<36,its weight><89,its weight>

Requirement Analysis

The solution should be like, we need to enter a set of number like <10, 36, 54, 89, 12, 27>, Then the program should validate each number whether a number is perfect number or whether it can be multiply by 4 or divided by 6 or whether its a prime number and we assign a weight based on what number it is.we need to obtain the sum of weight that assigned to the numbers and finally we need to sort the set of numbers based on weight in increasing order.

We need to have a basic understand of the following thins before start coding.

  • what is perfect number?
  • What is prime number?
  • Functions in C.
 Let’s Solve this..
Get a set of numbers….

One simple way is you just enter the how many numbers you want to get and store it in a array using a simple for loop.

But I did something different in this like just enter how many number you want to get in a particular set.In for if there is no number no previous number it will show the “<” . when a number is entered say 10 then the set will be updated to ‘ < 10, ‘ and 10 will be locked . when you enter the last number to the array it will show like ‘ < 10, 12, 13, ….16 > ‘ it will show the set as mentioned above.the following code snippet will do those awesome things.

 printf("\nEnter how many numbers you want to enter...");
 scanf("%d",&n);
 //printf("\nEnter the numbers...");
 for(i=0;i<n;i++){
 clrscr();
 printf("Enter the number...");
 if(i==0){
 printf("<");
 scanf("%d",&set[i]);
 }
 else{
 printf("<");
 for(j=0;j<i;j++){
 printf("%d,",set[j]);
 }
 scanf("%d",&set[i]);
 }
 }
 clrscr();
 printf("\nEntered set is...<");
 for(i=0;i<n;i++){
 if(i!=(n-1)){
 printf("%d,",set[i]);
 }
 else{
 printf("%d",set[i]);
 }
 }
 printf(">\n");

checking prime numbers:

The following code snippet will return true when the number passed to it is a prime number.

 int isprime(int no){
 int i,flag=0;
 if(no==1){
 return 0;
 }
 for(i=2;i<=no/2;i++){
 if(no%i==0){
 flag=1;
 break;
 }
 }
 if(flag==0){
 return 1;
 }
 else{
 return 0;
 }
 }

checking for multiple by 4 and divided by 6:

The following code snippet will return true when the number passed to it is a multiple of 4 and divisible by 6.

 

int ism4d6(int no){
 if(no%4==0 && no%6==0){
 return 1;
 }
 else{
 return 0;
 }
 }
Check for a perfect number:

The following code snippet will return true when the number passed to it is perfect number.

 

int isperfect(int no){
 int rem,sum=0,i;
 for(i=1;i<=no-1;i++){
 rem=no%i;
 if(rem==0){
 sum=sum+i;
 }
 }
 if(sum==no){
 return 1;
 }
 else{
 return 0;
 }
 }

 

Assigning Weight :

now after building all those functions  we can able to assign weight each number in the set. for that we will declare a array for weight which holds the weight of numbers. we will obtain the weight by traversing through the array and we will check whether its a perfect number, multiple of 4 and divisible by 6 or whether its a prime number . and then we will assign weight accordingly. the following code snippet will do the checking and assigning weight process.


for(i=0;i<n;i++){
 if(isperfect(set[i])){
 w[i]=5;
 }
 else if(ism4d6(set[i])){
 w[i]=4;
 }
 else if(isprime(set[i])){
 w[i]=3;
 }
 else{
 w[i]=0;
 }
 }


Adding weight together

Now we have the weight for each numbers in the set. Using for loop we can add it up as shown below.

for(i=0;i<n;i++){
     weight += w[i];
 }
Program Code

And the final compiled C program will be.

 /*
 *given a set of number , find sum of weight based on condition
 * 1. 5 if its a perfect number
 * 2. 4 if its a multiple of 4 and divided by 6
 * 3. 3 if its a prime number
 */
 #include<stdio.h>
 #include<conio.h>
 
 int isprime(int);
 int isperfect(int);
 int ism4d6(int);
 
 void main(){
	int set[20],n=0,w[20],weight=0,i,j;
	clrscr();
	printf("\nEnter how many numbers you want to enter...");
	scanf("%d",&n);
	//printf("\nEnter the numbers...");
	for(i=0;i<n;i++){
		clrscr();
		printf("Enter the number...");
		if(i==0){
			printf("<");
			scanf("%d",&set[i]);
		}
		else{
			printf("<");
			for(j=0;j<i;j++){
				printf("%d,",set[j]);
			}
			scanf("%d",&set[i]);
		}
	}
	clrscr();
	printf("\nEntered set is...<");
	for(i=0;i<n;i++){
		if(i!=(n-1)){
			printf("%d,",set[i]);
		}else{
			printf("%d",set[i]);
		}
	}
	printf(">\n");
	
	/*CHECK ARRAY and ASIGN WEIGHT*/
	for(i=0;i<n;i++){
		if(isperfect(set[i])){
			w[i]=5;
		}
		else if(ism4d6(set[i])){
			w[i]=4;
		}
		else if(isprime(set[i])){
			w[i]=3;
		}
		else{
			w[i]=0;
		}
	}
	
	for(i=0;i<n;i++){
		weight += w[i];
	}
	printf("sum of weight is...%d",weight);
	getch();
 }

int isprime(int no){
 int i,flag=0;
 if(no==1){
 return 0;
 }
 for(i=2;i<=no/2;i++){
 if(no%i==0){
 flag=1;
 break;
 }
 }
 if(flag==0){
 return 1;
 }
 else{
 return 0;
 }
 }

int ism4d6(int no){
 if(no%4==0 && no%6==0){
 return 1;
 }
 else{
 return 0;
 }
 }
 int isperfect(int no){
 int rem,sum=0,i;
 for(i=1;i<=no-1;i++){
 rem=no%i;
 if(rem==0){
 sum=sum+i;
 }
 }
 if(sum==no){
 return 1;
 }
 else{
 return 0;
 }
 }