📜  如何将两个整数值连接成一个?

📅  最后修改于: 2022-05-13 01:57:58.896000             🧑  作者: Mango

如何将两个整数值连接成一个?

给定两个整数 n1 和 n2,任务是将这两个整数连接成一个整数。
例子:

Input: n1 = 12, n2 = 34
Output: 1234

Input: n1 = 1, n2 = 93
Output: 193

方法:最简单的方法是:

  • 将两个数字都转换为字符串
  • 将两个字符串连接成一个,因为这比较容易
  • 将此连接的字符串转换回整数

程序:

C++
// CPP program to concatenate
// two integers into one
 
#include 
#include 
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)


C
// C program to concatenate
// two integers into one
 
#include 
#include 
#include 
 
// 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)


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));
    }
}


Python3
# Python3 program to concatenate
# two integers into one
 
# Function to concatenate
# two integers into one
def concat(a, b):
     
    # Convert both the integers to string
    s1 = str(a)
    s2 = str(b)
     
    # Concatenate both strings   
    s = s1 + s2
     
    # Convert the concatenated string
    # to integer
    c = int(s)
     
    # return the formed integer
    return c
 
# Driver code
 
a = 23
b = 43
 
print(concat(a, b))
 
# This code is contributed by shubhamsingh10


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 */


Javascript


输出:

2343