CONVERSION DECIMAL TO BINARY, DECIMAL TO OCTAL, DECIMAL TO HEXADECIMAL AND VICE VERSA




245. WAP to convert decimal number to binary number.
CLS
INPUT "ENTER DECIMAL NUMBER"; D
WHILE D < > 0
R = D MOD 2
S$ = STR$(R) + S$
D = D \ 2
WEND
PRINT "BINARY EQUIVALENT VALUE="; S$
END

USING SUB PROCEDURE
DECLARE SUB CONV(D)
CLS
INPUT "ENTER DECIMAL NUMBER"; D
CALL CONV(D)
END

SUB CONV(D)
WHILE D  < > 0
R = D MOD 2
S$ = STR$(R) + S$
D = D \ 2
WEND
PRINT "BINARY EQUIVALENT VALUE="; S$
END

USING FUNCTION PROCEDURE
DECLARE SUB CONV$(D)
CLS
INPUT "ENTER DECIMAL NUMBER"; D
PRINT "BINARY EQUIVALENT VALUE=";  CONV$(D)
END

SUB CONV$(D)
WHILE D  < > 0
R = D MOD 2
S$ = STR$(R) + S$
D = D \ 2
WEND
CONV$ = S$
END FUNCTION


246. WAP to convert binary number to decimal number.

CLS
INPUT "ENTER BINARY NUMBER"; N$
FOR I = LEN(N$) TO 1 STEP -1
B$ = MID$(N$, I, 1)
S = S + VAL(B$) * 2 ^ P
P = P + 1
NEXT I
PRINT "DECIMAL EQUIVALENT VALUE="; S
END
USING SUB PROCEDURE
DECLARE SUB CONV(N)
CLS
INPUT "ENTER BINARY NUMBER"; N
CALL CONV(N)
END
SUB CONV(N)
P = 0: S = 0
WHILE N <> 0
R = N MOD 10
S = S + R * 2 ^ P
P = P + 1
N = N \ 10
WEND
PRINT "DECIMAL EQUIVALENT VALUE="; S
END SUB

USING FUNCTION PROCEDURE
DECLARE FUNCTION CONV(N)
CLS
INPUT "ENTER BINARY NUMBER"; N
PRINT "DECIMAL EQUIVALENT VALUE="; CONV(N)
END
FUNCTION CONV(N)
P = 0: S = 0
WHILE N <> 0
R = N MOD 10
S = S + R * 2 ^ P
P = P + 1
N = N \ 10
WEND
CONV = S
END FUNCTION

247. WAP to convert decimal number to octal number.

CLS
INPUT "ENTER DECIMAL NUMBER"; D
WHILE D  < > 0
R = D MOD 8
S$ = STR$(R) + S$
D = D \ 8
WEND
PRINT "OCTAL EQUIVALENT VALUE="; S$
END

USING SUB PROCEDURE
DECLARE SUB CONV(D)
CLS
INPUT "ENTER DECIMAL NUMBER"; D
CALL CONV(D)
END

SUB CONV(D)
WHILE D  < > 0
R = D MOD 8
S$ = STR$(R) + S$
D = D \ 8
WEND
PRINT "OCTAL EQUIVALENT VALUE="; S$
END

USING FUNCTION PROCEDURE
DECLARE SUB CONV$(D)
CLS
INPUT "ENTER DECIMAL NUMBER"; D
PRINT "OCTAL EQUIVALENT VALUE=";  CONV$(D)
END

SUB CONV$(D)
WHILE D  < > 0
R = D MOD 8
S$ = STR$(R) + S$
D = D \ 8
WEND
CONV$ = S$
END FUNCTION

248. WAP to convert octal number to decimal number.
CLS
INPUT "ENTER OCTAL NUMBER"; N$
FOR I = LEN(N$) TO 1 STEP -1
B$ = MID$(N$, I, 1)
S = S + VAL(B$) * 8 ^ P
P = P + 1
NEXT I
PRINT "DECIMAL EQUIVALENT VALUE="; S
END
OR

USING SUB PROCEDURE
DECLARE SUB CONV(N)
CLS
INPUT "ENTER OCTAL NUMBER"; N
CALL CONV(N)
END
SUB CONV(N)
P = 0: S = 0
WHILE N <> 0
R = N MOD 10
S = S + R * 8 ^ P
P = P + 1
N = N \ 10
WEND
PRINT "DECIMAL EQUIVALENT VALUE="; S
END SUB

USING FUNCTION PROCEDURE
DECLARE FUNCTION CONV(N)
CLS
INPUT "ENTER OCTAL NUMBER"; N
PRINT "DECIMAL EQUIVALENT VALUE="; CONV(N)
END
FUNCTION CONV(N)
P = 0: S = 0
WHILE N <> 0
R = N MOD 10
S = S + R * 8 ^ P
P = P + 1
N = N \ 10
WEND
CONV = S
END FUNCTION


249. WAP to convert decimal number to hexadecimal number.

CLS
INPUT "ENTER DECIMAL NUMBER"; D
WHILE D <> 0
R = D MOD 16
IF R < 10 THEN
S$ = STR$(R) + S$
ELSE
S$ = CHR$(R + 55) + S$
END IF
D = D \ 16
WEND
PRINT "HEXADECIMAL EQUIVALENT VALUE="; S$
END
USING SUB PROCEDURE
DECLARE SUB CONV(D)
CLS
INPUT "ENTER DECIMAL NUMBER"; D
CALL CONV (D)
END
SUB CONV(D)
WHILE D <> 0
R = D MOD 16
IF R = 10 THEN
S$ = "A"
ELSEIF R = 11 THEN
S$ = "B"
ELSEIF R = 12 THEN
S$ = "C"
ELSEIF R = 13 THEN
S$ = "D"
ELSEIF R = 14 THEN
S$ = "E"
ELSEIF R = 15 THEN
S$ = "F"
ELSE
S$ = STR$(R) + S$
END IF
D = D \ 16
WEND
PRINT "HEXADECIMAL EQUIVALENT VALUE="; S$
END SUB

USING FUNCTION PROCEDURE
DECLARE FUNCTION CONV$ (D)
CLS
INPUT "ENTER DECIMAL NUMBER"; D
PRINT "HEXADECIMAL EQUIVALENT VALUE=";  CONV$ (D)
END
FUNCTION CONV$(D)
WHILE D < > 0
R = D MOD 16
IF R = 10 THEN
S$ = "A"
ELSEIF R = 11 THEN
S$ = "B"
ELSEIF R = 12 THEN
S$ = "C"
ELSEIF R = 13 THEN
S$ = "D"
ELSEIF R = 14 THEN
S$ = "E"
ELSEIF R = 15 THEN
S$ = "F"
ELSE
S$ = STR$(R) + S$
END IF
D = D \ 16
WEND
CONV$ = S$
END FUNCTION


250. WAP to convert hexadecimal number to decimal number.

CLS
INPUT "ENTER HEXADECIMAL NUMBER"; N$
FOR I = LEN(N$) TO 1 STEP -1
B$ = MID$(N$, I, 1)
IF B$ = "A" THEN B$ = "10"
IF B$ = "B" THEN B$ = "11"
IF B$ = "C" THEN B$ = "12"
IF B$ = "D" THEN B$ = "13"
IF B$ = "E" THEN B$ = "14"
IF B$ = "F" THEN B$ = "15"

S = S + VAL(B$) * 16 ^ P

P = P + 1
NEXT I
PRINT "DECIMAL EQUIVALENT VALUE="; S
END

USING SUB PROCEDURE
DECLARE SUB CONV(N$)
CLS
INPUT "ENTER HEXADECIMAL NUMBER"; N$
CALL CONV(N$)
END
SUB CONV(N$)
FOR I = LEN(N$) TO 1 STEP -1
B$ = MID$(N$, I, 1)
IF B$ = "A" THEN B$ = "10"
IF B$ = "B" THEN B$ = "11"
IF B$ = "C" THEN B$ = "12"
IF B$ = "D" THEN B$ = "13"
IF B$ = "E" THEN B$ = "14"
IF B$ = "F" THEN B$ = "15"
S = S + VAL(B$) * 16 ^ P
P = P + 1
NEXT I
PRINT "DECIMAL EQUIVALENT VALUE="; S
END SUB

USING FUNCTION PROCEDURE
DECLARE FUNCTION CONV(N$)
CLS
INPUT "ENTER HEXADECIMAL NUMBER"; N$
PRINT "DECIMAL EQUIVALENT VALUE="; CONV(N$)
END
FUNCTION CONV(N$)
FOR I = LEN(N$) TO 1 STEP -1
B$ = MID$(N$, I, 1)
IF B$ = "A" THEN B$ = "10"
IF B$ = "B" THEN B$ = "11"
IF B$ = "C" THEN B$ = "12"
IF B$ = "D" THEN B$ = "13"
IF B$ = "E" THEN B$ = "14"
IF B$ = "F" THEN B$ = "15"
S = S + VAL(B$) * 16 ^ P
P = P + 1
NEXT I
CONV = S
END FUNCTION

Comments

Unknown said…
I SEARCHED ALOT...BUT UR PROGRAM WAS HELPFUL.
THANKS ..
Unknown said…
how to convert binary to octal?
Jitendra limbu said…
Rem to convert binary to octal
cls
input "enter binary no"; b$
for i = len(b$) to 1 step 1
d$= mid$(b$,i,1)
d=val(d$)*2^a
dec=dec+d
a=a+1
next i
while dec<>0
p=dec mod 8
dec= fix(dec/8)
a$=str$(p)+a$
wend
print a$; "=" b$ ;
end
Unknown said…
sir you missed minus in STEP -1
Safal Khatiwada said…
How to convert binary number to hexadecimal number?
Puspa Wosti said…
how to convert binary to octal number using sub procedure
Niraj Regmi said…
This comment has been removed by the author.
Niraj Regmi said…
I have developed a QBasic program to convert Octal to Binary.

Here it is
CLS
INPUT "Enter octal number"; N$
FOR I = 1 TO LEN(N$)
A$ = MID$(N$, I, 1)
IF A$ = "0" OR A$ = "1" OR A$ = "2" OR A$ = "3" OR A$ = "4" OR A$ = "5" OR A$ = "6" OR A$ = "7" THEN
GOTO Start
ELSE
GOTO Terminate
END IF
NEXT I
Start:
PRINT "THE OCTAL EQUIVALENT NUMBER = ";
FOR J = 1 TO LEN(N$)
A$ = MID$(N$, J, 1)
IF A$ = "0" THEN
PRINT "000";
ELSEIF A$ = "1" THEN
PRINT "001";
ELSEIF A$ = "2" THEN
PRINT "010";
ELSEIF A$ = "3" THEN
PRINT "011";
ELSEIF A$ = "4" THEN
PRINT "100";
ELSEIF A$ = "5" THEN
PRINT "101";
ELSEIF A$ = "6" THEN
PRINT "110";
ELSEIF A$ = "7" THEN
PRINT "111";
END IF
NEXT J
END
Terminate:
PRINT "Invalid Input"
END
Komal Adhikari said…
the output of the "DECIMAL TO BINARY" using function procedure is not coming.
What is the mistake?
Unknown said…
How to convert decimal number into octal and hexadecimal in one program.

Popular posts from this blog

PROGRAM TO FIND SQUARE, CUBE, SQUARE ROOT AND CUBE ROOT OF GIVEN NUMBER

PROGRAM TO DISPLAY SUM, PRODUCT, DIFFERENCE AND PRODUCT OF TWO / THREE NUMBERS

PALINDROME AND ARMSTRONG PROGRAMS