Blog

  • C++ Program to Display Fibonacci Series

    In this tutorial, we will write a Fibonacci Series program in C++. Before that, you should have knowledge of the following topic in C++.

    What is Fibonacci Series?

    Fibonacci series is the series of numbers where the next number is achieved by the addition of the previous two numbers. The initial addition of two numbers is 0 and 1.

    For example: 0,1,1,2,3,5,8,13….etc.

    We will learn two ways to display the Fibonacci series in C.

    1. upto n terms
    2. upto certain number.

    1. Fibonacci Series up to n Number of Terms in C++

    The program takes user input for the number terms to be displayed, for loop is used to iterate and print the number. Also, if statements are used for the first two numbers (0, 1).

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int num, term1 = 0, term2 = 1, nextTerm = 0;
    
      cout << "Enter the number of terms: ";
      cin >> num;
    
      cout << "Fibonacci Series: ";
    
      for (int i = 1; i <= num; ++i)
      {
        // if is used for first two numbers in series
        if (i == 1)
        {
          cout << term1 << ", ";
          continue;
        }
    
        if (i == 2)
        {
          cout << term2 << ", ";
          continue;
        }
    
        nextTerm = term1 + term2;
        term1 = term2;
        term2 = nextTerm;
    
        cout << nextTerm << ", ";
      }
    
      return 0;
    }

    Output:

    Enter the number of terms: 5
    Fibonacci Series: 0, 1, 1, 2, 3,


    2. C++ Programs to display Fibonacci Series up to a certian number

    In this program, series is displayed up to an entered number. For example, if the user types 50 as an input then the program will display the series of terms present up to 50as shown below.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int num, term1 = 0, term2 = 1, next = 0;
    
      cout << "Enter a positive number: ";
      cin >> num;
    
      // for first two numbers
      cout << "Fibonacci Series: " << term1 << ", " << term2 << ", ";
    
      next = term1 + term2;
    
      while (next <= num)
      {
        cout << next << ", ";
    
        term1 = term2;
        term2 = next;
        next = term1 + term2;
      }
    
      return 0;
    }

    Output:

    Enter a positive number: 50
    Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

    You may go through the following:


  • C++ Program to find Factorial of a Number using recursion

    In this tutorial, we will write a Factorial Program in C++ using recursion. Let us start by understanding what is factorial of a number and recursion.

    Factorial of n number: Factorial of n number is the product of all the positive descending integers and is denoted by n!. Also factorial of 0 and 1 is 1.

    Recursion refers to the process when a function calls itself inside that function directly or indirectly or in a cycle.

    If you want to learn more about recursion in detail, click here. Although it is for C programming but the theory concept of recursion is the same for all programming languages.

    Example:

    factorial of n (n!) = n * (n-1) * (n-2) * (n-3)….1
    factorial of 5 (n!) = 5 * 4 * 3 * 2 * 1
    NOTE: Factorial of 0 (0!) = 1


    Factorial program using recursion in C++

    The program takes input from the user. The entered number is passed to a recursion function and returned the value back to the main function where the final result is displayed.

    #include <iostream>
    using namespace std;
    
    int factorialFunc(int);
    
    int main()
    {
       int fact, num;
    
       cout << "Enter a positive integer: ";
       cin >> num;
    
       //calling factorial function
       fact = factorialFunc(num);
    
       cout << "Factorial of " << num << " is: " << fact << endl;
       return 0;
    }
    
    int factorialFunc(int n)
    {
       if (n == 0)
          return (1);  //base condition
       else
       {
          return (n* factorialFunc(n - 1));	//recursion
       }
    }

    Output:

    Enter a positive integer: 5
    Factorial of 5 is: 120

    You may go through the following:


  • C++ Program to Find Factorial of a Number

    In this tutorial, we will write a Factorial Program in C++. Let us start by understanding what is factorial of a number.

    Factorial of n number: Factorial of n number is the product of all the positive descending integers and is denoted by n!. Also factorial of 0 and 1 is 1.

    Example:

    factorial of n (n!) = n * (n-1) * (n-2) * (n-3)….1
    factorial of 5 (n!) = 5 * 4 * 3 * 2 * 1
    NOTE: Factorial of 0 (0!) = 1

    We will write three different factorial programs in C++.

    • using while loop
    • using for loop
    • using function

    C++ program to calculate factorial of a number

    The user needs to enter the number for which the factorial needs to be calculated. The calculation is done using a while loop in C++.

    Source code: c++ program to find factorial of a number using while loop.

    #include<iostream>
    using namespace std;
     
    int main()
    {
        int num, fact = 1, i = 1;
         
        cout << "Enter an Integer: ";
        cin >> num;
         
        //calculation
        while(i <= num)
        {
            fact = fact * i;
            i++ ;
        }
         
        cout << "\nThe Factorial of " << num <<": " << fact;
        
        return 0;
    }

    Output:

    Enter an Integer: 5

    The Factorial of 5: 120


    Factorial program in C++ using for Loop

    It is the same as the above, the only difference is we use for loop instead of while loop in the following program.

    Source code:

    #include<iostream>
    using namespace std;
     
    int main()
    {
        int num, fact = 1, i = 1;
         
        cout << "Enter an Integer: ";
        cin >> num;
         
        //calculation
        for(i = 1; i <= num; i++)
        {
            fact *= i;
        }
         
        cout << "\nThe Factorial of " << num <<": " << fact;
        
        return 0;
    }

    Output:

    Enter an Integer: 4

    The Factorial of 4: 24


    Calculate the factorial of a number using function

    The program has a separate user-defined function that calculates the factorial of a number. The number entered by the user is passed as an argument to the function and after the calculation, the function returns the factorial value to the main function. The result is displayed on the main drive.

    #include<iostream>
    using namespace std;
    
    int factFunc(int n)
    {
        int i, fact = 1;
        
        for(i = n; i >= 1; i--)
            fact = fact*i;
            
        return fact;
    }
     
    int main()
    {
        int num, result;
         
        cout << "Enter an Integer: ";
        cin >> num;
        
        result = factFunc(num);
         
        cout << "\nThe Factorial of " << num <<": " << result;
        
        return 0;
    }

    Output:

    Enter an Integer: 6

    The Factorial of 6: 720


  • C++ Program to Check Whether Number is Even or Odd

    In this tutorial, we will write a C++ program to check if a given integer is even or odd. The following topic is used in the program, so you may go through them.

    Explanation: If the number is divisible by 2 and leaves the remainder 0 then the number is an even number else a number is an odd number.

    The program takes a number from the user as input and checks that number for odd or even. If..else statement is used to compute the result. Lastly, the result is displayed.


    C++ Program to Check Whether Number is Even or Odd usinf if else

    Source code: Even and Odd Program in C++

    #include <iostream>
    using namespace std;
    
    int main() 
    {
      int num;
    
      cout << "Enter an integer: ";
      cin >> num;
    
      if ( num % 2 == 0)
        cout << num << " is an even number.";
      else
        cout << num << " is an odd number.";
    
      return 0;
    }

    Output:

    //Run 1
    Enter an integer: 20
    20 is an even number.

    //Run2
    Enter an integer: 13
    31 is an odd number.


  • C++ Program to Swap Two Numbers without using Third Variable

    In this tutorial, we will write a program to swap numbers without using a temporary variable in C++. . There are two ways to swap variables.

    1. Using + and
    2. Using * and /

    Let us go through each of them with a C program. You may check out the following program.

    Both of the programs take the user input for two integers and swap those two values in two separate ways without using a temporary variable.


    C++ Program to Swap Two Numbers without using Third Variable

    1. Using + and –

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int num1, num2;
    
      cout << "Enter the first number: ";
      cin >> num1;
      cout << "Enter the second number: ";
      cin >> num2;
    
      num1 = num1 + num2;  //a = 10 + 20 = 30
      num2 = num1 - num2;  //b = 30 - 20 = 10
      num1 = num1 - num2;  //a = 30 - 10 = 20
    
      cout << "\nResult after Swapping Two Numbers:\n";
      cout << "num1: " << num1;
      cout << "\nnum2: " << num2;
    
      return 0;
    }

    Output:

    Enter the first number: 10
    Enter the second number: 20

    Result after Swapping Two Numbers:
    num1: 20
    num2: 10


    2. Using * and /

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int num1, num2;
    
      cout << "Enter the first number: ";
      cin >> num1;
      cout << "Enter the second number: ";
      cin >> num2;
    
      num1 = num1 * num2; //a = 10 * 20 = 200
      num2 = num1 / num2; //b = 200 / 20 = 10
      num1 = num1 / num2; //a = 200 / 10 = 20
    
      cout << "\nResult after Swapping Two Numbers:\n";
      cout << "num1: " << num1;
      cout << "\nnum2: " << num2;
    
      return 0;
    }

    Output:

    Enter the first number: 5
    Enter the second number: 10

    Result after Swapping Two Numbers:
    num1: 10
    num2: 5

    Note that the swapping using / and * will not work if one of the swapping values is zero (0).

    You may go through the following program in C++:


  • C++ Program to Swap Two Numbers

    This is the most basic of swapping two numbers in C++ program. This tutorial provides you with the source code to swap numbers in C++.

    There are basically two ways to swap numbers.

    • Using third Variable
    • Without using third variable.

    In this section, we will learn using the third variable and in the next section, you will learn without the use of the third variable.


    C++ Program to Swap Two Numbers using a Third Variable

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int num1, num2, temp;
    
      cout << "Enter num1: ";
      cin >> num1;
      cout << "Enter num2: ";
      cin >> num2;
    
      temp = num1;
      num1 = num2;
      num2 = temp;
    
      cout << "\nResult after Swapping Two Numbers:\n";
      cout << "num1: " << num1;
      cout << "\nnum2: " << num2;
    
      return 0;
    }

    Output:

    Enter num1: 10
    Enter num2: 20

    Result after Swapping Two Numbers:
    num1: 20
    num2: 10

    The program takes the two numbers from the users as input and swaps those values using a third or temporary variable. Finally, print the result of swapped values.

    You may go through the following program in C++:


  • C++ Program to Swap two Numbers using Functions

    In this tutorial, we will write a program for swapping of two numbers in C++ Using functions. The swapping number can be achieved in two ways through function.

    1. Call by Value
    2. Call by Reference

    You may click on the above link to go through the call by value and call by reference in C++.

    Explanation: Both of the programs take user input for the numbers to be swapped and pass it to the function as a parameter. The swapping computation is processed on the user-defined function


    2. C++ Program to Swap two Numbers using Call by Value

    #include <iostream>
    using namespace std;
    
    //user-defined function for swapping
    void swap(int x, int y)
    {
      int temp;
      temp = x;
      x = y;
      y = temp;
    
      cout << "\nResult after Swapping Two Numbers:\n";
      cout << "num1: " << x;
      cout << "\nnum2: " << y;
    }
    
    //main function
    int main()
    {
      int num1, num2;
    
      cout << "Enter num1: ";
      cin >> num1;
      cout << "Enter num2: ";
      cin >> num2;
    
      swap(num1, num2);
    
      return 0;
    }

    Output:

    Enter num1: 2
    Enter num2: 3

    Result after Swapping Two Numbers:
    num1: 3
    num2: 2


    2. C++ Program to Swap two Numbers using Call by Reference

    Since the call by reference deals with the address of the variable. Hence the use of pointer in the following program.

    #include <iostream>
    using namespace std;
    
    //user-defined function for swapping
    void swap(int *x, int *y)
    {
      int temp;
      temp = *x;
      *x = *y;
      *y = temp;
    }
    
    //main function
    int main()
    {
      int num1, num2;
    
      cout << "Enter num1: ";
      cin >> num1;
      cout << "Enter num2: ";
      cin >> num2;
    
      swap(&num1, &num2);
    
      cout << "\nResult after Swapping Two Numbers:\n";
      cout << "num1: " << num1;
      cout << "\nnum2: " << num2;
    
      return 0;
    }

    Output:

    Enter num1: 10
    Enter num2: 20

    Result after Swapping Two Numbers:
    num1: 20
    num2: 10

    You may go through the following program in C++:


  • C++ Program to Multiply two Numbers

    In this tutorial, we will write a C++ program to calculate the multiplication of two numbers. You may go through the following topics in C++ before writing a C++ program to find a product of two numbers.

    It is a simple program that takes two numbers from the user as input and multiplies them. We will use an arithmetic operator (*) to compute the product.

    Example: 3 * 5 = 15.


    C++ Program to Multiply two Numbers

    //C++ Program to Calculate Multiplication of two Numbers
    #include <iostream>
    using namespace std;
    
    int main()
    {
      double num1, num2, product;
    
      cout << "Enter the 1st number: ";
      cin >> num1;
    
      cout << "Enter the 2nd number: ";
      cin >> num2;
    
      //computing the product
      product = num1 * num2;
    
      cout << "\nProduct: " << product;
    
      return 0;
    }

    Output:

    Enter the 1st number: 5
    Enter the 2nd number: 8

    Product: 40

    You can change the data type as required to find the product of two numbers in C++.


  • C++ Program to Find Quotient and Remainder

    In this tutorial, we will write a C++ program to find the quotient and remainder of a given dividend and divisor.

    The program asked the user to enter the numbers (divisor and dividend). And to compute and find the quotient and remainder, we will use the division(/) and modulus(%) operators.


    C++ Program to Find Quotient and Remainder

    #include <iostream>
    using namespace std;
    
    int main()
    {    
       int denominator, numerator, quotient, remainder;
    
       cout << "Enter numerator: ";
       cin >> numerator;
    
       cout << "Enter denominator: ";
       cin >> denominator;
    
       quotient = numerator / denominator;
       remainder = numerator % denominator;
    
       cout << "\nQuotient: " << quotient << endl;
       cout << "Remainder: " << remainder;
    
       return 0;
    }

    Output:

    Enter numerator: 20
    Enter denominator: 10

    Quotient: 2
    Remainder: 0

    Enter numerator: 15
    Enter denominator: 4

    Quotient: 3
    Remainder: 3


  • C++ Program to Add Two Numbers

    In this tutorial, we will write a C++ program for the addition of two numbers. We can add the integers, floats, double, etc using the same program just changing the type.

    The program takes the user inputs for the number to be added then displays the result. We will see to add two numbers in three different ways in C++

    • Add two numbers in main function
    • Add two numbers using function
    • Add Two Numbers in using Class

    C++ Program to Add Two Numbers

    #include<iostream>
    using namespace std;
    
    int main()
    {
        int num1, num2, sum;
        
        cout << "Enter First Numbers: ";
        cin >> num1;
        
        cout << "Enter Second Numbers: ";
        cin >> num2;
        
        sum = num1 + num2;
        
        cout << "\nAddition Result: " << sum;
        
        return 0;
    }

    Output:

    Enter First Numbers: 5
    Enter Second Numbers: 3

    Addition Result: 8

    You can change the data type as required and use the same code.


    Add two numbers using Function in C++

    The program takes the user input the same as above, the only difference is the function is created for the calculation and return the result.

    #include<iostream>
    using namespace std;
    
    int add(int n1, int n2)
    {
        return (n1 + n2);
    }
    
    int main()
    {
        int num1, num2, sum;
        
        cout << "Enter First Numbers: ";
        cin >> num1;
        
        cout << "Enter Second Numbers: ";
        cin >> num2;
        
        sum = add(num1, num2);
        
        cout << "\nAddition Result: " << sum;
        
        return 0;
    }

    Output:

    Enter First Numbers: 20
    Enter Second Numbers: 5

    Addition Result: 25


    Add Two Numbers in C++ using Class

    Here separate class is created to get the values and add the numbers and return to the main function.

    #include <iostream>
    using namespace std;
    
    class AddClass
    {
       private:
          int num1, num2;
    
       public:
       void getData()
       {
          cout << "Enter First Numbers: ";
          cin >> num1;
    
          cout << "Enter Second Numbers: ";
          cin >> num2;
       }
    
       int add()
       {
          return (num1 + num2);
       }
    };
    
    int main()
    {
       int sum;
       AddClass obj;
    
       obj.getData();
       sum = obj.add();
    
       cout << "\nAddition result: " << sum;
    
       return 0;
    }

    Output:

    Enter First Numbers: 10
    Enter Second Numbers: 6

    Addition result: 16