0% found this document useful (0 votes)
50 views

Exercise 21 ByHarshKumar

The document contains sample code for several programs using C structures: 1. A program that adds two distances in inch-feet units by defining a structure to store feet and inches and getting user input. 2. A program that stores customer details like account number, name, and balance in a structure and prints accounts with a balance under 100. 3. A program that increases employee salaries based on hours worked by storing employee data in a structure.

Uploaded by

Harsh kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Exercise 21 ByHarshKumar

The document contains sample code for several programs using C structures: 1. A program that adds two distances in inch-feet units by defining a structure to store feet and inches and getting user input. 2. A program that stores customer details like account number, name, and balance in a structure and prints accounts with a balance under 100. 3. A program that increases employee salaries based on hours worked by storing employee data in a structure.

Uploaded by

Harsh kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

EXERCISE 21(STRUCTURE)

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);

   printf("\nEnter 2nd distance\n");


   printf("Enter feet: ");
   scanf("%d", &d2.feet);
   printf("Enter inch: ");
   scanf("%f", &d2.inch);
   
   result.feet = d1.feet + d2.feet;
   result.inch = d1.inch + d2.inch;

   while (result.inch >= 12.0) {


      result.inch = result.inch - 12.0;
      ++result.feet;
   }
   printf("\nSum of distances = %d\'-%.1f\"", result.feet,
result.inch);
   return 0;
}

Output:
output:
Enter 1st distance
Enter feet: 5
Enter inch: 2

Enter 2nd distance


Enter feet: 2    
Enter inch: 5

Sum of distances = 7'-7.0”

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

enter the account no


234657544

enter the name


parsar

enter the balance


90

enter the acc_no,name,balance

enter the account no


895565869

enter the name


anupam

enter the balance


600

enter the acc_no,name,balance

enter the account no


34657854644

enter the name


harsh

enter the balance


50

---------------------------------------------

The acc_no,name,balance below RS:-100

the account no:-234657544

the name:-parsar

the balance:-90

---------------------------------------------
The acc_no,name,balance below RS:-100

the account no:-298116276

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.

Hours of work per day 8 10 >=12


Increase in salary $50 $100 $150
Program:
#include<stdio.h>
struct Emp{
    char names[50];
    int salary;
    int hours;
};
int main(){
    struct Emp employee[5]={
        "Parasar",400,5,
        "Harsh",500,6,
        "Aditya",900,8,
        "Raj",400,10,
        "Avnish",700,12
        };
       
    printf("----------------------------------------------------------\n");
    for(int i=0;i<5;i++){
     
            if (employee[i].hours == 8)
            {
                employee[i].salary = employee[i].salary + 50 ;
                printf("Name : %s\nSalary :  %d$\nWorking Hours : %dhr\
n",employee[i].names,employee[i].salary, employee[i].hours);  
                printf("\
n----------------------------------------------------\n");          
            }
            if (employee[i].hours == 10)
            {
                employee[i].salary = employee[i].salary + 100;
                printf("Name : %s\nSalary :  %d$\nWorking Hours : %dhr\
n",employee[i].names,employee[i].salary, employee[i].hours);  
                printf("\
n----------------------------------------------------\n");          
            }
            if (employee[i].hours >=12)
            {
                employee[i].salary = employee[i].salary + 150;
                printf("Name : %s\nSalary :  %d$\nWorking Hours : %dhr\
n",employee[i].names,employee[i].salary, employee[i].hours);  
                printf("\
n----------------------------------------------------\n");          
            }
           
    }
            return 0;
}

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
        {
           

                if (date.month == '4' || '6' || '9' || '11')


                {
                    date.month++;
                    int oldDate1 = 45 - (30 - date.day);
                    if(oldDate1 >=31 ){
                        date.month++;
                        oldDate1 = oldDate1 - 31;
                        date.day = oldDate1;
                    }
                    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

                Old Date: 18-2-2022


                New Date: 4-4-2022
PS D:\BVUCOEP\P&PC\Practicals\Run time

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

How Many Records You Want to Add: 2


-------------------------------------
Add Details of 2 Book
-------------------------------------
Enter Book No.     : 01
Book Name          : c++
Enter Author Name  : Yashwant
Enter No. of Pages : 250
-------------------------------------
Enter Book No.     : 02
Book Name          : physicx
Enter Author Name  : HC Verma
Enter No. of Pages : -------------------------------------
                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:
How Many Records You Want to Add: -------------------------------------
Add Details of 2 Book
-------------------------------------
Enter Book No.     : Book Name          : Enter Author Name  : 01    
Enter No. of Pages : 202
-------------------------------------
Enter Book No.     : 23
Book Name          : mdlms
Enter Author Name  : MLad
Enter No. of Pages : 205
-------------------------------------
                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: 2

                Details of All Book


-----------------------------------------------------------
Book No.   Book Name      Author Name   No. of Pages
------------------------------------------------------------
------------------------------------------------------------
 1        Verma   01      202
 23       mdlms   MLad    205

You might also like