C++ Basics, Variables and Data Types Practice Questions with Solutions

C++ is one of the most powerful and widely used programming languages for software development, competitive programming, game development, embedded systems, and Data Structures & Algorithms (DSA). Developed by Bjarne Stroustrup, C++ extends the C programming language by introducing Object-Oriented Programming (OOP) concepts while maintaining high performance and efficiency.

Before learning advanced topics like functions, arrays, pointers, and object-oriented programming, it’s important to build a strong understanding of C++ basics, variables, and data types. These concepts form the foundation of every C++ program.

In this chapter, you’ll solve beginner-friendly practice questions designed to improve your programming logic and strengthen your understanding of C++ syntax. Each question includes a problem statement, complete C++ solution, sample input and output, explanation, and the key concepts covered.


1. C++ Program to Print “Hello, World!”

Problem Statement

Write a C++ program to display the message “Hello, World!” on the screen.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello, World!";

    return 0;
}

Sample Output

Hello, World!

Explanation

  • #include <iostream> includes the input/output library.
  • using namespace std; allows the use of standard library objects without writing std::.
  • main() is the entry point of every C++ program.
  • cout is used to display output on the screen.
  • return 0; indicates that the program executed successfully.

Concepts Covered

  • Basic Program Structure
  • Header Files
  • main() Function
  • cout
  • Output Statement

2. C++ Program to Print Your Name

Problem Statement

Write a C++ program to print your name.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    cout << "My name is Rahul";

    return 0;
}

Sample Output

My name is Rahul

Explanation

This program uses the cout object to display a text message on the screen. You can replace Rahul with your own name.

Concepts Covered

  • Output Statements
  • String Literals
  • Basic Syntax

3. C++ Program to Add Two Numbers

Problem Statement

Write a C++ program to input two integers and display their sum.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int num1, num2, sum;

    cout << "Enter first number: ";
    cin >> num1;

    cout << "Enter second number: ";
    cin >> num2;

    sum = num1 + num2;

    cout << "Sum = " << sum;

    return 0;
}

Sample Input

Enter first number: 15
Enter second number: 20

Sample Output

Sum = 35

Explanation

  • Two integer variables are declared.
  • The cin object accepts user input.
  • The values are added using the + operator.
  • The result is displayed using cout.

Concepts Covered

  • Variables
  • Integer Data Type
  • Input using cin
  • Output using cout
  • Arithmetic Operator

4. C++ Program to Find the Area of a Rectangle

Problem Statement

Write a C++ program to calculate the area of a rectangle using its length and width.

Formula

Area = Length × Width

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    float length, width, area;

    cout << "Enter length: ";
    cin >> length;

    cout << "Enter width: ";
    cin >> width;

    area = length * width;

    cout << "Area of Rectangle = " << area;

    return 0;
}

Sample Input

Enter length: 8
Enter width: 5

Sample Output

Area of Rectangle = 40

Explanation

The program reads the length and width from the user and calculates the area using the multiplication operator.

Concepts Covered

  • Float Data Type
  • User Input
  • Mathematical Formula
  • Multiplication Operator

5. C++ Program to Swap Two Numbers Using a Third Variable

Problem Statement

Write a C++ program to swap two numbers using a temporary variable.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int a, b, temp;

    cout << "Enter first number: ";
    cin >> a;

    cout << "Enter second number: ";
    cin >> b;

    temp = a;
    a = b;
    b = temp;

    cout << "After Swapping:" << endl;
    cout << "First Number = " << a << endl;
    cout << "Second Number = " << b;

    return 0;
}

Sample Input

Enter first number: 10
Enter second number: 25

Sample Output

After Swapping:
First Number = 25
Second Number = 10

Explanation

A temporary variable stores the value of the first variable before exchanging the values. This is the most common and beginner-friendly method of swapping two numbers.

Concepts Covered

  • Variables
  • Temporary Variable
  • Assignment Operator
  • Swapping Logic

6. C++ Program to Swap Two Numbers Without Using a Third Variable

Problem Statement

Write a C++ program to swap two numbers without using a temporary variable.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int a, b;

    cout << "Enter first number: ";
    cin >> a;

    cout << "Enter second number: ";
    cin >> b;

    a = a + b;
    b = a - b;
    a = a - b;

    cout << "After Swapping:" << endl;
    cout << "First Number = " << a << endl;
    cout << "Second Number = " << b;

    return 0;
}

Sample Input

Enter first number: 15
Enter second number: 30

Sample Output

After Swapping:
First Number = 30
Second Number = 15

Explanation

This program swaps two numbers using arithmetic operations without requiring an extra variable.

Concepts Covered

  • Arithmetic Operators
  • Variable Manipulation
  • Swapping Logic

7. C++ Program to Find the Size of Different Data Types

Problem Statement

Write a C++ program to display the memory size occupied by different data types.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    cout << "Size of char = "
         << sizeof(char)
         << " byte" << endl;

    cout << "Size of int = "
         << sizeof(int)
         << " bytes" << endl;

    cout << "Size of float = "
         << sizeof(float)
         << " bytes" << endl;

    cout << "Size of double = "
         << sizeof(double)
         << " bytes" << endl;

    return 0;
}

Sample Output

Size of char = 1 byte
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes

Note: Output may vary depending on your compiler and system architecture.

Explanation

The sizeof() operator returns the number of bytes occupied by a data type or variable in memory.

Concepts Covered

  • sizeof() Operator
  • Primitive Data Types
  • Memory Allocation

8. C++ Program to Find the ASCII Value of a Character

Problem Statement

Write a C++ program to input a character and display its ASCII value.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    char ch;

    cout << "Enter a character: ";
    cin >> ch;

    cout << "ASCII Value = "
         << (int)ch;

    return 0;
}

Sample Input

Enter a character: A

Sample Output

ASCII Value = 65

Explanation

Characters are internally stored as integers based on the ASCII table. Type casting converts the character into its integer representation.

Concepts Covered

  • Character Data Type
  • ASCII Code
  • Type Casting

9. C++ Program to Demonstrate Type Casting

Problem Statement

Write a C++ program to convert an integer into a floating-point number using explicit type casting.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number = 25;

    float result = (float)number;

    cout << "Integer Value = "
         << number << endl;

    cout << "Float Value = "
         << result;

    return 0;
}

Sample Output

Integer Value = 25
Float Value = 25

Explanation

Explicit type casting converts one data type into another. Here, an integer is converted into a floating-point value.

Concepts Covered

  • Explicit Type Casting
  • Integer
  • Float
  • Data Type Conversion

10. C++ Program to Calculate Simple Interest

Problem Statement

Write a C++ program to calculate Simple Interest.

Formula

Simple Interest = (Principal × Rate × Time) / 100

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    float principal;
    float rate;
    float time;
    float simpleInterest;

    cout << "Enter Principal: ";
    cin >> principal;

    cout << "Enter Rate: ";
    cin >> rate;

    cout << "Enter Time: ";
    cin >> time;

    simpleInterest =
        (principal * rate * time) / 100;

    cout << "Simple Interest = "
         << simpleInterest;

    return 0;
}

Sample Input

Enter Principal: 5000
Enter Rate: 8
Enter Time: 2

Sample Output

Simple Interest = 800

Explanation

The program accepts the principal amount, annual interest rate, and time period from the user. It then applies the Simple Interest formula and displays the result.

Concepts Covered

  • Float Variables
  • Mathematical Formula
  • Arithmetic Operators
  • User Input
  • Financial Calculations

11. C++ Program to Convert Temperature from Celsius to Fahrenheit

Problem Statement

Write a C++ program to convert temperature from Celsius to Fahrenheit.

Formula

Fahrenheit = (Celsius × 9 / 5) + 32

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    float celsius;
    float fahrenheit;

    cout << "Enter temperature in Celsius: ";
    cin >> celsius;

    fahrenheit =
        (celsius * 9 / 5) + 32;

    cout << "Temperature in Fahrenheit = "
         << fahrenheit;

    return 0;
}

Sample Input

Enter temperature in Celsius: 25

Sample Output

Temperature in Fahrenheit = 77

Explanation

The program converts Celsius into Fahrenheit using the standard temperature conversion formula.

Concepts Covered

  • Float Variables
  • Mathematical Formula
  • User Input
  • Arithmetic Operators

12. C++ Program to Calculate the Area of a Circle

Problem Statement

Write a C++ program to calculate the area of a circle using its radius.

Formula

Area = π × Radius × Radius

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    float radius;
    float area;

    cout << "Enter radius: ";
    cin >> radius;

    area = 3.14159 * radius * radius;

    cout << "Area of Circle = "
         << area;

    return 0;
}

Sample Input

Enter radius: 7

Sample Output

Area of Circle = 153.938

Explanation

The program calculates the area using the mathematical formula:

Area = π × r²

Concepts Covered

  • Float Variables
  • Mathematical Calculations
  • Constants
  • User Input

13. C++ Program to Calculate the Perimeter of a Rectangle

Problem Statement

Write a C++ program to calculate the perimeter of a rectangle.

Formula

Perimeter = 2 × (Length + Width)

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    float length;
    float width;
    float perimeter;

    cout << "Enter length: ";
    cin >> length;

    cout << "Enter width: ";
    cin >> width;

    perimeter =
        2 * (length + width);

    cout << "Perimeter = "
         << perimeter;

    return 0;
}

Sample Input

Enter length: 12
Enter width: 8

Sample Output

Perimeter = 40

Explanation

The perimeter is calculated by adding the length and width and multiplying the result by 2.

Concepts Covered

  • Arithmetic Operators
  • Formula-Based Programming
  • Float Data Type

14. C++ Program to Calculate the Average of Three Numbers

Problem Statement

Write a C++ program to calculate the average of three numbers entered by the user.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    float num1;
    float num2;
    float num3;
    float average;

    cout << "Enter first number: ";
    cin >> num1;

    cout << "Enter second number: ";
    cin >> num2;

    cout << "Enter third number: ";
    cin >> num3;

    average =
        (num1 + num2 + num3) / 3;

    cout << "Average = "
         << average;

    return 0;
}

Sample Input

Enter first number: 10
Enter second number: 20
Enter third number: 30

Sample Output

Average = 20

Explanation

The program adds three numbers and divides the result by 3 to calculate the average.

Concepts Covered

  • Arithmetic Operators
  • Float Variables
  • User Input
  • Average Calculation

15. C++ Program to Display All Basic Data Types

Problem Statement

Write a C++ program that declares variables of different data types and displays their values.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int age = 22;
    float percentage = 91.5;
    double salary = 75000.50;
    char grade = 'A';
    bool passed = true;

    cout << "Age = "
         << age << endl;

    cout << "Percentage = "
         << percentage << endl;

    cout << "Salary = "
         << salary << endl;

    cout << "Grade = "
         << grade << endl;

    cout << "Passed = "
         << passed;

    return 0;
}

Sample Output

Age = 22
Percentage = 91.5
Salary = 75000.5
Grade = A
Passed = 1

Explanation

This program demonstrates the most commonly used primitive data types in C++:

  • int → Integer values
  • float → Decimal values
  • double → High-precision decimal values
  • char → Single character
  • bool → Boolean value (1 = true, 0 = false)

Concepts Covered

  • Primitive Data Types
  • Variable Declaration
  • Output Formatting
  • Boolean Data Type

Chapter Summary

In this chapter, you learned the fundamental building blocks of C++ programming. You explored the structure of a C++ program, learned how to use input and output statements, declared variables of different data types, performed arithmetic calculations, swapped numbers, worked with ASCII values, applied type casting, and solved practical mathematical problems. These concepts form the foundation for all advanced C++ topics, including functions, arrays, pointers, object-oriented programming, and data structures.


Key Takeaways

  • Every C++ program starts with the main() function.
  • cout is used to display output, while cin accepts user input.
  • Variables store data, and each variable has a specific data type.
  • Common data types include int, float, double, char, and bool.
  • Arithmetic operators perform mathematical calculations.
  • Type casting converts one data type into another.
  • sizeof() helps determine memory usage.
  • Formula-based programs improve logical thinking and programming skills.
  • Proper variable naming improves code readability.
  • Mastering C++ basics makes advanced programming topics easier to learn.

Frequently Asked Questions (FAQs)

1. What is C++?

C++ is a general-purpose, object-oriented programming language used for software development, competitive programming, game development, operating systems, embedded systems, and data structures.


2. What is the purpose of the main() function?

The main() function is the entry point of every C++ program. Program execution always starts from this function.


3. What is the difference between cout and cin?

  • cout displays output on the screen.
  • cin accepts input from the keyboard.

4. Which data types are most commonly used in C++?

The most commonly used data types are:

  • int
  • float
  • double
  • char
  • bool

5. What is type casting in C++?

Type casting converts one data type into another. It is commonly used when working with integers, floating-point numbers, and characters.


6. What does the sizeof() operator do?

The sizeof() operator returns the amount of memory (in bytes) occupied by a variable or data type.


7. Why should beginners practice formula-based programs?

Formula-based programs help beginners understand variables, operators, input/output, and mathematical expressions while improving logical thinking.


8. Is learning C++ basics important before Data Structures and Algorithms (DSA)?

Yes. A strong understanding of variables, data types, operators, input/output, and basic programming logic is essential before learning DSA, competitive programming, or object-oriented programming.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top