76.8k views
3 votes
Assume that name and age have been declared suitably for storing names (like "Abdullah", "Alexandra" and "Zoe") and ages respectively. Write some code that reads in a name and an age and then prints the message "The age of NAME is AGE." where NAME and AGE are replaced by the values read in for the variables name and age. For example, if your code read in "Rohit" and 70 then it would print out "The age of Rohit is 70", on a line by itself. There should not be a period in the output.

User Genfood
by
5.1k points

1 Answer

7 votes

Answer:

I will write the code in C++ and JAVA

Step-by-step explanation:

C++ Program:

#include <iostream>

using namespace std;

int main()

{ std::string NAME;

// i have used std::string so that the input name can be more than a single character.

std::cout << " enter the name"; // take an input name from user

std::getline(std::cin,NAME);

int AGE;

cout<<"Enter age"; //takes age from the user as input

cin>>AGE;

cout<<"The age of "; std::cout <<NAME; cout<< " is " << AGE; }

/* displays the message for example the name is George and age is 54 so message displayed will be The age of George is 54 and this will be displayed without a period */

Step-by-step explanation:

The program first prompts the user to enter a name and the asks to input the age of that person. As per the requirement the if the user enter the name George and age 54, the program displays the following line as output:

The age of George is 54

Here std::string is used so that the input string can be more than one character long.

JAVA code

import java.util.*;

public class Main

{ public static void main(String[] args) {

String NAME;

Scanner sc = new Scanner(System.in);

System.out.println("Enter a name:");

NAME= sc.nextLine();

int AGE;

Scanner scanner = new Scanner(System.in);

System.out.println("Enter age:");

AGE = Integer.parseInt(scanner.nextLine());

System.out.print("The age of " + NAME + " is " + AGE); }}

Step-by-step explanation:

This is the JAVA code which will work the same as C++ code. The scanner class is used to read the input from the user. The output of the above JAVA code is as follows:

Enter a name: George

Enter age: 45

The age of George is 45

User Carlos Palma
by
5.4k points