Category Archives: C/C++ Puzzles

Complex numbers in C++ | Set 2

We introduced and discussed the concept in Complex numbers in C++ | Set 1 The remaining functions with example are discussed here: log() – It is used to return the log of the complex number. Output: The log of (-1,0) is (0,3.14159) cos() – It computes complex cosine of a complex value z. Mathematical definition… Read More »

Copy-and-Swap Idiom in C++

Before going into deep let us first have a look on the normal ‘overloaded assignment operator’ that we use. The above assignment operator does the following things: 1. Self assignment check. 2. If there assignment is not to self, then it does following.     a) Deallocating memory assigned to this->ptr     b) Allocating new memory to this->ptr… Read More »

exec family of functions in C

The exec family of functions replaces the current running process with a new process. It can be used to run a C program by using another C program. It comes under the header file unistd.h. There are many members in the exec family which are shown below with examples. execvp : Using this command, the created… Read More »

Inbuilt library functions for user Input | scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s

scanf() : The C library function int scanf (const char *format, …) reads formatted input from stdin. Syntax: int scanf(const char *format, …) int: return type format: string that contains the type specifier(s) “…” (ellipsis): indicates that the function accepts a variable number of arguments Each argument must be a memory address where the converted… Read More »

Write a URL in a C++ program

Writing URLs in a C++ code doesn’t affect the program and the compiler will not throw any error. Output: This code is error free This code is error free This code is error free This code is error free This code is error free Explanation: Any identifier followed by a : becomes a (goto) labels… Read More »

Complex numbers in C++ | Set 1

The complex library implements the complex class to contain complex numbers in cartesian form and several functions and overloads to operate with them. real() – It returns the real part of the complex number. imag() – It returns the imaginary part of the complex number. Output: Real part: 10 Imaginary part: 2 abs() – It… Read More »

Printing Heart Pattern in C

How to print below heart pattern in C? AAAAAAA AAAAAA AAAAAAAAA AAAAAAAA AAAAAAAAAAA AAAAAAAAAA AAAAAAAAAAAAA AAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBB BBBBBBBBBBBBB BBBBBBBBBBB BBBBBBBBB BBBBBBB BBBBB BBB B This is code to print the Heart pattern using simple C logic Output : AAAAAAA AAAAAA AAAAAAAAA AAAAAAAA AAAAAAAAAAA AAAAAAAAAA AAAAAAAAAAAAA AAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAA… Read More »

Loops in C++

Loops in programming comes into use when we need to repeatedly execute a block of statements. For example: Suppose we want to print “Hello World” 10 times. This can be done in two ways as shown below: Iterative Method Iterative method to do this is to write the printf() statement 10 times. Using Loops In… Read More »