Found 7197 Articles for C++

C++ Program to Calculate Power of a Number

Nishu Kumari
Updated on 09-May-2025 15:17:36

6K+ Views

In this article, we'll show you how to calculate the power of a number in C++. Calculating a power means multiplying the base by itself as many times as the exponent indicates. For example, 2 raised to the power of 3 (2^3) means multiplying 2 by itself three times: 2 * 2 * 2, which gives 8. In C++, there are different ways to calculate the power of a number. Below are the approaches we cover: Using a Loop Using Recursion Using the pow() Function ... Read More

C++ Program to Reverse a Number

Nishu Kumari
Updated on 09-May-2025 15:18:28

1K+ Views

In this article, we will show you how to reverse a number using C++. Reversing a number means changing the order of its digits, so the first digit moves to the end, the last digit moves to the front, and the middle digits follow in the opposite order. For example, if we start with the number 1234, the reversed number will be 4321. In C++, we can reverse a number in different ways. Some common approaches we can use are: Using a Loop Using Recursion ... Read More

C++ Program to Find LCM

Nishu Kumari
Updated on 09-May-2025 15:28:41

11K+ Views

In this article, we'll show you how to find the LCM of two numbers in a C++ program. The LCM(Least Common Multiple) is the smallest positive number that is exactly divisible by both numbers. For example, if we take 4 and 5: The multiples of 4 are: 4, 8, 12, 16, 20, 24, ... The multiples of 5 are: 5, 10, 15, 20, 25, ... The first common multiple is 20, so the LCM of 4 and 5 is 20. Approaches to Find LCM in C++ We can find the LCM of two ... Read More

C++ Program to Find GCD

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

14K+ 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

Nishu Kumari
Updated on 13-May-2025 17:06:55

5K+ Views

In this article, we will show you how to write a C++ program to generate the multiplication table of a number. A multiplication table shows how a number is multiplied by 1 to 10 and helps define the multiplication operation for that number. Each row displays the result of multiplying the number by values from 1 to 10. For example, the multiplication table of 4 looks like this: 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 ... Read More

C++ Program to Find Factorial

Nishu Kumari
Updated on 09-May-2025 15:30:29

23K+ Views

In this article, we'll show you how to write a C++ program to find the factorial of a number. The factorial of a number is the result of multiplying all the positive integers from 1 to that number. It is written as n! and is commonly used in mathematics and programming. Let's understand this with a few examples: //Example 1 Input: 5 The factorial of 5 is: 5 * 4 * 3 * 2 * 1 = 120 Output: 120 //Example 2 Input: 6 The factorial of 6 is: 6 * 5 * 4 * 3 * 2 ... Read More

C++ Program to Check Leap Year

Nishu Kumari
Updated on 13-May-2025 17:07:56

14K+ Views

A leap year contains one additional day, i.e february 29, to keep the calendar year synchronized with the earth's orbit around the sun. We will write a C++ program that checks whether a given year is a leap year or not. 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. Here is a list of some leap years and non-leap years: ... Read More

C++ Program to Calculate Sum of Natural Numbers

Nishu Kumari
Updated on 09-May-2025 15:31:03

3K+ Views

In this article, we will write a C++ program to calculate the sum of the first n natural numbers. Natural numbers are positive numbers starting from 1(i.e., 1, 2, 3, ...). We will take a positive number n as input and find the sum of all natural numbers from 1 up to n. Let's understand with examples: Input: n = 5 The sum of the first 5 natural numbers is: 1 + 2 + 3 + 4 + 5 = 15 Output: 15 Input: n = 8 The sum of the first 8 natural numbers is: 1 + ... Read More

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

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

8K+ 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

C++ Program to Find Largest Number Among Three Numbers

Samual Sam
Updated on 24-Jun-2020 07:22:40

5K+ Views

The largest number among three numbers can be found using if statement multiple times. This is given in a program as follows −Example Live Demo#include using namespace std; int main() {    int a = 5 ,b = 1 ,c = 9;    if(a>b) {       if(a>c)       cout

Advertisements