Found 34494 Articles for Programming

C++ Program to Calculate Power of a Number

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

5K+ 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^10 Power of a number can be calculated using recursive and non-recursive programs. Each of these are given as follows.Power of a Number Using Non-Recursive ProgramThe program to find the power of a number using a non-recursive program is given as follows −Example Live Demo#include using namespace std; int power(int x, int y) {    int i,power=1;    if(y == 0)    return 1;    for(i=1;i

C++ Program to Reverse a Number

Samual Sam
Updated on 24-Jun-2020 07:29:59

977 Views

Reversing a number means storing its digits in reverse order.For example: If the number is 6529, then 9256 is displayed in the output.A program to reverse a number is given as follows −Example Live Demo#include using namespace std; int main() {    int num = 63972, rev = 0;    while(num > 0) {       rev = rev*10 + num%10;       num = num/10;    }    cout

C++ Program to Find LCM

karthikeya Boyini
Updated on 24-Jun-2020 07:30:56

10K+ Views

The Least Common Multiple (LCM) of two numbers is the smallest number that is a multiple of both.For example: Let’s say we have the following two numbers: 15 and 9.15 = 5 * 3 9 = 3 * 3So, the LCM of 15 and 9 is 45.A program to find the LCM of two numbers is given as follows −Example Live Demo#include using namespace std; int main() {    int a=7, b=5, lcm;    if(a>b)    lcm = a;    else    lcm = b;    while(1) {       if( lcm%a==0 && lcm%b==0 ) {          cout

C++ Program to Find GCD

Samual Sam
Updated on 24-Jun-2020 07:31:44

13K+ 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 two numbers are 45 and 27.45 = 5 * 3 * 3 27 = 3 * 3 * 3So, the GCD of 45 and 27 is 9.A program to find the GCD of two numbers is given as follows.Example Live Demo#include using namespace std; int gcd(int a, int b) {    if (b == 0)    return a;    return gcd(b, a % b); } int main() {    int a = 105, b = 30;    cout

C++ Program to Generate Multiplication Table

karthikeya Boyini
Updated on 24-Jun-2020 07:32:07

4K+ Views

The multiplication table is used to define a multiplication operation for any number. It is normally used to lay the foundation of elementary arithmetic operations with base ten numbers.The multiplication table of any number is written till 10. In each row, the product of the number with 1 to 10 is displayed. An example of the multiplication table of 4 is as follows −4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 4 * 10 = 40A program that generates the multiplication table of the given number is as follows.Example Live Demo#include using namespace std; int main() {    int n = 7, i;    cout

C++ Program to Find Factorial

Samual Sam
Updated on 24-Jun-2020 07:33:15

21K+ Views

Factorial of a non-negative integer n is the product of all the positive integers that are less than or equal to n.For example: The factorial of 5 is 120.5! = 5 * 4 * 3 * 2 *1 5! = 120The factorial of an integer can be found using a recursive program or a non-recursive program. Example of both of these are given as follows.Factorial using Non-Recursive ProgramA for loop can be used to find the factorial of a number. This is demonstrated using the following program −Example Live Demo#include using namespace std; int main() {    int n = 5, fact = 1, i;    for(i=1; i

C++ Program to Check Leap Year

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

12K+ Views

A leap year contains one additional day that is added to keep the calendar year synchronized with the astronomical year.A year that is divisible by 4 is known as a leap year. However, years divisible by 100 are not leap years while those divisible by 400 are.The program that checks if a year is leap year or not is as follows −Example Live Demo#include using namespace std; int main() {    int year = 2016;    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))    cout

Python program to count the elements in a list until an element is a Tuple?

AmitDiwan
Updated on 11-Aug-2022 08:53:33

416 Views

In this article, we will count the elements in a list until an element is a Tuple. The list is the most versatile datatype available in Python, which can be written as a list of commaseparated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type. A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The main difference between the tuples and the lists is that the tuples cannot be changed unlike lists. Tuples use parentheses, whereas lists use square ... Read More

C++ Program to Calculate Sum of Natural Numbers

Samual Sam
Updated on 24-Jun-2020 07:36:14

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……Sum of the first n natural numbers can be calculated using the for loop or the formula.Programs specifying both of these methods are given as follows −Sum of Natural Numbers Using for loop.The program to calculate the sum of n natural numbers using for loop is given as follows.Example Live Demo#include using namespace std; int main() {    int n=5, sum=0, i;    for(i=1;i

C++ Program to Find All Roots of a Quadratic Equation

karthikeya Boyini
Updated on 24-Jun-2020 07:47:19

7K+ Views

A quadratic equation is in the form ax2 + bx + c. The roots of the quadratic equation are given by the following formula −There are three cases −b2 < 4*a*c - The roots are not real i.e. they are complexb2 = 4*a*c - The roots are real and both roots are the same.b2 > 4*a*c - The roots are real and both roots are differentThe program to find the roots of a quadratic equation is given as follows.Example#include #include using namespace std; int main() {    int a = 1, b = 2, c = 1;    float discriminant, ... Read More

Advertisements