Learn with Yasir

Share Your Feedback

C++ Coding Exercises


Practice C++ programming with coding exercises covering variables, data types, operators, input and output, type casting, conditional statements, loops, functions, arrays, and other C++ fundamentals.

1. Constants

Problem Statement 1: const and #define

Write a C++ program to demonstrate the use of const and #define for defining constant values.

The program should:

  1. Define the value of PI using #define.
  2. Declare a constant integer DAYS_IN_WEEK using const and assign it the value 7.
  3. Display the values of PI and DAYS_IN_WEEK.
  4. Declare a floating-point variable radius and assign it the value 5.0.
  5. Calculate the area of a circle using PI and radius.
  6. Display the radius and calculated area.
  7. Include a commented example showing that a const variable cannot be changed after initialization.

Expected Output:

Value of PI using #define: 3.14159
Days in a week using const: 7
Area of circle with radius 5 is: 78.5397

2. Type Casting

Problem Statement 1: Explicit Type Casting

Write a C++ program that:

  1. Declares two integer variables a = 5 and b = 2.
  2. Declares a third variable c of type float.
  3. Uses explicit type casting to convert one of the integer variables to float before performing the division.
  4. Stores the division result in variable c.
  5. Displays the value of c.

Expected Output

2.5