Category Archives: C++ Programs

c plus plus programs

C++ Program to Concatenate Two Strings

C++ Program to Concatenate Two Strings

In this tutorial, we will learn to create a C++ program that will Concatenate Two Strings 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 Concatenate Two Strings

Concatenation means to join two values i.e. Strings. In strings, only one variable is declared which can store a sentence. It’s a collection of characters, digits, and Special characters. First will take the input string one from the user. Then we will take a second-string from the user and now we will concatenate that two stings.

Using the below program example.

Program Code:-

 

Output:-

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

  • str1 = it will hold the string value.
  • str2 = it will hold the string value.
  • result = it will hold the string value.
  • i = it will hold the integer value.

Taking Input string from the user for copying the string.

Concatenating the strings taken from the user with the help of the for loop.

Printing the output Concatenated String.

C++ Program to Find length of String

C++ Program to Find Length of String

In this tutorial, we will learn to create a C++ program that will Find the length of 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 Find the length of String

In this program, we will take the input as a string from the user and then we will find the length of the input string. Using the predefined string function from the c++ libraries.

With the help of this below program, we can find the length of the String.

Program Code:-

 

Output:-

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

  • str1 = it will hold the string value.
  • str2 = it will hold the string value.
  • result = it will hold the string value.
  • i = it will hold the integer value.

Taking Input string from the user for copying the string.

Printing the output size of the string.

C++ Program to Calculate Standard Deviation

In this tutorial, we will learn to create a  c++  Program to Calculate Standard Deviation.

Prerequisites

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

  • C++ Operators.
  • Functions.
  • For Loop and while loop.
  • Array.
  • String.

What Is Standard Deviation.?

The concept behind Standard Deviation is a measure of how spread out numbers are in a given Scenario.In Mathematical  term, the Standard Deviation is  measure of the amount of variation on set of values.

formula is Standard deviation  is :=>

where ∑ sum means “sum of”.
x is a value in the data set.
μ is the mean of the data set.
and N is the number of data points.

in simple form Standard deviation is .

Find the mean ∑.
Find variation (Variance, take each difference, square it, and then average the result)
Sum the values from above step and divide by total numbers.
Standard Deviation is just the square root of Variance.

C++ Program to Calculate Standard Deviation

In this program , we will calculate Standard Deviation of a given numbers .First of all user will be prompted to enter a number and afterword passing these value to function to calculate Standard deviation.

Output

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

  • arr[] = it will hold entered elements.
  • value = it will hold a elements values.
  • i=  for iteration.
  • sum= add all elements to find mean.
  • mean=hold the mean value.
  • sd= gives us standard deviation.

Now let us look method to find standard deviation step by step.

  1. first or all take elements from user.
  2. then pass these elements to function SD.
  3. where first we calculate the mean (average) of each data set.
  4. then,subtracting the mean from each number.
  5. then,Square each deviation.
  6. after that add all the squared deviations.
  7. Divide the value obtained  by the number of items in the data set.
  8. Calculate the square root of the value obtained (Variance).
  9. And at the return the sd to main function were we print standard deviation.

C++ Program to Access Elements of an Array Using Pointer

C++ Program to Access Elements of an Array Using Pointer

In this tutorial, we will learn to create a C++ program that will Access Elements of an Array Using Pointer using 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.
  • Using pointer in C++ programming.

Array And Pointer:-

The array in the C++ programming language is a collection of similar data type elements. It can store multiple values in a single variable.

Pointer variables are used to access the element using the address of the variable.

Algorithm:-

Access Elements of an Array Using Pointer:-

In this program, we will create an array of five elements and take input from the user. Now the printing work of those elements will be done with the help of a pointer variable.

Pointer is a variable that can access elements another variable using its address.

Program:-

Access Elements of an Array Using Pointer.

Output:-

In the above program, we have to first initialize the required variable.

  • arry = it will hold the elements in an array.
  • i = it will hold the integer value to control the array.

Taking input from the user in an array the element in the array.

Printing the elements of the array using the pointer.

C++ Program to Sort Elements in Lexicographical Order

In this tutorial, we will learn to create a c++ Program to Sort Elements in Lexicographical Order.

Prerequisites

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

  • Operators.
  • Basic input/output.
  • Basic c++ programming.
  • Array and Multidimensional Array.
  • Loops.
  • Nested For loop.

What is Lexicographical Order?.

In Maths, the terminology  lexicographic or lexicographical  order is a Alphabetical order or the Dictionary ordered,more generally “elements of a ordered set(ascending order(a-z))”.

C++ Program to Sort Elements in Lexicographical Order.

In this program we will sort elements in Lexicographical Order using nested for loop.
first of all we declared and initialized the required variables after that user will prompt to input  elements ,then we using  different methods arrange all these Elements is in Lexicographical Order.
Here we takes 5 elements from the user and arrange them in lexicographical order using nested for loop.

Output

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

  • s = it will holds 5 elements.
  • tmp= holds temporary values.
  • i and j= for iterations.

In this  program we will find Lexicographical Order of given elements by user.first of all in this statement  we will ask the user to enter the 5 elements.


And we arrange the elements into Lexicographical Order using bubble sort algorithm.

Bubble Sort

This sorting algorithm are also called as sinking sort method,In this sorting algorithm  terms repeatedly swapping the adjacent elements if they are in wrong order.

Finally the result  printed in lexicographical order . This is given below −elements entered by user ,using algorithm we arrange them in sorted manner.

C++ Programs to Reverse Array Element Using Function

C++ Programs to Reverse Array Element Using Function

In this tutorial, we will learn to create a C++ program that will Reverse Array Element Using Function using 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.
  • Functions in C++ Programming.

Reversing of the array.

The array stores multiple elements in a single variable using the different index values. All the array operations are performed on the index values for manipulation in an array. In this tutorial, we will reverse the elements of the array from start to end.

Algorithm:-

Programs to Reverse Array Element Using Function:-

In the program, First, we will initialize the elements of an array. Then we will reverse that array using C++ Programming Language.

Program:-

To Reverse Array Element Using Function

 

Output:-

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

  • arr = it will hold the elements in an array.
  • n = it will hold the number of elements in an array.
  • i = it will hold the integer value to control the array.

Printing elements of the array

Calling of user-defined function in program.

Program Code for reverse function.

Program Code for Print function.

C++ Programs to Reverse Array Elements

In this tutorial, we will learn to create a c++ program to reverse an array entered by user at run-time 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.

What is array?

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

Programs to Reverse Array Elements.

In this program , elements  of an Array will be reverse . First of all user will be prompted to enter array elements and will we reverse the elements of array.

Output

C++ Programs to Reverse Array Elements

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

  • num = it will hold size of array.
  • arr[]= it will hold multiple elements.
  • i = for iteration of loop.

In our program, we print the elements of the array 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 number as input from user to entered number store it in an array.Declare  array of size equal to inputted num .
And now Input the array elements into the variable arr[].


And after that traverse the array in reverse from last to first and print the array element in reverse order that’s it.

C++ Programs to Find Duplicate Array Element

In this tutorial, we will learn to create a c++ program that find the Duplicate Array Element 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.
  • Array.
  • Function.

What Is Array ?

An Array is a collection of similar data type containing  collection of elements (values or variables), each identified by one Array index value, Array is a data structure that  hold  collection of elements.

C++ Programs to Find Duplicate Array Element.

In this program ,we will find duplicate elements in a Array .First of all user will be prompted to enter a the size or Array and after that user will give elements and we will find the repeating elements in a given Array. Let see the code for finding duplicate values.

Output

C++ Programs to Find Duplicate Array Element

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

  • no = it will hold size of array
  • ele = it will hold a value of array.
  • i & j =  for iteration.
  • arr= it will hold a value of array.
  • num= it will hold total number of array value.

And  in the next statement user will be prompted to enter size of a Array and value of Array.which will be assigned to variable ‘no’ and in ‘ele’ respectively.

and

Now ,loop through the size given  by user and take all values supply by user in ele variable.Afterword pass this value to function.

and within the function in each  iteration we will compare values of array  to find the occurrence of a similar elements and each matching condition we print the matched values .

This process  will continue until loop ends within the function, and at last we will print the total number occurrence of a Duplicate values.

C++ Programs to Sum of Array Elements

In this tutorial, we will learn to create a C++ program that will Calculate Sum of array elements 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.

What is array?

An Array is a collection variable elements  that can store multiple values in a single variable of same type.In Computer Science,an array is a data structure, or simply an array, is a data structure consisting of a collection of elements.

C++ Programs to Sum of Array Elements

In this program we will add array elements. We would first declared and initialized the required variables. Next, we would prompt user to input elements of array .Later we add all the elements of Array.

Output

C++ Programs to Sum of Array Elements

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

  • a[] = it will hold entered elements.
  • sum =hold sum or elements.
  • i = for iteration.
  • num= number of elements in array.

In our program, we will take value form user in array , after taking values we will add elements of an array.
First of all we  take size of an array from user in variable num

after taking size of array user will prompted to enter the elements of array.

The variables ‘sum’ is initialized as 0.Then asked user to enter the array elements.Within the loop, the elements are stored in the array ‘=a[]’.
And in next loop the sum is calculated and stored in sum.As shown in below image.

in every iteration. sum is calculated ans stored in variable sum  .At the end the value of sum will be printed.

c++ program to merge two arrays

C++ Program to Merge Two Arrays

In this tutorial, we will learn to create a C++ program that will merge two arrays using 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.

Algorithm:-

Merging of two arrays:-

In Today’s we will merge two different arrays into a single array with a very simple and shortcode. First, we will take two arrays in input and then we will merge them into one single array using short C++ programming.

Program:-

To find Merge the two array elements into a single array.

Output:-

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

  • arr1[] = it will hold the elements in an array1.
  • arr2[] = it will hold the elements in an array2.
  • arr3[] = it will hold the elements in an array3.
  • mergeArrays[] = it will hold the elements in an array.
  • i = it will hold the integer value to control array using for loop.
  • j = it will hold the integer value to control array using for loop.
  • k = it will hold the integer value to control array using for loop.

Taking input for array1 as well as array2.

Merging the two arrays.

Main function Code.

C++ Programs to Pass Array In Function

C++ Programs to Array In Function

In this tutorial, we will learn to create a C++ program that will Pass Array In Function using 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.
  • Functions in C++ Programming.

Algorithm:-

Program to pass Array In Function:-

As we all know array is a collection of similar data type elements. we can perform many different operations on arrays in c++ programming. In today’s program, we will reverse a given array using a user-defined function. Bypassing that array to a function.

Program Code:-

Pass Array In Function To Reverse Array Element Using Function

 

Output:-

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

  • arr = it will hold the elements in an array.
  • n = it will hold the number of elements in an array.
  • i = it will hold the integer value to control the array.

Printing elements of the array

Calling of user-defined function in the program.

Program Code for reverse function & Print function.

C++ Programs to Sort Array Element

In this tutorial, we will learn to create a C++ program that will sort Array Elements 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.
  • Array.

What is array?

An Array is a collection variable elements  that can store multiple values in a single variable of same type.In Computer Science,an array is a data structure, or simply an array, is a data structure consisting of a collection of elements.

C++ Programs to Sort Array Element.

In this program we will sort  Array elements. We would first declared and initialized the required variables. then  we would prompt user to input elements of array .Later we sort  an Array elements in ascending order.

Output

C++ Programs to Sort Array Element

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

  • a[] = it will hold entered elements.
  • num =it will hold size of array.
  • i and j= for iteration.
  • tmp= it hold temporary values.

In our program, we will take 5 values form user in an array , after taking these values we will sort the elements of an array.
First of all we  take size of an array from user in variable num.

After taking size of an array user will prompted to enter the elements of array.

Here  we will use selection sort algorithm sorts an array by repeatedly finding the minimum element from unsorted part and putting it at the beginning.

In every iteration sorted value is arranging in a[i] .At the end the value of sorted array will be printed.