Some Basic C++ Programs
Some Basic C++ Programs
com/cpp-programming/examples/largest-number-
among-three
https://www.slideshare.net/AdilAslam4/object-oriented-programming-using-c-
slides-13-69148519
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.
If you haven't already set up the environment to run C++ on your computer, visit Install C++ on
Your Computer.
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
Run Code
Output
Hello World!
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 function.
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.
The return 0; statement is the "Exit status" of the program. In simple terms, the
program ends with this statement.
int main() {
// Write your code here
}
int main() {
int number;
Output
Enter an integer: 23
You entered 23
When the user enters an integer, it is stored in variable number using cin.
Starting from this example, we will be using the std namespace using the code:
This will allow us to write cout, cin, endl, etc. instead of std::cout, std::cin, std::endl
respectively.
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++.
int main() {
// prints sum
cout << first_number << " + " << second_number << " = " << sum;
return 0;
}
Run Code
Output
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.
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.
Example: Compute quotient and remainder
#include <iostream>
using namespace std;
int main()
{
int divisor, dividend, quotient, remainder;
return 0;
}
Run Code
Output
Enter dividend: 13
Enter divisor: 4
Quotient = 3
Remainder = 1
The division operator / computes the quotient (either between float or integer variables).
The modulus operator % computes the remainder when one integer is divided by another
(modulus operator cannot be used for floating-type variables).
sizeof(dataType);
Example: Find Size of a Variable
#include <iostream>
using namespace std;
int main()
{
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
return 0;
}
Output
Note: You may get different result if you are using a old computer.
int main()
{
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
return 0;
}
Output
Before swapping.
a = 5, b = 10
After swapping.
a = 10, b = 5
The contents of the first variable is copied into the temp variable. Then, the contents of second
variable is copied to the first variable.
Finally, the contents of the temp variable is copied back to the second variable which completes
the swapping process.
You can also perform swapping using only two variables as below.
int main()
{
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
return 0;
}
The output of this program is the same as the first program above.
Note: We can use multiplication and division instead of addition and subtraction. However, this
won't work if one of the numbers is 0.
int a = 5, b = 10;
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.
What this means is that, if you assign 'A' to a character variable, 65 is stored in that variable
rather than 'A' itself.
int main() {
char c;
cout << "Enter a character: ";
cin >> c;
cout << "ASCII Value of " << c << " is " << int(c);
return 0;
}
Output
Enter a character: p
ASCII Value of p is 112
When we explicitly print the integer value of a char type, it's corresponding ASCII value is
printed.
int main() {
double num1, num2, product;
cout << "Enter two numbers: ";
return 0;
}
Run Code
Output
In this program, the user is asked to enter two numbers. These two numbers entered by the user
are stored in variable num1 and num2 respectively.
Then, the product of num1 and num2 is evaluated and the result is stored in variable product.
To understand this example, you should have the knowledge of the following C++ programming
topics:
And those integers that are not perfectly divisible by 2 are not known as odd numbers.
To check whether an integer is even or odd, the remainder is calculated when it is divided by 2
using modulus operator %. If the remainder is zero, that integer is even if not that integer is odd.
int main() {
int n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
Run Code
Output
Enter an integer: 23
23 is odd.
You can also use ternary operators ?: instead of if..else statement. The ternary operator is a
shorthand notation of if...else statement.
int main() {
int n;
(n % 2 == 0) ? cout << n << " is even." : cout << n << " is odd.";
return 0;
}
To understand this example, you should have the knowledge of the following C++ programming
topics:
Then this program finds out the largest number among three numbers entered by user and
displays it with proper message.
This program can be used in more than one way.
int main() {
float n1, n2, n3;
return 0;
}
Output
int main() {
float n1, n2, n3;
Output
int main() {
float n1, n2, n3;
return 0;
}
Output