Found 35164 Articles for Programming

C++ program to Calculate Factorial of a Number Using Recursion

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

1K+ 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 7 is 5040.7! = 7 * 6 * 5 * 4 * 3 * 2 *1 7! = 5040Let us see the code to calculate the factorial of a number using recursion.Example Live Demo#include using namespace std; int fact(int n) {    if ((n==0)||(n==1))    return 1;    else    return n*fact(n-1); } int main() {    cout

C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...case

karthikeya Boyini
Updated on 24-Jun-2020 07:55:33

1K+ Views

Let us see a program to create a simple calculator in C++, with Add, Subtract, Multiply and Divide operations.Example Live Demo#include using namespace std; void calculator(int a, int b, char op) {    switch (op) {       case '+': {          cout

C++ Program to Check Prime Number By Creating a Function

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

4K+ 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 ,17A program to check if a number is prime or not using a function is as follows.Example Live Demo#include using namespace std; void isPrime(int n) {    int i, flag = 0;    for(i=2; i

C++ Program to Check Whether a Number is Palindrome or Not

karthikeya Boyini
Updated on 24-Jun-2020 07:57:21

2K+ Views

A palindrome number remains the same if its digits are reversed i.e its value does not change. A palindrome number can also be called symmetric. For example: The numbers 12321, 1551, 11 etc are palindromes as they do not change even if their digits are reversed.A program that checks if a number is palindrome or not is as follows.Example Live Demo#include using namespace std; void palindrome(int num) {    int rev=0,val;    val = num;    while(num > 0) {       rev = rev * 10 + num % 10;       num = num / 10;    }    if(val==rev)    cout

C++ Program to Multiply two Numbers

Samual Sam
Updated on 24-Jun-2020 07:58:02

1K+ Views

Multiplication of two numbers a and b yields their product. Value of a is added as many times as the value of b to get the product of a and b.For example.5 * 4 = 20 7 * 8 = 56 9 * 9 = 81Program to Multiply two Numbers using * OperatorA program to multiply two numbers using the * operator is given as follows −Example Live Demo#include using namespace std; int main() {    int a = 6, b = 8;    cout

C++ Program to Find ASCII Value of a Character

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

8K+ Views

There are 128 characters in the ASCII (American Standard Code for Information Interchange) table with values ranging from 0 to 127.Some of the ASCII values of different characters are as follows −CharacterASCII ValueA65a97Z90z122$36&38?63A program that finds the ASCII value of a character is given as follows −Example Live Demo#include using namespace std; void printASCII(char c) {    int i = c;    cout

C++ Program to Subtract Complex Number using Operator Overloading

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

2K+ Views

Operator overloading can be done with most of the built-in operators in C++. The overloaded operators are functions with the keyword operator followed by the operator symbol that is defined. The overloaded operators have a return type and a parameter list like any function.A program that subtracts complex numbers using operator overloading is as follows −Example Live Demo#include using namespace std; class ComplexNum {    private:    int real, imag;    public:    ComplexNum(int r = 0, int i =0) {       real = r;       imag = i;    }    ComplexNum operator - (ComplexNum const ... Read More

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

919 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

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

Advertisements