Friday, October 4, 2013

Free calls . Java (figuring out if a number is a 1800, 855, 866, 877, 877 number)

FreeCalls.java
 1 /*
 2 Toll-Free numbers have become so popular that the 800 code prefix has run out of available numbers, so additional 3
 3 digit prefixes have been added (855, 866, 877, 888). Create a program that accepts a 10 - digit phone number 
 4 (no 1 in front) and tells the user if the message is a toll - free number. 
 5 
 6 Example:
 7 Input: 8001234567
 8 Output: (800) 123-4567 is a toll - free number
 9 
10 Programming Tip
11 
12 Scan the input in using a long integer, than use the operators / and % to break the number into individual sections
13 */
14 
15 package chapter4;
16 
17 import java.util.Scanner;
18 
19 public class FreeCalls {
20 
21         public static void main(String[] args) {
22                 
23                 Scanner scan = new Scanner(System.in);
24                 
25                 long phoneNumber, prefix, firstPart, secondPart;
26                 
27                 System.out.print("Enter a 10 digit phone number (no 1 in front): ");
28                 phoneNumber = scan.nextLong();
29                 
30                 prefix = phoneNumber / 10000000;
31                 firstPart = (phoneNumber % 10000000) / 10000;
32                 secondPart = phoneNumber % 10000;
33                 
34                 if(prefix == 855 || prefix == 866 || prefix == 877 || prefix == 888 || prefix == 800)
35                 {
36                         System.out.println("("+ prefix +") " + firstPart +"-" + secondPart + " is a toll - free number!");                      
37                 }
38                 
39                 else
40                 {
41                         System.out.println("("+ prefix +") " + firstPart +"-" + secondPart + " is not a toll - free number!");
42                 }
43         }
44 }
45 

1 comment: