You are on page 1of 12

Exercise 20 (Structure )

121. C Program to Store Information of a Student Using Structure


122. Store information of 10 students using structures
Program:
#include<stdio.h>
struct studentinfo
{
    char name[25];
    int rollno;
}s[10];
   int main(){
   int i;
  for (i = 0; i < 10; i++){
       s[i].rollno=i+1;
       printf("ROLL NUMBER %d\n",s[i].rollno);
       printf("ENTER THE NAME OF STUDENT : \n");
       scanf("%s",&s[i].name);
  }
 
printf("******************************************
***\n");
  printf("THE STUDENTS ARE : \n");
  for (i = 0; i < 10; i++){
   printf("ROLL NO : %d\n",s[i].rollno);
   printf("NAME OF STUDENT : %s\n",s[i].name);
  }
   return 0;
   }
Output:
ROLL NUMBER 1
ENTER THE NAME OF STUDENT :
a
ROLL NUMBER 2
ENTER THE NAME OF STUDENT :
b
ROLL NUMBER 3
ENTER THE NAME OF STUDENT :
c
ROLL NUMBER 4
ENTER THE NAME OF STUDENT :
d
ROLL NUMBER 5
ENTER THE NAME OF STUDENT :
e
ROLL NUMBER 6
ENTER THE NAME OF STUDENT :
f
ROLL NUMBER 7
ENTER THE NAME OF STUDENT :
g
ROLL NUMBER 8
ENTER THE NAME OF STUDENT :
h
ROLL NUMBER 9
ENTER THE NAME OF STUDENT :
i
ROLL NUMBER 10
ENTER THE NAME OF STUDENT :
j
*********************************************
THE STUDENTS ARE :
ROLL NO : 1
NAME OF STUDENT : a
ROLL NO : 2
NAME OF STUDENT : b
ROLL NO : 3
NAME OF STUDENT : c
ROLL NO : 4
NAME OF STUDENT : d
ROLL NO : 5
NAME OF STUDENT : e
ROLL NO : 6
NAME OF STUDENT : f
ROLL NO : 7
NAME OF STUDENT : g
ROLL NO : 8
NAME OF STUDENT : h
ROLL NO : 9
NAME OF STUDENT : i
ROLL NO : 10
NAME OF STUDENT : j
=============================================================
123. Write a program to create a structure named company which has name, address,
phone and noOfEmployee as member variables. Read name of company, its address,
phone and noOfEmployee. Finally display these members‟ value.
Program:
#include<stdio.h>
struct company
{
    char name[100];
    char address[200];
    int phone;
    int noofemployees;
}company[5];
int main()
{
int i;
   for (i = 0; i < 3; i++)
   {
       printf("company number %d\n", i+1);
       printf("Enter the name of the company : ");
       scanf("%s", &company[i].name);
       printf("Enter the address of the company :
");
       scanf("%s", &company[i].address);
       printf("Enter the phone number : ");
       scanf("%d", &company[i].phone);
       printf("Enter the no of employees : ");
       scanf("%d", &company[i].noofemployees);
   }
   printf("\n
*********************************************** \
n");
   for (i = 0; i < 3; i++)
   {
       printf("company number %d\n", i+1);
       printf("Company Name : %s\n",
company[i].name);
       printf("Address : %s\n",
company[i].address);
       printf("Phone Number : %d\n",
company[i].phone);
       printf("Number of employees : %d\n",
company[i].noofemployees);
       return 0;
   }
}
Output:
company number 1
Enter the name of the company : html
Enter the address of the company : .hpp
Enter the phone number : 123
Enter the no of employees : 5
company number 2
Enter the name of the company : c
Enter the address of the company : .c
Enter the phone number : 456
Enter the no of employees : 5
company number 3
Enter the name of the company : python
Enter the address of the company : .p
Enter the phone number : 789
Enter the no of employees : 5

***********************************************
company number 1
Company Name : html
Address : .hpp
Phone Number : 123
Number of employees : 5
|||
|||
|||
=============================================================
124. To perform the Addition, Subtraction, Multiplication and Divison operations
between complex number.
Program:
#include <stdio.h>
#include <stdlib.h>
struct complex
{
  int real, img;
};

int main()
{
  int choice, x, y, z;
  struct complex a, b, c;

  while(1)
  {
    printf("Press 1 to add two complex numbers.\n");
    printf("Press 2 to subtract two complex numbers.\n");
    printf("Press 3 to multiply two complex numbers.\n");
    printf("Press 4 to divide two complex numbers.\n");
    printf("Press 5 to exit.\n");
    printf("Enter your choice\n");
    scanf("%d", &choice);

    if (choice == 5)
      exit(0);

    if (choice >= 1 && choice <= 4)


    {
      printf("Enter a and b where a + ib is the first complex number.");
      printf("\na = ");
      scanf("%d", &a.real);
      printf("b = ");
      scanf("%d", &a.img);
      printf("Enter c and d where c + id is the second complex number.");
      printf("\nc = ");
      scanf("%d", &b.real);
      printf("d = ");
      scanf("%d", &b.img);
    }
    if (choice == 1)
    {
      c.real = a.real + b.real;
      c.img = a.img + b.img;

      if (c.img >= 0)
        printf("Sum of the complex numbers = %d + %di", c.real, c.img);
      else
        printf("Sum of the complex numbers = %d %di", c.real, c.img);
    }
    else if (choice == 2)
    {
      c.real = a.real - b.real;
      c.img = a.img - b.img;

      if (c.img >= 0)
        printf("Difference of the complex numbers = %d + %di", c.real, c.img);
      else
        printf("Difference of the complex numbers = %d %di", c.real, c.img);
    }
    else if (choice == 3)
    {
      c.real = a.real*b.real - a.img*b.img;
      c.img = a.img*b.real + a.real*b.img;

      if (c.img >= 0)
        printf("Multiplication of the complex numbers = %d + %di", c.real,
c.img);
      else
        printf("Multiplication of the complex numbers = %d %di", c.real, c.img);
    }
    else if (choice == 4)
    {
      if (b.real == 0 && b.img == 0)
        printf("Division by 0 + 0i isn't allowed.");
      else
      {
        x = a.real*b.real + a.img*b.img;
        y = a.img*b.real - a.real*b.img;
        z = b.real*b.real + b.img*b.img;

        if (x%z == 0 && y%z == 0)


        {
          if (y/z >= 0)
            printf("Division of the complex numbers = %d + %di", x/z, y/z);
          else
            printf("Division of the complex numbers = %d %di", x/z, y/z);
        }
        else if (x%z == 0 && y%z != 0)
        {
          if (y/z >= 0)
            printf("Division of two complex numbers = %d + %d/%di", x/z, y, z);
          else
            printf("Division of two complex numbers = %d %d/%di", x/z, y, z);
        }
        else if (x%z != 0 && y%z == 0)
        {
          if (y/z >= 0)
            printf("Division of two complex numbers = %d/%d + %di", x, z, y/z);
          else
            printf("Division of two complex numbers = %d %d/%di", x, z, y/z);
        }
        else
        {
          if (y/z >= 0)
            printf("Division of two complex numbers = %d/%d + %d/%di",x, z, y,
z);
          else
            printf("Division of two complex numbers = %d/%d %d/%di", x, z, y,
z);
        }
      }
    }
    else
      printf("Invalid choice.");

    printf("\nPress any key to enter choice again...\n");


  }
}
Output:
Press 1 to add two complex numbers.
Press 2 to subtract two complex numbers.
Press 3 to multiply two complex numbers.
Press 4 to divide two complex numbers.
Press 5 to exit.
Enter your choice
1
Enter a and b where a + ib is the first complex number.
a=2
b=5
Enter c and d where c + id is the second complex number.
c=5
d=2
Sum of the complex numbers = 7 + 7i
Press any key to enter choice again...
Press 1 to add two complex numbers.
Press 2 to subtract two complex numbers.
Press 3 to multiply two complex numbers.
Press 4 to divide two complex numbers.
Press 5 to exit.
Enter your choice
5
=============================================================
125. Create a function but make the argument the spelled-out name of the month
instead of the month number. Test the function in a simple program. Use the
following structure:
126. struct month months[12] ={ {"January", "jan", 31, 1}, {"February", "feb", 28, 2},
{"March", "mar", 31, 3}, {"April", "apr", 30, 4}, {"May", "may", 31, 5}, {"June",
"jun", 30, 6}, {"July", "jul", 31, 7}, {"August", "aug", 31, 8}, {"September", "sep",
30, 9}, {"October", "oct", 31, 10}, {"November", "nov", 30, 11}, {"December",
"dec", 31, 12}};
Program:
#include<stdio.h>
struct month
{
    char month_name[10];
    char month_abv[5];
    int days;
    int month_number;
};
int main()
{
    int i;
    struct month body[12]={
    "January", "jan", 31, 1,
    "February", "feb", 28, 2,
    "March", "mar", 31, 3,
    "April", "apr", 30, 4,
    "May", "may", 31, 5,
    "June", "jun", 30, 6,
    "July", "jul", 31, 7,
    "August", "aug", 31, 8,
    "September", "sep", 30, 9,
    "October", "oct", 31, 10,
    "November", "nov", 30, 11,
    "December", "dec", 31, 12};

    for (i=0; i<12; i++)


    {
        printf("Month name : %s\n",
body[i].month_name);
        printf("Month abbreviation : %s\n",
body[i].month_abv);
        printf("Days in the month : %d\n",
body[i].days);
        printf("Month of the year : %d\n",
body[i].month_number);
        printf("\n\n");
    }
    return 0;
}

Output:
Month name : January
Month abbreviation : jan
Days in the month : 31
Month of the year : 1

Month name : February


Month abbreviation : feb
Days in the month : 28
Month of the year : 2

Month name : March


Month abbreviation : mar
Days in the month : 31
Month of the year : 3

Month name : April


Month abbreviation : apr
Days in the month : 30
Month of the year : 4

Month name : May


Month abbreviation : may
Days in the month : 31
Month of the year : 5

Month name : June


Month abbreviation : jun
Days in the month : 30
Month of the year : 6

Month name : July


Month abbreviation : jul
Days in the month : 31
Month of the year : 7

Month name : August


Month abbreviation : aug
Days in the month : 31
Month of the year : 8

Month name : September


Month abbreviation : sep
Days in the month : 30
Month of the year : 9

Month name : October


Month abbreviation : oct
Days in the month : 31
Month of the year : 10

Month name : November


Month abbreviation : nov
Days in the month : 30
Month of the year : 11

Month name : December


Month abbreviation : dec
Days in the month : 31
Month of the year : 12
=============================================================

You might also like