Found 35164 Articles for Programming

C++ Program to Calculate Power Using Recursion

karthikeya Boyini
Updated on 24-Jun-2020 08:06:18

4K+ Views

The power of a number can be calculated as x^y where x is the number and y is its power.For example.Let’s say, x = 2 and y = 10 x^y =1024 Here, x^y is 2^10A program to find the power using recursion is as follows.Example Live Demo#include using namespace std; int FindPower(int base, int power) {    if (power == 0)    return 1;    else    return (base * FindPower(base, power-1)); } int main() {    int base = 3, power = 5;    cout

C++ program to Find Sum of Natural Numbers using Recursion

Samual Sam
Updated on 24-Jun-2020 08:06:47

2K+ Views

The natural numbers are the positive integers starting from 1.The sequence of natural numbers is −1, 2, 3, 4, 5, 6, 7, 8, 9, 10……The program to find the sum of first n natural numbers using recursion is as follows.Example Live Demo#include using namespace std; int sum(int n) {    if(n == 0)    return n;    else    return n + sum(n-1); } int main() {    int n = 10;    cout

C++ Program to Display Prime Numbers Between Two Intervals Using Functions

karthikeya Boyini
Updated on 24-Jun-2020 08:07:54

2K+ Views

A prime number is a whole number that is greater than one and the only factors of a prime number should be one and itself. Some of the first prime numbers are 2, 3, 5, 7, 11, 13 ,17 etc.There can be many prime numbers between two intervals. For example, the prime numbers between the intervals 5 and 20 are 5, 7, 11, 13, 17 and 19.The program to find and display the prime numbers between two intervals is given as follows.Example Live Demo#include using namespace std; void primeNumbers (int lbound, int ubound) {    int flag, i;    while (lbound

C++ Programs To Create Pyramid and Pattern

Samual Sam
Updated on 24-Jun-2020 08:08:59

1K+ Views

There are many different pyramid patterns that can be created in C++. These are mostly created using nested for loops. Some of the pyramids that can be created are as follows.Basic Pyramid PatternThe code to create a basic pyramid is given as follows.Example Live Demo#include using namespace std; int main() {    int n = 6, i, j;    for (i=1; i

C++ Program to Display Armstrong Number Between Two Intervals

Samual Sam
Updated on 24-Jun-2020 08:10:26

2K+ Views

An Armstrong Number is a number where the sum of the digits raised to the power of total number of digits is equal to the number.Some examples of Armstrong numbers are as follows −3 = 3^1 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 407 = 4^3 + 0^3 + 7^3 = 64 +0 + 343 = 407 1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634A program that displays the Armstrong numbers between two intervals is as follows.Example Live Demo#include #include using namespace std; int main() {    int lowerbound, upperbound, digitSum, temp, remainderNum, digitNum ;    lowerbound = 100;    upperbound = 500;    cout

C++ Program to Check Armstrong Number

karthikeya Boyini
Updated on 24-Jun-2020 08:11:14

820 Views

An Armstrong Number is a number where the sum of the digits raised to the power of total number of digits is equal to the number. Some examples of Armstrong numbers are as follows.3 = 3^1 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 371 = 3^3 + 7^3 + 1^3 = 27 + 343 + 1 = 371 407 = 4^3 + 0^3 + 7^3 = 64 +0 + 343 = 407A program that checks whether a number is an Armstrong number or not is as follows.Example Live Demo#include #include

C++ Program to Display Prime Numbers Between Two Intervals

Samual Sam
Updated on 24-Jun-2020 08:12:35

379 Views

A prime number is a whole number that is greater than one and the only factors of a prime number should be one and itself. Some of the first prime numbers are 2, 3, 5, 7, 11, 13 ,17 etc.There can be many prime numbers between two intervals. For example, the prime numbers between the intervals 5 and 20 are −5, 7, 11, 13, 17 and 19.The program to find and display the prime numbers between two intervals is given as follows.Example Live Demo#include using namespace std; void PrimeNumbers (int lbound, int ubound) {    int flag, i;    while (lbound

C++ Program to Find Largest Element of an Array

karthikeya Boyini
Updated on 12-Feb-2020 06:57:29

6K+ Views

An array contains multiple elements and the largest element in an array is the one that is greater than other elements.For example.51724In the above array, 7 is the largest element and it is at index 2.A program to find the largest element of an array is given as follows.Example Live Demo#include using namespace std; int main() {    int a[] = {4, 9, 1, 3, 8};    int largest, i, pos;    largest = a[0];    for(i=1; ilargest) {          largest = a[i];          pos = i;       }    }    cout

C++ Program to Convert Binary Number to Decimal and vice-versa

Samual Sam
Updated on 24-Jun-2020 07:49:03

2K+ Views

In a computer system, the binary number is expressed in the binary numeral system while the decimal number is in the decimal numeral system. The binary number is in base 2 while the decimal number is in base 10.Examples of decimal numbers and their corresponding binary numbers are as follows −Decimal NumberBinary Number100101070011125110011610000A program that converts the binary numbers into decimal and the decimal numbers into binary is as follows.Example Live Demo#include using namespace std; void DecimalToBinary(int n) {    int binaryNumber[100], num=n;    int i = 0;    while (n > 0) {       binaryNumber[i] = n % 2;       n = n / 2;       i++;    }    cout

C++ Program to Find G.C.D Using Recursion

karthikeya Boyini
Updated on 24-Jun-2020 07:51:29

7K+ Views

The Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them.For example: Let’s say we have following two numbers: 45 and 2763 = 7 * 3 * 3 42 = 7 * 3 * 2 So, the GCD of 63 and 42 is 21A program to find the GCD of two numbers using recursion is given as follows.Example Live Demo#include using namespace std; int gcd(int a, int b) {    if (a == 0 || b == 0)    return 0;    else if (a == b)    return a;    else if (a > b)    return gcd(a-b, b);    else return gcd(a, b-a); } int main() {    int a = 63, b = 42;    cout

Advertisements