Posts

CPP program to calculate the power

 In this solution try to solve, the problem of finding the power of a number manually and with pow() function in the cpp programming language. Firstly, look at manually finding the power of any number. Code: #include <iostream> using namespace std; int main()  {     int exponent;     float base, pow = 1;     cout << "Enter base and exponent respectively:  ";     cin >> base >> exponent;     cout << base << "^" << exponent << " = ";     for(int i=1;i<=exponent;i++){         pow *= base;     }     cout << pow;          return 0; } Output: Enter base and exponent respectively:  3 3 3^3 = 27 Compute the power of the number by using pow() function from the cmath library. code: #include <iostream> #include<cmath> using namespace std; int main()  {     int exponent;     float base, powe = 1;     cout << "Enter base and exponent respectively:  ";     cin >> base >> exp

CPP Program to find the factorial by recursion

  In this program, the solution of finding the factorial of any number positive number by using the recursion method in the cpp language. For a complete understanding of this code, you must have knowledge of the cpp recursion . Code: #include<iostream> using namespace std; int fact(int n); //declare the function int main() {     int n;     cout << "Enter a positive integer for finding the factorial ";     cin >> n;     cout << "Factorial of the number " << n << "= " << fact(n);     return 0; } int fact(int n) //define the function for factorial {     if(n >= 1)         return n * fact(n - 1);     else         return 1; } Output: Enter a positive integer for finding the factorial 4 Factorial of the number 4= 24

CPP Program concatenate two strings

In this program, we will discuss how to concatenate the two strings in the cpp programming language. Two methods of concatenation are discussed in the following.   By using a string object : If you use a string object to deal with the strings in cpp then this method is useful for you. Code : #include <iostream> using namespace std; int main() {     string str1, str2, sum;     cout << "Enter string str1  ";     getline (cin, str1); //same for second string     cout << "Enter string str2 ";     getline (cin, str2);     sum = str1 + str2;     cout << "Result of two string  = "<< sum;     return 0; } Output: Enter string str1  cpp is best programming Enter string str2 language Result of two string  = cpp is best programming language By using c style string: if you use c style to deal with the string in the cpp then this method is helpful for you. Code: #include <iostream> #include <cstring> using namespace std; int mai

CPP Program copy a string in three different ways

  In this program, discussion about how to copy the string in the three different methods in the cpp programming language. Ist method: string object Code: #include <iostream> using namespace std; int main() {     string str1, str2;     cout << "Enter string s1: ";     getline (cin, str1);     str2 = str1;     cout << "string1 = "<< str1 << endl;     cout << "string2 = "<< str2;     return 0; } Second method:  Copy c string Code : #include <iostream> #include<cstring> using namespace std; int main() {     char str1[100], str2[100];     cout << "Enter string s1: ";     cin.getline(str1,100);     strcpy(str2,str1);     cout << "string1 = "<< str1 << endl;     cout << "string2 = "<< str2;     return 0; } 3RD METHOD : By using loop Code: #include <iostream> #include<cstring> using namespace std; int main() {     char str1[100], str

Cpp program to find Quotient and Remainder

 Following is the code which help in the understanding how to compute quotient and remainder. Code: #include <iostream> using namespace std; int main() {         int divis, divid, quot, rem;     cout << "Enter dividend :-"<<endl;     cin >> divid;     cout << "Enter divisor:- "<<endl;     cin >> divis;     quot = divid / divis;     rem = divid % divis;     cout << "Quotient = " << quot << endl;     cout << "Remainder = " << rem<<endl;     return 0; } Output: In the above program, declare 4 variables of int data type and take input from the user by input method then divide the dividend by divisor store in quotient which is the quotient .  The modulus of dividend and divisor is store in the remainder because modulus computes the remainder of the two number. If you have any problem in understanding of the above program then write a query in the below comment section.

CPP Program multiply the two matrices

 In this program, discussion about the multiple the two matrices in the cpp programming language. But you have enough knowledge to understand the whole program. If you do not have knowledge then visit c++ 2D arrays c++ loops c++ pointers Code: #include<iostream> using namespace std; int main() { int firstmat[10][10], secondmat[10][10], multi[10][10],i, j,firstrow,firstcol,secondrow,secondcol; cout << "Enter rows and column for first matrix: "; cin >> firstrow >> firstcol; cout << "Enter rows and column for second matrix: "; cin >> secondrow >> secondcol; cout<< "Enter elements of matrix 1:" << endl; for(i = 0; i < firstrow; ++i) { for(j = 0; j < firstcol; ++j) { cout << "Enter elements a"<< i + 1 << j + 1 << ": "; cin >> firstmat[i][j]; } } cout << endl << "Enter elements of matrix 2:" <<

CPP Program find the largest number in the array

  In this program, you will learn about the find the largest number in the array in the cpp programming language. If you have knowledge about the c++ array, the c++ loop help in understanding the program. Code: #include <iostream> using namespace std; int main() {     int i, n;     float arr[10]={1,2,3,4,5,6,7,8,9,10};     // Loop to store largest number to arr[0]     for(i = 1;i < n; ++i)     {        // Change < to > if you want to find the smallest element        if(arr[0] < arr[i])            arr[0] = arr[i];     }     cout << "Largest element  = " << arr[0]<<endl;     return 0; } Output: Largest element  = 10