Q:

C# program to implement phonebook

0

C# program to implement phonebook

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to create a phonebook in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to implement phonebook in C#.
using System;
using System.Collections;
class program {
  public static void Main() {
    Hashtable phoneBook = new Hashtable();

    while (true) {
      Console.WriteLine("\n#############PhoneBook###########");
      Console.WriteLine("\t1:Add number to phone book");
      Console.WriteLine("\t2:Get number to phone book");
      Console.WriteLine("\t3:Exit");
      Console.WriteLine("\n#################################");
      Console.WriteLine("\n\nEnter choice: ");
      int choice = Convert.ToInt32(Console.ReadLine());

      switch (choice) {
      case 1:
        {
          long number = 0;
          string name = "";

          Console.Write("Enter your name : ");
          name = Console.ReadLine();

          Console.Write("Enter your phone number : ");
          number = Convert.ToInt64(Console.ReadLine());

          phoneBook.Add(name, number);
        }
        break;
      case 2:
        {
          long number = 0;
          string name = "";

          Console.Write("Enter your name : ");
          name = Console.ReadLine();

          if (phoneBook[name] == null) {
            Console.WriteLine("Given name is not found in phonebook");
          }
          else {
            number = Convert.ToInt64(phoneBook[name]);
            Console.WriteLine("Name: " + name + ", phone number: " + number);
          }
        }
        break;
      case 3:
        {
          goto OUT;
        }
        break;
      default:
        {
          Console.WriteLine("\nYou have entered wrong choice");
        }
        break;
      }
    }

    OUT:
    Console.WriteLine("\nThankyou for using phonebook");
  }
}

Output:

#############PhoneBook###########
        1:Add number to phone book
        2:Get number to phone book
        3:Exit
#################################


Enter choice:
1
Enter your name : Arvind
Enter your phone number : 8860572892

#############PhoneBook###########
        1:Add number to phone book
        2:Get number to phone book
        3:Exit
#################################

Enter choice:
2
Enter your name : Arvind
Name: Arvind, phone number: 8860572892

#############PhoneBook###########
        1:Add number to phone book
        2:Get number to phone book
        3:Exit
#################################

Enter choice:
3

Thankyou for using phonebook
Press any key to continue . . .

Explanation:

In the above program, we created a phonebook using HashTable. Here we provide two options 1st to add name and phone number to the phone book and 2nd option is used to getting numbers from the phonebook.

As we know that we can store items in the hash table in the form key/value pair. Here we used the name as a key and phone number as the value in the phonebook hash table.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to terminate the current running progra... >>
<< C# program to take height as input and print its c...