Practical-14
Write a program declare following structure member: name, code, age, weight and height. Read all members of the structure for 10 persons and find list of persons with all related data whose weight > 50 and height > 40 and print the same with suitable format and title.
Introduction
In this program, we will learn to declare a structure and store data for 10 persons. Then we will find the list of persons with weight > 50 and height > 40.
Algorithm
- Start
- Declare a structure called person, and it stores information about a person.
- Declare an array of 10 persons.
- Declare a variable to count the number of persons with weight > 50 and height > 40.
- Print instructions to enter the data for 10 persons.
- Loop through each person and store the data.
- Print the title and table header.
- Loop through each person and print details if condition holds true.
- End
Code
Practical-14.c
#include <stdio.h> // Include input and output library
struct person // Define the person structure
{
char name[100];
int code;
int age;
int weight;
int height;
};
void main() // Program starting point
{
struct person persons[10]; // Declare an array of person objects
int personsAboveWeight = 0; // To count the number of persons with weight > 50
printf("Enter the data for 10 persons\n"); // Instructions
// Loop through each person and store the data
for(int i = 0; i < 10; i++)
{
printf("Enter name: ");
scanf("%s", persons[i].name); // Read name
printf("Enter code: ");
scanf("%d", &persons[i].code); // Read code
printf("Enter age: ");
scanf("%d", &persons[i].age); // Read age
printf("Enter weight: ");
scanf("%d", &persons[i].weight); // Read weight
printf("Enter height: ");
scanf("%d", &persons[i].height); // Read height
}
printf("Persons with weight > 50 and height > 40:\n"); // Print title
printf("Name\tCode\tAge\tWeight\tHeight\n"); // Print table header
// Loop through each person and print details if condition holds true
for(int i = 0; i < 10; i++)
{
if(persons[i].weight > 50 && persons[i].height > 40)
{
personsAboveWeight++; // Increase count for number of persons
printf("%s\t%d\t%d\t%d\t%d\n", persons[i].name, persons[i].code, persons[i].age, persons[i].weight, persons[i].height); // Print the details
}
}
// Print the number of persons
printf("Number of persons with weight > 50 and height > 40: %d\n", personsAboveWeight);
}
Output
Enter the data for 10 persons
Enter name: Naruto
Enter code: 1
Enter age: 20
Enter weight: 60
Enter height: 50
Enter name: Sasuke
Enter code: 2
Enter age: 21
Enter weight: 50
Enter height: 40
Enter name: Sakura
Enter code: 3
Enter age: 20
Enter weight: 40
Enter height: 50
Enter name: Kakashi
Enter code: 4
Enter age: 30
Enter weight: 60
Enter height: 40
Enter name: Itachi
Enter code: 5
Enter age: 25
Enter weight: 50
Enter height: 50
Enter name: Madara
Enter code: 6
Enter age: 40
Enter weight: 60
Enter height: 50
Enter name: Obito
Enter code: 7
Enter age: 25
Enter weight: 50
Enter height: 40
Enter name: Minato
Enter code: 8
Enter age: 30
Enter weight: 40
Enter height: 40
Enter name: Hashirama
Enter code: 9
Enter age: 40
Enter weight: 60
Enter height: 50
Enter name: Tobirama
Enter code: 10
Enter age: 40
Enter weight: 50
Enter height: 40
Persons with weight > 50 and height > 40:
Name Code Age Weight Height
Naruto 1 20 60 50
Kakashi 4 30 60 40
Madara 6 40 60 50
Hashirama 9 40 60 50
Number of persons with weight > 50 and height > 40: 4
Explanation
In this program, we have declared a structure called person. It stores information about a person. We have declared an array of 10 persons. We have declared a variable to count the number of persons with weight > 50 and height > 40. We have printed instructions to enter the data for 10 persons. We have looped through each person and stored the data. We have printed the title and table header. We have looped through each person and printed details if condition holds true. We have printed the number of persons.