Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
raliis
/
IAX0583
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
0b8bad50
authored
Oct 16, 2017
by
raliis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
2ndarvu teisendaja 10ndsüsteemi programm
parent
6cc70089
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
0 deletions
bintodec.c
bintodec.c
0 → 100644
View file @
0b8bad50
/**
* 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
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment