Concatenation of number in java

 Concatenation of number

Steps to do this:-
1. take 2 integers a and b for two numbers and take 3 strings s1 ,s2 and s3.

2. scan two numbers

now we have to convert the number into a string so it will be easy to concatenate the numbers 

3. convert 1st number into a string, do this
s1=Integer.toString(a);

toString() is used to convert anything in string .
 do this step with b

s2=Integer.toString(b);

now our two integer value is converted into a string so now do the concatenation process 
s3=s1+s2;

s3 stores the concatenation value.
but now the value is in string format but we want this is an integer format so we just convert the string into a integer. to do this we will use parseInt() method it will bring back the integer, from string .
if we use parseFloat() the answer will be float format

do this.
int c=Integer.parseInt(s3);

4. print c 
5.save and run

The Source Code

import java.util.Scanner;
 class conca
 {
     public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int a,b;
        System.out.println("enter the 1st number");
        a=s.nextInt();
        System.out.println("enter the 2nd number");
        b=s.nextInt();
      
        String s1=Integer.toString(a);
        String s2=Integer.toString(b);
      
        String s3=s1+s2;
      
        int c=Integer.parseInt(s3);
      
        System.out.println("the concatenation is: "+s3);
    }
 }
                                        OUTPUT



Watch My Video





Comments

Popular Posts