Posts

Showing posts with the label cppExample

Find the Armstrong number in cpp

Image
 How to find the Armstrong number in the cpp programming language? Here , you will find the solution to the problem to find the Armstrong number in cpp. Following is the code to find the Armstrong number.First,you know  What is Armstrong Number? A positive number which the sum of cube power of every digit is equal to the original number is called the Armstrong number. For example: Armstrong Number Code: #include <iostream> using namespace std; int main() {     int numb, original, rem, result = 0;     cout << "Enter a three-digit integer: ";     cin >> numb;     original= numb;     while (original != 0) {                  rem = original % 10;                  result += rem * rem * rem;                           original /= 10;     }     if (result == numb)         cout << numb << " is Armstrong number.";     else         cout << numb << " is not Armstrong number.";     return 0; } Output: Enter a three-digit integer

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

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