0% found this document useful (0 votes)
386 views5 pages

EX - NO:6 Date: Sort The Numbers Based On The Weight

The document describes a C program to calculate body mass index (BMI) of individuals. The program populates a 2D array with height and weight of persons, calculates BMI for each person by dividing their weight by the square of their height, and displays the BMI and health status (underweight, healthy, overweight, etc.) for each individual. The program takes number of persons as input, populates the array with their height and weight, calculates BMI for each, and prints the output with person details and BMI classification.

Uploaded by

thanammal indu
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)
386 views5 pages

EX - NO:6 Date: Sort The Numbers Based On The Weight

The document describes a C program to calculate body mass index (BMI) of individuals. The program populates a 2D array with height and weight of persons, calculates BMI for each person by dividing their weight by the square of their height, and displays the BMI and health status (underweight, healthy, overweight, etc.) for each individual. The program takes number of persons as input, populates the array with their height and weight, calculates BMI for each, and prints the output with person details and BMI classification.

Uploaded by

thanammal indu
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/ 5

EX.

NO:6 SORT THE NUMBERS BASED ON THE WEIGHT

DATE:

AIM:
To write a C Program to perform the following: 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>.
ALGORITHM:

1. Start
2. Declare variables
3. Read the number of elements .
4. Get the individual elements.
5. Calculate the weight for each element by the conditions
• 5 if it is a perfect cube (pow)
• 4 if it is a multiple of 4 and divisible by 6 (modulus operator)
• 3 if it is a prime number(modulus operator)
6. Display the output of the weight calculations after sorting .
7. Stop

PROGRAM:
#include <stdio.h>
#include<conio.h>
#include <math.h>
int prime(int num)
{
int flag=1,i;
for(i=2;i<=num/2;i++)
if(num%i==0)
{
flag=0;
break;
}
return flag;
}
int percube(int num)
{
int i,flag=0;
for(i=2;i<=num/2;i++)
if((i*i*i)==num)
{
flag=1;
break;
}
return flag;
}
int main()
{
int nArray[50],wArray[50],nelem,i,j,t;
printf("\nEnter the number of elements in an array : ");
scanf("%d",&nelem);
printf("\nEnter %d elements\n",nelem);
for(i=0;i<nelem;i++)
scanf("%d",&nArray[i]);
//Calculate the weight
for(i=0; i<nelem; i++)
{
wArray[i] = 0;
if(percube(nArray[i]))
wArray[i] = wArray[i] + 5;
if(nArray[i]%4==0 && nArray[i]%6==0)
wArray[i] = wArray[i] + 4;
if(prime(nArray[i]))
wArray[i] = wArray[i] + 3;
}
for(i=0;i<nelem;i++)
printf("%d",nArray[i]);
for(i=0;i<nelem;i++)
printf("%d",wArray[i]);
// Sorting an array
for(i=0;i<nelem;i++)
for(j=i+1;j<nelem;j++)
if(wArray[i] > wArray[j])
{
t = wArray[i];
wArray[i] = wArray[j];
wArray[j] = t;
t = nArray[i];
nArray[i] = nArray[j];
nArray[j] = t;
}
for(i=0; i<nelem; i++)
printf("<%d,%d>\n", nArray[i],wArray[i]);
return 0;
}

RESULT:
Thus a C Program for Sort the numbers based on the weight was executed and the output
was obtained.
EX.NO:7 AVERAGE HEIGHT OF PERSONS

DATE:

AIM:
To write a C Program to populate an array with height of persons and find how many
persons are above the average height.

ALGORITHM:

1. Start
2. Declare variables
3. Read the total number of persons and their height.
4. Calculate avg=sum/n and find number of persons their h>avg.
5. Display the output of the calculations .
6. Stop

PROGRAM:

/* Get a Height of Different Persons and find how many of them are are above average */
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n,sum=0,count=0,height[100];
float avg;
clrscr();
printf("Enter the Number of Persons : ");
scanf("%d",&n);
printf("\n Enter the Height of each person in centimeter\n");
for(i=0;i<n;i++)
{
scanf("%d",&height[i]);
sum = sum + height[i];
}
avg = (float)sum/n;
//Counting
for(i=0;i<n;i++)
if(height[i]>avg)
count++;
printf("\n Average Height of %d persons is : %.2f\n",n,avg);
printf("\n The number of persons above average : %d ",count);
getch();
}

RESULT:
Thus a C Program average height of persons was executed and the output was obtained.
EX.NO:8 BODY MASS INDEX OF THE INDIVIDUALS

DATE:

AIM:
To write a C Program to Populate a two dimensional array with height and weight of
persons and compute the Body Mass Index of the individuals.

ALGORITHM:

1. Start
2. Declare variables
3. Read the number of persons and their height and weight.
4. Calculate BMI=W/H2for each person
5. Display the output of the BMI for each person.
6. Stop

PROGRAM:

#include <stdio.h>
#include <conio.h>
void main()
{
int stu[100][2];`
int index[100];
int i,n;
float h;
clrscr();
printf("Enter the number of students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Height(cm) and Weight(kg) of student %d :",i+1);
scanf("%d%d",&stu[i][0],&stu[i][1]);
h = (float)(stu[i][0]/100.0);
index[i] = (float)stu[i][1]/(float)(h*h);
}
printf("\nStu.No.\tHeight\tWeight\tBMI\tResult\n");
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d\t",i+1,stu[i][0],stu[i][1],index[i]);
if(index[i]<15)
printf("Starvation\n");
else if(index[i]>14 && index[i] < 18)
printf("Underweight\n");
else if(index[i] > 17 && index[i] < 26)
printf("Healthy\n");
else if(index[i] > 25 && index[i] < 31)
printf("Over weight\n");
else if(index[i] > 30 && index[i] < 36)
printf("Obese\n");
else
printf("Severe Obese\n");
} // for loop
getch();
}

RESULT:
Thus a C Program Body Mass Index of the individuals was executed and the output was
obtained.

You might also like