Hammad Hammad - 3 months ago 5
Java Question

Java every number in new line

I want to make a program that take 2 numbers input and sum it where every sum number show in new line .. Like ,

100 + 200 answer should be look like this

3

0

0

every sum number show in new line ..

int n,i=0,j,a=323,b=322;

int s[];

n=a+b;

while(n>9)

{

s[i]=n%10;

i++;

s[i]=n/10;

if(s[i]>9)

{

n=s[i];

}

}

j=i;

for(j=i;j>=0;j--)

{

System.out.println(+s[j]);

}
}

Well it is not working but I think it may work like this

Answer

With java 8 you can use

public void printSumVertical(int a, int b) {
    String.valueOf(a + b).chars().forEach(c -> System.out.println((char) c));
}

Note that adding ints may overflow and for negative results the - will be in an extra line, too

Comments