IN JAVA
Write a program that, given an integer, inserts a '*' between adjacent digits that are both even and a '-' between adjacent digits that are both odd. Zero should not be considered even or odd.
EX:
12467930
Expected Output
12*4*67-9-30
import java.util.Scanner; public class InsertSymbols { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); String result = ""; int d1, d2; for (int i = 0; i < s.length(); i++) { result += s.charAt(i); if (i < s.length()-1) { d1 = s.charAt(i)-'0'; d2 = s.charAt(i+1)-'0'; if (d1%2 == 0 && d2%2 == 0) { result += '*'; } if (d1%2 == 1 && d2%2 == 1) { result += '-'; } } } System.out.println(result); } }
Get Answers For Free
Most questions answered within 1 hours.