C++ program for binary search

In this example, you will learn a C++ program for the binary search algorithm. This program finds the position of the target value within the sorted array list.  How does Binary search work? In binary search, elements are searched from the middle of an array. It is applicable only on sorted arrays even in ascending or descending order.  If the array list is not sorted the first task is to sort it. Let’s look at an example below. Example: Binary search program in C++ Output 1 Output 2 Description and…

Read More

C++ program to count positive, negative, and zeros

In this example, we will learn a C++ program to count positive, negative, and zeros in an array. This program takes the maximum number for an array from the user and counts similar numbers. Example: C++ program to count positive negative and zeros Here is the code to take maximum array values from the user. Programs count their zeros, positive and negative numbers. Output Description and working of this program Initialize 5 variables and named it “countPositive=0”, “countNegative=0”, “countZero=0”, “i” and “number”. Initialize array named it “arr[50]”. Take the Maximum…

Read More

C++ program to find the area and perimeter of a rectangle

In this example, you will learn a c++ program to find the area and perimeter of a rectangle.  The area is calculated by the length times the width. If we have length =8 and width=5 the area will become 8X5 = 40. While on the other side the perimeter is the path that surrounds the rectangle and is calculated by 2X (length + width).  2X (8 + 5)=26 Example: C++ program to find the area and perimeter of a rectangle This program takes length and width from the user and…

Read More

C++ program to find HCF and LCM

In this example, you will learn a c++ program to find LCM and HCF. LCM means the least common multiple. If we have two numbers 6 and 10, The LCM of these numbers will be 30. It means that 6 and 10 are the least common multiple of 30. While on the other hand HCF means the highest common factor. If we have two numbers 30 and 25, the HCF of these numbers will be 5. It means that 5 is the highest common factor that is divisible by both…

Read More

C++ program to find the largest of two numbers

In this example, you will learn a c++ program to find the largest value between two numbers. This program takes to numbers from the user and displays the largest value. If the user enters 4 and 7 program finds the largest from both which is 7. Program: Find Largest of Two Numbers in C++ Output Description and working this program Take two numbers from the user and store them in “number1” and “number2”. Check condition if the first number is greater than the second than largest= number1 If this condition…

Read More

C++ program to find the smallest of two numbers

Smallest of two numbers in cpp

In this example, you will learn a C++ program to find the smallest of two numbers. This program takes two numbers from the user and displays the smallest value. If the user enters 4 and 7. The program finds the smallest from both which is 4. Program: Find the Smallest of Two Numbers in C++ Output Description and working of this program Recommended Articles C++ If-else Statements C++ Operators

Read More

C++ program to print Floyd’s triangle

In this example, you will learn a C++ program to print Floyd’s triangle.  Floyd’s triangle is a right-angle triangle of natural numbers whose all sides are equal. This program takes maximum rows from the user and prints Floyd’s triangle on the console screen. Example: Floyd’s triangle in C++ Output Description and working on this program This program takes a total number of rows from the user and stores it in the “totalRows” variable. Initialize for loop for the total number of rows. Initialize another loop to print natural numbers in…

Read More

C++ program to print Pascal’s triangle

In this example, you will learn a C++ program to print Pascal’s triangle on the screen.  Pascal’s triangle is formed by placing 1 along the right and left edges. After that, each value of the triangle is filled by the sum of the above row’s two values just above the given position. This triangle was named after the French mathematician Blaise Pascal.   Example: C++ program to print Pascal’s triangle Output Recommended Articles C++ For Loop C++ Input / Output

Read More

C++ program to reverse numbers

In this example, we will learn a C++ program to reverse numbers. This program takes one number from the user and programs reverses it. Like a number 235 after reverse become 532. Example: C++ program to reverse numbers Here is the code to take a number from the user. Programs reverse it and display it on the screen. Output Description and working of this program Initialize three variables and named them “number”, “revNum” and “remainder” Take the input number value from the user and store it in the “number”  variable…

Read More