Exercise 21 ByHarshKumar
Exercise 21 ByHarshKumar
126. Write a program to add two distances in inch-feet using structure. The values
of the distances is to be taken from the user.
Program:
#include <stdio.h>
struct Distance {
int feet;
float inch;
} d1, d2, result;
int main() {
printf("Enter 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
Output:
output:
Enter 1st distance
Enter feet: 5
Enter inch: 2
127. Write a 'C' program to accept customer details such as: Account_no, Name,
Balance using structure. Assume 3 customers in the bank. Write a function to print
the account no. and name of each customer whose balance < 100 Rs.
Program:
#include<stdio.h>
struct bank{
int AccountNo;
char Name[50];
int Balance;
};
int main(){
struct bank emp[3];
printf("~~~~~~~ Enter the details ~~~~~~~ \n");
for(int i=0; i<3;i++){
printf(" \n\n enter the acc_no,name,balance\n\n");
printf("\nenter the account no\n");
scanf("%d",&emp[i].AccountNo);
printf("\n enter the name\n");
scanf("%s",emp[i].Name);
printf("\n enter the balance\n");
scanf("%d",&emp[i].Balance);
}
for(int i=0;i<3;i++){
if(emp[i].Balance < 100)
{
printf("\n---------------------------------------------\n");
printf("\n\n The acc_no,name,balance below RS:-100\n\n");
printf("\n the account no:-%d\n",emp[i].AccountNo);
printf("\n the name:-%s\n",emp[i].Name);
printf("\n the balance:-%d\n",emp[i].Balance);
}
}
return 0;
}
Output:
~~~~~~~ Enter the details ~~~~~~~
enter the acc_no,name,balance
---------------------------------------------
the name:-parsar
the balance:-90
---------------------------------------------
The acc_no,name,balance below RS:-100
the name:-harsh
the balance:-50
128. Write a structure to store the names, salary and hours of work per day of 10
employees in a company. Write a program to increase the salary depending on the
number of hours of work per day as follows and then print the name of all the
employees along with their final salaries.
Output:
----------------------------------------------------------
Name : Aditya
Salary : 950$
Working Hours : 8hr
----------------------------------------------------
Name : Raj
Salary : 500$
Working Hours : 10hr
----------------------------------------------------
Name : Avnish
Salary : 850$
Working Hours : 12hr
----------------------------------------------------
130. Create a structure named Date having day, month and year as its elements.
Store the current date in the structure. Now add 45 days to the current date and
display the final date.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Assume
{
int day;
int month;
int year;
};
int main()
{
struct Assume date;
printf("Enter the Day of Date : ");
scanf("%d", &date.day);
while (date.day > 31)
{
printf("The days can't be more than 31 : ");
scanf("%d", &date.day);
}
// printf("%d",date.day);
printf("Enter the Month of the Date : ");
scanf("%d", &date.month);
while (date.month > 12)
{
printf("The days can't be more than 31 : ");
scanf("%d", &date.month);
}
printf("ENter the year of the date : ");
scanf("%d", &date.year);
printf("\n\n\t\tOld Date: %d-%d-%d\n", date.day, date.month,
date.year);
if (date.month == 2)
{
date.month++;
int oldDate2 = 45 - (28 - date.day);
date.day = oldDate2;
}
else
{
if (1)
{
if (date.month == '1'|| '3' || '5' || '7' ||'8'|| '10'
|| '12')
{
date.month++;
int oldDate = 45 - (31 - date.day);
if(oldDate > 30){
date.month++;
oldDate = oldDate - 30;
date.day = oldDate;
}else{
date.day = oldDate;
}
}
// }
}
else
{
date.day = oldDate1;
}
}
}
}
printf("\t\tNew Date: %d-%d-%d", date.day, date.month,
date.year);
}
Output:
Enter the Day of Date : 18
Enter the Month of the Date : 2
ENter the year of the date : 2022
131. Let us work on the menu of a library. Create a structure containing book
information like accession number, name of author, book title and flag to know
whether book is issued or not. Create a menu in which the following can be done. 1
- Display book information 2 - Add a new book 3 - Display all the books in the
library of a particular author 4 - Display the number of books of a particular title 5
- Display the total number of books in the library 6 - Issue a book (If we issue a
book, then its number gets decreased by 1 and if we add a book, its number gets
increased by 1)
Program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct book
{
int b_no;
char b_name[100];
char b_author[100];
int no_pages;
};
int main()
{
struct book b[20];
int ch,n,i,count = 0;
char temp[40];
do
{
printf("\t\tMENU");
printf("\n-------------------------------------\n");
printf("PRESS 1.TO ADD BOOK DETAILS.");
printf("\nPRESS 2.TO DISPLAY BOOK DETAILS.");
printf("\nPRESS 3.TO DISPLAY BOOK OF GIVEN AUTHOR.");
printf("\nPRESS 4.TO COUNT NUMBER OF BOOKS.");
printf("\nPRESS 5.TO EXIT.");
printf("\n-------------------------------------\n");
printf("Enter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nHow Many Records You Want to Add: ");
scanf("%d",&n);
printf("-------------------------------------\n");
printf("Add Details of %d Book\n",n);
printf("-------------------------------------\n");
for(i = 0 ; i < n ; i++)
{
printf("Enter Book No. : ");
scanf("%d",&b[i].b_no);
printf("Book Name : ");
scanf("%s",&b[i].b_name);
printf("Enter Author Name : ");
scanf("%s",&b[i].b_author);
printf("Enter No. of Pages : ");
scanf("%d",&b[i].no_pages);
printf("-------------------------------------\
n");
}
break;
case 2:
printf("\n\t\tDetails of All Book");
printf("\
n-----------------------------------------------------------\n");
printf("Book No. Book Name\t Author Name\tNo. of
Pages");
printf("\
n------------------------------------------------------------");
printf("\
n------------------------------------------------------------");
for( i = 0 ; i < n ; i++)
{
printf("\n %d\t %s\t %s\t
%d",b[i].b_no,b[i].b_name,b[i].b_author,b[i].no_pages);
}
printf("\n\n");
break;
case 3:
printf("\nEnter Author Name: ");
scanf("%s",temp);
printf("--------------------------------------");
for( i = 0 ; i < n ; i++)
{
if(strcmp(b[i].b_author,temp) == 0)
{
printf("\n%s\n",b[i].b_name);
}
}
break;
case 4 :
for( i = 0 ; i < n ; i++)
{
count++;
}
printf("\nTotal Number of Books in Library : %d\
n",count);
printf("-----------------------------------------\
n");
break;
case 5 :
exit(0);
}
}while(ch != 5);
return 0;
}
Output:
MENU
-------------------------------------
PRESS 1.TO ADD BOOK DETAILS.
PRESS 2.TO DISPLAY BOOK DETAILS.
PRESS 3.TO DISPLAY BOOK OF GIVEN AUTHOR.
PRESS 4.TO COUNT NUMBER OF BOOKS.
PRESS 5.TO EXIT.
-------------------------------------
Enter Your Choice: 1