How to concatenate two Integer values into one?
Given two integers n1 and n2, the task is to concatenate these two integers into one integer.
Example:
Input: n1 = 12, n2 = 34 Output: 1234 Input: n1 = 1, n2 = 93 Output: 193
Approach: The simplest approach to do this is:
- Convert both numbers to string
- Concatenate both strings into one, as this is comparatively easy
- Convert this concatenated string back to integer
Program:
C++
// CPP program to concatenate // two integers into one #include <iostream> #include <string> using namespace std; // Function to concatenate // two integers into one int concat(int a, int b) { // Convert both the integers to string string s1 = to_string(a); string s2 = to_string(b); // Concatenate both strings string s = s1 + s2; // Convert the concatenated string // to integer int c = stoi(s); // return the formed integer return c; } int main() { int a = 23; int b = 43; cout << concat(a, b) << endl; return 0; } // This code is contributed by Ayush Singla(@ayusin51) |
chevron_right
filter_none
C
// C program to concatenate // two integers into one #include <stdio.h> #include <string.h> #include <stdlib.h> // Function to concatenate // two integers into one int concat(int a, int b) { char s1[20]; char s2[20]; // Convert both the integers to string sprintf(s1, "%d", a); sprintf(s2, "%d", b); // Concatenate both strings strcat(s1, s2); // Convert the concatenated string // to integer int c = atoi(s1); // return the formed integer return c; } int main() { int a = 23; int b = 43; printf("%d\n", concat(a, b)); return 0; } // This code is contributed by Ayush Singla(@ayusin51) |
chevron_right
filter_none
Java
// Java program to concatenate // two integers into one public class GFG { // Function to concatenate // two integers into one static int concat(int a, int b) { // Convert both the integers to string String s1 = Integer.toString(a); String s2 = Integer.toString(b); // Concatenate both strings String s = s1 + s2; // Convert the concatenated string // to integer int c = Integer.parseInt(s); // return the formed integer return c; } public static void main(String args[]) { int a = 23; int b = 43; System.out.println(concat(a, b)); } } |
chevron_right
filter_none
C#
// C# program to concatenate // two integers into one using System; class GFG { // Function to concatenate // two integers into one static int concat(int a, int b) { // Convert both the integers to string String s1 = a.ToString(); String s2 = b.ToString(); // Concatenate both strings String s = s1 + s2; // Convert the concatenated string // to integer int c = int.Parse(s); // return the formed integer return c; } // Driver code public static void Main(String []args) { int a = 23; int b = 43; Console.WriteLine(concat(a, b)); } } /* This code contributed by PrinciRaj1992 */ |
chevron_right
filter_none
Output:
2343
Recommended Posts:
- C++ program to concatenate a string given number of times
- TCS Coding Practice Question | Concatenate 2 Strings
- Program to Convert Set of Integer to Array of Integer in Java
- Find minimum possible values of A, B and C when two of the (A + B), (A + C) and (B + C) are given
- Print intermediate values in an array
- Default array values in Java
- Returning Multiple values in Java
- How to Read and Print an Integer value in C
- How to Read and Print an Integer value in C++
- Count even and odd digits in an Integer
- How to assign values to variables in Python and other languages
- Convert a string to hexadecimal ASCII values
- How to Read and Print an Integer value in Java
- Count the number of holes in an integer
- Generate all unique partitions of an integer | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



