How can I solve this example in an easy way

+1 vote
Write a program that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to the-last digit of x followed by the second-to-the-last digit of y. And so on.
Sep 21, 2019 in Java by anonymous
• 130 points
858 views

1 answer to this question.

0 votes

You can use this code:

import java.util.*;

public class sample{

public static void main(String args[])

{

System.out.println("Enter value of X");

Scanner in = new Scanner(System.in);

String x = in. nextLine();

int x_int = Integer.parseInt(x);

int length_x = String.valueOf(x).length();

System.out.println("Enter value of Y");

String y = in. nextLine();

int y_int = Integer.parseInt(y);

int length_y = String.valueOf(y).length();

int result=0;

int temp;

int min;

if(y_int>x_int) {

min=length_x;

}

else {

min=length_y;

}


for(int i=1;i<=min;i++)

{

int dig = String.valueOf(result).length();

temp=y_int%10;

for (int j=0;j<dig;j++)

{

temp=temp*10;

}

result = result+temp;

y_int=y_int/10;

dig = String.valueOf(result).length();

temp=x_int%10;

for (int j=0;j<dig;j++)

{

temp=temp*10;

}

result = result+temp;

x_int=x_int/10;



}

if(y_int>x_int) {

for(int i=1;i<=length_y-min;i++)

{

int dig = String.valueOf(result).length();

temp=y_int%10;

for (int j=0;j<dig;j++)

{

temp=temp*10;

}

result = result+temp;

y_int=y_int/10;

}

}

else if(y_int<x_int){

for(int i=1;i<=length_x-min;i++)

{

int dig;

dig = String.valueOf(result).length();

temp=x_int%10;

for (int j=0;j<dig;j++)

{

temp=temp*10;

}

result = result+temp;

x_int=x_int/10;

}

}

System.out.println("Result = "+result/10);

}

}
answered Sep 23, 2019 by Omkar
• 69,220 points

Related Questions In Java

0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,920 points
13,313 views
0 votes
2 answers
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,370 points
880 views
+1 vote
6 answers

How can I separate the digits of an int number in Java?

You can also have a look here: To ...READ MORE

answered Dec 9, 2020 in Java by Roshni
• 10,480 points
210,169 views
0 votes
2 answers

How can I solve java.lang.NoClassDefFoundError in Java?

NoClassDefFoundError means that the class is present ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,920 points
34,139 views
0 votes
1 answer

How can I solve this: To prevent a memory leak, the JDBC Driver has been forcibly unregistered

What can you do? Ignore these warnings. Tomcat ...READ MORE

answered Jan 8, 2019 in Java by Daisy
• 8,140 points
31,626 views
0 votes
0 answers

How can I validate an email address in JavaScript?

To avoid the most basic mistyping, I'd ...READ MORE

Sep 20, 2022 in Java by Nicholas
• 7,760 points
462 views
0 votes
0 answers

How can I find the prime factors of an integer in JavaScript?

Using a for loop in javascript, I was attempting to determine the prime factors of a number, denoted as 'integer' below.  I can't seem to get it to work, and I'm not sure if it's because of my JavaScript or my calculating logic. //integer is the value for which we ...READ MORE

Dec 12, 2022 in Java by Nicholas
• 7,760 points
1,039 views
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,783 views
+29 votes
3 answers

How can we set java.library.path in Eclipse?

Go into the library settings for your ...READ MORE

answered Jul 3, 2018 in Java by sharth
• 3,370 points
22,454 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP