Problem-1: Write a program that read person name and telephone number.
Then input telephone no
and display name or vice-versa.
Theory:
We will have to use structure to store the name and associated phone number. Then access the
information from the structure and print them.
Source Code:
#include <stdio.h>
#include <string.h>
struct phonebook {
char name[30];
char phonenum[15];};
int main() {
struct phonebook a[100];
int i, n;
char ch, input[30];
printf("How many entries? ");
scanf("%d", &n);
getchar();
for (i = 0; i < n; i++) {
printf("Name: ");
fgets(a[i].name, sizeof(a[i].name), stdin);
printf("Phone Number: ");
fgets(a[i].phonenum, sizeof(a[i].phonenum), stdin);}
printf("Search by (n)ame or (p)honenumber? ");
scanf(" %c", &ch);
getchar();
if (ch == 'n' || ch == 'N') {
printf("Enter the name: ");
fgets(input, sizeof(input), stdin);
for (i = 0; i < n; i++) {
if (strcmp(input, a[i].name) == 0) {
printf("The phone number of %s is %s\n", a[i].name, a[i].phonenum);
break;}}
if (i == n) {
printf("Name not found in the phonebook.\n");}
}
else if (ch == 'p' || ch == 'P') {
printf("Enter the phone number: ");
fgets(input, sizeof(input), stdin);
for (i = 0; i < n; i++) {
if (strcmp(input, a[i].phonenum) == 0) {
printf("The phone number %s belongs to %s\n", a[i].phonenum, a[i].name);
break;} }
if (i == n) {
printf("Phone number not found in the phonebook.\n");}
} else {
printf("Invalid option selected.\n");}
return 0;}
Output:
Discussion: The name and numbers were taken as input to create the structure. Then by searching through the
structure, user could access the info either by name or phone number.
Problem-2: Write a program that reads some hotels name, address, grade, charge and room numbers,
then display all hotels information with given grade, and display all hotels information whose charge is
less than given charge.
Theory:
All the hotel information will be taken and stored in a structure. Then hotel grade will be taken input
for which all the hotel of that particular grade will be shown with every information. User can also input the
maximum charge. Output will be all the hotel under that charge.
Source Code:
#include <stdio.h>
#include <string.h>
struct Hotel {
char name[30];
char address[70];
char grade;
float charge;
int room_numbers;};
int main() {
struct Hotel hotels[100];
int n, i;
char grade;
float maximum_charge;
printf("Enter the number of hotels: ");
scanf("%d", &n);
getchar();
for (i = 0; i < n; i++) {
printf("\nEnter details for hotel %d:\n", i + 1);
printf("Name: ");
fgets(hotels[i].name, sizeof(hotels[i].name), stdin);
printf("Address: ");
fgets(hotels[i].address, sizeof(hotels[i].address), stdin);
printf("Grade (A-E): ");
scanf(" %c", &hotels[i].grade);
printf("Charge per night: ");
scanf("%f", &hotels[i].charge);
printf("Number of rooms: ");
scanf("%d", &hotels[i].room_numbers);
getchar();}
printf("\nEnter the hotel grade (A-E): ");
scanf(" %c", &grade);
printf("\nHotels with grade %c:\n", grade);
for (i = 0; i < n; i++) {
if (hotels[i].grade == grade) {
printf("Name: %s, Address: %s, Charge: %.2f, Rooms: %d\n", hotels[i].name, hotels[i].address,
hotels[i].charge, hotels[i].room_numbers);}
}
printf("\nEnter the maximum charge: ");
scanf("%f", &maximum_charge);
printf("\nHotels with charge less than %.2f:\n", maximum_charge);
for (i = 0; i < n; i++) {
if (hotels[i].charge < maximum_charge) {
printf("Name: %s, Address: %s, Grade: %c, Rooms: %d\n", hotels[i].name, hotels[i].address,
hotels[i].grade, hotels[i].room_numbers);}
}
return 0;}
Output:
Discussion:
User can search for hotels both by grade or maximum price. If grade is given as input, all the particular
hotels of that grade will be shown with every detail. If maximum price is given as input, hotels regardless of
their grade, will be shown whose price is less than the maximum price.