The Program in C to Read Personal info. of 10 peoples and print-details of those people living in particular city is given below:
#include <stdio.h>
struct Person {
char name[50];
char city[50];
int age;
};
int main() {
struct Person people[10];
char targetCity[50];
int i;
// Read personal information of 10 people
for (i = 0; i < 10; i++) {
printf("Enter name, city, and age of person %d: ", i + 1);
scanf("%s %s %d", people[i].name, people[i].city, &people[i].age);
}
// Read target city
printf("Enter the city to search for: ");
scanf("%s", targetCity);
// Print details of people who live in the target city
for (i = 0; i < 10; i++) {
if (strcmp(people[i].city, targetCity) == 0) {
printf("Name: %s\n", people[i].name);
printf("City: %s\n", people[i].city);
printf("Age: %d\n", people[i].age);
printf("\n");
}
}
return 0;
}
Output:
Enter name, city, and age of person 1: wednesday
nyc
19
Enter name, city, and age of person 2: smith
nyc
20
Enter name, city, and age of person 3: william
nyc
25
Enter name, city, and age of person 4: brian
nyc
29
Enter name, city, and age of person 5: kumar
nyc
29
Enter name, city, and age of person 6: ben
nyc
25
Enter name, city, and age of person 7: odin
nyc
26
Enter name, city, and age of person 8: liam
nyc
26
Enter name, city, and age of person 9: gary
nyc
48
Enter name, city, and age of person 10: harry
nyc
29
Enter the city to search for: nyc
Name: wednesday
City: nyc
Age: 19
Name: smith
City: nyc
Age: 20
Name: william
City: nyc
Age: 25
Name: brian
City: nyc
Age: 29
Name: kumar
City: nyc
Age: 29
Name: ben
City: nyc
Age: 5
Name: odin
City: nyc
Age: 26
Name: liam
City: nyc
Age: 26
Name: gary
City: nyc
Age: 48
Name: harry
City: nyc
Age: 29
Pro-Tips💡
This program defines a struct called Person
that contains three fields: name
, city
, and age
.
An array of Person
structs called people
is used to store the personal information of 10 people.
The program then uses a for loop to read the personal information of each person and store it in the corresponding element of the people
array.
After reading the personal information, the program prompts the user to enter a city to search for.
It then uses another for loop to iterate through the people
array and check if the city of each person is equal to the target city.
If it is, the program prints the name, city, and age of that person.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.