Category: CPlusPlus Programs

c++ logo

  • 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


  • C++ Program to Compare Two Strings

    This is the C++ tutorial where we will write a C++ program to compare two strings. If you want to learn more about string in C++, click the link below.

    We will compare two strings in two ways:

    1. Compare using strcmp() Function
    2. Compare two strings without using strcmp() function

    C++ Program to Compare Two Strings

    Both of the programs below take user input for the two strings that are to compare. Although to take input from the user you can use gets(str) function that will take the string input with spaces in it.

    The following program uses cin>> method to take a string input. It only takes a single word.

    1. C++ Program to compare two strings without using strcmp()

    This program compare string in C++ without using strcmp() function. We sill use while loop to iterate through the string and inside while loop if loop is used to see if every letter is equal or not on both of the string.

    If it is not equal then the if statement is executed and a flag variable will raise to 1 and the execution control comes out of the loop with the use of break statement.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      char str1[50], str2[50];
      int i = 0, flag = 0;
    
      cout << "Enter the first string: ";
      cin >> str1;
      cout << "Enter the second string: ";
      cin >> str2;
    
      while (str1[i] != '\0' || str2[i] != '\0')
      {
        if (str1[i] != str2[i]) //if string are not equal
        {
          flag = 1;
          break;
        }
        i++;
      }
    
      if (flag == 0)
        cout << "\nStrings are Equal.";
      else
        cout << "\nStrings are not Equal.";
    
      return 0;
    }

    Output:

    Enter the first string: simple2code
    Enter the second string: simple2code

    Strings are Equal.


    2. C++ Program to compare two strings using strcmp()

    The following program compare string in C++ using strcmp() function that is provided in the C++ library. This inbuilt function is defined under string.h header file. Hence, it is necessary to include this file in the program at the beginning.

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    {
      char str1[50], str2[50];
    
      cout << "Enter the first string: ";
      cin >> str1;
      cout << "Enter the second string: ";
      cin >> str2;
    
      if (strcmp(str1, str2) == 0)
        cout << "\nStrings are Equal.";
      else
        cout << "\nStrings are not Equal.";
    
      return 0;
    }

    Output:

    Enter the first string: coding
    Enter the second string: coding

    Strings are Equal.

    Enter the first string: coding
    Enter the second string: Coding

    Strings are not Equal.

    As you can see, the comparison on both of the programs are also case sensitive, the word “coding” is not equal to the word “coding”. Since the first letter is a lower letter in one string and capital on another.


  • C++ Program to Find the Length of a String using Pointers

    This is the C++ tutorial where we will Write a C++ program to find length of string using pointer. If you want to learn more about Pointers and Strings in C++, click the link below.

    Explanation: The program takes the string value from the user as an input. we also declare a character pointer in a program. The address of the first character of a string is initialized to this pointer and then the length is calculated using while.

    You can also perform the same operation using for loop.

    • &: It is the address of operator.
    • *: It is the value at operator.

    C++ Program to Find the Length of a String using Pointers

    #include <iostream>
    using namespace std;
    
    int main()
    {
      char str[100], *ptr;
      int length = 0;
    
      cout << "Enter the String: ";
      cin >> str;
    
      //initializing the pointer
      ptr = &str[0];
    
      while(*ptr)
      {
         length++;
         ptr++;
      }
    
      cout << "\nLength of a string: " << length;
    
      return 0;
    }

    Output:

    Enter the String: simple2code

    Length of a string: 11

    However, if you want to enter the string with spaces, use gets(str) instead of cin >>str.

    Also, you may go through the following program on string:


  • C++ Program to Find the Length of a String

    This is the C++ tutorial where we will write a c++ program to find the length of a string using strlen() function. If you want to learn more about string in C++, click the link below.

    Explanation: The program takes the string value from the user as an input and then using one of the built-in functions strlen() present in C++ calculates the length of a string.

    Strlen(): This built-in function takes a string and returns the length of a string. In other words, returns the number of character present in the string. It is present in a header file named <string.h> which is included at the beginning of the code in a program.


    Find Length of String using strlen() Function in C++

    Source code:

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    {
      char str[100];
      int length = 0;
    
      cout << "Enter the String: ";
      cin >> str;
    
      length = strlen(str);
    
      cout << "\nLength of a string: " << length;
    
      return 0;
    }

    Output:

    Enter the String: simple2code

    Length of a string: 11

    However, if you want to enter the string with spaces, use gets(str) instead of cin >>str.

    Also, you may go through the following program:


  • C++ Program to Find the Length of a String without using strlen

    This is the C++ tutorial where we will write a C++ program to find the length of a string without using a library function or any built-in function such as strlen() function. If you want to learn more about string in C++, click the link below.

    Explanation: The program takes the string value from the user as an input and then using for loop or while loop, we can find the length of that string as shown in the program below.

    There is no use of library function to find the length, it is totally logic-based using loops in C++.


    C++ Program to Find the Length of a String without using strlen

    Source code: Find Length of String without strlen() Function in C++.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      char str[100];
      int length = 0, i = 0;
    
      cout << "Enter the String: ";
      cin >> str;
    
      while (str[i])
      {
        length++;
        i++;
      }
    
      cout << "\nLength of a string: " << length;
    
      return 0;
    }

    Output:

    Enter the String: simple2code

    Length of a string: 11

    You can also use for loop instead of while loop in the following manner.

    for(i = 0; str[i] != '\0'; i++)
    {
       length++;
    }

    Replace the above code in place of the while loop then you will still get the same result. However, if you want to enter the string with spaces, use gets(str) instead of cin >>str.

    Also, you may go through the following program: