- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7347 Articles for C++
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
142 Views
Here we will see a program, if one number is given, another value k is also given, we have to check whether the number is power of k or not. But we have to perform the base changing method to solve this problem. Suppose a number is 27, and k = 3. Then by base changing method, the 27 will be 10003. Here after changing the base if there is only one occurrence of digit 1, and others are 0, then the number is power of k.To solve this problem, we will follow these steps.Steps −define flag := falsewhile number > ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
392 Views
In this section, we will see, if a number is the power of 8 or not using some easier method. If a number like 4096 is there, then the program will return true, as this is the power of 8.The trick is simple. we will calculate log8(num). If this is an integer, then n is the power of 8. Here we will use the tranc(n) function to find the closest integer of the double value.Example Live Demo#include #include using namespace std; bool isPowerOfEight(int n) { double val = log(n)/log(8); //get log n to the base 8 return ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
1K+ Views
Here we will check whether a number is positive, or negative or zero using bit operators. If we perform shifting like n >> 31, then it will convert every negative number to -1, every other number to 0. If we perform –n >> 31, then for positive number it will return -1. When we do for 0, then n >> 31, and –n >> 31, both returns 0. for that we will use another formula as below −1+(𝑛>>31)−(−𝑛>>31)So now, ifn is negative: 1 + (-1) – 0 = 0n is positive: 1 + 0 – (-1) = 2n is 0: ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
1K+ Views
Suppose a number is given, we have to check whether the number is a perfect square or not. We will not use the square root operation to check it. Suppose a number 1024 is there, this is a perfect square, but 1000 is not a perfect square. The logic is simple, we have to follow this algorithm to get the result.AlgorithmisPerfectSquare(n) −input − The number noutput − true, if the number is a perfect square, otherwise, falsebegin for i := 1, i2 ≤ n, increase i by 1: if n is divisible by i, and n ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
556 Views
Here we will see, how to check whether a number is a palindrome or not. The palindrome numbers are the same in both directions. For example, a number 12321 is a palindrome, but 12345 is not a palindrome.The logic is very straight forward. We have to reverse the number, and if the reversed number is the same as the actual number, then that is a palindrome, otherwise not. Let us see the algorithm to get a better idea.AlgorithmisPalindrome(n) −input − The number noutput − true, if the number is a palindrome, otherwise, falsebegin temp := n rev := ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
627 Views
Here we will see how to check a number is divisible by 5 or not. One simple approach is that if the number mod 5 = 0, then, the number is divisible by 5. But here we will not use / or % operator. To check whether a number is divisible by 5, we have to see the last number is 0 or 5. If that is 0 or 5, the number is divisible by 5, otherwise not. Here we can use some large numbers also as a string to check.Example#include using namespace std; bool isDiv5(string num){ int ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
299 Views
Here we will see one interesting problem to check whether a number is jumbled or not. A number is said to be jumbled if, for every digit, it's neighbor digit differs by max 1. For example, a number 1223 is jumbled, but 1256 is not jumbled.To solve this problem, we have to check if a digit has a neighbor with a difference greater than 1. If such digit is found, then return false, otherwise true.Example Live Demo#include #include using namespace std; bool isJumbled(int number) { if (number / 10 == 0) //for single digit number is is always ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
154 Views
Suppose, we have a number string, we have to find that the number is of given base B or not? If the string is “101110”, b = 2, then the program will return true. If the string is “A8F”, base is 16, it will be true.The approach is very simple. If all of the character is in the range of symbols of the given base, then return true, otherwise false.Example Live Demo#include using namespace std; bool inGivenBase(string s, int base) { if (base > 16) //program can handle upto base 1 return false; ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
39 Views
We have an array A, we have to check whether the array can represent a BST with n levels or not. As the level is , we can construct a tree in following manner. Assume a number is k, value greater than k moves to right side, and less than k moves to left side. Suppose two lists are there: {50, 20, 9, 25, 10}, and {50, 30, 20, 25, 10}The first one is not valid, but the second one is valid.To check this we can either create a BST and check the height, otherwise use array based approach. The ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
2K+ Views
Here we will see, if a matrix is magic square or not, a magic square is a square matrix, where the sum of each row, each column, and each diagonal are same.Suppose a matrix is like below −618753294This is a magic square, if we see, the sum of each row, column and diagonals are 15.To check whether a matrix is magic square or not, we have to find the major diagonal sum and the secondary diagonal sum, if they are same, then that is magic square, otherwise not.Example Live Demo#include #define N 3 using namespace std; bool isMagicSquare(int mat[][N]) { ... Read More