Computer Programming: Act of writing computer programs, which are a sequence of
instructions written using a computer programming language to perform a specified task by the
computer.
Algorithm: From the programming point of view, an algorithm is a step-by-step procedure to
solve any problem.
• An algorithm is an effective method expressed as a finite set of well-defined
instructions.
• Thus, a computer programmer lists down all the steps required to resolve a problem
before writing the actual code.
Example: An algorithm to find out the largest number from a given list of numbers: -
1. Get a list of numbers L1, L2, L3....LN
2. Assume L1 is the largest, Largest = L1
3. Take next number Li from the list and do the following
4. If Largest is less than Li
5. Largest = Li
6. If Li is last number from the list then
7. Print value stored in Largest and come out
8. Else repeat same process starting from step 3
Variable:
A variable provides us with named storage that our programs can manipulate. Each variable in
C++ has a specific type, which determines the size and layout of the variable's memory; the
range of values that can be stored within that memory; and the set of operations that can be
applied to the variable.
Data Types:
A data type is the type of data a variable can hold. For example, a Boolean variable can have
boolean data, and an integer variable can hold integer data.
While coding, we need to use different variables to store different information. Variables are
just storage locations reserved for storing values. Therefore, when you create the variable, you
reserve some space in memory. You may want to store information for different data types,
such as integers, floats, strings, and Boolean values. Memory is allocated based on the
variable's data type. The amount of memory required depends on the data type.
1. Integer: The keyword int can represent integer data types. The range of integers is -
2147483648 to 2147483647, and they take up 4 bytes of memory.
1
For example,
int data = 1526;
data here is an integer data type variable. The variable data requires 2 or 4 bytes of memory
space.
2. Character: The keyword char represent characters. It is 1 byte in size. Single quotes ' ' are
used to enclose characters in C++.
For example,
char ch = 's';
ch here is a character datatype variable. This means that the variable requires 1 byte of memory
space.
3. Boolean
The boolean data type's keyword is bool. True or false are the two possible values for the
boolean data type. Boolean values are generally used in conditional statements and loops.
For example,
bool is_true = true;
is_true here is a boolean data type variable. This means that the variable requires 1 byte of
memory space.
4. Floating Point
float is the keyword used to hold floating-point numbers (decimals and exponentials). The float
variable has a size of 4 bytes.
For example,
float val = 15.26;
val here is a floating-point datatype variable. This means that the variable requires 4 bytes of
memory space.
2
C++ Examples
1) A "Hello, World!" is a simple program that outputs Hello, World! on the screen.
// Your First C++ Program
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
Working of C++ "Hello World!" Program
// Your First C++ Program
In C++, any line starting with // is a comment. Comments are intended for the person reading
the code to better understand the functionality of the program. It is completely ignored by the
C++ compiler.
#include <iostream>
The #include is a pre processor directive used to include files in our program. The above code
is including the contents of the iostream file. This allows us to use cout in our program to
print output on the screen.
For now, just remember that we need to use #include <iostream> to use cout that allows us to
print output on the screen.
int main () {...}
A valid C++ program must have the main () function. The curly braces indicate the start and
the end of the function.
The execution of code beings from this function.
std::cout << "Hello World!";
std::cout prints the content inside the quotation marks. It must be followed by << followed by
the format string. In our example, "Hello World!" is the format string.
Note: ; is used to indicate the end of a statement.
return 0;
The return 0; statement is the "Exit status" of the program. In simple terms, the program ends
with this statement.
3
2) C++ Program to Print Number Entered by User
Example: Print Number Entered by User
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
cout << "You entered " << number;
return 0;
}
This program asks the user to enter a number.
When the user enters an integer, it is stored in variable number using cin. Then it is displayed
on the screen using cout.
Starting from this example, we will be using the std namespace using the code: using
namespace std; This will allow us to write cout, cin, endl, etc. instead of std::cout, std::cin,
std::endl respectively. This is simply to make our code cleaner and more readable.
3) C++ Program to Add Two Numbers
Example: Program to Add Two Integers
#include <iostream>
using namespace std;
int main() {
int first_number, second_number, sum;
cout << "Enter two integers: ";
cin >> first_number >> second_number;
// sum of two numbers in stored in variable sumOfTwoNumbers
sum = first_number + second_number;
// prints sum
cout << first_number << " + " << second_number << " = " << sum;
return 0;
}
4
In this program, the user is asked to enter two integers. These two integers are stored in
variables first_number and second_number respectively. Then, the variables are added using
the + operator and stored in the sum variable. Finally, sum is displayed on the screen.
4) C++ Program to Find All Roots of a Quadratic Equation.
Example: Roots of a Quadratic Equation
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
discriminant = b*b - 4*a*c;
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;