Category Archives: Misc

View Miscellaneous problems

Reverse a Linked List in pairs.

Question: Write a program to reverse a Linked List in pairs. That means we should reverse the 1st and 2nd numbers, 3rd and 4th numbers and so on. Input: 4, 8, 15, 16, 23, 42 Output: 8, 4, 16, 15, 42, 23 The basic idea behind solving this problem is that we need to traverse the entire list… Read More »

What is the difference between char s[] and char * s in C?

Question: In C you can do something like:- char s[] = “Hello World”; and char *s = “Hello World”; What is the difference between the two of them? What happens to the memory during compile time and run-time? This declaration: Creates ONE object – a char array of size 6, called s, initialized with the values ‘h’, ‘e’,… Read More »

How to use boolean type in C?

Question: C does not have the boolean data type by default. Is there a way by which we can implement it? In C language, we do not have the boolean datatype by default. That means we cannot do something like We can do this in C++ and JAVA. However, we have an alternative method to achieve the same… Read More »

What is the difference between calloc() and malloc() ?

Question: What is the difference between doing:         ptr = (char **) malloc (MAXELEMS * sizeof(char *)); or:         ptr = (char **) calloc (MAXELEMS, sizeof(char*)); When is it a good idea to use calloc and when to use malloc ? There are two differences:- Firstly it is in the number of arguments, malloc() takes a single argument… Read More »

What is an unsigned char?

Question: In C, what is an unsigned char used for? How is this different from a regular char? In C, there are three distinct character types: char signed char unsigned char If you are using character types for text, use the unqualified char: it is the type of character literals like ‘a’ or ‘0’. it is the type… Read More »

What is the difference between i++ and ++i ?

Question: In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop ? i++ is known as Post Increment whereas ++i is called Pre Increment. ++i will increment the value of i, and then return the incremented value. i++ will increment the value of i,… Read More »

Divide a number by 3 without using *, /, +, -, % operators

Question: How can you divide a number by 3 without using *, /, +, – % operator? Input: 48 Output: 16 The method below implements the method using bit-wise operators. For example if your number is 10 then convert to binary 10 =>00001010 if 10 > 3 then shift the binary number 2 bits Now num will be… Read More »

How to check if a given number is Fibonacci number?

Given a number ‘n’, how to check if n is a Fibonacci number. A simple way is to generate Fibonacci numbers until the generated number is greater than or equal to ‘n’. Following is an interesting property about Fibonacci numbers that can also be used to check if a given number is Fibonacci or not. A number is… Read More »