Found 7347 Articles for C++

C++ program to get length of strings, perform concatenation and swap characters

Arnab Chakraborty
Updated on 07-Oct-2021 07:37:09

245 Views

Suppose we have two strings s and t, we shall have to find output in three lines, the first line contains lengths of s and t separated by spaces, the second line is holding concatenation of s and t, and third line contains s and t separated by space but their first characters are swapped.So, if the input is like s = "hello", t = "programmer", then the output will be5 10 helloprogrammer pello hrogrammerTo solve this, we will follow these steps −display length of s then print one space and length of tdisplay s + ttemp := s[0]s[0] := ... Read More

C++ Program to take out integer from comma separated string

Arnab Chakraborty
Updated on 07-Oct-2021 07:35:03

3K+ Views

Suppose we have a string where we have few integers which are separated by comma. We shall have to separate them out and display each integer in different line. To do this we shall use stringstream (under sstream library) in C++. This is one string based stream class present in C++. We can use extraction operator () to insert something and str() functions to set contents of underlying string device object.So, if the input is like s = "56, 9, 85, 256, 47", then the output will be56 9 85 256 47To solve this, we will follow these steps −Define ... Read More

C++ program to reverse an array elements (in place)

Arnab Chakraborty
Updated on 07-Oct-2021 07:32:30

19K+ Views

Suppose we have an array with n different elements. We shall have to reverse the elements present in the array and display them. (Do not print them in reverse order, reverse elements in place).So, if the input is like n = 9 arr = [2, 5, 6, 4, 7, 8, 3, 6, 4], then the output will be [4, 6, 3, 8, 7, 4, 6, 5, 2]To solve this, we will follow these steps −for initialize i := 0, when i < quotient of n/2, update (increase i by 1), do:temp := arr[i]arr[i] := arr[n - i - 1]arr[n - ... Read More

C++ program to find addition and subtraction using function call by address

Arnab Chakraborty
Updated on 07-Oct-2021 07:29:43

2K+ Views

Suppose we have two numbers a and b. We shall have to define a function that can calculate (a + b) and (a - b) both. But using a function in C++, we can return at most one value. To find more than one output, we can use output parameters into function arguments using pointers, and call that function using addresses of those variables. Here in this problem we shall update a with a+b and b with a-b. When we call the function we shall have to pass the address of these two variables.So, if the input is like a ... Read More

C++ program to find greatest among four input integers

Arnab Chakraborty
Updated on 07-Oct-2021 07:27:19

5K+ Views

Suppose we have four integers a, b, c and d. We shall have to find the greatest number among them by making our own function. So we shall create one max() function that takes two numbers as input and finds the maximum, then using them we shall find maximum of all four numbers.So, if the input is like a = 75, b = 18, c = 25, d = 98, then the output will be 98.To solve this, we will follow these steps −define a function max(), this will take x and yreturn maximum of x and ytake four numbers ... Read More

C++ program to convert all digits from the given range into words

Arnab Chakraborty
Updated on 07-Oct-2021 07:24:41

316 Views

Suppose we have two digits a and b. We shall have to convert each digit into words and print them one by one. Printing digits into words means for a digit 5, it should print "Five".So, if the input is like a = 2, b = 6, then the output will beTwo Three Four Five SixTo solve this, we will follow these steps −if d < 0 and d > 9, then:return ("Beyond range of 0 - 9")otherwise when d is same as 0, then:return ("Zero")otherwise when d is same as 1, then:return ("One")otherwise when d is same as 2, ... Read More

C++ program to convert digits to words using conditional statements

Arnab Chakraborty
Updated on 07-Oct-2021 07:20:49

785 Views

Suppose we have a digit d, we shall have to convert it into words. So if d = 9, our output should be "Nine". If we provide some d which is beyond the range of 0 and 9, it will return appropriate output.So, if the input is like d = 3, then the output will be "Three".To solve this, we will follow these steps −Define a function solve(), this will take d, if d < 0 and d > 9, then:return ("Beyond range of 0 - 9")otherwise when d is same as 0, then:return ("Zero")otherwise when d is same as ... Read More

C++ program to take integer and float as input and return their sum

Arnab Chakraborty
Updated on 07-Oct-2021 07:07:54

2K+ Views

Suppose we have two numbers a and b, a is integer and b is a float. We shall have to take them from standard input and display the sum.So, if the input is like a = 10 b = 56.23, then the output will be Sum: 66.23To solve this, we will follow these steps −To display output into the standard output device we can use extraction operator ()ExampleLet us see the following implementation to get better understanding −#include using namespace std; int main(){    int a;    float b;    cout a >> b;    cout

Find the Nth Even Length Palindrome using C++

prateek jangid
Updated on 27-Sep-2021 10:58:29

654 Views

If you ever used C + +, then you must have heard about Palindrome numbers. So in this guide, we will explain everything about "Nth even-length Palindrome" using appropriate examples. Palindrome numbers are numbers that stay the same after reversing them. Not only numbers but a word whose spelling stays the same when its characters are reversed. For Example −Numbers = {1, 121, 131, 656, 1221, 1551}Words = {saas, malayalam, level, mom}It looks complicated but very easy to perform on any system. So let's discuss the palindrome in brief.Nth Even length Palindrome Number11, 22, 33, 44, 55, 66, 77, 88, ... Read More

Number of pairs with maximum sum in C++

Hafeezul Kareem
Updated on 26-Oct-2021 19:25:03

314 Views

Given an array, we need to find the number of pairs with maximum sum. Let's see an example.Inputarr = [3, 6, 5, 2, 1, 2, 3, 4, 1, 5]Output2 The maximum pair sum is 10. There are 2 pairs with maximum sum. They are (5, 5) and (6, 4).AlgorithmInitialise the array with random numbers.Initialise the maximum sum to minimum.Iterate over the array with two loops.Find the maximum sum of pairs.Initialise the count to 0.Now, iterate over the array again.Increment the count if the current pair sum is equal to the maximum pair sum.Return the pairs count.ImplementationFollowing is the implementation of ... Read More

Advertisements