Found 1339 Articles for C

C/C++ Program for Maximum height when coins are arranged in a triangle?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

619 Views

In this section, we will see one interesting problem. There are N coins. we have to find what is the max height we can make if we arrange the coins as pyramid. In this fashion, the first row will hold 1 coin, second will hold 2 coins and so on.In the given diagram, we can see to make a pyramid of height three we need minimum 6 coins. We cannot make height 4 until we have 10 coins. Now let us see how to check the maximum height.We can get the height by using this formula.Example Live Demo#include #include using namespace ... Read More

c32rtomb() function in C/C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

143 Views

In C++, we can use 32-bit character representations. The c32rtomb() function is used to convert 32-bit character representation to narrow multi-byte character representation. We can find this function inside the uchar.h header file.This function takes three parameters. These are −The string where multi-byte character will be stored32-bit character to convertThe pointer of type mbstate_t object. which is used to interpret multibyte string.This function returns number of bytes written to the character array, when it is successful, otherwise returns -1. Let us see an example to get better idea.Example Live Demo#include #include #include using namespace std; int main() { ... Read More

c16rtomb() function in C/C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

192 Views

In C++, we can use 16-bit character representations. The c16rtomb() function is used to convert 16-bit character representation to narrow multi-byte character representation. We can find this function inside the uchar.h header file.This function takes three parameters. These are −The string where multi-byte character will be stored16-bit character to convertThe pointer of type mbstate_t object. which is used to interpret multibyte string.This function returns number of bytes written to the character array, when it is successful, otherwise returns -1. Let us see an example to get better idea.Example Live Demo#include #include #include using namespace std; int main() { ... Read More

A data structure for n elements and O(1) operations?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

550 Views

Here we will see one data-structure with n elements, and O(1) operations. So the operations will take constant amount of time to execute.The data structure will hold n elements (from 0 to n-1). The data can be in any order. The Insertion, deletion and searching will take O(1) amount of time.To solve this problem, we will use one Boolean array. This will indicate that the item is present or not at position i. If the item is present, it will hold 1, otherwise 0.Algorithminitialization(n)begin    fill all elements of the Boolean array as 0 endinsert(i)begin    set element at index ... Read More

A C Programming Language Puzzle?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

478 Views

Here we will see one C programming language puzzle question. Suppose we have two numbers 48 and 96. We have to add the first number after the second one. So final result will be like 9648. But we cannot use any logical, arithmetic, string related operations, also cannot use any pre-defined functions. So how can we do that?This is easy. We can do by using Token Pasting operator(##) in C. The Token Pasting operator is a preprocessor operator. It sends commands to compiler to add or concatenate two tokens into one string. We use this operator at the macro definition.Example Live ... Read More

2’s compliment for a given string using XOR ?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

929 Views

In this section we will see how we can find the 2’s complement using the XOR operation on a binary string. The 2’s complement is actually the 1’s complement + 1. We will use XOR operation to get the 1’s complement.We will traverse the string from LSb, and look for 0. We will flip all 1’s to 0 until we get a 0. Then flip the found 0.We will traverse from LSb. Then ignoring all 0’s until we get 1. Ignoring the first 1, we will toggle all bits using the XOR operation.Algorithmget2sComp(bin)begin    len := length of the binary ... Read More

1 to n bit numbers with no consecutive 1s in binary representation?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

492 Views

In this problem, we have to find some binary numbers which have no consecutive 1s. In a 3-bit binary string, there are three binary numbers 011, 110, 111, who have consecutive 1s, and five numbers are there which have no consecutive 1s. So after applying this algorithm for 3-bit numbers, the answer will be 5.If a[i] be the set of binary numbers, whose number of bits are i, and not containing any consecutive 1s, and b[i] is the set of binary number, where number of bits are i, and containing consecutive 1s, then there are recurrence relations like −a[i] := ... Read More

# and ## Operators in C ?

Vivek Verma
Updated on 12-Jul-2025 23:12:11

3K+ Views

In C, the "#" and "##" are the pre-processor operators that are used to convert a macro parameter to a String Literal. The macro parameters are the parameters of the macro definition, which are declared using the #define directive. The "#" operator is known as the Stringize operator, whereas the "##" operator is known as the Token Pasting operator. The #define Directive in C The #define directive is a preprocessor command that is used to create macros in the C programming language. Here is a syntax to create macros: #define MACRO_NAME replacement_text Or #define MACRO_NAME(param1, param2, …paramN) replacement_text ... Read More

attribute((constructor)) and attribute((destructor)) syntaxes in C in tutorials point ?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

3K+ Views

Here we will see how to write a code where two functions are present, and one function will be executed before the main function, and another function will be executed after main function. These features are used to do some startup task before executing main, and some clean up task after executing main.To do this task we have to put attribute for these two functions. When the attribute is constructor attribute, then it will be executed before main(), and when the attribute is destructor type, then it will be executed after main().We are using GCC functions. The function is __attribute__(). ... Read More

C/C++ program to make a simple calculator?

karthikeya Boyini
Updated on 14-Feb-2025 18:07:39

1K+ Views

A simple calculator allows the user to perform basic math operations like addition, subtraction, multiplication, and division . The user inputs two numbers and selects an operation. For example, if the user enters 4 and 3, then selects the "+" operation, the calculator will perform addition and output the result, 7. Similarly, if the user selects "-", the program will perform subtraction (4 - 3 = 1), and so on. In C++, this can be done using conditional statements and basic math operators. In this article, we will show you how to write a simple calculator program in ... Read More

Advertisements