-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelephoneNumber.java
More file actions
64 lines (63 loc) · 1.13 KB
/
TelephoneNumber.java
File metadata and controls
64 lines (63 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* Write a program that converts an integer input to a telephone number format.
For example:
Input: 1234567890
Output: (123) 456-7890
*/
import java.util.*;
public class TelephoneNumber{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("Enter the Number");
String str=in.nextLine();
char ch[]=str.toCharArray();
System.out.println("Telephone Number is:");
if(ch.length<=10)
{
for(int i=0;i<ch.length;i++)
{
if(i==0)
{
System.out.print("("+ch[i]);
}
else if(i==2)
{
System.out.print(ch[i]+") ");
}
else if(i==5)
{
System.out.print(ch[i]+"-");
}
else
{
System.out.print(ch[i]);
}
}
}
else
{
for(int i=0;i<ch.length;i++)
{
if(i==0)
{
System.out.print("(+"+ch[i]);
}
else if(i==2)
{
System.out.print(ch[i]+") ");
}
else if(i==5)
{
System.out.print("-"+ch[i]);
}
else if(i==8)
{
System.out.print("-"+ch[i]);
}
else
{
System.out.print(ch[i]);
}
}
}
}
}