Commit 0b8bad50 by raliis

2ndarvu teisendaja 10ndsüsteemi programm

parent 6cc70089
Showing with 57 additions and 0 deletions
/**
* File: bintodec.c
* Author: Rainer Liis
* Created: 10.10.2017
* Last edit: 16.10.2017
*
* Description: Converting binary numbers to decimal.
*/
#include <stdio.h>
int i; // counter
int n; //length of number
int y = 0; //decimal number
int binary[20]; //array for binary digits
int main(void)
{
printf ("How long is the binary number you wish to convert?(max 20)");
scanf ("%d", &n); // length of binary number
for (i = 0; i < n; i++)
{
printf ("Enter digit nr %d of the binary number you wish to convert"
" to decimal: \n", i + 1);
scanf ("%d", &binary[i]); // writes digits into array
do // checks if digit is binary, if not enter new digit
{
if (binary[i] != 1 && binary[i] != 0)
{
printf ("You have entered a non-binary digit, re-enter a new "
"single digit nr %d\n", i);
scanf ("%d", &binary[i]);
}
}
while (binary[i] != 1 && binary[i] != 0); // while digit is not binary
}
printf(" \n");
for (i = 0; i < n; i++)
{
printf ("%d", binary[i]); // prints out array, showing the binary number
}
printf (" converted to decimal is: ");
for (i = 0; i < n; i++)
{
y = 2*y + binary[i]; // calculates the decimal value
}
printf ("%d", y);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment