Saturday, October 22, 2016

Convert Binary to Decimal [Qbasic Program]



183. 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

184. 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
185. 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
186. 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


187. 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
188. 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

4 comments:

  1. very long and very hard to understand

    ReplyDelete
  2. Thanks for your hard work and solving difficulty questions

    ReplyDelete