C++ Coding Exercises - C++ Basics
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. C++ Basics
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:
- Define the value of
PIusing#define. - Declare a constant integer
DAYS_IN_WEEKusingconstand assign it the value7. - Display the values of
PIandDAYS_IN_WEEK. - Declare a floating-point variable
radiusand assign it the value5.0. - Calculate the area of a circle using
PIandradius. - Display the radius and calculated area.
- Include a commented example showing that a
constvariable 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.5397Problem Statement 2: Explicit Type Casting
Write a C++ program that:
- Declares two integer variables
a = 5andb = 2. - Declares a third variable
cof typefloat. - Uses explicit type casting to convert one of the integer variables to
floatbefore performing the division. - Stores the division result in variable
c. - Displays the value of
c.
Expected Output
2.5