The program must accept two integers A and B as the input. The task is to combine the numbers by alternating the digits. If one number runs out of digits, append the remaining digits from the other number. The program should then print the resulting combined number.
Boundary Condition(s):
1 <= A, B <= 10^9
Input Format:
The first line contains the number A.
The second line contains the number B.
Output Format:
The first line contains the combined number.
Example Input/Output 1:
Input:
123
456
Output:
142536
Explanation:
The digits from numbers 123 and 456 are combined alternately to form the number 142536.
Example Input/Output 2:
Input:
12
3456
Output:
132456
Explanation:
The digits from numbers 12 and 3456 are combined alternately. After the number 12 runs out of digits, the remaining digits from 3456 are appended to form the number 132456.
a, b = input().strip(), input().strip()
result = []
i, j = 0, 0
while i < len(a) or j < len(b):
if i < len(a):
result.append(a[i])
i += 1
if j < len(b):
result.append(b[j])
j += 1
print("".join(result))
#include<stdio.h>
#include<string.h>
int main() {
char a[12], b[12];
scanf("%s", a);
scanf("%s", b);
int i = 0, j = 0;
while (i < strlen(a) || j < strlen(b)) {
if (i < strlen(a)) {
printf("%c", a[i++]);
}
if (j < strlen(b)) {
printf("%c", b[j++]);
}
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string a, b;
cin >> a >> b;
int i = 0, j = 0;
while (i < a.length() || j < b.length()) {
if (i < a.length()) {
cout << a[i++];
}
if (j < b.length()) {
cout << b[j++];
}
}
return 0;
}
import java.util.Scanner;
public class CombineNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
int i = 0, j = 0;
StringBuilder result = new StringBuilder();
while (i < a.length() || j < b.length()) {
if (i < a.length()) {
result.append(a.charAt(i++));
}
if (j < b.length()) {
result.append(b.charAt(j++));
}
}
System.out.println(result);
}
}