Posts

CPP Program find the frequency of characters in a string

  Here, you will learn about how to find the frequency of the character in the string in the cpp programming language. Code: #include <iostream> using namespace std; int main() {     string str = "CPP is best for gaming";     char checkChar = 'i';     int count = 0;     for (int i = 0; i < str.size(); i++)     {         if (str[i] ==  checkChar)         {             ++ count;         }     }     cout << "Number of  " << checkChar << " : " << count;     return 0; }  In the above program, find the length of the string then apply the for loop with check if (str[i] ==  checkChar) increment in the count in each iteration. The loop is iterate until condition the end of the string. If you have any problem in the above program write your query in the below comment section

CPP program Accessing the element of array by using pointer

  Here, in this example of the cpp program declare an array of five lengths with integer type and take input from the user then access all the elements of the array through a pointer. For understanding you have knowledge about the following topics: C++ pointers C++ Arrays Code: #include <iostream> using namespace std; int main() {    int arr[5];    cout << "Enter elements :- ";    for(int i = 0; i < 5; ++i)    {       cin >> arr[i];   }    cout << "You entered arr :- "<<endl;;    for(int i = 0; i < 5; ++i)    {       cout <<  *(arr + i)<<endl;   }    return 0; } Output: Enter elements:- 1 2 3 4 5 You entered arr:- 1 2 3 4 5

CPP Program to find length of string

Here,you will find the knowledge about how to find the length of the string or size of string in the cpp programming language. To find the length of string object size () and length() both functions are used.They are synonyms to each other and exactly done same job. Code: #include <iostream> using namespace std; int main() {     string str = "C++ Programming";     // can  use str.length()     cout << "String Length = " << str.length()<<endl;     //also use str.size()      cout << "String Length = " << str.size();     return 0; } Output: String Length = 15 String Length = 15 In the cpp, another way to calculate the length by using a function  from the library cstring Code: #include <iostream> #include<cstring> using namespace std; int main() {     char str[] = "C++ Programming";     cout << "String Length = " << strlen(str)<<endl;          return 0; } Output: String Length

CPP Program convert octal to binary

 In this example, you will learn about the conversion of octal to binary number in the cpp programming language .you must have knowledge about the following topics to understand the working of the program. Cpp while loop Cpp input method Cpp output method Following is the code of conversion. Code: #include <iostream> #include <cmath> using namespace std; int main() {     int octalNumber,original;     cout << "Enter an octal number: ";     cin >> octalNumber; original=octalNumber;     int decimalNumber = 0, i = 0;     long long binaryNumber = 0;     while(octalNumber != 0)     {         decimalNumber += (octalNumber%10) * pow(8,i);         ++i;         octalNumber/=10;     }     i = 1;     while (decimalNumber != 0)     {         binaryNumber += (decimalNumber % 2) * i;         decimalNumber /= 2;         i *= 10;     }      cout << original << " in octal = " << binaryNumber << " in binary";       return 0; }

CPP Program convert binary to octal

In this program, you will learn about the conversion of binary numbers to octal numbers in the cpp programming language. Code: #include <iostream> #include <cmath> using namespace std; int main() {     long long binaryNumber,original;     cout << " Enter a binary number  ";     cin >> binaryNumber; original=binaryNumber;        int octalNumber = 0, decimalNumber = 0, i = 0;     while(binaryNumber != 0)     {         decimalNumber += (binaryNumber%10) * pow(2,i);         ++i;         binaryNumber/=10;     }     i = 1;     while (decimalNumber != 0)     {         octalNumber += (decimalNumber % 8) * i;         decimalNumber /= 8;         i *= 10;     }       cout << original << "   in binary   " << octalNumber<< "  in octal  "<<endl; return 0; } Output:  Enter a binary number  1111 1111   in binary   17  in octal In the above program,first convert binary number to decimal then convert decimal to octal num

CPP Program convert decimal to octal

 In this example, you will learn about the conversion of decimal to octal number in the CPP programming language. For understanding the program you have the knowledge about the following topic. while loop input method output method Code: #include <iostream> #include <cmath> using namespace std; int main() {    int decimalNumber,original;    cout << "Enter a decimal number ";    cin >> decimalNumber;   original=decimalNumber;     int remainder, i = 1, octalNumber = 0;     while (decimalNumber != 0)     {         remainder = decimalNumber % 8;         decimalNumber /= 8;         octalNumber += remainder * i;         i *= 10;     }            cout << original << " in decimal  =  " << octalNumber << "  in octal "<<endl;    return 0; } Output: Enter a decimal number 6789 6789 in decimal  =  15205  in octal In the above code, while loop is used to convert the decimal number, separate the number by modulus 8,

CPP program convert octal to decimal

 In this program, you will learn about the conversion of octal to decimal number in the cpp programming language. In order to understand the following code must have the knowledge of the following topics: cpp while loop cpp input method cpp output method Code: #include <iostream> #include <cmath> using namespace std; int main() {    int octalNumber,originalnumber;    cout << "Enter an octal number "<<endl;    cin >> octalNumber;   originalnumber=octalNumber;     int decimalNumber = 0, i = 0, remainder;     while (octalNumber != 0)      {         remainder = octalNumber % 10;         octalNumber /= 10;         decimalNumber += remainder * pow(8, i);         i++;     }      cout << originalnumber << " in octal = " << decimalNumber << " in decimal"<<endl;    return 0; } Output: Enter an octal number 12345 12345 in octal = 5349 in decimal In the above code, while loop is used to convert the number, se