Cad Guru is the best programing language institute in Uttam Nagar, Delhi. we provide 100% job placement assistance.
https://www.cadguru.in
Introduction to C++ lecture ************Emad Helal
Here is a C++ program to calculate the volume of a sphere:
#include <iostream>
#define PI 3.14159
using namespace std;
int main() {
float radius, volume;
cout << "Enter the radius of the sphere: ";
cin >> radius;
volume = (4/3.0) * PI * radius * radius * radius;
cout << "Volume of the sphere is: " << volume << endl;
return 0;
}
This program first includes the iostream library for input/output functions. It then defines PI as a constant. In main(), it prompts the user to enter the radius, reads it using cin and stores it in
C++ programs are converted to machine-readable code through compilation. The document outlines the basics of C++ programming, including compilers, program structure, variables, data types, input/output, and basic elements like identifiers, literals, operators, and comments. It provides examples of simple C++ programs and explains how they work, demonstrating concepts like printing output, declaring variables to store values, and performing calculations.
C++ is a general-purpose programming language used to create computer programs like art applications, music players, and video games. It was derived from C and is largely based on it. C++ is an open, compiled language that is standardized and allows for control, though it is unsafe. A basic C++ program structure includes main(), curly brackets to indicate functions, and statements ending with semicolons. Variables are declared with a data type and identifier before use, and can be assigned values and used in operations.
The document provides an introduction to programming fundamentals in C++, including basic syntax and components of a C++ program. It covers variables and data types, input/output, comments, and how to write a simple C++ program with preprocessor directives and a main function. The key topics discussed are variable declaration, fundamental data types like int, float, char, comments, and how to write a basic "Hello World" program in C++.
This document summarizes key concepts from an introduction to C++ programming chapter, including:
- The main parts of a C++ program are comments, preprocessor directives, the main() function, and statements.
- Variables are used to store and manipulate data in a program. Variables are declared with a name and type before use.
- Arithmetic operators allow performing calculations in C++ programs. Expressions follow order of operations rules.
- Input and output streams allow getting user input and displaying output to the screen.
The document provides an introduction to programming in C++. It discusses the software development cycle including compiling, linking, and debugging programs. It also covers key programming concepts like variables, data types, functions, classes and objects. The evolution of C++ from C is described. Input/output statements like cout and cin are demonstrated along with basic program structure.
This document provides an overview of basic C++ programming concepts including:
1) The main parts of a C++ program including comments, preprocessor directives, namespaces, and the main function.
2) Different data types in C++ like integers, characters, floating-point numbers, and Booleans.
3) Keywords, variables, literals, and operators used in C++.
4) Input and output streams like cout and cin for displaying and getting user input.
3) Concepts like expressions, order of operations, comments, and programming style.
If your concepts for C++ are weak then have a look at this
this will help you to know some concepts.If Any query plz tell me about C++ .
Also check my last uploads
and Please Do follow
C++ is an extension of the C programming language that adds object-oriented programming capabilities. A basic C++ program includes header files, uses namespaces, and contains a main function that returns 0. The document introduces key elements of C++ like keywords, comments, identifiers, and programming style best practices. It provides examples of simple C++ programs that demonstrate basic concepts like input/output and arithmetic operations.
The document summarizes key concepts about input/output statements in the C++ programming language. It discusses how input streams are used for reading data from a source, while output streams are used for writing data to a destination. It provides examples of using the standard input stream cin and standard output stream cout to take user input and display output. It also demonstrates how to perform file reading and writing using ifstream and ofstream classes. Finally, it discusses formatted outputs using functions like setw and setprecision to control output formatting.
This document discusses C++ comments, including single-line and multi-line comments. Single-line comments begin with // and multi-line comments begin with /* and end with */. It also covers basic data types in C++ like int, char, float, and double, as well as variables, constants, user input, operators, and identifiers.
#Code2Create series: C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, functional, and so on.Start your programming journey and join us to learn C++ basics!
by Google Developers Group and Women Tech-markers Kuwait chapter:
Instagram and Twitter: @GDGWTMKUWAIT
Introduction to Programming for students who have very basic knowledge of coding and either want to learn basics of coding or are taking a course in their Bachelors. I have made this presentation to teach Industrial Engineering students in Pakistan. Other students can also take it up for a light reference. You can consult with me about the content through PIM or comments
This document provides an introduction to C++ programming concepts including variables, data types, constants, and I/O streams. It discusses basic C++ code structure and comments. Integral data types like char, bool, and integers are explained. Floating point types float and double are also introduced. The document demonstrates declaring and initializing variables as well as basic math operations on variables. Constants and the const keyword are described along with the #define preprocessor directive.
The document provides an introduction to C++ programming including setting up a compiler, the structure of a basic C++ program, variables, and input/output. It explains that a compiler is needed to convert C++ code into an executable. The main() function is called at startup and returns an integer value. A basic "Hello World" program is shown using #include, cout, and cin.get(). Variables are declared with a data type like int or char and stored user input. Comments are added using // or /* */.
This document provides an introduction and overview of key concepts in C++ programming, including data types, operators, conditional statements, loops, functions, variables, input/output, and more. It begins with an introduction and overview of topics to be covered. It then demonstrates the basic structure of a C++ program, use of comments, printing to the screen, getting user input, and chaining stream operations. The document continues explaining additional concepts like special characters, data types, operators, conditional statements, switch statements, and looping structures. It provides examples throughout to demonstrate each concept.
Introduction to cpp language and all the required information relating to itPushkarNiroula1
C++ is an object-oriented programming language developed in the early 1980s as an extension of C with additional features like classes, inheritance, and function overloading. A simple C++ program prints a string to the screen using the cout output stream and iostream header. C++ programs typically contain functions, comments, and use operators like << for output and >> for input.
This document provides an introduction to variables in C++ programming for grade 12 students. It defines different variable types like int, double, char, string, and bool and explains how to declare variables by specifying their type and assigning a value. It also demonstrates how to display variable values using cout with the insertion operator <<. The last section provides an exercise for students to create an integer variable and assign it a value to output.
The document introduces programming concepts in C++ including:
- The software development cycle of compile, link, and execute source code using an IDE.
- Key programming language elements like keywords, variables, operators, and constructs and how every language has a defined syntax.
- Object-oriented programming concepts in C++ like classes, objects, and inheritance hierarchies.
- A simple "Hello World" C++ program structure and basic data types and output statements.
The aim of this list of programming languages is to include all notable programming languages in existence, both those in current use and ... Note: This page does not list esoteric programming languages. .... Computer programming portal ...
This document introduces C++ and provides an overview of its key concepts. C++ can be used to create high-performance applications and gives programmers low-level control. It supports primary data types like int and float, as well as user-defined types like classes and structures. Programs begin with a main function and use statements like cout to display output. C++ code is compiled and executed to achieve the desired output.
This document provides an overview of basic C++ programming concepts including:
1) The main parts of a C++ program including comments, preprocessor directives, namespaces, and the main function.
2) Different data types in C++ like integers, characters, floating-point numbers, and Booleans.
3) Keywords, variables, literals, and operators used in C++.
4) Input and output streams like cout and cin for displaying and getting user input.
3) Concepts like expressions, order of operations, comments, and programming style.
If your concepts for C++ are weak then have a look at this
this will help you to know some concepts.If Any query plz tell me about C++ .
Also check my last uploads
and Please Do follow
C++ is an extension of the C programming language that adds object-oriented programming capabilities. A basic C++ program includes header files, uses namespaces, and contains a main function that returns 0. The document introduces key elements of C++ like keywords, comments, identifiers, and programming style best practices. It provides examples of simple C++ programs that demonstrate basic concepts like input/output and arithmetic operations.
The document summarizes key concepts about input/output statements in the C++ programming language. It discusses how input streams are used for reading data from a source, while output streams are used for writing data to a destination. It provides examples of using the standard input stream cin and standard output stream cout to take user input and display output. It also demonstrates how to perform file reading and writing using ifstream and ofstream classes. Finally, it discusses formatted outputs using functions like setw and setprecision to control output formatting.
This document discusses C++ comments, including single-line and multi-line comments. Single-line comments begin with // and multi-line comments begin with /* and end with */. It also covers basic data types in C++ like int, char, float, and double, as well as variables, constants, user input, operators, and identifiers.
#Code2Create series: C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, functional, and so on.Start your programming journey and join us to learn C++ basics!
by Google Developers Group and Women Tech-markers Kuwait chapter:
Instagram and Twitter: @GDGWTMKUWAIT
Introduction to Programming for students who have very basic knowledge of coding and either want to learn basics of coding or are taking a course in their Bachelors. I have made this presentation to teach Industrial Engineering students in Pakistan. Other students can also take it up for a light reference. You can consult with me about the content through PIM or comments
This document provides an introduction to C++ programming concepts including variables, data types, constants, and I/O streams. It discusses basic C++ code structure and comments. Integral data types like char, bool, and integers are explained. Floating point types float and double are also introduced. The document demonstrates declaring and initializing variables as well as basic math operations on variables. Constants and the const keyword are described along with the #define preprocessor directive.
The document provides an introduction to C++ programming including setting up a compiler, the structure of a basic C++ program, variables, and input/output. It explains that a compiler is needed to convert C++ code into an executable. The main() function is called at startup and returns an integer value. A basic "Hello World" program is shown using #include, cout, and cin.get(). Variables are declared with a data type like int or char and stored user input. Comments are added using // or /* */.
This document provides an introduction and overview of key concepts in C++ programming, including data types, operators, conditional statements, loops, functions, variables, input/output, and more. It begins with an introduction and overview of topics to be covered. It then demonstrates the basic structure of a C++ program, use of comments, printing to the screen, getting user input, and chaining stream operations. The document continues explaining additional concepts like special characters, data types, operators, conditional statements, switch statements, and looping structures. It provides examples throughout to demonstrate each concept.
Introduction to cpp language and all the required information relating to itPushkarNiroula1
C++ is an object-oriented programming language developed in the early 1980s as an extension of C with additional features like classes, inheritance, and function overloading. A simple C++ program prints a string to the screen using the cout output stream and iostream header. C++ programs typically contain functions, comments, and use operators like << for output and >> for input.
This document provides an introduction to variables in C++ programming for grade 12 students. It defines different variable types like int, double, char, string, and bool and explains how to declare variables by specifying their type and assigning a value. It also demonstrates how to display variable values using cout with the insertion operator <<. The last section provides an exercise for students to create an integer variable and assign it a value to output.
The document introduces programming concepts in C++ including:
- The software development cycle of compile, link, and execute source code using an IDE.
- Key programming language elements like keywords, variables, operators, and constructs and how every language has a defined syntax.
- Object-oriented programming concepts in C++ like classes, objects, and inheritance hierarchies.
- A simple "Hello World" C++ program structure and basic data types and output statements.
The aim of this list of programming languages is to include all notable programming languages in existence, both those in current use and ... Note: This page does not list esoteric programming languages. .... Computer programming portal ...
This document introduces C++ and provides an overview of its key concepts. C++ can be used to create high-performance applications and gives programmers low-level control. It supports primary data types like int and float, as well as user-defined types like classes and structures. Programs begin with a main function and use statements like cout to display output. C++ code is compiled and executed to achieve the desired output.
Decipher the Magic of Quantum Entanglement.pdfSaikatBasu37
Welcome to the mysterious world of quantum physics!
Today, we'll explore quantum entanglement—a mind-bending phenomenon that connects particles across vast distances.
Science in retreat: The consequences for the Arctic and our planetZachary Labe
1 June 2025…
The Weather and Climate Livestream (Invited – Keynote): Science in retreat: The consequences for the Arctic and our planet. Remote Presentation.
Affordable Website Developers Ind is a reputable company known for their cost-effective and efficient web development services. With a focus on quality and affordability, they cater to businesses looking to establish a strong online presence without breaking the bank. Partnering with Zonvoir Technologies, Affordable Website Developers Ind ensures that their clients receive top-notch, professional websites that are tailored to their specific needs. By collaborating with Zonvoir Technologies, they leverage cutting-edge technology and expertise to deliver modern and responsive websites that leave a lasting impression on visitors. Overall, Affordable Website Developers Ind, in partnership with Zonvoir Technologies, stands out as a reliable choice for businesses seeking high-quality web development solutions at competitive prices. https://zonvoir.com/
2. What is C++?
Introduction to C++
C++ is a cross-platform language that can be used to create high-
performance applications.
C++ was developed by Bjarne Stroustrup, as an extension to the C
language.
C++ gives programmers a high level of control over system resources and
memory.
The language was updated 5 major times in 2011, 2014, 2017, 2020, and
2023 to C++11, C++14, C++17, C++20, and C++23.
3. C++ Syntax
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Line 1: #include <iostream> is a header
fi
le library that lets us work with input and output objects, such as cout (used in line 5). Header
fi
les add functionality to C++ programs.
Line 2: using namespace std means that we can use names for objects and variables from the standard library.
Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.
Line 4: Another thing that always appear in a C++ program is int main(). This is called a function. Any code inside its curly brackets {} will
be executed.
Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<) to output/print text. In our example, it will
output "Hello World!".
Note: C++ is case-sensitive: "cout" and "Cout" has different meaning.
Line 6: return 0; ends the main function.
Line 7: Do not forget to add the closing curly bracket } to actually end the main function.
4. Omitting Namespace
You might see some C++ programs that runs without the standard
namespace library. The using namespace std line can be omitted and
replaced with the std keyword, followed by the :: operator for some objects:
Example
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
5. C++ Statements
A computer program is a list of "instructions" to be "executed" by a computer.
In a programming language, these programming instructions are called statements.
• The following statement "instructs" the compiler to print the text "Hello World" to the screen:
Example
cout << "Hello World!";
• It is important that you end the statement with a semicolon ;
• If you forget the semicolon (;), an error will occur and the program will not run:
Example
cout << "Hello World!"
error: expected ';' before 'return'
6. Many Statements
Most C++ programs contain many statements.
• The statements are executed, one by one, in the same order as they are written:
Example
cout << "Hello World!";
cout << "Have a good day!";
return 0;
Example explained
From the example above, we have three statements:
1.cout << "Hello World!";
2.cout << "Have a good day!";
3.return 0;
The
fi
rst statement is executed
fi
rst (print "Hello World!" to the screen).
Then the second statement is executed (print "Have a good day!" to the screen).
And at last, the third statement is executed (end the C++ program successfully).
7. C++ Output (Print Text)
The cout object, together with the << operator, is used to output values and print text.
Just remember to surround the text with double quotes (“"):
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
8. C++ Output (Print Text)
You can add as many cout objects as you want.
However, note that it does not insert a new line at the end of the output:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
9. C++ Print Numbers
You can also use cout() to print numbers.
However, unlike text, we don't put numbers inside double quotes:
Example
#include <iostream>
using namespace std;
int main() {
cout << 3;
return 0;
}
You can also perform mathematical calculations:
Example
cout << 3 + 3;
Example
cout << 2 * 5;
10. New Lines
To insert a new line in your output, you can use the n character:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World! n";
cout << "I am learning C++";
return 0;
}
You can also use another << operator and place the n character after the text, like this:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << "n";
cout << "I am learning C++";
return 0;
}
11. New Lines
Tip: Two n characters after each other will create a blank line:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << "nn";
cout << "I am learning C++";
return 0;
}
Another way to insert a new line, is with the endl manipulator:
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0;
}
12. New Lines
Both n and endl are used to break lines. However, n is most used.
But what is n exactly?
The newline character (n) is called an escape sequence, and it forces the cursor to
change its position to the beginning of the next line on the screen. This results in a
new line.
Examples of other valid escape sequences are:
t Creates a horizontal tab
Inserts a backslash character ()
” Inserts a double quote character
13. C++ Comments
Comments can be used to explain C++ code, and to make it more readable. It can
also be used to prevent execution when testing alternative code. Comments can be
singled-lined or multi-lined.
Single-line Comments
• Single-line comments start with two forward slashes (//).
• Any text between // and the end of the line is ignored by the compiler (will not be
executed).
This example uses a single-line comment before a line of code:
Example
// This is a comment
cout << "Hello World!";
14. C++ Comments
This example uses a single-line comment at the end of a line of code:
Example
cout << "Hello World!"; // This is a comment
C++ Multi-line Comments
• Multi-line comments start with /* and ends with */.
• Any text between /* and */ will be ignored by the compiler:
Example
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";
15. C++ Variables
Variables are containers for storing data values.
In C++, there are different types of variables (de
fi
ned with different keywords), for
example:
• int - stores integers (whole numbers), without decimals, such as 123 or -123
• double - stores
fl
oating point numbers, with decimals, such as 19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values are surrounded by
single quotes
• string - stores text, such as "Hello World". String values are surrounded by double
quotes
• bool - stores values with two states: true or false
16. Declaring (Creating) Variables
To create a variable, specify the type and assign it a value:
Syntax
type variableName = value;
Where type is one of C++ types (such as int), and variableName is the name of the
variable (such as x or myName). The equal sign is used to assign values to the
variable.
To create a variable that should store a number, look at the following example:
Example
• Create a variable called myNum of type int and assign it the value 15:
int myNum = 15;
cout << myNum;
17. Declaring (Creating) Variables
You can also declare a variable without assigning the value, and assign the value later:
Example
int myNum;
myNum = 15;
cout << myNum;
Note that if you assign a new value to an existing variable, it will overwrite the previous
value:
Example
int myNum = 15; // myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum; // Output 10
18. Other Types
A demonstration of other data types:
Example
int myNum = 5; // Integer (whole number without decimals)
double myFloatNum = 5.99; // Floating point number (with decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)
19. Display Variables
The cout object is used together with the << operator to display variables.
• To combine both text and a variable, separate them with the << operator:
Example
int myAge = 35;
cout << "I am " << myAge << " years old.";
Add Variables Together
• To add a variable to another variable, you can use the + operator:
Example
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
20. Display Many Variables
To declare more than one variable of the same type, use a comma-separated list:
Example
int x = 5, y = 6, z = 50;
cout << x + y + z;
One Value to Multiple Variables
• You can also assign the same value to multiple variables in one line:
Example
int x, y, z;
x = y = z = 50;
cout << x + y + z;
21. C++ Identi
fi
ers
• All C++ variables must be identi
fi
ed with unique names.
• These unique names are called identi
fi
ers.
• Identi
fi
ers can be short names (like x and y) or more descriptive names (age, sum,
totalVolume).
Note: It is recommended to use descriptive names in order to create understandable and
maintainable code:
Example
// Good
int minutesPerHour = 60;
// OK, but not so easy to understand what m actually is
int m = 60;
22. The general rules for naming variables are:
•Names can contain letters, digits and underscores
•Names must begin with a letter or an underscore (_)
•Names are case-sensitive (myVar and myvar are different variables)
•Names cannot contain whitespaces or special characters like !, #, %, etc.
•Reserved words (like C++ keywords, such as int) cannot be used as names
23. Constants
• When you do not want others (or yourself) to change existing variable values, use
the constkeyword (this will declare the variable as "constant", which
means unchangeable and read-only):
Example
const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable ‘myNum'
• You should always declare the variable as constant when you have values that are
unlikely to change:
Example
const int minutesPerHour = 60;
const
fl
oat PI = 3.14;
24. Notes On Constants
• When you declare a constant variable, it must be assigned with a value:
Example
Like this:
const int minutesPerHour = 60;
This however, will not work:
const int minutesPerHour;
minutesPerHour = 60; // error
25. Variable Example
Write a C++ program that stores a student's details such as student ID,
age, fee, and grade using appropriate data types. Then, print these
details in a readable format using cout.
Write a C++ program to calculate the area of a rectangle.
Initialize length = 4 and width = 6, then compute and print the area.
26. C++ User Input
• You have already learned that cout is used to output (print) values.
• Now we will use cin to get user input.
cin is a prede
fi
ned variable that reads data from the keyboard with the extraction operator
(>>).
In the following example, the user can input a number, which is stored in the variable x. Then
we print the value of x:
Example
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value
27. C++ User Input
Good To Know
• cout is pronounced "see-out". Used for output, and uses the insertion operator (<<)
• cin is pronounced "see-in". Used for input, and uses the extraction operator (>>)
Creating a Simple Calculator
Write a program in which the user must input two numbers. Then we print the sum by calculating
(adding) the two numbers.
28. C++ Data Types
• As explained in the Variables slides, a variable in C++ must be a speci
fi
ed
data type:
Example
int myNum = 5; // Integer (whole number)
fl
oat myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String
29. Basic Data Types
•The data type speci
fi
es the size and type of information the variable will store:
D
a
t
a
Type Size Description
boole
a
n 1 byte Stores true or false values
ch
a
r 1 byte
Stores a single character/letter/number, or ASCII
values
int 2 or 4 bytes Stores whole numbers, without decimals
f
lo
a
t 4 bytes
Stores fractional numbers, containing one or more
decimals. Suf
fi
cient for storing 6-7 decimal digits
Double 8 bytes
Stores fractional numbers, containing one or more
decimals. Suf
fi
cient for storing 15 decimal digits
30. Numeric Types
• Use int when you need to store a whole number without decimals, like 35 or 1000,
and
fl
oat or double when you need a
fl
oating point number (with decimals), like 9.99 or
3.14515.
int
int myNum = 1000;
cout << myNum;
fl
oat
fl
oat myNum = 5.75;
cout << myNum;
31. Numeric Types
double
double myNum = 19.99;
cout << myNum;
fl
oat vs. double
The precision of a
fl
oating point value indicates how many digits the value can have after the decimal
point. The precision of
fl
oat is only six or seven decimal digits, while double variables have a precision of
about 15 digits. Therefore it is safer to use double for most calculations.
Scienti
fi
c Numbers
• A
fl
oating point number can also be a scienti
fi
c number with an "e" to indicate the power of 10:
Example
fl
oat f1 = 35e3;
double d1 = 12E4;
cout << f1;
cout << d1;
32. Boolean Types
• A boolean data type is declared with the bool keyword and
can only take the values true or false.
When the value is returned, true = 1 and false = 0.
Example
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun; // Outputs 1 (true)
cout << isFishTasty; // Outputs 0 (false)
33. Character Types
• The char data type is used to store a single character.
The character must be surrounded by single quotes,
like 'A' or 'c':
Example
char myGrade = 'B';
cout << myGrade;
34. String Types
• The string type is used to store a sequence of characters (text). This is not a built-in type, but it behaves
like one in its most basic usage. String values must be surrounded by double quotes:
Example
string greeting = "Hello";
cout << greeting;
• To use strings, you must include an additional header
fi
le in the source code, the <string> library:
Example
// Include the string library
#include <string>
// Create a string variable
string greeting = "Hello";
// Output string value
cout << greeting;
35. Write a C++ program to calculate the total cost of items in a
store. The program should:
1.Create an integer variable items initialized to 50.
2.Create a double variable cost_per_item initialized to 9.99.
3.Calculate the total cost as total_cost = items *
cost_per_item.
4.Create a char variable currency initialized to '$'.
5.Print the number of items, cost per item, and the total cost
with the currency symbol.
36. C++ Operators
• Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Example
int x = 100 + 50;
• Although the + operator is often used to add together two values, like in the example above, it
can also be used to add together a variable and a value, or a variable and another variable:
Example
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
37. C++ divides the operators into the following
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Bitwise operators
38. Assignment Operators
• Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the value 10 to a variable
called x:
Example
int x = 10;
The addition assignment operator (+=) adds a value to a variable:
Example
int x = 10;
x += 5;
40. Comparison Operators
• Comparison operators are used to compare two values (or variables). This is
important in programming, because it helps us to
fi
nd answers and make decisions.
• The return value of a comparison is either 1 or 0, which means true (1) or false (0).
In the following example, we use the greater than operator (>) to
fi
nd out if 5 is greater
than 3:
Example
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater than 3
42. Logical Operators
• As with comparison operators, you can also test for true (1) or false (0) values with logical
operators.
Logical operators are used to determine the logic between variables or values: