Friday, November 15, 2013

US Telephone Keypads


Given the following information about a US telephone touch tone keypad:

   1: (NONE)         2: A,B,C        3: D,E,F
     4: G,H,I              5: J,K,L         6: M,N,O
     7: P,R,S              8: T,U,V        9: W,X,Y

calculate the product of each characters value.

As an example, say the user enters: "Practice", the product would be:
7 * 7 * 2 * 2 * 8 * 4 * 2 * 3 = 37,632

What is the value of this string: "Programming Challenges are fun"?

Language C++:

#include<iostream>
#include<string.h>
using namespace std;

class uskeypad {
 
    private:
           string my_string;
           long long int prod;
    public:
  
           uskeypad() { 
                  prod = 1;
                  my_string = "Programming challenges are fun";
           }
           void calc_prod() {
   
                  int i;
                  int len = my_string.length();
                  for (i = 0 ; i < len ; i++) {
    
                            if(my_string[i] == ' ') {
                                      prod = prod * 1;
                            }
                            else if(
                                  my_string[i] == 'a' || 
                                  my_string[i] == 'b' || 
                                  my_string[i] == 'c' || 
                                  my_string[i] == 'A' || 
                                  my_string[i] == 'B' ||
                                  my_string[i] == 'C' 
                                  ) {
     
                                           prod = prod * 2;
                             }
                             else if(
                                  my_string[i] == 'd' || 
                                  my_string[i] == 'e' || 
                                  my_string[i] == 'f' || 
                                  my_string[i] == 'D' || 
                                  my_string[i] == 'E' ||
                                  my_string[i] == 'F' 
                                    ) {
                                           prod = prod * 3;
                             }
                             else if(
                                  my_string[i] == 'g' || 
                                  my_string[i] == 'h' || 
                                  my_string[i] == 'i' || 
                                  my_string[i] == 'G' || 
                                  my_string[i] == 'H' ||
                                  my_string[i] == 'I' 
                                    ) { 
                                           prod = prod * 4;
                             }
                             else if(
                                  my_string[i] == 'j' || 
                                  my_string[i] == 'k' || 
                                  my_string[i] == 'l' || 
                                  my_string[i] == 'J' || 
                                  my_string[i] == 'K' ||
                                  my_string[i] == 'L' 
                                   ) { 
                                           prod = prod * 5;
                             }
                             else if(
                                  my_string[i] == 'm' || 
                                  my_string[i] == 'n' || 
                                  my_string[i] == 'o' || 
                                  my_string[i] == 'M' || 
                                  my_string[i] == 'N' ||
                                  my_string[i] == 'O' 
                                   ) {
                                           prod = prod * 6;
                             }
                             else if(
                                  my_string[i] == 'p' || 
                                  my_string[i] == 'r' || 
                                  my_string[i] == 's' || 
                                  my_string[i] == 'P' || 
                                  my_string[i] == 'R' ||
                                  my_string[i] == 'S' 
                                   ) { 
                                           prod = prod * 7;
                             }
                             else if(
                                  my_string[i] == 't' || 
                                  my_string[i] == 'u' || 
                                  my_string[i] == 'v' || 
                                  my_string[i] == 'T' || 
                                  my_string[i] == 'U' ||
                                  my_string[i] == 'V' 
                                   ) {
                                           prod = prod * 8;
                             }
                             else if(
                                   my_string[i] == 'w' || 
                                   my_string[i] == 'x' || 
                                   my_string[i] == 'y' || 
                                   my_string[i] == 'W' || 
                                   my_string[i] == 'X' ||
                                   my_string[i] == 'Y' 
                                    ) { 
                                           prod = prod * 9;
                              }
                  }
                  cout<<"prod = "<<prod<<endl;
   
        }
};
int main() {

       uskeypad obj;
       obj.calc_prod();
       return(0); 
}

Output:
prod = 208129028102553600

By +Dixit Singla 
Post a Comment