Decimal to binary

Task: Create an algorithm which accepts a decimal integer and outputs it’s binary equivalent.

This algorithm was designed and implemented in Flowgorithm:

See the video below for an explanation of how the algorithm works:

Decimal to binary algorithm explained

The code in C is shown below:



Copy and paste from here:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>//library needed for string operations

int main()
{
    //declare and identify integers
    int number, remainder;

    //declare, identify and initialize all strings
    char  result[20]="";
    char  remainderString[20]="";


    //initialize all other variables
    number=-1;
    remainder=-1;

    //get input
    printf("Enter a number in decimal\n");
    scanf("%d", &number);
    printf("\n");

    while (number!=0)
    {
        remainder=number%2;

            /* #Implementation_Result

                Here we implement the following:

                    result=remainder + result
            */

            //first convert the remainder to a string,
            //and store the converted data in "remainderString"
            itoa(remainder,remainderString,10);

            //add "remainderString" to "result" in that order.
            //the result of the concatenation is stored in "remainderString"
            strcat(remainderString,result);

            //now copy our data so far back to "result"
            strcpy(result,remainderString);
            /*End #Implementation_Result*/


        number=number/2;

    }

    printf("The binary Result is %s \n",result);
    return 0;
}

© 2019  Vedesh Kungebeharry. All rights reserved. 

Implementing Incomplete functions – Programming practise (Part 3)

Download this file: ProgrammingPractise 3 – Commissions and Rewards.fprg

Be sure to read the comments at the start of each function to get an idea of what needs to be implemented. I’ll be happy to answer any questions that you have in class.

See this post if for some detail on how to use the above file: https://islandclass.wordpress.com/2019/05/16/implementing-incomplete-functions-programming-practise/

© 2019  Vedesh Kungebeharry. All rights reserved.