Found 7347 Articles for C++

Add N digits to A such that it is divisible by B after each addition?

sudhir sharma
Updated on 16-Aug-2019 10:51:17

87 Views

Given a, b and n. And we have to consider the following conditions and find the optimal solution to add n digits to a such that it is divisible by b after every iteration.Add a digit to a in such a way that after adding it, a is divisible by b.Print the smallest value of a possible after n iterations of step1.Print fail if the operation fails.check divisibility after every digit addition.Inputa=5 b=4 n=4Output52000ExplanationThe first digit to be added from 0 to 9, if none of the digits make a divisible by b then the answer is -1 which means the if n digits are ... Read More

Addition of two number using ‘-‘ operator?

sudhir sharma
Updated on 16-Aug-2019 12:05:43

3K+ Views

Operator overloading is an important concept in C++. It is a type of polymorphism in which an operator is overloaded to give user-defined meaning to it. The overloaded operator is used to perform the operation on the user-defined data type. For example, '+' operator can be overloaded to perform addition on various data types, like for Integer, String(concatenation), etc.Input10 20 20 30Output30 50ExplanationTo perform addition of two numbers using ‘-‘ operator by Operator overloading. Binary operators will require one object as an argument so they can perform the operation. If we are using Friend functions here then it will need ... Read More

C++ Program for Dijkstra’s shortest path algorithm?

sudhir sharma
Updated on 09-Aug-2019 13:08:53

14K+ Views

Dijkstra's algorithm (or Dijkstra's Shortest Path First algorithm, SPF algorithm) is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. The algorithm creates a tree of shortest paths from the starting vertex, the source, to all other points in the graph.Dijkstra’s algorithm finds a shortest path tree from a single source node, by building a set of nodes that have minimum distance from the source.The graph has the following−vertices, or nodes, denoted in the algorithm by v or u.weighted edges that connect two nodes: (u, v) denotes an edge, and ... Read More

C/C++ Program for nth Catalan Number?

sudhir sharma
Updated on 13-Aug-2019 06:45:25

456 Views

Catalan numbers are a sequence of numbers. Catalan numbers form a sequence of natural numbers that occur in various counting problems, often involving recursively-defined objects.Cn is the number of Dyck words of length 2n. A Dyck word is a string consisting of n X's and n Y's such that no initial segment of the string has more Y's than X's. For example, the following are the Dyck words of length 6XXXYYY XYXXYY XYXYXY XXYYXY XXYXYY.Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, Cn counts the number of expressions containing n pairs of parentheses which ... Read More

Concatenate a string given number of times in C++ programming

sudhir sharma
Updated on 09-Aug-2019 12:24:08

600 Views

A program to concatenate a string a given number of times will run the string concatenate method n number of times based on the value of n.The result would be string repeated a number of times.Examplegiven string: “ I love Tutorials point” n = 5OutputI love Tutorials pointI love Tutorials pointI love Tutorials pointI love Tutorials point I love Tutorials pointAfter seeing the output, it is clear that what the function will do.Example#include #include using namespace std; string repeat(string s, int n) {    string s1 = s;    for (int i=1; i

Bitset all() function in C++ STL

sudhir sharma
Updated on 09-Aug-2019 12:21:03

110 Views

The bitset all() function an inbuilt function of the C++ STL( Standard Template Library). This function returns a Boolean value. The returned value is true if all the bits of the calling bitset are 1 else it will return false.The function does not accept any parameter and returns a Boolean value.SyntaxBool bitset_name .all()SampleBitset = 100101OutputfalseBecause all bits of the set need to be true in order to return a true value.Example#include using namespace std; void printer(bool val){    if(val){       cout

bitset::flip() in C++ STL

sudhir sharma
Updated on 09-Aug-2019 12:18:27

424 Views

The bitset flip() method is an inbuilt method of C++ STL( Standard Template Library). It flips the bits of the calling bitset. This method flips all 0’s to 1’s and all 1’s to 0’s, which means it reverse each and every bit of the calling bitset when no parameter is passed.If a parameter is passed the flip method will flip only the nth bit for the integer n passed. For example, if 5 is passed then the flip method will flip 5th bit of of the calling bitset.Syntaxbitset_name.flip(int pos)SampleInitial bitset: 011001After applying the bits flip function with no parameter: 100110After ... Read More

C++ Programming Internals?

sudhir sharma
Updated on 09-Aug-2019 07:28:43

375 Views

C++ Internals means how the working of C++ compiler compiling the .cpp code and giving us the output. C++ is a popular programming language mostly used for writing system software. It is an extension of the C programming language. C is a compiled language. The C++ compiler compiles C++ code to an object or executable file is generated. The executable or the binary files contains machine executable instructions and some metadata of the machine instructions.A typical way of compiling a C++ program is to run the compiler on C++ code. The compiler will generate machine instructions which are set of ... Read More

C++ mutable keyword?

sudhir sharma
Updated on 09-Aug-2019 07:24:21

4K+ Views

Mutable data members are those members whose values can be changed in runtime even if the object is of constant type. It is just opposite to constant.Sometimes logic required to use only one or two data member as a variable and another one as a constant to handle the data. In that situation, mutability is very helpful concept to manage classes.Example#include using namespace std; code class Test {    public:       int a;    mutable int b;    Test(int x=0, int y=0) {       a=x;       b=y;    }    void seta(int x=0) ... Read More

C++ Program string class and its applications?

sudhir sharma
Updated on 08-Aug-2019 08:18:41

130 Views

String is a sequence of characters. In C++ programming language, The strings can be defined in two ways −C style strings: treats the string as a character array.String Class in C++The string class can be used in a C++ program from the library ‘string’. It stores the string as a character array in the memory but shows it as a string object to the user. In C++ there are many methods that support the C++ string class and help in the proper function of the object and increase the efficiency of the code.ExampleSome common string application are found where string ... Read More

Advertisements