C language algorithm