w3resource

C#: Insert the information of two books

C# Sharp STRUCTURE: Exercise-9 with Solution

Write a program in C# Sharp to insert two books' information.

Sample Solution:-

C# Sharp Code:

using System;

// Declaration of a structure 'book' to hold book information
struct book 
{
    public string title; // Field to store the title of the book
    public string author; // Field to store the author of the book
}

// Main class
public class strucExer9
{
    public static void Main ()  
    {
        int nobook = 1000; // Maximum number of books to be stored
        book[] books = new book[nobook]; // Array of books to store book information
        int i, j, n = 1, k = 1; // Variables for iteration and book count
        Console.Write("\n\nInsert the information of two books :\n");
        Console.Write("-----------------------------------------\n");

        // Loop to input information for 'n' books
        for (j = 0; j <= n; j++)
        {
            Console.WriteLine("Information of book {0}:", k);

            // Input the name of the book
            Console.Write("Input name of the book : ");
            books[j].title = Console.ReadLine();

            // Input the author of the book
            Console.Write("Input the author : ");
            books[j].author = Console.ReadLine();

            k++; // Increment the book count
            Console.WriteLine();
        }

        // Displaying the information of the entered books
        for (i = 0; i <= n; i++)
        {
            // Displaying the title and author of each book
            Console.WriteLine("{0}: Title = {1},  Author = {2}", i + 1, books[i].title, books[i].author);
            Console.WriteLine();
        }
    }
}

Sample Output:

Insert the information of two books :                    
-----------------------------------------                
Information of book 1 :                                  

Input name of the book : BASIC                           

Input the author : S.TROELSTRA                           
                                                         
Information of book 2 :  
                                
Input name of the book : C+ 
                             
Input the author : G.RTRTG                               

1: Title = BASIC,  Author = S.TROELSTRA                  
                                                         
2: Title = C+,  Author = G.RTRTG 

Flowchart:

Flowchart: Insert the information of two books.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to demonstrates struct initialization without using the new operator.
Next: Write a program in C# Sharp to implement a method that returns a structure including calling the method and using its value.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/csharp-exercises/structure/csharp-structure-exercise-9.php