Write a program that prompts the user to enter his or her first and last name (assigned to two different variables) and then joins them together in a single string (concatenation).

Solution

The program is shown here.

PHP

<?php
  echo "Enter first name: ";
  $first_name = trim(fgets(STDIN));
  echo "Enter last name: ";
  $last_name = trim(fgets(STDIN));
  
  $full_name = $first_name . " " . $last_name;
  echo $full_name;
?>

Java

public static void main(String[] args) throws java.io.IOException {
  java.io.BufferedReader cin = new java.io.
          BufferedReader(new java.io.InputStreamReader(System.in));
  String first_name, last_name, full_name;

  System.out.print("Enter first name: ");
  first_name = cin.readLine();

  System.out.print("Enter last name: ");
  last_name = cin.readLine();
  
  full_name = first_name + " " + last_name;
  System.out.println(full_name);
}

C++

#include <iostream>
using namespace std;
int main() {
  string first_name, last_name, full_name;

  cout << "Enter first name: ";
  cin >> first_name;
  cout << "Enter last name: ";
  cin >> last_name;
  
  full_name = first_name + " " + last_name;
  cout << full_name;
  return 0;
}

C#

static void Main() {
  string first_name, last_name, full_name;

  Console.Write("Enter first name: ");
  first_name = Console.ReadLine();
  Console.Write("Enter last name: ");
  last_name = Console.ReadLine();
  
  full_name = first_name + " " + last_name;
  Console.Write(full_name);
  Console.ReadKey();
}

Visual Basic

Sub Main()
  Dim first_name, last_name, full_name As String

  Console.Write("Enter first name: ")
  first_name = Console.ReadLine()
  Console.Write("Enter last name: ")
  last_name = Console.ReadLine()

  full_name = first_name + " " + last_name
  Console.Write(full_name)

  Console.ReadKey()
End Sub

Python

first_name = input("Enter first name: ")
last_name = input("Enter last name: ")

full_name = first_name + " " + last_name
print(full_name)

Notice: Please note the extra space character added between the first and last name.