Object

Write a program to read data for a book and print it.

Algorithm

  1. Declare a structure with the tag book.
  2. Declare strings name and author, an integer variable pages and a float variable price as the members of structure.
  3. Declare a variable bk of data type struct book.
  4. Read structure members bk.name, bk.author, bk.pages and bk.price.
  5. Write structure members bk.name, bk.author, bk.pages and bk.price.
  6. Exit.

Flowchart

C++ Source Code

// program 97
#include<iostream>
#include <string>
using namespace std;

struct book
{
  string name, author;
  int pages;
  float price;
};

int main()
{
  book bk;
  cout << "Enter the name of the book: ";
  getline(cin, bk.name);
  cout << "Enter the name of author: ";
  getline(cin, bk.author);
  cout << "Enter the number of pages in the book: ";
  cin >> bk.pages;
  cout << "Enter the price of the book: Rs. ";
  cin >> bk.price;

  cout << "\nThe name of the book is: " << bk.name;
  cout << "\nThe name of author is: " << bk.author;
  cout << "\nThe number of pages in the book are: " << bk.pages;
  cout << "\nThe price of the book is: Rs. " << bk.price << endl;
  return 0;
}

C Source Code

/*program 97*/
#include<stdio.h>

int main()
{
  struct book
  {
   char name[50];
   char author[50];
   int pages;
   float price;
  };
  struct book bk;
  printf("Enter the name of the book: ");
  gets(bk.name);
  printf("Enter the name of author: ");
  gets(bk.author);
  printf("Enter the number of pages in the book: ");
  scanf("%d", &bk.pages);
  printf("Enter the price of the book: Rs. ");
  scanf("%f", &bk.price);

  printf("\nThe name of the book is: %s", bk.name);
  printf("\nThe name of author is: %s", bk.author);
  printf("\nThe number of pages in the book are: %d", bk.pages);
  printf("\nThe price of the book is: Rs. %.2f", bk.price);
  return 0;
}

Output

Enter the name of the book: Using Information Technology
Enter the name of author: Gul Shahzad Sarwar
Enter the number of pages in the book: 272
Enter the price of the book: Rs. 310.5

The name of the book is: Using Information Technology
The name of author is: Gul Shahzad Sarwar
The number of pages in the book are: 272
The price of the book is: Rs. 310.5

Design a site like this with WordPress.com
Get started