Computer Science, asked by AyushiKala, 9 months ago

Q2. Accept a number. Find the square of that number, split the square into two parts such that the left part must have same number of digits as the number and right part can have equal or more, display the sum of both parts
ex - if input is 65
square = 4225
output : 67 (42+25)
java programming loop​

Answers

Answered by CoderRishav
0

Answer:

import java.util.*;

class no

{

public static void main()

{

Scanner in = new Scanner(System.in);

System.out.println("Enter a number - ");

int n = in.nextInt();

int sq = n * n;

String h = Integer.toString(sq);

int len = h.length();

int div = 0;

double part = 0.0;

int p1 = 0;

int h = 0;

int m = 0;

int p2 = 0;

String gk = "";

int L = 0;

String con = "";

String con1 = "";

String sum = "";

int sum1 = 0;

if(len %2 == 0)

{

div = len / 2;

part = Math.pow(10,div);

p1 = sq % (int)part;

p2 = sq / (int)part;

System.out.println("Output is "+(p1+p2));

}

else

{

h = sq % 10;

m = sq / 10;

gk =Integer.toString(m) ;

L = gk.length();

div = L / 2;

part = Math.pow(10,div);

p2 = m / (int)part;

p1 = m % (int)part;

con =Integer.toString(p2) ;

con1 = Integer.toString(h);

sum = con + con1;

sum1 = Integer.parseInt(sum);

System.out.println("Output is "+(p1+sum1) );

}

}

}

Explanation:

First we have taken a number;

then we have make the square of number in String;

then we take length;

then we see if length is divisible by 2 it means we can take two equal no. of no. from both side ;

then we add both

if the length is odd we first make it even by removing last, but if it's even I give the last number at right side.

please analize this code.

Question is interesting and nice

Plz bro mark as brainlist and please vote it because we take Time to make logic :)

Similar questions