0% found this document useful (0 votes)
8 views19 pages

1 - C++

Uploaded by

rahimoo2926
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views19 pages

1 - C++

Uploaded by

rahimoo2926
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

C++ "HELLO, WORLD!

"
PROGRAM
A "Hello, World!" is a simple program that
outputs Hello, World! on the screen. Since it's
a very simple program, it's often used to
introduce a new programming language to a
newbie.
Let's see how C++ "Hello, World!" program
works.
If you haven't already set up the environment
to run C++ on your computer, visit Install C+
+ on Your Computer.
C++ "HELLO WORLD!"
PROGRAM
// Your First C++ Program

#include <iostream>

int main() {
std::cout << "Hello World!";
return 0;
}
Run Code
Output
Hello World!
WORKING OF C++ "HELLO
WORLD!" PROGRAM
1 . // 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.
2. #include <iostream>

The #include is a preprocessor 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.
3. int main() {...}

A valid C++ program must have the main() function. The curly braces indicate the start and the end of the
WORKING OF C++ "HELLO
WORLD!" PROGRAM
4. 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.


5. return 0;

The return 0; statement is the "Exit status" of the program. In simple terms, the program ends with this
statement.
THINGS TO TAKE AWAY
We use std:cout in order to print output on the screen.
We must include iostream if we want to use std::cout.
The execution of code begins from the main() function. This function is mandatory.
This is a valid C++ program that does nothing.

int main() {
// Write your code here
}
C++
PROGRA
M TO
PRINT
NUMBER
ENTERED
BY USER
C++ PROGRAM TO PRINT
NUMBER ENTERED BY USER
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.
However, using the std namespace is considered a bad practice and we highly recommend you drop
this practice once you've mastered the basics of C++.
In this program, the
user is asked to enter
two integers. These
C++ two integers are
stored in variables
PROGRA first_number and
M TO second_number
respectively.
ADD TWO Then, the variables
NUMBER are added using the +
S operator and stored in the
sum variable.
Finally, sum is
displayed on the
screen.
C++ PROGRAM TO FIND
QUOTIENT AND REMAINDER
In this program, the user is asked
to enter two integers (divisor and dividend)
and the quotient and the remainder
of their division is computed.
To compute quotient and remainder,
both divisor and dividend should be
integers.
The division operator /
computes the
C++ quotient (either
PROGRAM between float or
TO FIND integer variables).
QUOTIENT The modulus operator
% computes the
AND remainder when one
REMAIND integer is divided by
ER another (modulus
operator cannot be
used for floating-
type variables).
C++
PROGRAM
TO FIND
SIZE OF INT,
FLOAT,
DOUBLE AND
CHAR IN
YOUR
SYSTEM
Swap Numbers (Using Temporary Variable)
To perform swapping in
above example, three
variables are used.
The contents of the first
variable is copied into
the temp variable. Then,
the contents of second
C++ variable is copied to the
PROGRAM first variable.
Finally, the contents of
TO SWAP the temp variable is
TWO copied back to the
second variable which
NUMBERS completes the swapping
process.
You can also perform
swapping using only two
variables as below.
The output of this
Swap Numbers Without Using Temporary Variables
program is the same
as the first program
above.
Let us see how this
program works:
1.Initially, a = 5 and b = 10.
2.Then, we add a and
C++ b and store it in a with
PROGRAM the code a = a + b. This
means a = 5 + 10 . So, a = 15

TO SWAP now.
TWO 3.Then we use the
code b = a - b. This means b =
NUMBERS 15 - 10 . So, b=5 now.
4.Again, we use the
code a = a - b. This means a =
15 - 5 . So finally, a = 10 .
Hence, the numbers
Swap Numbers Without Using Temporary Variables

C++
PROGRAM
TO SWAP
TWO
NUMBERS
C++ PROGRAM TO FIND ASCII
VALUE OF A CHARACTER
A character variable holds ASCII value
(an integer number between 0 and 127)
rather than that character itself in C
programming.
That value is known as ASCII value.
For example, ASCII value of 'A' is 65.
What this means is that, if you assign 'A'
to a character variable,
65 is stored in that variable rather than 'A' itself.
C++
PROGRAM
TO FIND
ASCII
VALUE OF
A
CHARACTE
R
C++
PROGRAM
TO FIND
ASCII
VALUE OF
A
CHARACTE
R
Print ASCII Value in C++

C++
PROGRAM
TO FIND
ASCII
VALUE OF
A
CHARACTE
R
In this program, the
user is asked to
enter two numbers.
These two numbers
entered by the user
C++ are stored in
PROGRAM variable num1 and
TO num2 respectively.
Then, the product of
MULTIPLY num1 and num2 is
TWO evaluated and the
NUMBERS result is stored in
variable product.
Finally, the
product is displayed
on the screen.

You might also like