Category Archives: C++ Programs

c plus plus programs

C++ Program to Subtract Complex Number Using Operator Overloading

C++ Program to Subtract Complex Number Using Operator Overloading

In this tutorial, we will learn to create a C++ program that will Subtract Complex Number Using Operator Overloading in C++ programming.

Prerequisites.

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • While loop in C++ programming.

What is a Complex Number?

A number is said to be a complex number if it is in the form of x+yi, where x and y are real numbers, and i is an indeterminate satisfying the condition i2 = −1.

For example, 2 + 3i

Algorithm:-

Program to Subtract Complex Number Using Operator Overloading:-

In this program, we will subtract a Complex Number Using Operator Overloading. First, we will take the numbers input from the user and second, we will solve that number using operator overloading.

Program:-

 

Output:-

In the above program, we have first initialized the required variable.

  • a = it will hold the complex value.
  • b = it will hold the complex value.
  • c = it will hold the complex value.
  • temp = it will hold the complex value.
  • real = it will hold the float value.
  • img = it will hold the integer value.

Taking the input integer number and exponent from the user.

Calculating the complex numbers.
Printing the output numbers.

C++ Program to Demonstrate Increment ++ and Decrement — Operator Overloading

 

In this tutorial, we will learn to create a C++ program that will Demonstrate Increment ++ and Decrement — Operator Overloading in C++ programming.

Prerequisites.

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • Classes and Objects in C++ programming.

What are the operators?

Operators are symbols with special meanings in a programming language. The operators are used between one or more operand.

There are three types of operators on the basis of operands.

  1. Unary operator.
  2. Binary operator.
  3. Ternary operator.

In this program, we will overload the unary operator i.e. “++”,”–“.

C++ Program for Operator overloading:-

In this program, we will Overload increment and decrement operators. We will use the operators to work for user-defined classes. It means we will use the operators with special meaning in the program for a data type.

Program to overload increment operator:-

 

Program to overload decrement operator:-

Output for increment:-

Output for decrement:-

Overloading the increment operator.

Overloading the decrement operator.
Main function body.
Printing the outputs
1. Increment Operator overloading.
2. Decrement operator overloading.

C++ Program to Demonstrate Operator Overloading

C++ Program to Demonstrate Operator Overloading

In this tutorial, we will learn to create a C++ program that will Demonstrate Operator Overloading in C++ programming.

Prerequisites.

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • Classes and Objects in C++ programming.

What are the operators?

Operators are symbols with special meanings in a programming language for datatypes. The operators are used between one or more operands to perform the operations.

There are three types of operators

In this program, we will overload the Arithmetic operator i.e. “++”.

Algorithm:-

Program for Operator overloading:-

In this program, we will Overload the increment operator. We will use the operators to work for user-defined classes. It means we will use the operators with special meaning in the program for a data type.

Program to overload increment operator:-

Output for increment:-

Declaring the required variables for the program.

Creating a constructor.
Overload operator.
Main function body.
Printing the outputs

C++ Program to Convert Binary to Decimal

In this tutorial, we will create a c++ program to covert Binary to Decimal using C++ programming language.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Basic input/output Functions.
  • Basic c++ programming.
  • C++ Functions.
  • User-defined Functions in C++.
  • C++ Recursion.
  • if…else statement.
  • Loops.

What Is Binary Number?

A number system in which numbers are represented by using only two digits (0 and 1) with a base 2 is called a binary number system.In the concept of converting a number from binary to a decimal number , extract all the digits of the binary number (starting from right of binary number )and multiply the each digits with the base(power of 2) lastly add each  number.As shown below.

Example: 1111
Decimal number =1*(2^3) + 1*(2^2) + 1*(2^1) + 1*(2^0)=8 + 4 +0+1 = 15.

C++ Program to Convert Binary to Decimal.

In this program we will covert Binary to Decimal using user define function. We would first declared and initialized the required variables. Next, we would prompt user to input the number . Later we will covert specified number fro Binary to Decimal.

Output

In the above program, we have first declared and initialized a set variables required in the program.

  • num = it will hold given binary number.
  • BD =  User define function.
  • no= catch value passing through function calling.
  • i= for iteration.
  • rem= hold the remainder.
  • d= hold the decimal value.

In the next statement user will be prompted to enter a Binary number which will be pass through BD(num) function as a parameter. Now within the function using while loop digits of binary are extracted one by one starting from right as shown in below image.

then extracted digits with remainders of the number are multiplied with powers of 2.
i.e. power of 2.   res = rem*pow(2,i) .
using inbuilt power function.
After multiplying each digit  results  added and stored in variable.
d(decimal) :=> d += res.
This process continues until while condition false ,and we finally we will print the result in output.

C++ Program to Remove all Characters in a String Except Alphabets

C++ Program to Remove all Characters in a String Except Alphabets

In this tutorial, we will learn to create a C++ program that will Remove all Characters in a String Except Alphabets in C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • For loop in C++ programming.
  • Conditional statements in C++ programming.
  • String functions of C++ programming.

What are Characters in a String?

The characters can be anything typed on the screen i.e. letter or number in a string. The string is a collection of characters. In C++ we can take input string from the user and store it in a variable.

Algorithm:-

Program to Remove all Characters in a String Except Alphabets:-

In today’s program, we will take the input string from the user and store it in a variable then we will remove all Characters in a String Except Alphabets. After removing we will print that string on the output screen.

The below will be an example program to remove the characters from the string.

Program:-

 

Output:-

In the above program, we have first initialized the required variable.

  • str = it will hold the string value.
  • flag = it will hold the string value.

Taking the input string from the user.

Remove all Characters in a String Except Alphabets.
Printing the output.

C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String

 

In this tutorial, we will learn to create a C++ program that will Find the Number of Vowels, Consonants, Digits, and White Spaces in a String in C programming

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • For loop in C++ programming.

About The Program

The string is a collection of numbers, vowels, consonants, and white spaces. In today’s program, we will count the vowels, consonants, digits, and white spaces from the input string. For this, we will pass that string to multiple conditions and loop to remove the vowels, consonants, Digits, and blank spaces.

Algorithm:-

 

Program to count Number of Vowels, Consonants, Digits and White Spaces:-

 

Output:-

In the above program, we have first initialized the required variable.

 

  • str[] = it will hold the string value.
  • vowels = it will hold the integer value for vowels.
  • consonant  = it will hold the integer value for consonants.
  • digits = it will hold the integer value for numbers.
  • spaces = it will hold the integer value for spaces.

Input strings from the user.

Calculating the Number of Vowels, Consonants, Digits, and White Spaces in a String.

Printing output.

C++ Program to Count Vowels in String

In this tutorial, we will create a c++  program to count vowels in entered string by the user using c++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators
  • Condition statement.
  • Array.
  • Strings.
  • logical operator.

What Is Vowel and Consonant?

Vowel :=> Sound that allowing breath to flow out of mouth, without closing any part of the mouth.
Consonant:=> Sound  made by blocking air from flowing out of the mouth with the teeth or throat.
Example

As we know A E I O U are called vowels. And renaming alphabets except these 5 vowels are called consonants.

C++ Program to Count Vowels in String.

Output

In the above program, we have first declared a variables required in the program.

  • strng= hold given string.
  • i= for iteration.
  • vow= to count vowels in a string.

First of all we will  prompted  user to enter any string.Then with the help of program we will find  the number of vowel in  inputted string using following logic.

As user inputs any string we check all vowel with in the string in both lower-case and upper-case as shown in above example using for loop we Initialize for loop and terminate it at the end of string.

As show in above here we will  check  the entire string with in the loop and count the number of vowels in a string .After the completion of a loop.we will get the number  vowel in a following  string and print total number vowel present within the string.

C++ Program to Find the Frequency of Characters in a String

In this tutorial, we will create a c++ program to  find frequency of occurrence of a character in a String  using C++ programming language.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators.
  • looping statements.
  • Basic input/output.
  • Basic c++ programming
  • For Loop.
  • String.

What Is frequency of occurrence of a character ?

frequency means  to find and count  given  character present in the string.i.e total number of occurrence of a particular character in a string.

C++ Program to Find the Frequency of Characters in a String.

In this program we will we will find frequency of a given character. We would first declared and initialized the required variables. Next, user will be prompted to enter a string and a character to find the frequency of given character.

Output

In the above program, we have first declared and initialized a set variables required in the program.

  • strng = it will hold entered string.
  • ch = it will hold a finding character.
  • i=  for iteration.
  • count= count number of occurrence of a character.

And  in the next statement user will be prompted to enter string and a character  which will be assigned to variable ‘strng’ and in ‘ch’ respectively. Now we will loop through the size of  a string  using sizeof() function in each  iteration we will compare string to given character to find the occurrence of a character and each matching condition we increase the counter value by  one as shown in  image.

This process  will continues until the loop ends , and at last we will print the total number  of  occurrence of a given character in the string.This is the process  of finding  the frequency of occurrence of a character in a String.

C++ Program to Copy String

C++ Program to Copy String

In this tutorial, we will learn to create a C++ program that will Copy String in C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators in C++ Programming.
  • Basic Input and Output function in C++ Programming.
  • Basic C++ programming.
  • Using Strings in C++ Programming.

Algorithm:-

Program to Copy String 

As we all know the String is a collection of characters, symbols, digits, and blank spaces. In strings, only one variable is declared which can store multiple values. First will take the input string from the user. And then will copy the string. The C++ programming language has many pre-defined functions for string manipulation. but in today’s tutorial, we will do copy work with the help of a simple assignment operator without using a predefined function.

With the help of the below program, we can Copy String.

Program Code:-

 

Output:-

In the above program, we have first initialized the required variable.

  • s1 = it will hold the string value.
  • s2 = it will hold the string value.

Taking Input string from the user for copying the string.

Reading the input string from the user with getline statement.

Copying the string with the help of the assignment operator.

Printing the output copied string2.

C++ Program to Reverse of String

In this tutorial, we will learn to create a C++ program that  will Reverse of String  using C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators.
  • looping statements.
  • Basic input/output.
  • Basic c++ programming
  • For Loop.
  • String.
  • Array.

String

Usually  “string,” means a collection of characters called ‘string’ that represents text data in languages like c,c++ and Java.

What is array?

An Array is a collection variable elements  that can store multiple values in a single variable of same type.

C++ Program to Reverse of String

In this program we will reversing a string. We would first declared and initialized the required variables. Next, we would prompt user to input a string .Later we will reverse a string.

In the above program, we have first declared and initialized a set variables required in the program.

  • strng = it will hold entered string.
  • tmp = it will hold a finding character.
  • i and j=  for iteration.

In our program, we print the string in reverse order.
i.e the last element of array should be displayed first,followed by second last element and so on.

First of all we  take a string as input from user and store the value in variable strng.

And now with the help of strlen() function we will find the length of a string and store the length of string in variable j.

Within the loop we reverse the value of string with help of tmp variable  as shown in below image.

And after  traversing the whole string traverse from last to first and print the string element in reverse order.

C++ Program to Compare Two Strings

In this tutorial, we will learn to create a C++ program that will Compare two Strings using C++ programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:

  • Operators.
  • looping statements.
  • Basic input/output.
  • Basic c++ programming
  • For Loop.
  • String.

String

Usually”string,” means a collection of characters called ‘string’ that represents text data in languages like c,c++ and Java.

What is array?

An Array is a collection variable elements  that can store multiple values in a single variable of same type.

C++ Program to Compare two Strings.

In this program we will compare two strings. We would first declared and initialized the required variables. Next, we would prompt user to input two strings .Later we will compare  strings.

output

In the above program, we have first declared and initialized a set variables required in the program.

  • strng1 = it will hold entered string one.
  • strng2 = it will hold entered string two.

In our program, we will compare two string if both are similar we will print string are equal if not they are not equal.

First of all we  take two strings input from user and store their value in variable strng1 and strng2.

And now with the help of strcmp() function we will compare these two strings .we check the string in if else condition if string are equal strcmp() gives true value

and if string are not equal strcmp() gives false value.

Here Strcmp()function is a pre-defined function in a string.h header file.The strcmp() function take two strings as a arguments, and returns an integer value where the integer value can be zeropositive or Negative.

On comparing the strings by using the strcmp() function, i.e., strcmp(str1,str2). This function will compare both the strings strng1 and strng2. the function returns 0 if values of strings are equal ,means that both the strings are same, otherwise the strings are not equal.