Contents
Introduction to Problem Solving
⯈Algorithm and its Characteristics
⯈Elementary Problems
- Addition of two numbers
- Calculate area and circumference of circle
- Calculate area of triangle
- Calculate simple interest
- Calculate slope and distance between two points
- Convert length in feet to inches
- Weighted score in exam
- Convert temperature in degree Celsius to Fahrenheit
- Swap two numbers
- Swap two numbers without using extra variable
Basics of C Programming
- Overview of C Programming Language
- Getting started with C Programming Language
- Keywords
- Identifiers
- Constants
- Operators
- Expression Evaluation
- Mathematical expression to C equivalent expressions
- Datatypes
- Variables
- Integer representation in C
- Character representation in C
- Type conversion in C
- sizeof operator
- Comments
- Mathematical Functions
- input output statements
- width specifiers in C
- structure of a C program
- header files
- Compilation process of a C program
- Types of initializations.
⯈Basic C Programs
- C program to add two numbers
- C program to find area and circumference of circle
- C program to swap two numbers
- C program to swap two numbers without using extra variable
- C program to swap two numbers using bitwise XOR
- C program to convert temperature in Celsius to Fahrenheit
- C program to calculate gross salary of an employee
- C program to count number of digits in a positive integer
- C program to count number of digits in binary representation
- C program to count number of digits in base ‘K’
- C program to convert kilometer to meter, feet, inches and centimeters
- C program to find first and last digit of a number
- C program to find minimum number of currency denominations
- C program to convert cartesian coordinates to polar coordinates
- C program to find distance between two places on earth in nautical miles
- C program to find slope and distance between two points
- C program to add 1 to the number using ‘+’ operator
- C program to find maximum of two numbers using ternary operator
- C program to find maximum of three numbers using ternary operator
- C program to find kth bit of a number
- C program to find last four bits of a byte
- C Program to reset right most set bit of a number
C program to count number of digits in a positive integer
The number of digits in a positive integer can be calculated using log10 function. (log to base 10)
Ex: N = 123, The number of digits in 123 is 3.
We know that,
log10(1) = 0
log10(10) = 1
log10(100) = 2
log10(1000) = 3
log10(10000) = 4
log10(100000) = 5
We can see that the number of digits from 0 to 9 is 1 and log10(1) and log10(9) lies between [0 , 1)
Similarly the number of digits from 100 to 999 is 3, and log10(100) and log10(999) lies between [2,3)
If we take the floor value of log10(N) and add 1 to it, we get the number of digits in the given positive number.
Reading the input,
scanf("%d",&n);
Calculating the number of digits,
d = floor(log10(N)) + 1
Program
#include<stdio.h>
#include<math.h
int main()
{
int n;
int d;
printf("Enter n:");
scanf("%d",&n);
d = floor(log10(n)) + 1;
printf("Number of digits in %d = %d\n",n,d);
return 0;
}
Dry Run
Let N = 123,
d = floor(log10(123)) + 1
= floor(2.08)+1
= 2 + 1
d = 3