Blog

  • C++ Program to Convert Decimal to Octal Number

    In this tutorial, we will write a program to convert decimal to octal in C++. Before that, you must have knowledge of the following topics in C++.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Let us go through a program for the Decimal to Octal Conversion in C++.


    C++ Program to Convert Decimal to Octal Number

    The program takes a user input for a decimal number. And then iterate using a while loop. You can also use for loop instead of while loop, the code remains the same. Lastly, display the equivalent octal value.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       int deci, oct[50], i = 0;
    
       cout << "Enter a Decimal number: ";
       cin >> deci;
    
       // calculation
       while (deci != 0)
       {
          oct[i] = deci % 8;
          i++;
          deci = deci / 8;
       }
    
       //Display the result
       cout << "Octal Value: ";
       for (i = (i - 1); i >= 0; i--)
          cout << oct[i];
    
       return 0;
    }

    Output:

    Enter a Decimal number: 8
    Octal Value: 10

    You may go through the following program for vice-versa:


  • C++ Program to Convert Octal to Decimal

    In this tutorial, we will write a program to convert octal numbers into decimal numbers in C++. Before that, you must have knowledge of the following topics in C++.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Let us go through a program for the Octal to Decimal Conversion in C++.


    C++ Program to Convert Octal to Decimal Number

    The program takes a user input for an octal number. And then iterate using a while loop. You can also use for loop instead of while loop, the code remains the same. Lastly, display the equivalent decimal value.

    One of the libraries math functions (pow()) is used. This function returns the value of power calculation maths. It is present in math.h header file.

    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main()
    {
       int oct, deci = 0, i = 0, rem;
    
       cout << "Enter the Octal Number: ";
       cin >> oct;
    
       while (oct != 0)
       {
          rem = oct % 10;
          deci = deci + (rem* pow(8, i));
          i++;
          oct = oct / 10;
       }
    
       cout << "Decimal Value: " << deci;
    
       return 0;
    }

    Output:

    Enter the Octal Number: 10
    Decimal Value: 8


    You can also achieve the octal to decimal conversion using a function in C++. The way to do that is by creating a separate function where you pass the user entered octal value as an argument in a function.

    After calculation, the function returns the decimal to the main function where the final result is displayed.

    //convert octal to decimal using function in C++
    
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    // User defined function
    int conversionFunc(int oct)
    {
       int rem, deci = 0, i = 0;
    
       while (oct != 0)
       {
          rem = oct % 10;
          oct = oct / 10;
          deci = deci + (rem* pow(8, i));
          ++i;
       }
    
       return deci;
    }
    
    //MAIN FUNCTION
    int main()
    {
       int octal, result;
       cout << "Enter an octal number: ";
       cin >> octal;
    
       //calling a function
       result = conversionFunc(octal);
       cout << "Decimal value: " << result;
    
       return 0;
    }

    Output:

    Enter an octal number: 10
    Decimal value: 8

    You may go through the following program for vice-versa:


  • C++ Program to Convert Binary to Decimal

    In this tutorial, we will write a program to convert a binary number into decimal in C++. Before that, you must have knowledge of the following topics in C++.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed by the combination of 0 to 9 digits such as 24, 345, etc.

    Here is the chart where for the equivalent value of decimal and binary numbers.

    Decimal to binary

    Now let us go through a program for the conversion of binary to decimal in C++.


    C++ Program to Convert Binary to Decimal

    The program asks the user to enter a binary number and using a while loop it calculates the decimal value. Lastly, display the equivalent decimal value.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       int bin, deci = 0, i = 1, rem;
    
       cout << "Enter a Binary Number: ";
       cin >> bin;
    
       while (bin != 0)
       {
          rem = bin % 10;
          deci = deci + (rem *i);
          i = i * 2;
          bin = bin / 10;
       }
    
       //Display the equivalent decimal number
       cout << "Decimal Value: " << deci;
    
       return 0;
    }

    Output:

    Enter a Binary Number: 1010
    Decimal Value: 10

    Also, check out a similar program:


  • C++ Program to Convert Decimal to Binary

    In this tutorial, we will learn to convert a decimal number into binary in C++. Before that, you must have knowledge of the following topics in C++.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Decimal Number

    These are the numbers with a base 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Here is the chart where for the equivalent value of decimal and binary numbers.

    Decimal to binary

    Now let us go through a program for the conversion of decimal to binary in C++.


    C++ Program to Convert Decimal to Binary

    The program asks the user to enter a decimal number and using a for loop it calculates the binary value. Lastly, display the equivalent binary value.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       int bin[10], deci, i;
    
       cout << "Enter the decimal number: ";
       cin >> deci;
    
       for (i = 0; deci > 0; i++)
       {
          bin[i] = deci % 2;
          deci = deci / 2;
       }
    
       //Display the converted decimal number
       cout << "Binary value: ";
       for (i = i - 1; i >= 0; i--)
          cout << bin[i];
    
       return 0;
    }

    Output:

    Enter the decimal number: 10
    Binary value: 1010

    Also, check out a similar program:


  • C++ Program to Convert Fahrenheit to Celsius

    In this tutorial, we will learn the conversion of a temperature from Fahrenheit to Celsius (or Centigrade) in C++. Let us start by understanding the formula for the conversion of Fahrenheit into Celsius.

    Fahrenheit and Celsius, both are the unit for measuring the temperature. Fahrenheit is represented by oF and Celsius by oC.

    Formula to convert Fahrenheit into Celsius

    celsius = (fahrenheit – 32)*5/9

    We will write two programs for the conversion of Fahrenheit to Celsius in C++.

    1. Without the use of Function
    2. With the use of function

    Fahrenheit to Celsius in C++

    The program simply asks the user for the Fahrenheit temperature value in order to convert temperature from Fahrenheit to Celsius in C++. Then converts into its equivalent Celsius value.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       float f, c;
    
       cout << "Enter the Fahrenheit value: ";
       cin >> f;
    
       // conversion calculation
       c = (f - 32) * 5/9;
    
       cout << "Equivalent Celsius value: " << c;
    
       return 0;
    }

    Output:

    Enter the Fahrenheit value: 64
    Equivalent Celsius value: 17.7778


    C++ Program to convert Fahrenheit to Celsius using function

    Here, we create a separate user-defined function in C++ to convert Fahrenheit to Celsius. The value of Fahrenheit entered by the user is passed to a function as an argument. The function returns the value after the calculation of the celsius value.

    #include <iostream>
    using namespace std;
    
    // conversion function
    float conversion(float f)
    {
       float c;
    
       c = (f - 32) * 5/9;
       return c;
    }
    
    int main()
    {
       float f, c;
    
       cout << "Enter the Fahrenheit value: ";
       cin >> f;
    
       // calling function
       c = conversion(f);
    
       cout << "Equivalent Celsius value: " << c;
    
       return 0;
    }

    Output:

    Enter the Fahrenheit value: 80
    Equivalent Celsius value: 26.6667


  • C++ Program to Convert Celsius to Fahrenheit

    In this tutorial, we will learn the conversion of a temperature from Celsius to Fahrenheit in C++. Let us start by understanding the formula for the conversion of Fahrenheit into Celsius.

    Fahrenheit and Celsius, both are the unit for measuring the temperature. Fahrenheit is represented by oF and Celsius by oC.

    Formula to convert Celsius to Fahrenheit

    fahrenheit = ((celsius * 9/5) + 32)

    We will write two programs for the conversion of Celsius to Fahrenheit in C++.

    1. Without the use of Function
    2. With the use of function

    Celsius to Fahrenheit in C++

    The program simply asks the user for the Celsius temperature value in order to convert temperature from Celsius to Fahrenheit in C++. Then converts into its equivalent Fahrenheit value.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       float f, c;
    
       cout << "Enter the Celsius value: ";
       cin >> c;
    
       // conversion calculation
       f = (c * 9/5) + 32;
    
       cout << "Equivalent Fahrenheit value: " << f;
    
       return 0;
    }

    Output:

    Enter the Celsius value: 35
    Equivalent Fahrenheit value: 95


    C++ Program to convert Celsius to Fahrenheit using function

    Here, we create a separate user-defined function in C++ to convert Celsius to Fahrenheit. The value of Celsius entered by the user is passed to a function as an argument. The function returns the value after the calculation of the Fahrenheit value.

    #include <iostream>
    using namespace std;
    
    // conversion function
    float conversion(float c)
    {
       float f;
    
       f = (c * 9/5) + 32;
       return f;
    }
    
    int main()
    {
       float c, result;
    
       cout << "Enter the Celsius value: ";
       cin >> c;
    
       // calling function
       result = conversion(c);
    
       cout << "Equivalent Fahrenheit value: " << result;
    
       return 0;
    }

    Output:

    Enter the Celsius value: 35
    Equivalent Fahrenheit value: 95


  • C++ Program to Check Leap Year

    In this tutorial, we will write a leap year program in C++. You may go through the following topics first in order to understand the problem.

    A leap year comes after every 4 years and has 366 days that year instead of 365 days. In the leap year, an additional day is added to the February month and it becomes 29 days instead of 28 days.

    Now let us understand through mathematical logic,

    • If a year is divisible by 4 then it is leap year.
    • If a year is divisible by 400 and not divisible by 100 then it is also a leap year.
    • Example: 2000, 2004, 2008, etc are the leap years.

    Question: write a c++ program to check whether a year is a leap year or not.

    We will learn two ways to do the program:

    • Within main function
    • With user-defined function

    C++ Program to Check Leap Year

    The program takes user input for the year that needed to be checked and check with the condition using an if-else statement

    //C++ Program to check for the Leap year
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int year;
    
       cout << "Enter a year to check: ";
       cin >> year;
    
       //checking for leap year
       if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
          cout << year << " is a leap year";
       else
          cout << year << " is not a leap year";
    
       return 0;
    }

    Output:

    //Run 1
    Enter a year to check: 2014
    2014 is not a leap year


    //Run 2
    Enter a year to check: 2024
    2024 is a leap year


    Check for leap year using a function in C++

    We will create a separate function to check for the leap year. The year entered by the user is passed to the function as an argument.

    #include <iostream>
    using namespace std;
    
    // function to check for leap year
    int checkLeapYear(int year)
    {
       if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
          return 1;
       else
          return 0;
    }
    
    // main function
    int main()
    {
       int yr, result;
    
       cout << "Enter the Year: ";
       cin >> yr;
    
       //calling a function
       result = checkLeapYear(yr);
    
       if (result == 1)
          cout << yr << " is a Leap Year";
       else
          cout << yr << " is NOT a Leap Year";
    
       return 0;
    }

    Output:

    Enter the Year: 1998
    1998 is NOT a Leap Year


  • Reverse a String in C++

    In this tutorial, we will write a C++ Program to Reverse a String. There are three different ways to reverse a string.

    • Using library function, reverse()
    • Using loops instead of reverse()
    • Using recursion

    However, using recursion is discussed in the next tutorial, you will get the link down below. Before beginning, you must be familiar with the following topics in C++.


    Reverse a String in C++

    The program takes user input for the string and then reverses it using various ways.

    1. C++ Program to Reverse a String using reverse() function

    Question: reverse a string in c++ using library function.

    The reverse() is a built-in function provided in C++ to reverse a string. This function is defined in an algorithm header file which needs to be included at the beginning of the program.

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
      string str;
    
      cout << "Enter the string: ";
      cin >> str;
    
      reverse(str.begin(), str.end());
    
      cout << "Reversed String: " << str;
    
      return 0;
    }

    Output:

    Enter the string: simple2code
    Reversed String: edocp2elmis


    2. How to Reverse a String in C++ using while loop

    Here the program uses a while loop to iterate and reverse the string.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      char str[50], temp;
      int length, i = 0, j;
    
      cout << "Enter the String: ";
      cin >> str;
    
      //getting the length of a string
      while (str[i] != '\0')
        i++;
    
      length = i;
      i = 0;
      j = length - 1;
    
      //swapping to reverse the string
      while (i < j)
      {
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
    
        i++;
        j--;
      }
    
      cout << "Reversed string " << str;
    
      return 0;
    }

    Output:

    Enter the String: programs
    Reversed string smargorp

    You can use strlen() function to calculate the length of a string and use that to iterate using a while loop.


    3. Reverse a String in C++ using for loops

    Here the program uses a for loop. The program uses strlen() function to calculate the length of a string then uses that value to iterate through the string.

    Since you are using one of the string built-in functions, make sure to include string.h header file in a program.

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    {
      char str[50], temp;
      int length, i = 0, j;
    
      cout << "Enter the String: ";
      cin >> str;
    
      //getting the length of a string
      length = strlen(str);
      j = length - 1;
    
      //swapping to reverse the string
      for (i = 0; i < j; i++, j--)
      {
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
      }
    
      cout << "Reversed string " << str;
    
      return 0;
    }

    Output:

    Enter the String: programs
    Reversed string smargorp

    You may go through the following program on a string.


  • C++ Program to Reverse a String using Recursion

    In this tutorial, we will write a Program to Reverse a String in C++ using recursion. Before beginning, you must be familiar with the following topics in C++.

    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.


    C++ Program to Reverse a String using Recursion

    #include <iostream>
    using namespace std;
    
    void reverseFunc(char *str); //function prototype
    
    //main function
    int main()
    {
      char str[] = "This is simple2code.com";
    
      cout << "Actual String: " << str << endl;
      cout << "\nReversed String: ";
    
      reverseFunc(str);
    
      return 0;
    }
    
    //recursion function
    void reverseFunc(char *str)
    {
      if (*str == '\0')  //base condition
      {
        return;
      }
      else
      {
        reverseFunc(str + 1);
        cout << *str;
      }
    }

    Output:

    Actual String: This is simple2code.com

    Reversed String: moc.edoc2elpmis si sihT

    You may go through the following program on a string.


  • C++ Program to Copy One String to Another

    We will write a C++ Program to Copy Strings. Before that, you may go through the following topics in C++.

    There are various ways to copy strings in C++. in this tutorial we will look at the following to copy strings.

    1. Using library function, strcpy()
    2. Without the use of strcpy() function

    C++ Program to Copy Strings

    The program takes user input for the string and then copy that string to another string in various ways shown below.

    1. C++ Program to Copy Strings Using strcpy() Function

    The strcpy() function takes two arguments, first where it should be copied and second what should be copied and returns the copied value.

    It is defined in string.h header file that needed to be included at the beginning of the program as shown below.

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    {
      char str1[50], str2[50];
    
      cout << "Enter the string: ";
      cin >> str1;
    
      // copy the string
      strcpy(str2, str1);
    
      cout << "Copied String (str2): " << str2;
    
      return 0;
    }

    Output:

    Enter the string: simple2code
    Copied String (str2): simple2code


    2. C++ Program to Copy Strings without using strcpy() Function

    Here we have used for loop instead of the library function. Also, you can perform the same operation using a while loop too.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      char str1[50], str2[50];
      int i;
    
      cout << "Enter the string: ";
      cin >> str1;
    
      // copy the string
      for (i = 0; str1[i] != '\0'; ++i)
      {
        str2[i] = str1[i];
      }
    
      str2[i] = '\0';
    
      cout << "Copied String (str2): " << str2;
    
      return 0;
    }

    Output:

    Enter the string: John
    Copied String (str2): John