This is a dynamic program for the implementation of the Restoring division Algorithm in the C. Restoring Division uses Left Shift Operations, Twos’ Compliment and Binary Addition.

#include<stdio.h> #include<malloc.h> int * a, * q, * m, * mc, * c, n, d; int powr(int x, int y) { int s = 1, i; for (i = 0; i < y; i++) s = s * x; return s; } void print(int arr[], int n) { int i; for (i = 0; i < n; i++) printf("%d ", arr[i]); } void bin(int n, int arr[]) { int r, i = 0; do { r = n % 2; n /= 2; arr[i] = r; i++; } while (n > 0); } void set(int array[], int x) { int i, tmp[20] = { 0 }; for (i = x - 1; i >= 0; i--) tmp[x - 1 - i] = array[i]; for (i = 0; i < x; i++) array[i] = tmp[i]; } int len(int x) { int i = 0; while (powr(2, i) <= x) i++; return ++i; } void addBinary(int a1[], int a2[]) { int bi[2] = { 0 }, ca[20] = { 0 }; int t = len(n), tmp = 0; int * su = (int * ) malloc(sizeof(int) * len(n)); while (t-- > 0) { tmp = a1[t] + a2[t] + ca[t]; bin(tmp, bi); su[t] = bi[0]; ca[t - 1] = bi[1]; bi[0] = 0; bi[1] = 0; } for (t = 0; t < len(n); t++) a1[t] = su[t]; free(su); } void twoCom(int arr[]) { int i; int * one = (int * ) malloc(sizeof(int) * len(n)); for (i = 0; i < len(n) - 1; i++) one[i] = 0; one[i] = 1; for (i = 0; i < len(n); i++) { arr[i] = 1 - arr[i]; } addBinary(arr, one); free(one); } void ls(int alen, int blen) { int i = 0; for (i = 0; i < alen - 1; i++) a[i] = a[i + 1]; a[i] = q[0]; for (i = 0; i < blen - 1; i++) q[i] = q[i + 1]; q[i] = -1; } void printaq() { print(a, len(n)); printf("t"); print(q, len(n) - 1); printf("t"); printf("n"); } int main() { int i, cnt = 0; printf("Enter The Numerator/Denominator: "); scanf("%d/%d", & n, & d); q = (int * ) malloc(sizeof(int) * len(n) - 1); bin(n, q); m = (int * ) malloc(sizeof(int) * (len(n))); bin(d, m); a = (int * ) malloc(sizeof(int) * (len(n))); for (i = 0; i < len(n); i++) a[i] = 0; mc = (int * ) malloc(sizeof(int) * (len(n))); bin(d, mc); set(q, len(n) - 1); set(m, len(n)); set(mc, len(n)); twoCom(mc); cnt = len(n) - 1; printf("t Att Qtt Mt Countn"); printf("t---------t-------tt--------- ---------n"); while (cnt > 0) { printf("t"); print(a, len(n)); printf("t"); print(q, len(n) - 1); printf("t"); print(m, len(n)); printf("t%dn", cnt); ls(len(n), len(n) - 1); printf("LSHIFTt"); printaq(); addBinary(a, mc); printf("A=A-Mt"); printaq(); if (a[0] == 1) { q[len(n) - 2] = 0; addBinary(a, m); } else q[len(n) - 2] = 1; printf("A=A+Mt"); printaq(); cnt -= 1; } return 0; }

Found Bugs,

Feel free to report them !

- Restoring Division Algorithm Implementation In C - February 26, 2020
- NATASHA: AI assistant using Raspberry Pi - October 26, 2017
- Hackerrank Modified Kaprekar Numbers Solution - October 31, 2015