Concatenate Integers

Last Updated : 17 Sep, 2026

Given two integers a and b, you need to concatenate them so the output is ab and print it.

Examples:

Input: a = 5, b = 6
Output: 56
Explanation: Concatenate them.

Input: a = 1, b = 9
Output: 19
Explanation: Concatenate them.

Try It Yourself
redirect icon

String Concatenation - O(d) Time and O(d) Space

The idea is to convert both integers into strings, concatenate the strings, and convert the resulting string back into an integer.

Language-specific built-in functions

C++:

  • to_string(a) -> converts integer to string
  • stoi(s) -> converts string to integer

Java:

  • String.valueOf(a) -> converts integer to string
  • Integer.parseInt(s) -> converts string to integer

Python:

  • str(a) -> converts integer to string
  • int(s) -> converts string to integer

C#:

  • a.ToString() -> converts integer to string
  • int.Parse(s) -> converts string to integer

JavaScript:

  • String(a) -> converts integer to string
  • Number(s) -> converts concatenated string back to number

Let us understand with an example:
Input: a = 5, b = 6

  • Convert a and b to strings -> "5" and "6".
  • Concatenate both strings -> s = "56".
  • Convert the string "56" back to an integer -> res = 56.
  • Print res, so the output is 56.
C++
#include <iostream>
#include <string>
using namespace std;

int main()
{

    int a = 5, b = 6;

    // Convert integers to strings and concatenate them
    string s = to_string(a) + to_string(b);

    // Convert the concatenated string back to integer
    int res = stoi(s);

    // Print the result
    cout << res << endl;

    return 0;
}
Java
import java.util.*;

public class GFG {
    public static void main(String[] args)
    {
        int a = 5, b = 6;

        // Convert integers to strings and concatenate them
        String s
            = Integer.toString(a) + Integer.toString(b);

        // Convert the concatenated string back to integer
        int res = Integer.parseInt(s);

        // Print the result
        System.out.println(res);
    }
}
Python
if __name__ == "__main__":
    a = 5
    b = 6

    # Convert integers to strings and concatenate them
    s = str(a) + str(b)

    # Convert the concatenated string back to integer
    res = int(s)

    print(res)
C#
using System;

class GFG {
    static void Main()
    {
        int a = 5, b = 6;

        // Convert integers to strings and concatenate them
        string s = a.ToString() + b.ToString();

        // Convert the concatenated string back to integer
        int res = int.Parse(s);

        // Print the result
        Console.WriteLine(res);
    }
}
JavaScript
// Convert integers to strings and concatenate them
let a = 5, b = 6;

let s = a.toString() + b.toString();

// Convert the concatenated string back to integer
let res = parseInt(s);

// Print the result
console.log(res);

Output
56

Direct Printing - O(d) Time and O(1) Space

The idea is to directly print a and b one after another without adding any separator.

Working of Approach:

  • Take two integers a and b as input.
  • Print a first.
  • Immediately print b after a.
  • Since there is no space or newline between them, they appear concatenated.

Let us understand with an example:
Input: a = 5, b = 6

  • Read the input values -> a = 5, b = 6.
  • cout << a prints 5.
  • cout << "" prints nothing, so there is no space between 5 and 6.
  • cout << b prints 6 immediately after 5.
  • Therefore, the output is 56.
C++
#include <iostream>
using namespace std;

int main()
{
    int a = 5, b = 6;

    // Print a and b one after another
    cout << a << "" << b << endl;

    return 0;
}
Java
public class GFG {
    public static void main(String[] args)
    {
        int a = 5, b = 6;

        // Print a and b one after another
        System.out.print(a + " " + b);
    }
}
Python
if __name__ == "__main__":
    a = 5
    b = 6

    # Print a and b one after another
    print(str(a) + '' + str(b))
C#
using System;

class GFG {
    static void Main()
    {
        int a = 5, b = 6;

        // Print a and b one after another
        Console.Write(a + " " + b);
    }
}
JavaScript
let a = 5, b = 6;

// Print a and b one after another
console.log(a + ''+ b);

Output
56
Comment