C++ Program to Multiply two Numbers

In the previous article, we have discussed C++ Program to Remove all Non Alphabet Characters from a String. In this article, we will see C++ Program to Multiply two Numbers.

C++ Program to Multiply two Numbers

  • Write a C++ program to multiply two numbers using * arithmetic operator.

In this program, we are going to perform basic arithmetic operation of multiplying two integers using multiplication(*) operation. Multiplication is one of the five fundamental arithmetic operators supported by C++ programming language, which are addition(+), subtraction(-), multiplication(*), division(/) and modulus(%). Multiplication operator multiplies both operands and returns the result.

For Example:

6 * 5; will evaluate to 30.

C++ Program to Multiply Two Numbers

C++ Program to Multiply Two Numbers

// C++ program to find product of two numbers
#include <iostream>
using namespace std;
 
int main() {
    int x, y, product;
     
    cout << "Enter two integers\n";
    cin >> x >> y;
     
    // Multiplying x and y
    product = x*y;
    cout << "Product = " << product;
     
    return 0;
}

Output

Enter two integers
4 8
Product = 32

In above program, we first take two integers as input from user using cin and store it in variable x and y. Then we multiply x and y using * operator and store the result of multiplication in variable product. Then finally, we print the value of product on screen using cout.

Points to Remember

  • If both operands are integers, then the product of two operands is also an integer.
  • Multiplication operator have higher priority than addition and subtraction operator.

The comprehensive list of C++ Programs Examples covered in our pages are very useful for every beginners and experienced programmers. So, make use of these C++ Coding Exercises & hold a grip on the programming language.

C++ Program to Find Sum of Natural Numbers Using Recursion

In the previous article, we have discussed C++ program to Check Whether a Number can be Split into Sum of Two Prime Numbers. In this article, we will see C++ Program to Find Sum of Natural Numbers Using Recursion.

C++ Program to Find Sum of Natural Numbers Using Recursion

  • How to find sum of natural numbers using recursion in C++.
  • Write a C++ program to find sum of all natural numbers using recursion.

C++ program to calculate sum of all natural numbers using recursion

C++ program to calculate sum of all natural numbers using recursion

#include<iostream>
 
using namespace std;
 
int getSum(int N);
int main() {
    int n;
    cout << "Enter an Integer\n";
    cin >> n;
     
    cout << "Sum of Numbers from 1 to " << n << " = " << getSum(n);
    return 0;
}
 
int getSum(int N) {
    if(N >= 1)
        return N + getSum(N-1);
    else
        return 0;
}

Output

Enter an Integer
10
Sum of Numbers from 1 to 10 = 55

Learn and explore basic C++ Practice Programs that help to enhance your skills & knowledge of the programming language. Get to know how to write simple and clear code on this page.

C++ program to Check Whether a Number can be Split into Sum of Two Prime Numbers

In the previous article, we have discussed C++ Program to Check Prime Number Using User-defined Function. In this article, we will see C++ program to Check Whether a Number can be Split into Sum of Two Prime Numbers.

C++ program to Check Whether a Number can be Split into Sum of Two Prime Numbers

  • How to split a number into two prime numbers.
  • Write a C++ program to split a number in two prime numbers.

C++ program to Check Whether a Number can be Express as Sum of Two Prime Numbers

C++ program to Check Whether a Number can be Express as Sum of Two Prime Numbers

#include<iostream>
 
using namespace std;
  
int isPrime(int num);
 
int main() {
  int num, i;
  cout << "Enter a positive number\n";
  cin >> num;
  for(i = 2; i <= num/2; i++){
      if(isPrime(i)){
          if(isPrime(num-i)){
         cout << num << " = " << i << " + " << num-i;
              return 0; 
          } 
      }
  }
   
  cout << "Not Possible";
        
  return 0;
}
 
// returns 1 if num is prime number otherwise 0
int isPrime(int num){
    int i;
    // Check whether num is divisible by any 
    // number between 2 to (num/2)
    for(i = 2; i <=(num/2); ++i) {
        if(num%i==0) {
            return 0;
        }
    }
    return 1;
}

Output

Enter a positive number
24
24 = 5 + 19
Enter a positive number
27
Not Possible

Explore various CPP Codes lists that are used in C++ programming to write a sample program. These codes play an important role while learning the programs.

C++ Program to Add Two Complex Numbers Using Structure

In the previous article, we have discussed C++ Program to Calculate Difference Between Two Time Periods Using Structure. In this article, we will see C++ Program to Add Two Complex Numbers Using Structure.

C++ Program to Add Two Complex Numbers Using Structure

  • Write a C++ program to find sum of two complex numbers using structure.

In this C++ program, we will add two complex numbers using a user defined structure. A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers and i is the imaginary unit, which satisfies the equation i2 = -1.

In complex number a + bi, a is the real part and b is the imaginary part.

For Example :

5 + 7i, -3 -2i, 2 - 6i

How to Add Two Complex Numbers.

Let Sum(x + iy) is the sum of C1 and C2
Sum = C1 + C2
(x + iy) = (a + ib) + (c + id)
(x + iy) = (a + c) + i(b + d)
x = (a + c) and,
y = (b + d)
We will create a custom structure name “complex” which contains two member variables realPart and imaginaryPart.

struct Complex {
   int realPart;
   int imaginaryPart;
};

We will use variables of structure Complex, to store complex numbers.

C++ Program to Find Sum of Two Complex Numbers Using Structure

C++ Program to Find Sum of Two Complex Numbers Using Structure

// C++ program to add two complex numbers using structure and function
#include <iostream>
using namespace std;
  
/* Structure to store complex number in the form of x + yi, 
 * realPart = x and imaginaryPart = y;
 */
struct Complex {
   int realPart;
   int imaginaryPart;
};
   
int main() {
   Complex c1, c2, sum;
   
   cout << "Enter value of A and B  where A + iB is first complex number\n";
   cin >> c1.realPart >> c1.imaginaryPart;
     
   cout << "Enter value of A and B  where A + iB is second complex number\n";
   cin >> c2.realPart >> c2.imaginaryPart;
     
   /* (A + Bi) + (C + Di) = (A+C) + (B+D)i */
   sum.realPart = c1.realPart + c2.realPart;
   sum.imaginaryPart = c1.imaginaryPart + c2.imaginaryPart;
      
   if(sum.imaginaryPart >= 0 )
      cout << sum.realPart << " + " << sum.imaginaryPart<<"i";
   else
      cout << sum.realPart << " - " << sum.imaginaryPart<<"i";
     
   return 0;
}

Output

Enter value of A and B  where A + iB is first complex number
2 5
Enter value of A and B  where A + iB is second complex number
7 4
9 + 9i

In this program, we take two complex numbers as input from user in the form of A + iB and store in structure variable c1 and c2. We will add real parts of input complex numbers to get the real part of sum complex and add imaginary part of input complex numbers to get imaginary part of sum complex number.

Explore various CPP Codes lists that are used in C++ programming to write a sample program. These codes play an important role while learning the programs.

C++ Program to Find LCM of Two Numbers

In the previous article, we have discussed C++ Program to Check Whether Number is Even or Odd. In this article, we will see C++ Program to Find LCM of Two Numbers.

C++ Program to Find LCM of Two Numbers

  • Write a C++ program to find LCM (Least Common Multiple) of Two Numbers using Functions.
  • C++ Program to find LCM and GCD of two Numbers

In this C++ program we will learn about finding least common multiple(LCM) of two numbers. The LCM of two integers X and Y, denoted by LCM (a, b), is the smallest positive integer that is divisible by both a and b. Here, we will discuss about two ways to find LCM of two numbers.

C++ Program to find LCM of two numbers

C++ Program to find LCM of two numbers

// C++ program to find LCM of two numbers
#include <iostream>
using namespace std;
  
// Function to find LCM
int getLCM(int a, int b) {
 int max;
    // Find maximum of a and b
    max = (a > b) ? a : b;
    // Find smallest number divisible by both a and b
    do {
        if (max % a == 0 && max % b == 0) {
            return max;
        } else {
         max++;
  }
    } while (true);
}
 
 
int main() {
    int x, y;
     
    cout << "Enter two integers\n";
    cin >> x >> y;
     
    cout << "LCM = " << getLCM(x, y);
    return 0;
}

Output

Enter two integers
6 15
LCM = 30

In this program, we first take two integers as input from user and store it in variable x and y. Then we call getLCM function by passing x and y as parameters. Inside getLCM function, we first find the maximum of a and b and store it in variable max. Now, we are trying to find smallest number greater than both a and b which is divisible by both a and b. Using a do while, we are testing every number greater than max till we find LCM.

C++ Program to find LCM by Finding GCD First

The highest common factor(HCF) of two or more integers, is the largest positive integer that divides the numbers without a remainder. HCF is also known as greatest common divisor(GCD) or greatest common factor(GCF).
Here is the relationship between LCM and HCF of two numbers.

LCM(A, B) X HCF(A, B) = A*B

If we know LCM or HCF of two numbers, then we can find the other one using above equation.
C++ Program to find LCM by Finding GCD First

/ C++ program to find LCM of two numbers
#include <iostream>
using namespace std;
  
// Function to find LCM
int getLCM(int a, int b) {
 int max;
    // Find maximum of a and b
    max = (a > b) ? a : b;
    // Find smallest number divisible by both a and b
    do {
        if (max % a == 0 && max % b == 0) {
            return max;
        } else {
         max++;
 }
    } while (true);
}
 
 
int main() {
    int x, y;
     
    cout << "Enter two integers\n";
    cin >> x >> y;
     
    cout << "LCM = " << getLCM(x, y);
    return 0;
}

Output

Enter two integers
6 15
LCM = 30

In this program, we first take two integers as input from user and store it in variable x and y. To find LCM of two number, we will first find HCF of two number and use above equation to find the LCM. We defined two function “getLcm” and “getGcd” to calculate LCM and GCD(HCF) of two numbers respectively. getLcm function internally calls getGcd function to get the HCF of two numbers and then use above equation to find LCM.

The list of C++ Example Programs include swapping number, addition, multiplication of two numbers, factorial of a number and many more. Interested people can get the cpp code to get a clear idea on the programming language.

C++ Program to Calculate Difference Between Two Time Periods Using Structure

In the previous article, we have discussed C++ Program to Store Information of a Book in a Structure. In this article, we will see C++ Program to Calculate Difference Between Two Time Periods Using Structure.

C++ Program to Calculate Difference Between Two Time Periods Using Structure

In this C++ program, we will find the difference between two time periods using a user defined structure. A time period is uniquely defined as triplets of hours, minutes and seconds.

For Example :
2 hour 20 minutes and 10 seconds.

Points to remember about Structures in C++

  • Keyword struct is used to declare a structure.
  • Structure in C++ programming language is a user defined data type that groups logically related information of different data types into a single unit.
  • We can declare any number of member variables inside a structure.
  • We can access the member of structure either using dot operator(.) or arrow operator(->) in case of structure pointer.

To store a time period we will define a user defined structure “Time” having three member variables hour, mins and secs.

struct Time {
  int hour;
  int mins;
  int secs;
};

We will use variables of structure Time, to time periods.

C++ Program to Calculate Difference Between Two Time Periods

C++ Program to Calculate Difference Between Two Time Periods

// C++ program to find difference between two time periods
#include <iostream>
using namespace std;
 
struct Time {
  int hour;
  int mins;
  int secs;
};
 
Time findTimeDifference(Time t1, Time t2);
 
int main() {
    Time t1, t2, diff;
     
    cout << "Enter earlier time in hours, minutes and seconds\n";
    cin >> t1.hour >> t1.mins >> t1.secs;
 
    cout << "Enter current time in hours, minutes and seconds\n";
    cin >> t2.hour >> t2.mins >> t2.secs;
     
    diff = findTimeDifference(t1, t2);
 
    cout << "Difference = "<< diff.hour << ":" << diff.mins << ":" << diff.secs;
    return 0;
}
 
Time findTimeDifference(Time t1, Time t2){
 Time diff;
    if(t2.secs > t1.secs){
        --t1.mins;
        t1.secs += 60;
    }
 
    diff.secs = t1.secs - t2.secs;
    if(t2.mins > t1.mins) {
        --t1.hour;
        t1.mins += 60;
    }
     
    diff.mins = t1.mins-t2.mins;
    diff.hour = t1.hour-t2.hour;
     
    return diff;
}

Output

Enter earlier time in hours, minutes and seconds
5 15 40
Enter current time in hours, minutes and seconds
2 40 14
Difference = 2:35:26

In this program, we take two Time periods as input from user in the form of hours, minutes and seconds and store in structure variable t1 and t2. To find the difference between t1 and t2, we call “findTimeDifference” function by passing t1 and t2. Finally, we display the difference of time periods in screen using cout.

We know that C++ is an object oriented programming language. The advanced C++ Topics are enumerated constants, multi-dimensional arrays, character arrays, structures, reading and writing files and many more. Check the complete details of all those from this article.

C++ Program to Store Information of a Book in a Structure

In the previous article, we have discussed C++ Program to Multiply Two Numbers Without Using ‘*’ Operator. In this article, we will see C++ Program to Store Information of a Book in a Structure.

C++ Program to Store Information of a Book in a Structure

In this C++ program, we will store the information of a book in a structure variable and then display it in screen. We want to store following information for a book Name, Price and ISBN. Here is a sample book record :
Name : Harry Potter
Price : 500
ISBN Code : 7645364
To store the information of a book, we will define an Book structure having three member variable name, price and ISBN.

struct Book {
    char name[100];
    int price;
    int ISBN;
};

Then we will create a variable of structure Book, let’s say book1. Then to access the members of book1, we will use member access operator or dot(.) operator.

Points to remember about Structures in C++.

  • We can declare any number of member variables inside a structure.
  • Structure in C++ programming language is a user defined data type that groups logically related information of different data types into a single unit.
  • Keyword struct is used to declare a structure.
  • We can access the member of structure either using dot operator(.) or arrow operator(->) in case of structure pointer.

C++ Program to Store Information of a Book in a Structure

C++ Program to Store Information of a Book in a Structure

// C++ program to store and print data from a structure variable
#include <iostream>
using namespace std;
 
// A structure for book
struct Book {
    char name[100];
    int price;
    int ISBN;
};
 
int main() {
    Book b;
     
    cout << "Enter name of book\n";
    cin.getline(b.name, 100);
    cout << "Enter price of employee\n";
    cin >> b.price;
    cout << "Enter ISBN code\n";
    cin >> b.ISBN;
     
    // Printing Book details 
    cout << "\n*** Book Details ***" << endl;
    cout << "Name : " << b.name << endl;
 cout << "Price : " << b.price << endl;
    cout << "ISBN Code : " << b.ISBN;
     
    return 0;
}

Output

Enter name of book
Harry Potter
Enter price of employee
500
Enter ISBN code
6453645

*** Book Details ***
Name : Harry Potter
Price : 500
ISBN Code : 7645364

In above program, we first declare a variable of type Book as Book b;

Then we ask user to enter book details i.e Name, Price and ISBN and store it in corresponding fields of structure variable b. Finally we print the information of variable b on screen using cout.

C++ Codes list contains the general functions, nested loops, statements, libraries. Beginners can get the important codes list of C++ programming language from this page easily.

C++ Program to Read an Integer and Character and Print on Screen

C++ Program to Read an Integer and Character and Print on Screen

  • Write a program in C++ to read a character and Integer from user and print on screen.
  • How to read integer and character in C++ programming language.

C++ program to read and print an integer and character

In this program, we will take an integer and character as input from user using cin stream and print it on screen using cout. This program helps in understanding basic input and output of C++ programming language.
C++ program to read and print an integer and character

/*
C++ program to read a number and a character fron user 
and print it on screen
*/
#include <iostream>
 
using namespace std;
 
int main() {
    int val;
    char c;
    // Read input 
    cout << "Enter an Integer and a Character\n";
    cin >> val >> c;
    // Print on screen
    cout << "Entered Integer : " << val << endl;
    cout << "Entered Character : " << c;
     
    return 0;
}

Output

Enter an Integer and a Character
12 A
Entered Integer : 12
Entered Character : A

C++ Program to Find GCD or HCF of Two Numbers Using Recursion

In the previous article, we have discussed C++ Program to Find Sum of Natural Numbers Using Recursion. In this article, we will see C++ Program to Find GCD or HCF of Two Numbers Using Recursion.

C++ Program to Find GCD or HCF of Two Numbers Using Recursion

  • How to find GCD (Greatest Common Divisor) or two numbers using recursion in C++.
  • Write a C++ program to calculate GCD or HCF of two numbers using recursion.

C++ program to calculate GCD using recursion

C++ program to calculate GCD using recursion

#include <iostream>
 
using namespace std;
   
int getGcd(int a, int b);
 
int main(){
    int num1, num2, gcd;
     
 cout << "Enter two numbers\n";
    cin >> num1 >> num2;
     
    gcd = getGcd(num1, num2);
 
    cout << "GCD of " << num1 << " and " << num2 << " is " << gcd;
 
    return 0;
}
/*
 * Function to calculate Greatest Common Divisor of two number
 */
 int getGcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  else {
    return getGcd(b, a % b);
  }
}

Output

Enter two numbers
8 60
GCD of 8 and 60 is 4

Exploring basic to advanced concepts in the C++ programming language helps beginners and experienced programmers to excel in C++ Programs. Go with this page & enhance your coding skills.

C++ Program to Display Armstrong Number Between Two Intervals

In the previous article, we have discussed C++ Program to Get the List of all Files in a Given Directory and its Sub-Directories. In this article, we will see C++ Program to Display Armstrong Number Between Two Intervals.

C++ Program to Display Armstrong Number Between Two Intervals

  • Write a C++ program to print all armstrong numbers between two intervals.

In this C++ program, we will find all armstrong numbers between two given integers. Here is a brief introduction of armstrong number:

An Armstrong number is a number whose sum of cubes of every digit of a number is equal to the number itself.
For Example:

407 is an Armstrong number
407 = 4*4*4 + 0*0*0 + 7*7*7

Algorithm to check for Armstrong Number

  • Take a number as input from user and store it in an integer variable(Let’s call it inputNumber).
  • Find the cubic sum of digits of inputNumber, and store it in sum variable.
  • Compare inputNumber and sum.
  • If both are equal then input number is Armstrong number otherwise not an Armstrong number.

In this program, we will take two two integers as input from user and then print all armstrong numbers between given two integers. Here is the C++ program to print all armstrong number between given interval.

C++ program to print all armstrong numbers between two integers

C++ program to print all armstrong numbers between two integers

// C++ Program to Print Armstrong Number Between Two Intervals
#include <iostream>
using namespace std;
  
/*
 * Funtion to calculate the sum of cubes of digits of a number
 * getCubicSumOfDigits(123) = 1*1*1 + 2*2*2 + 3*3*3;
 */
int getCubicSumOfDigits(int number){
    int lastDigit, sum = 0;
    while(number != 0){
        lastDigit = number%10;
        sum = sum + lastDigit*lastDigit*lastDigit;
        number = number/10;
    }
    return sum;
}
 
int main(){
    int x, y, sum, i;
     
    cout << "Enter two integers\n";
    cin >> x >> y;
     
 cout << "Armstrong numbers between " << x <<" and "<< y << endl;
    // Iterate from x till y, and check for Armstrong number
    for(i = x; i <= y; i++){
        sum = getCubicSumOfDigits(i);
        if(sum == i){
            cout << i << endl;
        }
    }
 
    return 0;
}

Output

Enter two integers
200 500
Armstrong numbers between 200 to 500
370
371
407

In above program, we first take two numbers as input from user and store it in variable x and y. Using a for loop, we iterate from x till y and check for each number whether it is armstrong number or not.

We have defined a function “getCubicSumOfDigits”, which takes an integer parameter as input and then returns the sum of cubes of digits of a number. Inside getCubicSumOfDigits function, we extract digits of number one by one add the cube of the digit to a variable sum.

For Example:

getCubicSumOfDigits(123) = 1*1*1 + 2*2*2 + 3*3*3 = 36.

Finally, we compare the cubic sum of digits of a number with the number itself. If both are equal than number is an armstromg number otherwise not an armstromg number.

Write simple C++ programs to print a message, swap numbers, and many more to get an idea on how the program works. Here given C++ Programs for Practice are useful to master your coding skills and learn various topics.