Prerequisite: Finite Automata Introduction, Designing Finite Automata

**Problem-1:**

Construct DFA, which accepts set of all strings over {0, 1} which interpreted as binary number is divisible by 2.

**Explanation:**

Consider the following inputs,

{0, 01, 10, 11, 100, 101, 110........}

The state transition diagram of the language will be like:

In this DFA there are two states q0 and q1 and the input is strings of {0, 1} which is interpreted as binary number.

The state q0 is final state and q1 is non-final state. State q0 will be representing all the numbers that are divisible by 2, that is {0, 10, 100, 110…..}.

State q1 will be representing all the numbers that are not divisible by 2, that is {01, 11, 101, ……}.

`def` `stateq0(n): ` ` ` `#if length found 0 ` ` ` `#print not accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"string accepted"` `) ` ` ` `else` `: ` ` ` ` ` `#if at index 0 ` ` ` `#'0' found call ` ` ` `#function stateq0 ` ` ` `if` `(n[` `0` `]` `=` `=` `'0'` `): ` ` ` `stateq0(n[` `1` `:]) ` ` ` ` ` `#else if '1' found ` ` ` `#call function q1. ` ` ` `elif` `(n[` `0` `]` `=` `=` `'1'` `): ` ` ` `stateq1(n[` `1` `:]) ` ` ` `def` `stateq1(n): ` ` ` `#if length found 0 ` ` ` `#print not accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"string not accepted"` `) ` ` ` `else` `: ` ` ` ` ` `#if at index 0 ` ` ` `#'0' found call ` ` ` `#function stateq0 ` ` ` `if` `(n[` `0` `]` `=` `=` `'0'` `): ` ` ` `stateq0(n[` `1` `:]) ` ` ` ` ` `#else if '1' found ` ` ` `#call function q1. ` ` ` `elif` `(n[` `0` `]` `=` `=` `'1'` `): ` ` ` `stateq1(n[` `1` `:]) ` ` ` ` ` `#take number from user ` `n` `=` `int` `(` `input` `()) ` `#converting number to binary ` `n ` `=` `bin` `(n).replace(` `"0b"` `, "") ` ` ` `#call stateA ` `#to check the input ` `stateq0(n) ` |

*chevron_right*

*filter_none*

INPUT: 5 OUTPUT: String Not Accepted

The above automata will accept set of all strings over {0, 1} which when interpreted as binary number is divisible by 2.

Whenever the number is not divisible by 2 then it will go from state q0 to q1. When the number is divisible by 2, then it will go from state q1 to q0 or if it was initially in q0 then it will accept it.

**Problem-2:**

Construct DFA, which accepts set of all strings over {0, 1} which interpreted as binary number is divisible by 3.

**Explanation:**

Refer for solution: Binary string multiple of 3 using DFA.

**Problem-3:**

Construct DFA, which accepts set of all strings over {0, 1} which interpreted as binary number is divisible by 4.

**Explanation:**

Consider the following inputs,

{0, 01, 10, 11, 100, 101, 110........}

The state transition diagram of the language will be like:

**Explanation:**

In this DFA there are three states q0, q1, q2, q3 and the input is strings of {0, 1} which is interpreted as binary number. The state q0 is final state and q1, q2, q3 are non-final state.

- State q0 will be representing all the numbers that are divisible by 4, that is {0, 100, 1000…..}.
- State q1 will be representing all the numbers that are not divisible by 4 and give remainder 1 when divided by 4, that is {01, 101,,……}.
- State q2 will be representing all the numbers that are not divisible by 4 and give remainder 2 when divided by 4, that is {10, 110, ……}.
- State q4 will be representing all the numbers that are not divisible by 4 and give remainder 3 when divided by 4, that is {11, 111, ……}.

The above automata will accept set of all strings over {0, 1} which when interpreted as binary number is divisible by 4.

- State Whenever the number is not divisible by 4 and gives remainder of 1 then it will go to state q1.
- State Whenever the number is not divisible by 4 and gives remainder of 2 then it will go to state q2.
- State Whenever the number is not divisible by 4 and gives remainder of 3 then it will go to state q3.
- State Whenever the number is divisible by 4, then it will go to state q0 or if it was initially in q0 then it will accept it.

`def` `stateq0(n): ` ` ` `#if length found 0 ` ` ` `#print not accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"string accepted"` `) ` ` ` `else` `: ` ` ` ` ` `#if at index 0 ` ` ` `#'0' found call ` ` ` `#function stateq0 ` ` ` `if` `(n[` `0` `]` `=` `=` `'0'` `): ` ` ` `stateq0(n[` `1` `:]) ` ` ` ` ` `#else if '1' found ` ` ` `#call function q1. ` ` ` `elif` `(n[` `0` `]` `=` `=` `'1'` `): ` ` ` `stateq1(n[` `1` `:]) ` ` ` `def` `stateq1(n): ` ` ` `#if length found 0 ` ` ` `#print not accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"string not accepted"` `) ` ` ` `else` `: ` ` ` ` ` `#if at index 0 ` ` ` `#'0' found call ` ` ` `#function stateq2 ` ` ` `if` `(n[` `0` `]` `=` `=` `'0'` `): ` ` ` `stateq2(n[` `1` `:]) ` ` ` ` ` `#else if '1' found ` ` ` `#call function q3. ` ` ` `elif` `(n[` `0` `]` `=` `=` `'1'` `): ` ` ` `stateq3(n[` `1` `:]) ` ` ` `def` `stateq2(n): ` ` ` `#if length found 0 ` ` ` `#print not accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"string not accepted"` `) ` ` ` `else` `: ` ` ` ` ` `#if at index 0 ` ` ` `#'0' found call ` ` ` `#function stateq0 ` ` ` `if` `(n[` `0` `]` `=` `=` `'0'` `): ` ` ` `stateq0(n[` `1` `:]) ` ` ` ` ` `#else if '1' found ` ` ` `#call function q1. ` ` ` `elif` `(n[` `0` `]` `=` `=` `'1'` `): ` ` ` `stateq1(n[` `1` `:]) ` ` ` `def` `stateq3(n): ` ` ` `#if length found 0 ` ` ` `#print not accepted ` ` ` `if` `(` `len` `(n)` `=` `=` `0` `): ` ` ` `print` `(` `"string not accepted"` `) ` ` ` `else` `: ` ` ` ` ` `#if at index 0 ` ` ` `#'0' found call ` ` ` `#function stateq2 ` ` ` `if` `(n[` `0` `]` `=` `=` `'0'` `): ` ` ` `stateq2(n[` `1` `:]) ` ` ` ` ` `#else if '1' found ` ` ` `#call function q3. ` ` ` `elif` `(n[` `0` `]` `=` `=` `'1'` `): ` ` ` `stateq3(n[` `1` `:]) ` ` ` ` ` ` ` `#take number from user ` `n` `=` `int` `(` `input` `()) ` `#converting number to binary ` `n ` `=` `bin` `(n).replace(` `"0b"` `, "") ` ` ` `#call stateA ` `#to check the input ` `stateq0(n) ` |

*chevron_right*

*filter_none*

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

## Recommended Posts:

- Program to construct a DFA which accept the language L = {a
^{n}b^{m}| n mod 2=0, m≥1} - Construct a DFA which accept the language L = {w | w ∈ {a,b}* and Na(w) mod 3 = Nb (w) mod 3}
- Construct a DFA which accept the language L = {a
^{n}b^{m}| n > =1, (m) mod 3 = 1} - DFA in LEX code which accepts even number of zeros and even number of ones
- DFA of a string in which 2nd symbol from RHS is 'a'
- DFA of a string in which 3rd symbol from RHS is ‘a’
- Build a DFA to accept Binary strings that starts or ends with "01"
- Build a DFA to accept a binary string containing "01" i times and "1" 2j times
- DFA machines accepting odd number of 0’s or/and even number of 1’s
- DFA that recognizes number of 0 is multiple of 3 on input {0,1}
- Construct Turing Machine for incrementing Binary Number by 1
- Conversion from NFA to DFA
- Minimization of DFA
- DFA for accepting the language L = { a
^{n}b^{m}| n+m=even } - Program to Implement NFA with epsilon move to DFA Conversion
- DFA for Strings not ending with "THE"
- DFA of a string with at least two 0’s and at least two 1’s
- Union process in DFA
- Concatenation process in DFA
- Complementation process in DFA

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.