C++ Programs Examples with Output – Programming Code Examples
C++

C++ Programs Examples with Output41 min read

C++ is one of the most powerful and high-level programming language. The main purpose of C ++ programming was to add object-orientation to the C language.

To understand a programming language you must practice the programs, this way you can learn the language faster. This page includes c++ programs on various c++ topics such as control statements, loops, classes & objects, functions, arrays etc. All the programs are tested and provided with the output.




Here is the list of all C++ basic program we have created for you that might help you to learn the language quickly. Try running and modifying the program in Online C++ Editor to better understand.

C++ Basic Examples

C++ program to print hello world

A “Hello world!” program is usually a computer program that displays the message “Hello world!”. It is very simple in most of the programming languages.

Let see how to write hello world program in c++.

Output:

 

C++ program to print to console

The standard C++ library iostream provides three built-in ways to print into the console.

  1. cout
  2. clog
  3. cerr

Cout

It is used to print any kind of message or data on the console. Unlike C language here we don’t need to provide the type of data we want to print.

Output:

Clog

Clog is much similar to cout but it is mainly used for logging.

Output:

Cerr

Cerr also produces output like cout, but the main purpose of cerr is to print errors on the console.

Output:

C++ basic input output Integer

Unlike C language C++ does not require to specify the type of data for IO (Input/Output) operation. You can directly print any desirable data to the console using cout with the insertion operator (<<). Similarly, for taking input you can use cin with the extraction operator(>>).

Output:

 

C++ basic input output string

To read string input from the user getline method can be used with the string datatype. You can also use cin for reading string input, but cin will only read string without space, once it encounters space it stops reading further characters.

Output:

C++ addition of two user given numbers

This is an example program that demonstrates how you can perform the addition of two user given numbers in C++.

In this program first we are taking two integer numbers from the user, and then displaying the result of addition into the console.

Output:

 

C++ swap two numbers using temporary variable

Swapping numbers

Swapping numbers means exchanging the values between two or more variables. In this program, we are going to see how we can swap two user-given number with the help of one temporary variable.

Logic

  1. Assign variable a’s value to variable temp, so now the temp is holding a’s original value.
  2. Assign b’s value to a.
  3. Assign temp’s (holding a) value to b.

Output:

 

C++ swap two numbers without temporary variable

Swapping numbers

Swapping numbers means exchanging the values between two or more variables. In this program we are going to swap two numbers without using any temporary variable.

Logic

  1. Store addition of variable a and b (a+b) to variable a.
  2. Now extract b from a and store it to b.
  3. Extract b from a and store it to a.

Now b is holding a’s original value and similarly a is holding b’s original value.


Output:

 

C++ program to calculate area of a circle

Area of a circle

The area of a circle is the number of square units within the circle. To calculate the area of a circle the standard formula is: Area = Pi R Square

Area of circle = (A = πr²)

Logic

Here we have taken radius as input from the user, and to calculate the area of the circle we have to multiply the value of pi with the square of the radius.

Value of Pi = (3.14159)

Output:

 

C++ program to calculate simple interest

Simple interest is a simple way to calculate the interest on a loan. Simple interest is determined by multiplying the principal by the daily interest rate multiplied by the number of days elapsed between payments. – Investopedia

Output:

 

C++ program to calculate compound interest

Compound interest (or compounding interest) is the interest calculated on the principal, including all cumulative interest on deposits or loans in the previous period. – Investopedia

Output:

 

C++ program to find ASCII value of a character

ASCII (American Standard Code for Information Interchange) is the character encoding standard of electronic communication. ASCII codes represent text within computers, telecommunications equipment, and other devices. – Wikipedia

Output:

 

C++ calculate year week and days from given total days

In this C++ program, we are going to see how we can calculate Year, Week and Days from given total days. We all know that 1 year = 365 (ignoring leap year) and 1 week = 7 days on this basis we can determine the year, week and days from given total no of days.

Output:

 

C++ program to print size of different datatypes

This program shows how you can print the size of any data type available in C++ language. To find the size of any data type, C and C++ have one special operator sizeof. It simply calculates and returns the size of any given data type in bytes.

Output:

 

C++ If Else Examples

C++ check whether a character is alphabet or not

In this program, we are going to see whether a user given character is an alphabet or not. To determine the type of character, we have to check it’s ASCII value range.

Output:

 

C++ check whether a character is alphabet, digit or special character

This program is much similar to the previous one but here we are checking whether the given character is an alphabet, digit or a special character.

Output:

 

C++ find largest number among three number using if statement

In this program, we are going to see how to find the largest number among three numbers using if statement.

Logic

Here we have to compare each number with another two numbers, if it is greater than the both then simply print it.

Let’s say A = 11, B = 22 and C = 15

Then steps would be

  1. if A > B and A > C that means A is the largest number.
  2. if B > A and B > C that means B is the largest number.
  3. Similarly if C > A and C > B that means C is the largest number.

Output:

 

C++ find largest number among three number using if else statement

This program is similar to the previous one, with a single modification, here we will use if else statement to find the largest number.

Logic

The logic to find the largest number among the three numbers, we have to compare each number with the other two numbers.

Let three variables be: A = 400, B = 200 and C = 300

  1. if A > B and A > C, then print A.
  2. else if B > A and B > C, then print B.
  3. else print C.

Output:

 

C++ find largest number among three number using nested if else statement

In this program, we are going to find the largest number among three numbers, similar to the previous one, but it is nested if-else version.

Logic

Let three variables be: A = 400, B = 200 and C = 300

The logic goes like this:

  1. if A >= B then check for if A >= C, then print A else print C.
  2. else part: if B >= C then print B else print C.

Output

 

C++ program to check whether a year is leap year or not

Leap year

A leap year is a calendar year that includes an additional day to synchronize the calendar year with the astronomical or seasonal year. – Wikipedia

Logic

The Logic to check this is quite simple. We only need to check if the given year is multiple of 4 or 400, but it should not be multiple of 100.

Output

 

C++ program to check whether number is even or odd

Even number

Even numbers are numbers that have a difference of 2 unit or number. In other words, if the number is completely divisible by 2 then it is an even number.

Odd number

Opposite of even numbers, odd numbers are having a difference of 3 unit or number. In other words, if the number is not completely divisible by 2 then it is an odd number.

Logic

To find if a number is even or odd we only need to check if the given number is multiple of 2 or not, If it is multiple of 2 that means it is even number otherwise an odd number.

To check if it is multiple of 2 or not we use modulus operator.

If the expression number % 2 returns 0 that means the number is a multiple of 2. In other words, it is completely divisible by 2.

Output

 

C++ check whether a number is negative, positive or zero

Positive number

All the numbers greater than 0, but not equal to 0 are positive numbers.

Negative number

Similarly all the number less than 0, but not equal to 0 are negative numbers.

Logic

If the number is greater than zero that means it is positive. If the number is less than zero that means it is negative. If the number is equal to zero that means it is absolute zero.

  1. if number > 0 then print Positive.
  2. if number < 0 then print Negative.
  3. if number == 0 then print Zero.

Output

 

C++ check whether a character is upper or lowercase alphabet

If the ASCII value of a character lies between 65 (A) to 90 (Z) then it is an uppercase character or if the character’s ASCII value lies between 97 (a) to 122 (z) then it is a lowercase character.

Output

 

C++ check whether a character is vowel or consonant

There are five vowel characters {a, e, i, o, u}. If the user given character input is one of them that means it is a vowel otherwise it is a consonant.

Logic

Here we have to manually check the given character with all the vowel characters, we cannot use ASCII value range to determine whether it is a vowel or a consonant. The given character can also be in the form of uppercase.

Output

 

C++ print day name of week from number

In this program, we are going to print day name based on week no. Like – If the user enters 1 that means it is Monday.

Logic

After taking input (Week no) from the user, we have to compare that number with 1 to 7 or we can say comparing that number with a day of a week. So the logic goes like – if the number is equal to 1 then it is Monday, if the number is 2 then it is Tuesday etc… Like that we have to compare with each day of the week.

Output

C++ Loop Examples

C++ program to print all odd numbers from 1 to N

Odd number

Opposite of even numbers, odd numbers are having a difference of 3 unit or number. In other words, if the number is not completely divisible by 2 then it is an odd number.

Logic

The logic for printing odd numbers are pretty simple and straight forward, we only need to check if the number is not divisible by 2. If the number is not divisible by 2, then we have to print it.

Output

 

C++ program to calculate sum of first N odd numbers

Odd number

The Opposite of even numbers. Odd numbers have a difference of 3 unit or number. In other words, if the number is not completely divisible by 2 then it is an odd number.

Output

 

C++ program to print all even numbers from 1 to N

Even number

Even number are numbers that have a difference of 2 unit or number. In other words, if the number is completely divisible by 2 then it is an even number.

Output

 

C++ program to calculate sum of first N even numbers

Even number

Even numbers are numbers that have a difference of 2 unit or number. In other words, if the number is completely divisible by 2 then it is an even number.

Output

 

C++ program to print first N natural numbers using for loop

Natural number

Natural numbers are numbers that are common and clearly in nature. As such, it is a whole, nonnegative number.

Logic

To print the first N natural number we only have to run one single loop from 1 to N.

After taking input (num) from user start one loop from 1 to num, and then inside the loop simply print the current variable “i”.

Output

 

C++ program to calculate sum of first N natural numbers

Natural number

Natural numbers are numbers that are common and clear in nature. As such, it is a whole, non-negative number.

Logic

To print the sum of first N natural numbers, we need to run one loop from 1 to N, and each time inside the loop we have to add / sum value of “i” (current number) into one temporary variable.

Once the loop is over, outside of the loop we have to print a temporary variable containing the sum of natural numbers.

Output

 

C++ program to print all prime numbers between 1 to N

Prime number

A prime number is an integer greater than 1 whose only factors are 1 and itself. A factor is an integer that can be divided evenly into another number.

Logic

To print all the prime numbers up to N, we start one loop from 2 to N and then inside the loop we check current number or “num” is prime or not. To check if it is prime or not we again need one nested loop. It is not an efficient way to check prime number but it is simpler to understand the basic of looping in C++.

Output

 

C++ program to find sum of prime numbers between 1 to N

Prime number

A prime number is an integer greater than 1 whose only factors are 1 and itself. A factor is an integer that can be divided evenly into another number.

Logic

To print the sum of all prime numbers up to N we have to iterate through each number up to the given number and check if the number is a prime or not if it is a prime number then simply sum it or add it in one temporary variable.

Once the outer loop is completed we have to print that temporary variable containing the sum of primes.

 

C++ program to find all factors of a number

Factor

A factor is an integer that can be divided evenly into another number or in other words factors of a number are numbers that multiply to form a product.

Logic

To print all the factors of a particular number we have to iterate through all the smaller numbers up to the given number. If the user given number is completely divisible by any number then it is a factor of that number.

Output

 

C++ program to print alphabets from a to z

In this program, we are going to see how we can print alphabets in C++. The program is pretty simple and straight forward. Here ASCII value of the characters comes into the picture, we use the ASCII value of starting and end character to run the loop.

Logic

ASCII value of small letter “a” is 97 and for “z” it is 122, so we run the loop from 97 to 122 and then inside the loop we have to print the current character.

Output

 

C++ program to check whether a given number is perfect number or not

Perfect number

A perfect number is a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3.

Logic

To check if the number is perfect or not we have to run one loop from 1 to N and sum all the numbers between 1 to N, if the sum is equal to N then it is a perfect number.

Output

 

Output

 

Output

 

Output

 

Output

 

Output

 


Output

 


Output

 

Power of a number

The power of a number represents the number of times to use that number in a multiplication. Usually, power is represented with a base number and an exponent.

Logic

Let’s declare one temporary variable power with value 1, we have used the data-type long long so that it can hold a big long value.

To calculate the power of a number we need a base and an exponent value, we are taking these two values from the user, after taking input (base, exponent) from the user we start one loop from 1 to exponent.

Inside the loop, for every iteration, we multiply the base by power (base * power) and store the result again in variable power until the loop is completed.

Once the loop is over, simply print the variable power containing the resultant power value of a given number.

Output

 

Output

 

 

Leave a Comment