Found 33676 Articles for Programming

Basics of File Handling in C Programming

sudhir sharma
Updated on 17-Jul-2020 11:48:45

21K+ Views

File Handling is the storing of data in a file using a program. In C programming language, the programs store results, and other data of the program to a file using file handling in C. Also, we can extract/fetch data from a file to work with it in the program.The operations that you can perform on a File in C are −Creating a new fileOpening an existing fileReading data from an existing fileWriting data to a fileMoving data to a specific location on the fileClosing the fileCreating or opening file using fopen()The fopen() function is used to create a new ... Read More

attributes in C++

sudhir sharma
Updated on 17-Jul-2020 11:39:44

4K+ Views

Attributes are modern ways in C++ to standardize things if their code runs on different compilers. Attributes are used to provide some extra information that is used to enforce conditions (constraints), optimization and do specific code generation if required.These are like an information manual for the compilers to do some enforcing which will improve performance of the code. Attributes were first seen in C++ 11 and are important parts of the programming language since then, Also with every version some revisions are continuously made to make them more powerful and better.Let’s see how we can define attributes in C++For different ... Read More

Ways to multiply n elements with an associative operation in C++

sudhir sharma
Updated on 17-Jul-2020 11:35:40

146 Views

In this problem, we are given an integer n which is the number of elements. Our task is to create a program that counts the number of ways to multiply n elements with the associative operation.Associative operations return the same result irrespective of the manner the numbers are arranged.Let’s take an example to understand the problem, Input3Output12Explanation(x*(y*z)), (x*(z*y)), (y*(x*z)), (y*(z*x)), (z*(x*y)), (z*(y*x)), ((x*y)*z), ((y*x)*z), ((x*z)*y), ((z*x)*y), ((z*y)*x), ((y*z)*x).To solve this problem, we will try to find if there is any relation or any type of series that can be created so that we can generalize our results. Let’s see the ... Read More

Ways to paint N paintings such that adjacent paintings don’t have same colors in C++

sudhir sharma
Updated on 17-Jul-2020 11:32:40

241 Views

In this problem, we are given two integers n and m, where n is the number of paintings and m is the number of colors available. Our task is to create a program that will find the total number of ways in which we can paint the paintings in such a way that no to consecutive paintings have the same color.Let’s take an example to understand the problem, Inputn = 3, m =3Output12ExplanationP1 P2 P3 C1 C2 C3 C1 C3 C2 C1 C2 C1 C1 C3 C1 C2 C1 C2 C2 C3 C2 C2 C1 C3 C2 C3 C1 C3 ... Read More

Ways to paint stairs with two colors such that two adjacent are not yellow in C++

sudhir sharma
Updated on 17-Jul-2020 11:30:06

195 Views

We are given n stairs and 2 colors (red and yellow) with which these stairs are to be painted. Our task is to count the number of ways in which we can paint the stairs such that no two consecutive steps will be yellow-colored.Let’s take an example to understand the problem, Input3Output5ExplanationThe ways in which stairs can be painted are YRY, RYR, YRR, RRY, RRR. here R denotes red color, Y denotes yellow color.To solve this problem, let’s see the number of ways the stairs can be painted.N = 1, ways(1) = 2 : R, YN = 2, ways(2) = ... Read More

Ways to place items in n^2 positions such that no row/column contains more than one in C++

sudhir sharma
Updated on 17-Jul-2020 11:27:40

103 Views

In this problem, we are given an integer n such that there are n lines vertically and n horizontally that are placed such that there are n2 intersection between these lines. Our task is to find the total number of ways by which 4 items can be placed on these intersections insuch a way that no row and column contains more that one item.Let’s take an example to understand the problem, Inputn=4Output24ExplanationTo solve this problem, we will have to choose 4 horizontal lines from n lines that will have items which will be nC4. Now, every horizontal line has n ... Read More

Ways to remove one element from a binary string so that XOR becomes zero in C++

sudhir sharma
Updated on 17-Jul-2020 11:25:29

199 Views

In this problem, we are given a binary string. Our task is to count the total number of ways in which we can remove one element such that XOR becomes zero.Let’s take an example to understand the problem, Inputn = 11010Output3to solve this problem, we need the logic that if the number of 1’s is even then XOR of the string will be 0, otherwise, we need to remove one 1 from the string. We can remove any number of 0’s without affecting the XOR.Program to show the implementation of our solution, Example Live Demo#include #include using namespace std; int wayXorZero(string ... Read More

Ways to select one or more pairs from two different sets in C++

sudhir sharma
Updated on 17-Jul-2020 11:23:43

170 Views

In this problem, we are given two positive numbers n and m (n > 1;       x = (1LL * x * x) % p;    }    return res; } void calculate(int n){    fact[0] = inverseMod[0] = 1;    for (int i = 1; i

Ways to sum to N using array elements with repetition allowed in C++

sudhir sharma
Updated on 17-Jul-2020 11:19:38

711 Views

In this problem, we are given an array of integers and a number N. Our task is to count the total number of ways N can be generated by adding elements of the array. All combinations and repetitions are allowed.Let’s take an example to understand the problem, Inputarr = {1, 3, 5} N = 6Output8ExplanationThe ways are −5+1, 1+5, 3+3, 3+1+1+1, 1+3+1+1, 1+1+3+1, 1+1+1+3, 1+1+1+1+1+1To solve this problem, we need to use a different approach as all types of combinations will be treated differently so, if the number is a sum of 4 elements of array 4 different ways are ... Read More

Ways to write N as sum of two or more positive integers in C++

sudhir sharma
Updated on 17-Jul-2020 11:16:05

499 Views

In this problem, we are given an integer n. Our task is to find the total number of ways in can be expressed as sum of two or more positive integers.Let’s take an example to understand the problem, InputN = 4Output5Explanation4 can be written as the sum in these ways, 4, 3+1, 2+2, 2+1+1, 1+1+1+1To solve this problem, we will use Euler’s recurrence formula. For a number n the total number of ways it can be generated p(n) by, Σ∞n=0 p(n)xn = Π∞k=1 (1/(1-xk ))Using this formula, we will derive formula for p(n), p(n) = p(n-1) + p(n-2) - p(n-5) ... Read More

Advertisements