Category: CPlusPlus Programs

c++ logo

  • C++ Program to Print Prime numbers in a Given Range

    In this C++ programming example, we will write a C++ Program to display Prime Numbers between two intervals. Let us start by understanding what is a prime number.

    Prime Number:

    A Prime Number is a number that is only divisible by 1 and itself. Example: 2, 3, 5, 7, 11, 13, 17, etc. These numbers are only divisible by 1 and the number itself.

    Note:
    0 and 1 is not a prime number and 2 is the only even and smallest prime number.

    To understand the following program, you should have a basic idea about the following topics in C++ programming.


    C++ Program to Print Prime Numbers

    The program takes the user input for the starting number ( lower range) and the end number (upper range). Then prints all the prime numbers that fall within that range.

    Also notice in the program that 0 and 1 are specifically checked to make sure that those two are not printed even if the lower range entered by the user is 0.

    Print Prime Numbers in a Given Range in C++.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int lrRange, upRange, i, flag;
    
      cout << "Enter the lower range: ";
      cin >> lrRange;
      cout << "Enter the upper range: ";
      cin >> upRange;
    
      cout << "\nList of Prime numbers between " << lrRange << " & " << upRange << ": " << endl;
    
      while (lrRange < upRange)
      {
        flag = 0;
    
        // checking for 0 and 1
        if (lrRange == 0 || lrRange == 1)
        {
          flag = 1;
        }
        else
        {
          for (i = 2; i <= lrRange / 2; ++i)
          {
            if (lrRange % i == 0)
            {
              flag = 1;
              break;
            }
          }
        }
    
        if (flag == 0)
          cout << lrRange << " ";
    
        ++lrRange;
      }
    
      return 0;
    }

    Output:

    Enter the lower range: 0
    Enter the upper range: 30

    List of Prime numbers between 0 & 30:
    2 3 5 7 11 13 17 19 23 29

    You can also print the prime numbers between 0 to any range or 100 using the same logic as above. You may go through the following program for more.


  • C++ Program to Check Whether a Number is Prime or Not

    In this C++ programming example, we will learn about the prime number program in C++. We specifically write a program to check the entered number is prime or not. Let us start by understanding what is a prime number.

    Prime Number:

    A Prime Number is a number that is only divisible by 1 and itself. Example: 2, 3, 5, 7, 11, 13, 17, etc. These numbers are only divisible by 1 and the number itself.

    Note:
    0 and 1 is not a prime number and 2 is the only even and smallest prime number.

    To understand the following program, you should have a basic idea about the following topics in C++ programming.


    Check Prime Number in C++

    The program takes a user input for a number that needed to be checked for prime number. Since 0 and 1 are not prime numbers so we first check them using the if statement. And if the number is other than 0 and 1 the execution control goes to the else part of the statement.

    C++ Program to Check Whether a Number is Prime or Not

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int i, num, flag;
    
      cout << "Enter a positive integer: ";
      cin >> num;
    
      // checking for 0 and 1
      if (num == 0 || num == 1)
      {
        flag = 1;
      }
      else
      {
        for (i = 2; i <= num / 2; ++i)
        {
          if (num % i == 0)
          {
            flag = 1;
            break;
          }
        }
      }
    
      if (flag == 0)
        cout << num << " is a prime number";
      else
        cout << num << " is NOT a prime number";
    
      return 0;
    }

    Output:

    Enter a positive integer: 13
    13 is a prime number

    You may go through the following program of prime numbers in C++.


  • C++ Program to find ASCII Value of a Character

    In this tutorial, you will learn how to find ASCII Value of a Character in C++. Before that, you need to have knowledge of the following in C++ programming.

    ASCII stands for American Standard Code for Information Interchange. It is a 7-bit character set that contains 128 (0 to 127) characters. It represents the numerical value of a character.

    Example: ASCII value for character A is 65, B is 66 but a is 97, b is 98, and so on.


    C++ Program to find ASCII Value of a Character

    #include <iostream>
    using namespace std;
    
    int main() 
    {
     char ch;
     
     cout << "Enter a character: ";
     cin >> ch;
     
     cout << "ASCII Value of " << ch << ": " << int(ch);
     
     return 0;
    }

    Output:

    //Run 1
    Enter a character: q
    ASCII Value of q: 113

    //Run 2
    Enter a character: Q
    ASCII Value of Q: 81

    In the above program to print ASCII value in C++, there is an explicit conversion of character into an integer. When the explicit conversion is performed to convert into an integer, its ASCII value is printed.


  • Fibonacci series using Recursion in C++

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

    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.

    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, the theory concept of recursion is the same for all programming languages.


    Fibonacci series using Recursion in C++

    #include <iostream>
    using namespace std;
    
    void recurFunc(int);  //function prototype
    
    int main()
    {
      int num;
    
      cout << "Enter the number of elements: ";
      cin >> num;
    
      cout << "Fibonacci Series: ";
      cout << "0 " << "1 ";
    
      //calling function
      //since two numbers are alredy printed so (n-2)
      recurFunc(num - 2);
    
      return 0;
    }
    
    void recurFunc(int n)
    {
      static int term1 = 0, term2 = 1, next;
    
      if (n > 0)
      {
        next = term1 + term2;
        term1 = term2;
        term2 = next;
    
        cout << next << " ";
    
        recurFunc(n - 1);
      }
    }

    Output:

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

    You may go through the following:


  • 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++: