Saturday, October 22, 2016

Qbasic Program: Sum of Digits



104. WAP to input number and find sum of digits.
CLS
INPUT "ENTER ANY NUMBER"; N
S = 0
WHILE N < > 0
R = N MOD 10
S = S + R
N = N \ 10
WEND
PRINT "SUM OF DIGITS"; S
END
USING SUB PROCEDURE
DECLARE SUB SUM (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL SUM (N)
END
SUB SUM (N)
S = 0
WHILE N < > 0
R = N MOD 10
S = S + R
N = N \ 10
WEND
PRINT "SUM OF DIGITS"; S
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION SUM (N)
CLS
INPUT "ENTER ANY NUMBER"; N
SU = SUM (N)
PRINT "SUM OF DIGITS"; SU
END
FUNCTION SUM (N)
S = 0
WHILE N < > 0
R = N MOD 10
S = S + R
N = N \ 10
WEND
SUM = S : END FUNCTION
105.   WAP to input number and find sum of odd digits.
CLS
INPUT "ENTER ANY NUMBER"; N
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN S = S + R
N = N \ 10
WEND
PRINT "SUM OF ODD DIGITS"; S
END
USING SUB PROCEDURE
DECLARE SUB SUMODD (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL SUMODD (N)
END
SUB SUMODD (N)
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN S = S + R
N = N \ 10
WEND
PRINT "SUM OF ODD DIGITS"; S
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION SUMODD (N)
CLS
INPUT "ENTER ANY NUMBER"; N
SU = SUMODD (N)
PRINT "SUM OF ODD DIGITS"; SU
END
FUNCTION SUMODD (N)
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN S = S + R
N = N \ 10
WEND
SUMODD = S : END FUNCTION
106.   WAP to input number and find sum of even digits.
CLS
INPUT "ENTER ANY NUMBER"; N
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN S = S + R
N = N \ 10
WEND
PRINT "SUM OF EVEN DIGITS"; S
END
USING SUB PROCEDURE
DECLARE SUB SUMEVEN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL SUMEVEN (N)
END
SUB SUMEVEN (N)
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN S = S + R
N = N \ 10
WEND
PRINT "SUM OF EVEN DIGITS"; S
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION SUMEVEN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
SU = SUMEVEN (N)
PRINT "SUM OF EVEN DIGITS"; SU
END
FUNCTION SUMEVEN (N)
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN S = S + R
N = N \ 10
WEND
SUMEVEN = S : END FUNCTION
107. WAP to input number and find sum square of digits.
CLS
INPUT "ENTER ANY NUMBER"; N
S = 0
WHILE N < > 0
R = N MOD 10
S = S + R ^ 2
N = N \ 10
WEND
PRINT "SUM OF SQUARE OF DIGITS"; S
END
USING SUB PROCEDURE
DECLARE SUB SUMSQ (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL SUMSQ (N)
END
SUB SUMSQ (N)
S = 0
WHILE N < > 0
R = N MOD 10
S = S + R ^ 2
N = N \ 10
WEND
PRINT "SUM OF SQUARE OF  DIGITS"; S
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION SUMSQ (N)
CLS
INPUT "ENTER ANY NUMBER"; N
SU = SUMSQ (N)
PRINT "SUM OF SQUARE OF DIGITS"; SU
END
FUNCTION SUMSQ (N)
S = 0
WHILE N < > 0
R = N MOD 10
S = S + R ^ 2
N = N \ 10
WEND
SUMSQ = S : END FUNCTION
108.   WAP to input number and find sum of square of odd digits.
CLS
INPUT "ENTER ANY NUMBER"; N
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN S = S + R ^ 2
N = N \ 10
WEND
PRINT "SUM OF SQUARE OF ODD DIGITS"; S
END
USING SUB PROCEDURE
DECLARE SUB SUMODD (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL SUMODD (N)
END
SUB SUMODD (N)
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN S = S + R ^ 2
N = N \ 10
WEND
PRINT "SUM OF SQUARE OF ODD DIGITS"; S
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION SUMODD (N)
CLS
INPUT "ENTER ANY NUMBER"; N
SU = SUMODD (N)
PRINT "SUM OF SQUARE OF ODD DIGITS"; SU
END
FUNCTION SUMODD (N)
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN S = S + R ^ 2
N = N \ 10
WEND
SUMODD = S : END FUNCTION

109.   WAP to input number and find sum of square of even digits.
CLS
INPUT "ENTER ANY NUMBER"; N
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN S = S + R ^ 2
N = N \ 10
WEND
PRINT "SUM OF SQUARE OF EVEN DIGITS"; S
END
USING SUB PROCEDURE
DECLARE SUB SUMEVEN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL SUMEVEN (N)
END
SUB SUMEVEN (N)
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN S = S + R ^ 2
N = N \ 10
WEND
PRINT "SUM OF SQUARE OF EVEN DIGITS"; S
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION SUMEVEN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
SU = SUMEVEN (N)
PRINT "SUM OF SQUARE OF EVEN DIGITS"; SU
END
FUNCTION SUMEVEN (N)
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN S = S + R ^ 2
N = N \ 10
WEND
SUMEVEN = S : END FUNCTION
110. WAP to input number and find sum of cube of digits.
CLS
INPUT "ENTER ANY NUMBER"; N
S = 0
WHILE N < > 0
R = N MOD 10
S = S + R ^ 3
N = N \ 10
WEND
PRINT "SUM OF CUBE OF DIGITS"; S
END
USING SUB PROCEDURE
DECLARE SUB SUMCB (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL SUMCB (N)
END
SUB SUMCB (N)
S = 0
WHILE N < > 0
R = N MOD 10
S = S + R ^ 3
N = N \ 10
WEND
PRINT "SUM OF CUBE OF  DIGITS"; S
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION SUMCB (N)
CLS
INPUT "ENTER ANY NUMBER"; N
SU = SUMCB (N)
PRINT "SUM OF CUBE OF DIGITS"; SU
END
FUNCTION SUMCB (N)
S = 0
WHILE N < > 0
R = N MOD 10
S = S + R ^ 3
N = N \ 10
WEND
SUMCB = S : END FUNCTION
111.   WAP to input number and find sum of cube of odd digits.
CLS
INPUT "ENTER ANY NUMBER"; N
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN S = S + R ^ 3
N = N \ 10
WEND
PRINT "SUM OF CUBE OF ODD DIGITS"; S
END
USING SUB PROCEDURE
DECLARE SUB SUMODD (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL SUMODD (N)
END
SUB SUMODD (N)
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN S = S + R ^ 3
N = N \ 10
WEND
PRINT "SUM OF CUBE OF ODD DIGITS"; S
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION SUMODD (N)
CLS
INPUT "ENTER ANY NUMBER"; N
SU = SUMODD (N)
PRINT "SUM OF CUBE OF ODD DIGITS"; SU
END
FUNCTION SUMODD (N)
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN S = S + R ^ 3
N = N \ 10
WEND
SUMODD = S : END FUNCTION
112.   WAP to input number and find sum of cube of even digits.
CLS
INPUT "ENTER ANY NUMBER"; N
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN S = S + R ^ 3
N = N \ 10
WEND
PRINT "SUM OF CUBE OF EVEN DIGITS"; S
END
USING SUB PROCEDURE
DECLARE SUB SUMEVEN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL SUMEVEN (N)
END
SUB SUMEVEN (N)
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN S = S + R ^ 3
N = N \ 10
WEND
PRINT "SUM OF CUBE OF EVEN DIGITS"; S
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION SUMEVEN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
SU = SUMEVEN (N)
PRINT "SUM OF CUBE OF EVEN DIGITS"; SU
END
FUNCTION SUMEVEN (N)
S = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN S = S + R ^ 3
N = N \ 10
WEND
SUMEVEN = S : END FUNCTION
113.   WAP to input number and count total no. of digits.
CLS
INPUT "ENTER ANY NUMBER"; N
C = 0
WHILE N < > 0
C = C + 1
N = N \ 10
WEND
PRINT "TOTAL NUMBER OF DIGITS"; C
END
USING SUB PROCEDURE
DECLARE SUB COUNT (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL COUNT (N)
END
SUB COUNT (N)
C = 0
WHILE N < > 0
C = C + 1
N = N \ 10
WEND
PRINT "TOTAL NUMBER OF DIGITS"; C
END SUB
USING FUNCTION PROCEDURE

DECLARE FUNCTION COUNT (N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "TOTAL NUMBER OF DIGITS"; COUNT (N)
END

FUNCTION SUM (N)
C = 0
WHILE N < > 0
C = C + 1
N = N \ 10
WEND
COUNT = C
END FUNCTION
114.   WAP to input number and count total no. of odd digits.
CLS
INPUT "ENTER ANY NUMBER"; N
C = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN C = C + 1
N = N \ 10
WEND
PRINT "TOTAL NUMBER OF ODD DIGITS"; C
END
USING SUB PROCEDURE
DECLARE SUB COUNT (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL COUNT (N)
END
SUB COUNT (N)
C = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN C = C + 1
N = N \ 10
WEND
PRINT "TOTAL NUMBER OF ODD DIGITS"; C
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION COUNT (N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "TOTAL NUMBER OF ODD DIGITS"; COUNT (N)
END
FUNCTION SUM (N)
C = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN C = C + 1
N = N \ 10
WEND
COUNT = C : END FUNCTION
115.   WAP to input number and count total no. of even digits.
CLS
INPUT "ENTER ANY NUMBER"; N
C = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN C = C + 1
N = N \ 10
WEND
PRINT "TOTAL NUMBER OF EVEN DIGITS"; C
END
USING SUB PROCEDURE
DECLARE SUB COUNT (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL COUNT (N)
END
SUB COUNT (N)
C = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN C = C + 1
N = N \ 10
WEND
PRINT "TOTAL NUMBER OF EVEN DIGITS"; C
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION COUNT (N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "TOTAL NUMBER OF EVEN DIGITS"; COUNT (N)
END
FUNCTION SUM (N)
C = 0
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN C = C + 1
N = N \ 10
WEND
COUNT = C : END FUNCTION
116. WAP TO ENTER ANY DIGIT AND DISPLAY EVEN DIGITS
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "EVEN DIGITS ARE ";
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 0 THEN PRINT R;
N = N \ 10
WEND
END
USING SUB PROCEDURE
DECLARE SUB EVEN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL EVEN (N)
END
SUB EVEN (N)
PRINT "EVEN DIGITS ARE ";
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 0 THEN PRINT R;
N = N \ 10
WEND
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION EVEN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "EVEN DIGITS ARE ";
D = EVEN(N)
END

FUNCTION EVEN (N)
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 0 THEN PRINT R;
N = N \ 10
WEND

END FUNCTION
117. WAP TO ENTER ANY DIGIT AND DISPLAY ODD DIGITS
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "ODD DIGITS ARE ";
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 1 THEN PRINT R;
N = N \ 10
WEND
END
USING SUB PROCEDURE
DECLARE SUB ODD (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL ODD (N)
END
SUB ODD (N)
PRINT "ODD DIGITS ARE ";
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 1 THEN PRINT R;
N = N \ 10
WEND
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION ODD (N)
REM
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT " ODD DIGITS ARE ";
D = ODD(N)
END
FUNCTION ODD (N)
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 1 THEN PRINT R;
N = N \ 10
WEND
END FUNCTION

118. WAP TO ENTER ANY DIGIT AND DISPLAY SQUARE OF EVEN DIGITS
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "SQUARE OF EVEN DIGITS ARE ";
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 0 THEN PRINT R ^ 2;
N = N \ 10
WEND
END
USING SUB PROCEDURE
DECLARE SUB EVEN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL EVEN (N)
END
SUB EVEN (N)
PRINT "SQUARE OF EVEN DIGITS ARE ";
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 0 THEN PRINT R ^ 2;
N = N \ 10
WEND
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION EVEN (N)
REM
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "SQUARE OF EVEN DIGITS";
D = EVEN(N)
END
FUNCTION EVEN (N)
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 0 THEN PRINT R ^ 2;
N = N \ 10
WEND
END FUNCTION

119. WAP TO ENTER ANY DIGIT AND DISPLAY SQUARE OF ODD
 DIGITS
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "SQUARE OF ODD DIGITS ARE ";
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 1 THEN PRINT R;
N = N \ 10
WEND
END
USING SUB PROCEDURE
DECLARE SUB ODD (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL ODD (N)
END
SUB ODD (N)
PRINT " SQUARE OF ODD DIGITS ARE ";
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 1 THEN PRINT R;
N = N \ 10
WEND
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION ODD (N)
REM
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "SQUARE OF ODD DIGITS";
D = ODD(N)
END
FUNCTION ODD (N)
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 1 THEN PRINT R ^ 2;
N = N \ 10
WEND
END FUNCTION
120. WAP TO ENTER ANY DIGIT AND DISPLAY CUBE OF EVEN DIGITS
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "CUBE OF EVEN DIGITS ARE ";
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 0 THEN PRINT R ^ 3;
N = N \ 10
WEND
END
USING SUB PROCEDURE
DECLARE SUB EVEN (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL EVEN (N)
END
SUB EVEN (N)
PRINT "SQUARE OF EVEN DIGITS ARE ";
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 0 THEN PRINT R ^ 3;
N = N \ 10
WEND
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION EVEN (N)
REM
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "CUBE OF EVEN DIGITS";
D = EVEN(N)
END
FUNCTION EVEN (N)
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 0 THEN PRINT R ^ 3;
N = N \ 10
WEND
END FUNCTION

121. WAP TO ENTER ANY DIGIT AND DISPLAY CUBE OF ODD DIGITS
REM
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "CUBE OF ODD DIGITS ARE ";
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 1 THEN PRINT R ^ 3;
N = N \ 10
WEND
END
USING SUB PROCEDURE
DECLARE SUB ODD (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL ODD (N)
END
SUB ODD (N)
PRINT "CUBE OF ODD DIGITS ARE ";
WHILE N <> 0
R = N MOD 10
IF  MOD 2 = 1 THEN PRINT R ^ 3;
N = N \ 10
WEND
END SUB
USING FUNCTION PROCEDURE
DECLARE FUNCTION ODD (N)
REM
CLS
INPUT "ENTER ANY NUMBER"; N
PRINT "CUBE OF ODD DIGITS";
D = ODD(N)
END
FUNCTION ODD (N)
WHILE N <> 0
R = N MOD 10
IF R MOD 2 = 1 THEN PRINT R ^ 3;
N = N \ 10
WEND
END FUNCTION
122.  WAP to enter any 10 numbers and find the sum of numbers.

CLS
FOR I = 1 TO 10
INPUT "ENTER THE NUMBERS"; N(I)
S = S + N(I)
NEXT I
PRINT "SUM OF 10 NUMBERS"; S
END

USING SUB PROCEDURE
DECLARE SUB SUM (N( ))
CLS
FOR I = 1 TO 10
INPUT "ENTER THE NUMBERS"; N(I)
NEXT I
CALL SUM(N( ))
END

SUB SUM (N())
FOR I = 1 TO 10
S = S + N(I)
NEXT I
PRINT "SUM OF 10 NUMBERS"; S
END SUB

USING FUNCTION PROCEDURE
DECLARE FUNCTION SUM (N( ))
CLS
FOR I = 1 TO 10
INPUT "ENTER THE NUMBERS"; N(I)
NEXT I
PRINT "SUM OF 10 NUMBERS"; SUM(N( ))
END
FUNCTION SUM (N())
FOR I = 1 TO 10
S = S + N(I)
NEXT I
SUM = S
END FUNCTION
123.   WAP to enter any 10 numbers and find the sum of odd numbers.
CLS
FOR I = 1 TO 10
INPUT "ENTER THE NUMBERS"; N(I)
IF N(I) MOD 2 = 1 THEN S = S + N(I)
NEXT I
PRINT "SUM OF 10 ODD NUMBERS"; S
END
USING SUB PROCEDURE
DECLARE SUB SUM (N( ))
CLS
FOR I = 1 TO 10
INPUT "ENTER THE NUMBERS"; N(I)
NEXT I
CALL SUM(N( ))
END
SUB SUM (N())
FOR I = 1 TO 10
IF N(I) MOD 2 = 1 THEN S = S + N(I)
NEXT I
PRINT "SUM OF 10 ODD NUMBERS"; S
END SUB

USING FUNCTION PROCEDURE
DECLARE FUNCTION SUM (N( ))
CLS
FOR I = 1 TO 10
INPUT "ENTER THE NUMBERS"; N(I)
NEXT I
PRINT "SUM OF 10 ODD NUMBERS"; SUM(N( ))
END


FUNCTION SUM (N())
FOR I = 1 TO 10
IF N(I) MOD 2 = 1 THEN S = S + N(I)
NEXT I
SUM = S
END FUNCTION
124. WAP to enter any 10 numbers and find the sum of even numbers.
CLS
FOR I = 1 TO 10
INPUT "ENTER THE NUMBERS"; N(I)
IF N(I) MOD 2 = 0 THEN S = S + N(I)
NEXT I
PRINT "SUM OF 10 EVENNUMBERS"; S
END

USING SUB PROCEDURE
DECLARE SUB SUM (N( ))
CLS
FOR I = 1 TO 10
INPUT "ENTER THE NUMBERS"; N(I)
NEXT I
CALL SUM(N( ))
END

SUB SUM (N())
FOR I = 1 TO 10
IF N(I) MOD 2 = 0 THEN S = S + N(I)
NEXT I
PRINT "SUM OF 10 EVENNUMBERS"; S
END SUB

USING FUNCTION PROCEDURE
DECLARE FUNCTION SUM (N( ))
CLS
FOR I = 1 TO 10
INPUT "ENTER THE NUMBERS"; N(I)
NEXT I
PRINT "SUM OF 10 EVEN NUMBERS"; SUM(N( ))
END
FUNCTION SUM (N())
FOR I = 1 TO 10
IF N(I) MOD 2 = 0 THEN S = S + N(I)
NEXT I
SUM = S
END FUNCTION

2 comments:

  1. Thanks man !! May Jesus bless you for your help towards student's bright future.

    ReplyDelete