C++ Programs C++ Programming Examples - C++ Programming Tutorial
C++ Course / C++ Programs / C++ Programs C++ Programming Examples

C++ Programs C++ Programming Examples

BLUF: Mastering C++ Programs C++ Programming Examples is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Programs C++ Programming Examples

C++ is renowned for its efficiency. Learn how C++ Programs C++ Programming Examples enables low-level control and high-performance computing in the tutorial below.

C++ coding tasks are commonly requested during interviews. These tasks may cover fundamental concepts as well as topics like arrays, strings, pointers, linked lists, file handling, and more. Now, let's explore a compilation of key C++ programs.

1) Fibonacci Series

To generate the Fibonacci series in C++, we can create two separate programs - one using recursion and the other without using recursion.

  1. Fibonacci Series without Recursion:
Example

#include <iostream>
using namespace std;

int main() {
    int n, first = 0, second = 1, next;

    cout << "Enter the number of terms: ";
    cin >> n;

    cout << "Fibonacci Series: ";

    for (int i = 0; i < n; i++) {
        if (i <= 1)
            next = i;
        else {
            next = first + second;
            first = second;
            second = next;
        }
        cout << next << " ";
    }

    return 0;
}
  1. Fibonacci Series using Recursion:
Example

#include <iostream>
using namespace std;

int fibonacci(int n) {
    if (n <= 1)
        return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
    int terms;

    cout << "Enter the number of terms: ";
    cin >> terms;

    cout << "Fibonacci Series: ";
    for (int i = 0; i < terms; i++) {
        cout << fibonacci(i) << " ";
    }

    return 0;
}

Input: 10

Output: 0 1 1 2 3 5 8 13 21 34

2) Prime number

Write a c++ program to check prime number.

Input: 17

Output: not prime number

Input: 57

Output: prime number

3) Palindrome number

Write a c++ program to check palindrome number.

Input: 121

Output: not palindrome number

Input: 113

Output: palindrome number

4) Factorial

Create a C++ program that calculates and displays the factorial of a given number.

Input: 5

Output: 120

Input: 6

Output: 720

5) Armstrong number

Write a c++ program to check armstrong number.

Input: 371

Output: armstrong

Input: 342

Output: not armstrong

6) Sum of Digits

Write a c++ program to print sum of digits.

Input: 23

Output: 5

Input: 624

Output: 12

7) Reverse Number

Write a c++ program to reverse given number.

Input: 234

Output: 432

8) Swap two numbers without using third variable

Create a C++ program to exchange the values of two numbers without the need for a third variable.

Example

#include <iostream>
using namespace std;

int main() {
    int num1, num2;

    cout << "Enter two numbers: ";
    cin >> num1 >> num2;

    cout << "Before swapping - num1: " << num1 << ", num2: " << num2 << endl;

    num1 = num1 + num2;
    num2 = num1 - num2;
    num1 = num1 - num2;

    cout << "After swapping - num1: " << num1 << ", num2: " << num2 << endl;

    return 0;
}

Input: a=5 b=10

Output: a=10 b=5

9) Matrix Multiplication

Develop a C++ program to display the multiplication result of two matrices.

Input:

Example

first matrix elements:

1 2 3

1 2 3

1 2 3

second matrix elements

1 1 1

2 1 2

3 2 1

Output:

Output

multiplication of the matrix:

14 9 8

14 9 8

14 9 8

10) Decimal to Binary

Develop a C++ program that converts a decimal number into its binary equivalent.

Here is a sample code snippet to achieve this conversion:

Example

#include <iostream>
using namespace std;

void decimalToBinary(int decimalNum) {
    int binaryNum[32];
    int i = 0;
    
    while (decimalNum > 0) {
        binaryNum[i] = decimalNum % 2;
        decimalNum = decimalNum / 2;
        i++;
    }
    
    for (int j = i - 1; j >= 0; j--) {
        cout << binaryNum[j];
    }
}

int main() {
    int decimalNum;
    cout << "Enter a decimal number: ";
    cin >> decimalNum;
    
    cout << "Binary equivalent: ";
    decimalToBinary(decimalNum);
    
    return 0;
}

In this program, the decimalToBinary function takes a decimal number as input and converts it into binary by repeatedly dividing by 2 and storing the remainders in an array. Finally, the binary equivalent is printed by iterating through the array in reverse order.

Input: 9

Output: 1001

Input: 20

Output: 10100

11) Alphabet Triangle

Write a c++ program to print alphabet triangle.

Output:

Output

A

    ABA

   ABCBA

  ABCDCBA

 ABCDEDCBA

12) Number Triangle

Write a c++ program to print number triangle.

Input: 7

Output:

Output

enter the range= 6

      1

     121

    12321

   1234321

  123454321

 12345654321

13) Fibonacci Triangle

Create a C++ program to display a Fibonacci triangle.

Input: 5

Output:

Output

1

1	1	

1	1	2	

1	1	2	3	

1	1	2	3	5

14) Number in Characters

Create a C++ program that converts a number into its equivalent in words.

Input: 74254

Output: Seven Four Two Five Four

Input: 203

Output: two zero three

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience