Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Server Side Programming Articles - Page 2141 of 2646
1K+ Views
Suppose there are few friends. They have borrowed money from each other. So there will be some cash flow on the network. Our task is to minimize the cash flow in the network. Suppose there are three friends P1, P2, and P3. The cash flow among them is like below −This cash flow is not minimum; we have to minimize it. Then the final diagram will be like −To solve this problem we will use the greedy approach. Here in each step settle all amounts of one person and recur for remaining n-1 persons. Now the question comes, how to ... Read More
393 Views
Suppose, one parabola is given (the vertex coordinate (h, k) and distance from focus and vertex is a), another point is also given. We have to find whether the point is inside the parabola or not. To solve it, we have to solve the following equation for the given point (x, y)\left(y-k\right)^2=4a\left(x-h\right)If the result is less than 0, then this is present inside the parabola if it is 0, then it is on the parabola, and if greater than 0, then outside of the parabola.Example Live Demo#include #include using namespace std; int isInsideParabola(int h, int k, int x, int ... Read More
806 Views
Suppose, one ellipse is given (the center coordinate (h, k) and semi-major axis a, and semi-minor axis b), another point is also given. We have to find whether the point is inside the ellipse or not. To solve it, we have to solve the following equation for the given point (x, y).$$\frac{\left(x-h\right)^2}{a^2}+\frac{\left(y-k\right)^2}{b^2}\leq1$$If the result is less than one, then the point is inside the ellipse, otherwise not.Example Live Demo#include #include using namespace std; bool isInsideEllipse(int h, int k, int x, int y, int a, int b) { int res = (pow((x - h), 2) / pow(a, 2)) + ... Read More
245 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
531 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
2K+ 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
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
764 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
757 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
526 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