ITERATIVE VERSION The iterative version can be done in this way:- Input: num Initialize rev_num = 0 Loop while num > 0 Multiply rev_num by 10 and add remainder of num divide by 10 to rev_num rev_num = rev_num*10 + num%10; Divide num by 10 Return rev_num Here is an implementation of the same. RECURSIVE WAY
Trivia
Some general stuff
-
-
From Wikipedia: A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system). Segmentation is one approach to memory management and protection in the operating system. It has been superseded by paging for most purposes, but much of the terminology of segmentation is still used, “segmentation fault” being an example.…
-
This is one of the method, not everyone is aware of in C. To generate a random number, C itself provides us a method srand(). We can use it in this way. NOTE: The function srand(time(NULL)) can only be called once in the program.
-
Many of us must have heard of the clause “Real Programmers code in C” and it is faster than any other language because “it is close to the machine“. What is so special about this language that makes it different from any other language. Let us try to explore this answer to some extent. There isn’t much that’s special about C. That’s one of the reasons why it’s fast. Newer languages which have support for garbage collection, dynamic typing and other facilities which make it easier for the programmer to…
-
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’, ‘l’, ‘l’, ‘o’, ‘\0’. Where this array is allocated in memory, and how long it lives for, depends on where the declaration appears. If the declaration is within a function, it will…
-
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 functionality. Before moving on to the implementation it is important to know that in C, if a value is ‘0 (zero)’ it is treated as false, and any other value other than…
-
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 (memory required in bytes), while calloc() needs two arguments. Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO. calloc() allocates a memory area, the length…
-
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 that makes up C strings like “abcde” It also works out as a number value, but it is unspecified whether that value is treated as signed or unsigned. Beware character comparisons through…
-
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, but return the original value that i held before being incremented. For a for loop, either works. ++i seems more common, perhaps because that is what is used in Dennis Ritchie. In…
-
Here are some of the basic operations on a singly Linked List. Creating a new List Adding new elements Traversing a List Printing a List Basic functions to perform the above techniques have been defined in the code below. Creating a new list A new Linked List creation means that we do not have any elements in the list and we want to start fresh. This means allocating space in the memory for a node and then inserting the data into it. Since only a single node is created, the…