C Program to Compute x^n — Fast Recursive Exponentiation

Computing x raised to the power n (written xn) naively requires n multiplications. Binary exponentiation (also called fast power or exponentiation by squaring) reduces this to O(log n) multiplications by halving the exponent at each step: if n is even, compute xn/2 and square it; if n is odd, multiply x by xn-1. The original …

C Program to Find Length of a String Without strlen

The standard library’s strlen() counts characters in a string by walking from the first character until it reaches the null terminator (‘\0’) — the zero byte that marks the end of every C string. Implementing strlen() yourself is a classic exercise that teaches exactly how strings work in C. The original post had two serious …

C Program to convert days to years, weeks and days

This program converts a given number of days into years, weeks, and remaining days using integer division and modulo arithmetic. The formula uses 365 days per year and 7 days per week (leap years are ignored). It is a straightforward exercise in chained division and remainder operations. The original post used void main() and broken …

C Program to compute the surface area and volume of a cube

A cube is a three-dimensional shape with six equal square faces. Given the side length s, the surface area is 6s² (six faces, each with area s²) and the volume is s³. This program reads the side length and computes both values using only basic arithmetic — no need for pow() and the -lm flag …

C Program to accept a string and a substring and check if the substring is present in the given string

Checking whether one string contains another is one of the most common string operations in C. The standard library provides strstr() in <string.h> — it returns a pointer to the first occurrence of the substring in the main string, or NULL if not found. The position of the match is calculated by subtracting the start …

C Program: Array Summation Using Pointers

This program reads N integers into a dynamically allocated array and computes their sum using pointer arithmetic instead of array subscript notation. Understanding the equivalence between *(a + i) and a[i] is fundamental to C — both produce identical machine code. The program also demonstrates malloc() and free() for runtime-sized arrays. The original post used …