
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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\n", log2n(num)); return 0; }
Output
4
- Related Questions & Answers
- 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++
- Python Program to Input a Number n and Compute n+nn+nnn
- Golang Program to Read a Number (n) and Compute (n+nn+nnn)
- Compute log-determinants for a stack of matrices in Python
- 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
- Log functions in C#
- log() function in C++
- Prime Factorization using Sieve O(log n) for multiple queries in C++
Advertisements