Skip to main content

Posts

Showing posts from December, 2023

C++ = Code for Binary Search

Who does Binary Search Work? When describing an algorithm to a fellow human being, an incomplete description is often good enough. Some details may be left out of a recipe for a cake; the recipe assumes that you know how to open the refrigerator to get the eggs out and that you know how to crack the eggs. People might intuitively know how to fill in the missing details, but computer programs do not. That's why we need to describe computer algorithms completely. In order to implement an algorithm in a programming language, you will need to understand an algorithm down to the details. What are the inputs to the problem? The outputs? What variables should be created, and what initial values should they have? What intermediate steps should be taken to compute other values and to ultimately compute the output? Do these steps repeat instructions that can be written in simplified form using a loop? Let's look at how to describe binary search carefully. The main idea of binary search ...

C++ = Code for Counter

Code: #include <iostream> #include <conio.h> using namespace std; int main() {     int count = 0;     char input;     cout << "--------------------------" << endl;     cout << "          Counter         " << endl;     cout << "--------------------------" << endl;     cout << endl;     cout << "Press 1 to counter" << endl;     cout << "Press 0 to quit" << endl;     cout << endl;     cout << "Enter the number continuously : ";     while (true)     {         input = getch();         if (input == '1')         {             count += 1;             cout << "Your counts are " << count << endl;   ...

C++ = Code to check number wheater it is prime or not.

Code no 1= Without any other function with using another function. #include <iostream> using namespace std; int main() { int number; bool prime=true; cout<<"Enter any number : "; cin>>number; if (number<=1) { prime=false; } else { for (int i=2;i<number;i++) { if (number%i==0) { prime=false; break; } } } if (prime) { cout<<"The number is a prime number."<<endl; } else { cout<<"The number is not a prime number."<<endl; } } Code no: 2= With using another function #include <iostream> #include <cmath> using namespace std; bool prime(int n) { if (n<=1) { return false; } for (int i=2;i<n/2;i++) { if (n%i==0) { return false; } } return true; } int main() { int number; cout<<"Enter any number : "; cin>>number; if (prime(number)) { cout<<"The numbe...

C++ = How convert lowercase into uppercase and uppercase into lowercase.

First you need to know that what are the ASCII codes for the alphabets. As you can see there, that the ASCII code for A is 65 and for a is 97, whose difference is 32 or we can say that the condition for it is ('a'-'A') which is also equal to 32 i.e (97-65=32). We will be trying to do a brainstroming. Now, see we know that Uppercase aplhabets have lower value and Lowercase aplhabets have higher value but there is difference of 32 between every corresponding element like z to Z. If we want to go lowercase to uppercase we need to subtract 32 from the given character. And If we want to go from uppercase to lowercase we need to to add 32 to that given character. Note: It is our human preception that how 32 will be subtracted or added from a character but it is not case with computer, computer will consider its ASCII code. So the below code is making condition of the below logic: Code: #include <iostream> using namespace std; int main() { char character; cout<...

C++ = Multiplication of 2 matrices of order m*n by taking input by user.

Code: #include <iostream> using namespace std; int main() { int rows1,columns1,rows2,columns2; cout<<"Enter the rows of first column : "; cin>>rows1;     cout<<"Enter the columns of first column : ";     cin>>columns1;          cout<<"Enter the rows of second column : ";     cin>>rows2;     cout<<"Enter the columns of second column : ";     cin>>columns2;     int matrix1[rows1][columns1],matrix2[rows2][columns2];          if (rows2==columns1)     {     cout<<"Enter the elements of first matrix : ";     for (int i=0;i<rows1;i++)     {     for (int j=0;j<columns1;j++)     {     cin>>matrix1[i][j]; } }          cout<<"Enter the elements of second matrix : ";     for (int i=0;i<...