How Does 10 digits/s counter Work?
Counters in Digital Logic
A Counter is a device which stores (and sometimes displays) the number of times a particular event or process has occurred, often in relationship to a clock signal. Counters are used in digital electronics for counting purpose, they can count specific event happening in the circuit. For example, in UP counter a counter increases count for every rising edge of clock. Not only counting, a counter can follow the certain sequence based on our design like any random sequence 0,1,3,2 .They can also be designed with the help of flip flops. They are used as frequency dividers where the frequency of given pulse waveform is divided. Counters are sequential circuit that count the number of pulses can be either in binary code or BCD form. The main properties of a counter are timing , sequencing , and counting. Counter works in two modes
For more information, please visit 10 digits/s counter.
Up counter
Down counter
Counter Classification
Counters are broadly divided into two categories
- Asynchronous counter
- Synchronous counter
1. Asynchronous Counter
In asynchronous counter we dont use universal clock, only first flip flop is driven by main clock and the clock input of rest of the following flip flop is driven by output of previous flip flops. We can understand it by following diagram-
It is evident from timing diagram that Q0 is changing as soon as the rising edge of clock pulse is encountered, Q1 is changing when rising edge of Q0 is encountered(because Q0 is like clock pulse for second flip flop) and so on. In this way ripples are generated through Q0,Q1,Q2,Q3 hence it is also called RIPPLE counter and serial counter. A ripple counter is a cascaded arrangement of flip flops where the output of one flip flop drives the clock input of the following flip flop
2. Synchronous Counter
Unlike the asynchronous counter, synchronous counter has one global clock which drives each flip flop so output changes in parallel. The one advantage of synchronous counter over asynchronous counter is, it can operate on higher frequency than asynchronous counter as it does not have cumulative delay because of same clock is given to each flip flop. It is also called as parallel counter.
Synchronous counter circuit
Timing diagram synchronous counter
From circuit diagram we see that Q0 bit gives response to each falling edge of clock while Q1 is dependent on Q0, Q2 is dependent on Q1 and Q0 , Q3 is dependent on Q2,Q1 and Q0.
Decade Counter
A decade counter counts ten different states and then reset to its initial states. A simple decade counter will count from 0 to 9 but we can also make the decade counters which can go through any ten states between 0 to 15(for 4 bit counter).
Truth table for simple decade counter
Decade counter circuit diagram
We see from circuit diagram that we have used nand gate for Q3 and Q1 and feeding this to clear input line because binary representation of 10 is
And we see Q3 and Q1 are 1 here, if we give NAND of these two bits to clear input then counter will be clear at 10 and again start from beginning.
Important point: Number of flip flops used in counter are always greater than equal to (log2 n) where n=number of states in counter.
Some previous years gate questions on Counters
Q1. Consider the partial implementation of a 2-bit counter using T flip-flops following the sequence 0-2-3-1-0, as shown below
To complete the circuit, the input X should be
(A) Q2?
(B) Q2 + Q1
(C) (Q1 ? Q2)
(D) Q1 ? Q2 (GATE-CS-)
Solution:
From circuit we see
T1=XQ1+XQ1-(1)
AND
T2=(Q2 ? Q1)-(2)
AND DESIRED OUTPUT IS 00->10->11->01->00
SO X SHOULD BE Q1Q2+Q1Q2 SATISFYING 1 AND 2.
SO ANS IS (D) PART.
Q2. The control signal functions of a 4-bit binary counter are given below (where X is dont care)
The counter is connected as follows:
Assume that the counter and gate delays are negligible. If the counter starts at 0, then it cycles through the following sequence:
(A) 0,3,4
(B) 0,3,4,5
(C) 0,1,2,3,4
(D) 0,1,2,3,4,5 (GATE-CS-)
Solution:
Initially A1 A2 A3 A4 =
Clr=A1 and A3
So when A1 and A3 both are 1 it again goes to
Hence (init.) -> (A1 and A3=0)-> (A1 and A3=0) -> (A1 and A3=0) -> (A1 and A3=1)[ clear condition satisfied] ->(init.) so it goes through 0->1->2->3->4
Ans is (C) part.
Quiz on Digital Logic
Article contributed by Anuj Batham,
GeeksforGeeks
Improve
Please
Login
to comment...
Program to count digits in an integer (4 Different Methods)
Last Updated : 08 May,
Program to count digits in an integer (4 Different Methods)
Given a number N, the task is to return the count of digits in this number.
Example:
Simple Iterative Solution to count digits in an integer
The integer entered by the user is stored in the variable n. Then the while loop is iterated until the test expression n != 0 is evaluated to 0 (false). We will consider as the input integer.
After the first iteration, the value of n will be updated to 345 and the count is incremented to 1.
After the second iteration, the value of n will be updated to 34 and the count is incremented to 2.
After the third iteration, the value of n will be updated to 3 and the count is incremented to 3.
In the fourth iteration, the value of n will be updated to zero and the count will be incremented to 4.
Then the test expression is evaluated ( n!=0 ) as false and the loop terminates with final count as 4.
Below is the implementation of the above approach:
C++// Iterative C++ program to count
// number of digits in a number
#include
<bits/stdc++.h>
using
namespace
std
;
int
countDigit
(
long
long
n
)
{
if
(
n
==
0
)
return
1
;
int
count
=
0
;
while
(
n
!=
0
)
{
n
=
n
/
10
;
++
count
;
}
return
count
;
}
// Driver code
int
main
(
void
)
{
long
long
n
=
;
cout
<<
"Number of digits : "
<<
countDigit
(
n
);
return
0
;
}
// This code is contributed
// by Akanksha Rai
C
// Iterative C program to count number of
// digits in a number
#include
<stdio.h>
int
countDigit
(
long
long
n
)
{
if
(
n
==
0
)
return
1
;
int
count
=
0
;
while
(
n
!=
0
)
{
n
=
n
/
10
;
++
count
;
}
return
count
;
}
// Driver code
int
main
(
void
)
{
long
long
n
=
;
printf
(
"Number of digits : %d"
,
countDigit
(
n
));
return
0
;
}
Java
// JAVA Code to count number of
// digits in an integer
import
java.io.*
;
public
class
GFG
{
static
int
countDigit
(
long
n
)
{
int
count
=
0
;
while
(
n
!=
0
)
{
n
=
n
/
10
;
++
count
;
}
return
count
;
}
/* Driver code */
public
static
void
main
(
String
[]
args
)
{
long
n
=
;
System
.
out
.
print
(
"Number of digits : "
+
countDigit
(
n
));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python
# Iterative Python program to count
# number of digits in a number
def
countDigit
(
n
):
count
=
0
while
n
!=
0
:
n
//=
10
count
+=
1
return
count
# Driver Code
n
=
print
(
"Number of digits :
% d
"
%
(
countDigit
(
n
)))
# This code is contributed by Shreyanshi Arun
C#
// C# Code to count number of
// digits in an integer
using
System
;
class
GFG
{
static
int
countDigit
(
long
n
)
{
int
count
=
0
;
while
(
n
!=
0
)
{
n
=
n
/
10
;
++
count
;
}
return
count
;
}
/* Driver code */
public
static
void
Main
()
{
long
n
=
;
Console
.
WriteLine
(
"Number of"
+
" digits : "
+
countDigit
(
n
));
}
}
// This code is contributed by anuj_67.
Javascript
<
script
>
// Iterative Javascript program to count
// number of digits in a number
function
countDigit
(
n
)
{
let
count
=
0
;
while
(
n
!=
0
)
{
n
=
Math
.
floor
(
n
/
10
);
++
count
;
}
return
count
;
}
// Driver code
n
=
;
document
.
write
(
"Number of digits : "
+
countDigit
(
n
));
// This code is contributed by Mayank Tyagi
<
/script>
PHP
<?php
// Iterative PHP program to count
// number of digits in a number
function
countDigit
(
$n
)
{
$count
=
0
;
while
(
$n
!=
0
)
{
$n
=
round
(
$n
/
10
);
++
$count
;
}
return
$count
;
}
// Driver code
$n
=
;
echo
"Number of digits : "
.
countDigit
(
$n
);
//This code is contributed by mits
?>
Output
Number of digits : 9
Time Complexity : O(log10(n)) or O(num digits)
Auxiliary Space: O(1) or constant
Recursive Solution to count digits in an integer
Keep dividing the number by 10 this reduces the input number size by 1 and keeps track of the number of sizes reduced.
Algorithm:
The base condition of this recursive approach is when we divide the number by 10 and the number gets reduced to 0, so return 1 for this operation.
Make a function call by dividing the number by 10, reducing the input size of the given number by 1, and adding 1 for this operation.
Below is the implementation of the above approach:
C++// Recursive C++ program to count number of
// digits in a number
#include
<bits/stdc++.h>
using
namespace
std
;
int
countDigit
(
long
long
n
)
{
if
(
n
/
10
==
0
)
return
1
;
return
1
+
countDigit
(
n
/
10
);
}
// Driver code
int
main
(
void
)
{
long
long
n
=
;
cout
<<
"Number of digits :"
<<
countDigit
(
n
);
return
0
;
}
// This code is contributed by Mukul Singh.
C
// Recursive C program to count number of
// digits in a number
#include
<stdio.h>
int
countDigit
(
long
long
n
)
{
if
(
n
/
10
==
0
)
return
1
;
return
1
+
countDigit
(
n
/
10
);
}
// Driver code
int
main
(
void
)
{
long
long
n
=
;
printf
(
"Number of digits : %d"
,
countDigit
(
n
));
return
0
;
}
Java
// JAVA Code to count number of
// digits in an integer
import
java.util.*
;
class
GFG
{
static
int
countDigit
(
long
n
)
{
if
(
n
/
10
==
0
)
return
1
;
return
1
+
countDigit
(
n
/
10
);
}
/* Driver code */
public
static
void
main
(
String
[]
args
)
{
long
n
=
;
System
.
out
.
print
(
"Number of digits : "
+
countDigit
(
n
));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python
# Recursive Python program to count
# number of digits in a number
def
countDigit
(
n
):
if
n
//
10
==
0
:
return
1
return
1
+
countDigit
(
n
//
10
)
# Driver Code
n
=
print
(
"Number of digits :
% d
"
%
(
countDigit
(
n
)))
# This code is contributed by Shreyanshi Arun
C#
// C# Code to count number of
// digits in an integer
using
System
;
class
GFG
Additional reading:
What Are the Advantages of Ndt Products Suppliers?
If you want to learn more, please visit our website china arbitrary waveform generators.
{
static
int
countDigit
(
long
n
)
{
if
(
n
/
10
==
0
)
return
1
;
return
1
+
countDigit
(
n
/
10
);
}
/* Driver Code */
public
static
void
Main
()
{
long
n
=
;
Console
.
WriteLine
(
"Number of "
+
"digits : "
+
countDigit
(
n
));
}
}
// This code is contributed by anuj_67.
Javascript
function
countDigit
(
n
)
{
if
(
parseInt
(
n
/
10
)
===
0
)
return
1
;
return
1
+
countDigit
(
parseInt
(
n
/
10
));
}
// Driver code
var
n
=
;
console
.
log
(
"Number of digits: "
+
countDigit
(
n
));
PHP
<?php
// Recursive PHP program to count
// number of digits in a number
function
countDigit
(
$n
)
{
if
(
$n
/
10
==
0
)
return
1
;
return
1
+
countDigit
((
int
)(
$n
/
10
));
}
// Driver Code
$n
=
;
print
(
"Number of digits : "
.
(
countDigit
(
$n
)));
// This code is contributed by mits
?>
Output
Number of digits :9
Time Complexity : O(log(n))
Auxiliary Space : O(log(n))
Log-based Solution to count digits in an integer
We can use log10(logarithm of base 10) to count the number of digits of positive numbers (logarithm is not defined for negative numbers).
Digit count of N = upper bound of log10(N).
Below is the implementation of the above idea:
C++// Log based C++ program to count number of
// digits in a number
#include
<bits/stdc++.h>
using
namespace
std
;
int
countDigit
(
long
long
n
)
{
return
floor
(
log10
(
n
)
+
1
);
}
// Driver code
int
main
(
void
)
{
long
long
n
=
;
cout
<<
"Number of digits : "
<<
countDigit
(
n
);
return
0
;
}
// This code is contributed by shivanisinghss
C
// Log based C program to count number of
// digits in a number
#include
<math.h>
#include
<stdio.h>
int
countDigit
(
long
long
n
)
{
return
floor
(
log10
(
n
)
+
1
);
}
// Driver code
int
main
(
void
)
{
long
long
n
=
;
printf
(
"Number of digits : %d"
,
countDigit
(
n
));
return
0
;
}
Java
// JAVA Code to count number of
// digits in an integer
import
java.util.*
;
class
GFG
{
static
int
countDigit
(
long
n
)
{
return
(
int
)
Math
.
floor
(
Math
.
log10
(
n
)
+
1
);
}
/* Driver code */
public
static
void
main
(
String
[]
args
)
{
long
n
=
;
System
.
out
.
print
(
"Number of digits : "
+
countDigit
(
n
));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python
# Log based Python program to count number of
# digits in a number
# function to import ceil and log
import
math
def
countDigit
(
n
):
return
math
.
floor
(
math
.
log10
(
n
)
+
1
)
# Driver Code
n
=
print
(
"Number of digits :
% d
"
%
(
countDigit
(
n
)))
# This code is contributed by Shreyanshi Arun
C#
// C# Code to count number of
// digits in an integer
using
System
;
class
GFG
{
static
int
countDigit
(
long
n
)
{
return
(
int
)
Math
.
Floor
(
Math
.
Log10
(
n
)
+
1
);
}
/* Driver code */
public
static
void
Main
()
{
long
n
=
;
Console
.
WriteLine
(
"Number of digits : "
+
countDigit
(
n
));
}
}
// This code is contributed by anuj_67..
Javascript
<
script
>
// Log based Javascript program to count number of
// digits in a number
function
countDigit
(
n
)
{
return
Math
.
floor
(
Math
.
log10
(
n
)
+
1
);
}
// Driver code
var
n
=
;
document
.
write
(
"Number of digits : "
+
countDigit
(
n
));
// This code is contributed by noob
<
/script>
PHP
<?php
// Log based php program to
// count number of digits
// in a number
function
countDigit
(
$n
)
{
return
floor
(
log10
(
$n
)
+
1
);
}
// Driver code
$n
=
;
echo
"Number of digits : "
,
countDigit
(
$n
);
// This code is contributed by ajit
?>
Output
Number of digits : 9
Time Complexity: O(1) or constant
Auxiliary Space: O(1) or constant
Converting given number to string solution
to count digits in an integerWe can convert the number into a string and then find the length of the string to get the number of digits in the original number.
Note: It gives TLE for numbers other than range of int,example long and long long .
C++#include
<bits/stdc++.h>
using
namespace
std
;
// To count the no. of digits in a number
void
count_digits
(
int
n
)
{
// converting number to string using
// to_string in C++
string
num
=
to_string
(
n
);
// calculate the size of string
cout
<<
num
.
size
()
<<
endl
;
}
//Driver Code
int
main
()
{
// number
int
n
=
345
;
count_digits
(
n
);
return
0
;
}
// This code is contributed by Shashank Pathak
Java
import
java.util.*
;
public
class
GFG
{
// To count the no. of digits in a number
static
void
count_digits
(
int
n
)
{
// converting number to string using
// to_string in C++
String
num
=
Integer
.
toString
(
n
);
// calculate the size of string
System
.
out
.
println
(
num
.
length
());
}
// Driver code
public
static
void
main
(
String
args
[]
)
{
// number
int
n
=
345
;
count_digits
(
n
);
}
}
// Code is contributed by shivanisinghss
Python
# Python3 implementation of the approach
def
count_digits
(
n
):
n
=
str
(
n
)
return
len
(
n
)
# Driver code
n
=
print
(
count_digits
(
n
))
C#
// C# implementation of the above approach
using
System
;
using
System.Collections.Generic
;
class
GFG
{
// To count the no. of digits in a number
static
void
count_digits
(
int
n
)
{
// converting number to string using
// to_string in C#
string
num
=
Convert
.
ToString
(
n
);
// calculate the size of string
Console
.
WriteLine
(
+
num
.
Length
);
}
// Driver Code
public
static
void
Main
(
string
[]
args
)
{
// number
int
n
=
345
;
count_digits
(
n
);
}
}
// This code is contributed by shivanisinghss
Javascript
<
script
>
// Javascript implementation of the above approach
// To count the no. of digits in a number
function
count_digits
(
n
)
{
// converting number to string using
// to_string in javascript
let
num
=
n
.
toString
();
// calculate the size of string
document
.
write
(
num
.
length
);
}
// number
let
n
=
345
;
count_digits
(
n
);
<
/script>
Output
3
Time Complexity: O(1) or constant
Auxiliary Space: O(Number of digits in an integer)
This article is contributed by Suruchi Kumari .
GeeksforGeeks
Improve
Please
Login
to comment...
Contact us to discuss your requirements of power quality class. Our experienced sales team can help you identify the options that best suit your needs.