
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Program to compute Log n in C
Given with the value of n as an input and the task is to compute the value of Log n through a function and display it.
Logarithm or Log is the inverse function to exponentiation which means to calculate log the raised power must be calculated as a base.
IF
$$\log_b x\;\:=\: y\:than\:b^{y}=x$$
Like
$$\log_2 64\;\:=\: 6\:than\:2^{6}=64$$
Example
Input-: Log 20 Output-: 4 Input-: Log 64 Output-: 6
Algorithm
Start In function unsigned int log2n(unsigned int num) Step 1-> Return (num > 1) ? 1 + log2n(num / 2) : 0 In function int main() Step 1-> Declare and assign num = 20 Print log2n(num) Stop
Example
#include <stdio.h> //We will be using recursive Approach used below is as follows unsigned int log2n(unsigned int num) { return (num > 1) ? 1 + log2n(num / 2) : 0; } int main() { unsigned int num = 20; printf("%u
", log2n(num)); return 0; }
Output
4
- Related Articles
- C++ Program to compute division upto n decimal places
- Find minimum number of Log value needed to calculate Log upto N in C++
- Program for power of a complex number in O(log n) in C++
- Compute log-determinants for a stack of matrices in Python
- Python Program to Input a Number n and Compute n+nn+nnn
- Golang Program to Read a Number (n) and Compute (n+nn+nnn)
- C program to compute geometric progression
- C program to compute linear regression
- C++ Program to Compute Combinations using Factorials
- C++ Program to Compute DFT Coefficients Directly
- C Program to Compute Quotient and Remainder?
- Log functions in Python Program
- C++ Program to Compute Determinant of a Matrix
- C program to compute the polynomial regression algorithm
- Prime Factorization using Sieve O(log n) for multiple queries in C++

Advertisements