Found 1401 Articles for C

A C Programming Language Puzzle?

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

328 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

695 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

348 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 ?

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

2K+ Views

In this section we will see what are the Stringize operator(#) and Token Pasting operator(##) in C. The Stringize operator is a preprocessor operator. It sends commands to compiler to convert a token into string. We use this operator at the macro definition.Using stringize operator we can convert some text into string without using any quotes.Example Live Demo#include #define STR_PRINT(x) #x main() {    printf(STR_PRINT(This is a string without double quotes)); }OutputThis is a string without double quotesThe Token Pasting operator is a preprocessor operator. It sends commands to compiler to add or concatenate two tokens into one string. We use ... Read More

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

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

2K+ 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 30-Jul-2019 22:30:26

804 Views

A simple calculator is a calculator that performs some basic operations like ‘+’ , ‘-’ , ‘*’ , ‘/’. A calculator does basic operation in a fast way. We will use switch statement to make a calculator.SampleOperator − ‘+’ => 34 + 324 = 358 Operator − ‘-’ => 3874 - 324 = 3550 Operator − ‘*’ => 76 * 24 = 1824 Operator − ‘/’ => 645/5 = 129Example Code#include op;    cout

A comma operator question in C/C++ ?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

200 Views

Comma Operator in C/C++ programming language has two contexts −As a Separator −As an operator − The comma operator { , } is a binary operator that discards the first expression (after evaluation) and then use the value of the second expression. This operator has the least precedence.Consider the following codes and guess the output −Example Live Demo#include int main(void) {    char ch = 'a', 'b', 'c';    printf("%c", ch);    return 0; }OutputIt gives an error because the works as a separator.prog.c: In function ‘main’: prog.c:5:20: error: expected identifier or ‘(’ before 'b' char ch = 'a', 'b', ... Read More

Area of Circumcircle of a Right Angled Triangle?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

878 Views

The area of circumcircle of a right-angle triangle when the hypotenuse(H) of the triangle is given is found using the formula πH2/4.This formula is derived using the fact that the circumcircle touches all the corners of the triangle, in this case the maximum length between two points in the hypotheses which passes through the center of the circle. This makes the hypotenuse the diameter of the circle.This is why the area of circle which is πd2/4. (d = 2r) replaces the d by H.ExampleHypotenuse = 8Area of circle = 50.26Example Code Live Demo#include int main(void) {    int H = ... Read More

Area of circle which is inscribed in an equilateral triangle?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

8K+ Views

The area of a circle inscribed inside an equilateral triangle is found using the mathematical formula πa2/12.Lets see how this formula is derived, Formula to find the radius of the inscribed circle = area of the triangle / semi-perimeter of triangle.Area of triangle of side a = (√3)a2/4Semi-perimeter of triangle of side a = 3a/2According to formula, Radius of circle = (√3)a22/4 / 3a/2 = a/2√3Area of circle = πr2 = πa2/12Example Code Live Demo#include int main(void) {    int a = 5;    float pie = 3.14;    float area = (float)((pie*a*a)/12);    printf("the area of circle inscribed in ... Read More

Area of circle inscribed within rhombus?

Sharon Christine
Updated on 30-Jul-2019 22:30:26

2K+ Views

Circle inscribed in a rhombus touches its four side a four ends. The side of rhombus is a tangent to the circle.Here, r is the radius that is to be found using a and, the diagonals whose values are given.Now the area of triangle AOB = ½ * OA * OB = ½ * AB * r (both using formula ½*b*h).½ *a/2*b/2 = ½ *( √ (a2/4 + b2/4))*ra*b/8 = √ (a2+ b2 )*r /4r = a*b/ 2√ (a2+ b2 )Area of circle = π*r*r = π*(a2*b2)/4(a2+ b2 )ExampleThe diagonal of the rhombus 5 & 10.Area is 15.700000Example Code Live Demo#include ... Read More

Advertisements