Server Side Programming Articles - Page 2201 of 2650

C Program for the Difference between sums of odd and even digits?

sudhir sharma
Updated on 19-Aug-2019 11:41:47

2K+ Views

Given a number, find the difference between sum of odd digits and sum of even digits. Which means we will be count all even digits and all odd digits and the subtracting their sums.SampleInput:12345 Output:3Explanationthe odd digits is 2+4=6 the even digits is 1+3+5=9 odd-even=9-6=3Taking every digit out of number and checking whether the digit is even or odd if even then add it to even sum if not then to odd sum and then take difference of them.Example#include using namespace std; int main() {    int n, r=0;    int diff =0;    int even=0;    int odd=0; ... Read More

C Program for the cube sum of first n natural numbers?

sudhir sharma
Updated on 19-Aug-2019 11:40:22

573 Views

The cube sum of first n natural numbers is the program to add cubes of all natural numbers up to n. It is sum of series 1^3 + 2^3 + …. + n^3 that is sum of cube of n natural numbers.Input:6 Output:441Explanation1^3 + 2^3 + 3^3 + 4^3 + 5^3 + 63 = 441Using For loop to increase the no. and cubing it and taking sum of them.Example#include using namespace std; int main() {    int n = 6;    int sum = 0;    for (int i = 1; i

C Program for the compound interest?

sudhir sharma
Updated on 19-Aug-2019 11:39:05

271 Views

Compound interest is the simple interest that is compounded annually i.e. the interest will be calculated and added to the principal amount every year. This increases the overall interest as compared to simple interest. There is a different mathematical formula to calculate the compound interest. Lets see with the an example,Input:p=5, r=4, t=5 Output:1.083263ExplanationCompound Interest = Principle * (1 + Rate / 100)^time CI=5*(1+4/100)^5 CI=1.083263Example#include #include using namespace std; int main() {    float p, r, t, ci;    p=5;    r=4;    t=5;    ci = p * pow((1 + r / 100), t) - p;    printf("%f", ci);    return 0; }

C/C++ Program for the n-th Fibonacci number?

sudhir sharma
Updated on 19-Aug-2019 11:37:38

824 Views

The Fibonacci sequence is a series where the next term is the sum of the previous two terms.The first two terms of the Fibonacci sequence is 0 followed by 1. In this problem, we will find the nth number in the Fibonacci series. For this we will calculate all the numbers and print the n terms.Input:8 Output:0 1 1 2 3 5 8 13Explanation0+1=1 1+1=2 1+2=3 2+3=5Using For loop to sum of previous two terms for next termExample#include using namespace std; int main() {    int t1=0,t2=1,n,i,nextTerm;    n = 8;    for ( i = 1; i

C++ Program for the Recursive Bubble Sort?

sudhir sharma
Updated on 19-Aug-2019 11:35:50

584 Views

In Bubble sort compares adjacent pairs and swaps them if they are in the wrong order. In this type of bubble sort we use the recursive function that calls itself.Input:53421 Output:12345ExplanationUse of recursive (self-calling) function compares adjacent pairs and swaps them if they are in the wrong order until the array is in orderExample#include using namespace std; void bubbleSort(int arr[], int n) {    for (int i = 0; i < n - 1; i++) {       if (arr[i] > arr[i + 1]) {          int temp = arr[i];          arr[i] ... Read More

C++ Program for the Range sum queries without updates?

sudhir sharma
Updated on 19-Aug-2019 11:34:19

238 Views

We need to compute sum of elements from index i to index j. The queries consisting of i and j index values will be executed multiple times.Input:arr[] = {5, 6, 3, 4, 1 } i = 1, j =3 Output: 13Explanation6 + 3 + 4 = 13 sum[] = {5, 6+5, 3+6+5, 4+3+6+5, 1+4+3+6+5 } sum[]={5, 11, 14, 18, 19} sum[j]-sum[i-1]=sum[3]-sum[1-1]= sum[3]-sum[0]=18-5=13The logic is very basic in this starting the loop form i index to till j index and sum up the elements between those indexes. But we can’t store them in extra variable so we will use another array ... Read More

C++ Program for the Largest K digit number divisible by X?

sudhir sharma
Updated on 19-Aug-2019 11:31:51

176 Views

Two integers X and K are given. K is the number of digit in integer number. The logic is to find largest K-digit number divisible by X.Input: X = 30, K = 3 Output: 980Explanation980 is the largest three digit number divisible by 30. By taking the K in power of 10 then subtracting it with 1 will give us the largest K digit number after that we will try to get the largest no. which is divided by X.Example#include #include using namespace std; int main() {    int X = 20;    int K = 3;    int MAX = pow(10, K) - 1;    cout

C++ Program for the Gnome Sort?

sudhir sharma
Updated on 19-Aug-2019 11:30:27

232 Views

Gnome sort is a sorting algorithm which is similar to Insertion sort, except that moving an element to its proper place is accomplished by a series of swaps, as in bubble sort.Input: 53421 Output: 12345ExplanationSorting algorithm that moving an element to its proper place is accomplished by a series of swaps, as in bubble sort. It is simply requiring loops.Example#include using namespace std; int main() {    int temp;    int arr[] = { 5, 3, 4, 2, 1 };    int n=5;    int i;    i = 0;    while (i < n) {       if (i == 0 || arr[i - 1]

C++ Program for GCD of more than two (or array) numbers?

sudhir sharma
Updated on 19-Aug-2019 11:28:26

2K+ Views

The common divisor of two numbers are the numbers that are divisors of both of them.For example, the divisors of 12 are 1, 2, 3, 4, 6, 12. The divisors of 18 are 1, 2, 3, 6, 9, 18. Thus, the common divisors of 12 and 18 are 1, 2, 3, 6. The greatest among these is, perhaps unsurprisingly, called the of 12 and 18. The usual mathematical notation for the greatest common divisor of two integers a and b are denoted by (a, b). Hence, (12, 18) = 6.The greatest common divisor is important for many reasons. For example, ... Read More

C++ Program for the Comb Sort?

sudhir sharma
Updated on 19-Aug-2019 11:27:25

541 Views

Comb sort is similar to bubble sort and cocktail sort. Comb sort doesn’t start off looking at adjacent elements but instead looks at elements a certain number of indexes apart, this is called the gap. The gap is defined as [n/c] where n is the number of elements and c is the shrink factor. After each iteration, this number is against divided by c and floored until eventually, the algorithm is looking at adjacent elements.Input:53421 Output:12345ExplanationComb sort compare two elements with a gap which is defined as [n/c] where n is the number of elements and c is the shrink ... Read More

Advertisements