How can I solve this example in an easy way??

0 votes
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 20 in Java by anonymous
• 120 points
63 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 by Omkar
• 68,480 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,880 points
167 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,350 points
83 views
0 votes
3 answers

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

import java.util.*; public class AmstrongNumber { public static void ...READ MORE

answered Aug 6 in Java by anonymous
6,796 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,880 points
1,586 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 in Java by Daisy
• 8,050 points
5,640 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 41,000 points
218 views
0 votes
2 answers

How can I create File and write data in it using Java?

import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class WriteFiles{ ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
134 views
+15 votes
18 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 in Java by Suresh
• 700 points
95,778 views
+28 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,350 points
2,317 views