0% found this document useful (0 votes)
3 views13 pages

Some Basic C++ Programs

The document provides a comprehensive guide on basic C++ programming concepts, including examples for creating a 'Hello, World!' program, user input, arithmetic operations, and control structures like if-else statements. It covers various topics such as finding the largest number among three inputs, checking even or odd numbers, and performing arithmetic operations like addition, subtraction, multiplication, and division. Each example is accompanied by code snippets and explanations to help beginners understand the syntax and functionality of C++.

Uploaded by

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

Some Basic C++ Programs

The document provides a comprehensive guide on basic C++ programming concepts, including examples for creating a 'Hello, World!' program, user input, arithmetic operations, and control structures like if-else statements. It covers various topics such as finding the largest number among three inputs, checking even or odd numbers, and performing arithmetic operations like addition, subtraction, multiplication, and division. Each example is accompanied by code snippets and explanations to help beginners understand the syntax and functionality of C++.

Uploaded by

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

https://www.programiz.

com/cpp-programming/examples/largest-number-
among-three

https://www.slideshare.net/AdilAslam4/object-oriented-programming-using-c-
slides-13-69148519

C++ "Hello, World!" Program


In this example, we will learn to create a simple program named "Hello World" in C++
programming.

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 function.

The execution of code beings from this function.


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++ Program to Print Number Entered by


User
In this example, you'll learn to print the number entered by a user using C++ cout statement.

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;
}

Output

Enter an integer: 23
You entered 23

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++.

C++ Program to Add Two Numbers


In this program, user is asked to enter two integers. Then, the sum of those two integers is stored
in a variable and displayed on the screen. Primary tabs
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;
}
Run Code

Output

Enter two integers: 4


5
4 + 5 = 9

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.

C++ Program to Find Quotient and


Remainder
In this example, you will learn to find the quotient and remainder of a given dividend and divisor.

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;

cout << "Enter dividend: ";


cin >> dividend;

cout << "Enter divisor: ";


cin >> divisor;

quotient = dividend / divisor;


remainder = dividend % divisor;

cout << "Quotient = " << quotient << endl;


cout << "Remainder = " << 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).

C++ Program to Find Size of int, float, double


and char in Your System
This program declares 4 variables of type int, float, double and char. Then, the size of each
variable is evaluated using sizeof operator.

To find the size of variable, sizeof operator is used.

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

Size of char: 1 byte


Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes

Note: You may get different result if you are using a old computer.

C++ Program to Swap Two Numbers


This example contains two different techniques to swap numbers in C programming. The first
program uses temporary variable to swap numbers, whereas the second program doesn't use
temporary variables.

Example 1: Swap Numbers (Using Temporary Variable)


#include <iostream>
using namespace std;

int main()
{
int a = 5, b = 10, temp;

cout << "Before swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

temp = a;
a = b;
b = temp;

cout << "\nAfter swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

return 0;
}
Output

Before swapping.
a = 5, b = 10

After swapping.
a = 10, b = 5

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
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.

Example 2: Swap Numbers Without Using Temporary


Variables
#include <iostream>
using namespace std;

int main()
{

int a = 5, b = 10;

cout << "Before swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

a = a + b;
b = a - b;
a = a - b;

cout << "\nAfter swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

return 0;
}

The output of this 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 b and store it in a with the code a = a + b. This means a = 5 + 10.
So, a = 15 now.
3. Then we use the code b = a - b. This means b = 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 have been swapped.

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;

// using multiplication and division for swapping


a = a * b; // a = 50
b = a / b; // b = 5
a = a / b; // a = 10

C++ Program to Find ASCII Value of a


Character
In this example, you will learn to find ASCII value of a character in C++.

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.

Resource: ASCII chart of all 127 characters in C++.

Example: Print ASCII Value in C++


#include <iostream>
using namespace std;

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.

C++ Program to Multiply two Numbers


In this program, user is asked to enter two numbers (floating point numbers). Then, the product of
those two numbers is stored in a variable and displayed on the screen.

C++Program to Multiply Two Numbers


#include <iostream>
using namespace std;

int main() {
double num1, num2, product;
cout << "Enter two numbers: ";

// stores two floating point numbers in num1 and num2 respectively


cin >> num1 >> num2;

// performs multiplication and stores the result in product variable


product = num1 * num2;

cout << "Product = " << product;

return 0;
}
Run Code

Output

Enter two numbers: 3.4


5.5
Product = 18.7

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.

Finally, the product is displayed on the screen.


C++ Program to Check Whether Number is
Even or Odd
In this example, if...else statement is used to check whether a number entered by the user is even
or odd.

To understand this example, you should have the knowledge of the following C++ programming
topics:

 C++ if, if...else and Nested if...else


 C++ Ternary Operator

Integers that are perfectly divisible by 2 are called even numbers.

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.

Example 1: Check Whether Number is Even or Odd using if


else
#include <iostream>
using namespace std;

int main() {
int n;

cout << "Enter an integer: ";


cin >> 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.

In this program, an if..else statement is used to check whether n % 2 == 0 is true or not.

If this expression is true, n is even. Else, n is odd.

You can also use ternary operators ?: instead of if..else statement. The ternary operator is a
shorthand notation of if...else statement.

Example 2: Check Whether Number is Even or Odd using


ternary operators
#include <iostream>
using namespace std;

int main() {
int n;

cout << "Enter an integer: ";


cin >> n;

(n % 2 == 0) ? cout << n << " is even." : cout << n << " is odd.";

return 0;
}

C++ Program to Find Largest Number


Among Three Numbers
In this example, you'll learn to find the largest number among three numbers using if, if else and
nested if else statements.

To understand this example, you should have the knowledge of the following C++ programming
topics:

 C++ if, if...else and Nested if...else

In this program, user is asked to enter three numbers.

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.

Example 1: Find Largest Number Using if Statement


#include <iostream>
using namespace std;

int main() {
float n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if(n1 >= n2 && n1 >= n3)


cout << "Largest number: " << n1;

if(n2 >= n1 && n2 >= n3)


cout << "Largest number: " << n2;

if(n3 >= n1 && n3 >= n2)


cout << "Largest number: " << n3;

return 0;
}

Output

Enter three numbers: 2.3


8.3
-4.2
Largest number: 8.3

Example 2: Find Largest Number Using if...else Statement


#include <iostream>
using namespace std;

int main() {
float n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if((n1 >= n2) && (n1 >= n3))


cout << "Largest number: " << n1;
else if ((n2 >= n1) && (n2 >= n3))
cout << "Largest number: " << n2;
else
cout << "Largest number: " << n3;
return 0;
}

Output

Enter three numbers: 2.3


8.3
-4.2
Largest number: 8.3

Example 3: Find Largest Number Using Nested if...else


statement
#include <iostream>
using namespace std;

int main() {
float n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if (n1 >= n2) {


if (n1 >= n3)
cout << "Largest number: " << n1;
else
cout << "Largest number: " << n3;
}
else {
if (n2 >= n3)
cout << "Largest number: " << n2;
else
cout << "Largest number: " << n3;
}

return 0;
}

Output

Enter three numbers: 2.3


8.3
-4.2
Largest number: 8.3

You might also like