Found 34494 Articles for Programming

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

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

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

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

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

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

Advertisements