Sunday 5 April 2020

Modular Programming Solution of Green Publication 2076


Modular Programming
Perform using sub procedure:
a.     Write a program to input base and height of the triangle from user. The program should calculate and display the area of triangle using SUB – END SUB statement.(Hint: A=1/2bh)

DECLARE SUB Triangle( )
CLS
        CALL Triangle
END
SUB Triangle
        INPUT "Enter a base of triangle"; b
        INPUT "Enter a height of triangle"; h
        LET Area = (1/2) *b*h
        PRINT "Area of triangle is", Area
END SUB
       
b.      Write a sub program to print area of a cylinder, radius and height of cylinder are given by the user in main module and pass them as parameter list when sub program is called.

DECLARE SUB cyl(r, h)
CLS
           INPUT "Enter a radius"; r
           INPUT "Enter a height"; h
CALL cyl(r, h)
END
SUB Triangle
           LET Area = (22/7)*r^2*h
           PRINT "Area of triangle is", Area
END SUB

c.       Write a sub-procedure to find the solution of quadratic equation ax2 +bx +c=0. The program should ask the value of a, b, and c from user in main module and pass them as parameter list when a sub program is called.

DECLARE SUB value(a, b, c)
CLS
           PRINT "Calculate ax^2+bx+c=0"
           INPUT "Enter a value of a"; a
           INPUT "Enter a value of b"; b
           INPUT "Enter a value of c"; c
CALL value(a, b, c)
END
SUB Triangle
           LET d=(b*b-4*a*c)^1/2
           LET x = (-b+d)/2*a
           LET y = (-b-d)/2*a
           PRINT "Result of quadratic equation are x, y"; x, y
END SUB

d.      Write a sub program to display the acceleration of car. The program should ask initial velocity, final velocity and time taken by the car from user in main module and pass them as parameter list when a sub program is called.

DECLARE SUB accel(u, uf, t)
CLS
      PRINT "Acceleration of Car"
      INPUT "Enter initial velocity of a car"; a
      INPUT "Enter final velocity of a car"; b       
INPUT "Enter time taken by of a car"; c
CALL accel(a, b, c)
END
SUB accel(u, uf, t)
      LET ac = (uf-u)/t
      PRINT "Acceleration of car is:::::"; ac
END SUB

e.       Write a sub program to check whether an input number is “Positive” or “Negative” or “Zero”. The program should ask a number from user in main module and pass it as parameter list when a program is called.

DECLARE SUB PONE (A)
CLS
     INPUT "Enter a number"; PBR
     CALL PONE(PBR)
END
SUB PONE (B)
     C= SGN(B)
     IF C= -1 THEN
`          PRINT "Negative"
     ELSEIF C=1 THEN
           PRINT "Positive"
     ELSE
           PRINT "Zero"
     END IF
END SUB

f.        Write a program to decide whether an input number is even or odd. The program should ask a number from user in the main module and pass them as parameter list.
DECLARE SUB OE(N)
CLS
           INPUT "Enter a Number"; PKR
           PRINT OE(PKR)
END
SUB OE(M)
                       R=M mod 2
           IF R=0 THEN
                       PRINT"Even"
           ELSE
                       PRINT "Odd"
           END IF
END SUB

g.      Write a program to declare a sub-procedure module to decide whether a triangle can be formed or not if there sides of a triangle are keyed by the user.
DECLARE SUB triangle(a, b, c)
CLS
            INPUT "Enter first side"; a
            INPUT "Enter second side"; b           
INPUT "Enter third side"; c
CALL triangle(a, b, c)
END
SUB triangle(a, b, c)
            IF (a+b)>c and (b+c)>a and (a+c)>b then
            PRINT "Triangle can be formed by given sides"
END SUB

h.      Write a sub program to print first 10 natural numbers followed by its square and cube.
DECLARE SUB NSC( )
CLS
CALL NSC
END
SUB NSC
            PRINT "Number", "Square", "Cube"
            FOR I = 1 to 10
                        PRINT I, I^2, I^3
            NEXT I
END SUB

i.    Write a sub program using SUB ....  END SUB statement to calculate and display the geometric series. The program should ask first term, common ratio and numbers of terms in main module and call the sub program to get the result.
DECLARE SUB NSC(a, b, c)
CLS
      INPUT "Enter a first term", a
      INPUT "Enter a common ratio"; b
      INPUT "Enter a number of terms"; c
CALL NSC(a, b, c)
END
SUB NSC (a, b, c)
      FOR I = 1 to c
                  PRINT a;
                  a=a^b
                  b=b+1
      NEXT I
END SUB

j.        Write a sub-procedure to display the sum of first 20 even numbers.
DECLARE SUB SUM( )
CLS
            CALL SUM
END
SUB SUM
            FOR I = 2 to 20 STEP 2
                        S=S+I
            NEXT I
                        PRINT "Sum of first 20 even numbers is :"; S
END SUB

k.      Write a sub-procedure to display the sum of first 30 odd numbers.
DECLARE SUB SUM( )
CLS
      CALL SUM
END
SUB SUM
      FOR I = 1 to 30 STEP 2
                  S=S+I
      NEXT I
                  PRINT "Sum of first 30 odd numbers is :"; S
END SUB

l.        Write a sub-procedure to display the numbers which are multiples of 5 and 6 from 5 to 100.
DECLARE SUB multiple( )
CLS
      CALL multiple
END
SUB multiple
      FOR I = 5 to 100
                  IF I MOD 5 =0 and I MOD 6 =0 THEN
                              PRINT I;
                  END IF
      NEXT I
END SUB

m.    Write a sub program to decide whether an input year is leap year or not.
DECLARE FUNCTION leap( )
CLS
     CALL leap
END
SUB leap
     INPUT "Enter a year"; y
     IF y MOD 4 = 0 and y mod 100= 0 then
                             PRINT "Leap year"
     ELSE
                             PRINT "Not leap year"
     END IF
END SUB

n.      Write a program to input a number and check whether the number is Armstrong or not using SUB - END SUB.
DECLARE SUB arm( )
CLS
     CALL arm
END
SUB arm
     INPUT "Enter a number"; N
                 M=N
     WHILE N<>0
                 R=N MOD 10
                 S= S+R^3
                 N=N\10
     WEND
                 IF M=S THEN
                             PRINT "Armstrong"
                 ELSE
                             PRINT " Not Armstrong"
                 END IF
END SUB

o.   Write a program to input and check whether the number is prime or not using SUB - END SUB.
DECLARE SUB prime( )
CLS
     CALL prime
END
SUB prime
     INPUT "Enter a number"; N
     FOR C = 1 to N
                 IF N MOD C = 0 then S=S+1
     NEXT C
                 IF S=2 THEN PRINT "Prime Number"
END SUB

p.   Write a sub procedure program using SUB..... END SUB statement to display the prime number from 2 to 100.
DECLARE SUB prime( )
CLS
   CALL prime
END
SUB prime
   FOR P = 2 to 100
               FOR C = 1 to P
                           IF P MOD C = 0 then S=S+1
               NEXT C
                           IF S=2 THEN PRINT P;
   NEXT P
END SUB

q.   Write a sub program using SUB......END SUB statement to find sum from 1 to p given number. For example: sum = 1+2+3+4+ ……+N.

DECLARE SUB SUM( )
CLS
            CALL SUM
END
SUB SUM
            INPUT "Enter a number"; B
            FOR I = 1 to B
                        S=S+I
            NEXT I
                        PRINT "Sum of all numbers up to";B;"is="; S
END SUB

r.    Write a sub-program using SUB....END SUB statement to display factorial of an input number. Note: The product from 1 to given number is called factorial of number. For Example: The factorial of 5 is 120. That is 1*2*3**5=120.

DECLARE SUB fact( )
CLS
            CALL fact
END
SUB fact
            P=1
            INPUT "Enter a number"; B
            FOR I = 1 to B
                        P=P*I
            NEXT I
                        PRINT "Factorial of ";B;"is="; P
END SUB

s.     Write a sub-program using SUB.....END SUB statement to check whether input string is palindrome or not. For example, LIRIL is a palindrome word. Because the word pronounced same forward and backward.

DECLARE SUB palin( )
CLS
            CALL palin
END
SUB palin
            INPUT "Enter a word"; B$
            FOR I = 1 to len(B$)
                        P$=MID$(B$, I, 1)
                        PAL$=P$+PAL$
            NEXT I
                        IF UCASE$(B$)=UCASE$(PAL$) THEN
                                    PRINT "Palindrome"
                        ELSE
                                    PRINT "Not Palindrome"
                        END IF
END SUB
t.        Write a Sub Program to check whether an input character is alphabet or digit or symbol. The program should ask a character from user in main module and pass is as parameter list when a sub program is called.
DECLARE SUB ADS(CH$)
CLS
  INPUT "Enter a character"; PBR$
  CALL ADS(PBR$)
END
SUB ADS(RPG$)
  S=ASC(RPG$)
  IF S>=48 and S<=57 TNEN
              PRINT "Entered character is Number"
  ELSEIF S>=65 AND S<=90 OR S>=97 AND S<=122 THEN
              PRINT "Entered character is Alphabet"
  ELSE
              PRINT "Entered character is Symbol"
  END IF
END SUB

u.      Write a sub program to enter a multi digits number and print sum of even digits and products of odd digits.
DECLARE SUB SP( )
CLS
            CALL SP
END
SUB SP
            P = 1
            INPUT "Enter a multi digit number"; N
            WHILE N<>0
                        R = N MOD 10
                        IF R MOD 2 =0 THEN
                                    S= S+ R
                        ELSE
                                    P = P*R
                        END IF
                        N=N\10
            WEND
                        PRINT "Sum of Even digit is:::"; S
                        PRINT " Product of Odd digit is ::::"; P
END SUB


v.   Write a sub program to print the series 7,22,11,34 …. Up to 10th times.
DECLARE SUB FUL( )
CLS
           CALL FUL
END
SUB FUL
           A = 7
           FOR P = 1 to 10
                       PRINT A;
                       IF A MOD 2 = 0 then
A=A\2
                                                ELSE
                                                            A=A*3+1
                       END IF
           NEXT P
END SUB

w.    Write a sub.... end sub program to enter 10 different numbers and find the smallest one using array variable.
DECLARE SUB SMALL (S( ))
CLS
DIM S(10)
FOR K = 1 to 10
            INPUT "Enter the numbers"; S(K)
NEXT K
            CALL SMALL (S( ))
END
SUB SMALL (S( ))
            SM=S(1)
            FOR C = 2 to 10
                        IF S(C)>SM THEN SM=S(C)
            NEXT C
            PRINT "The smallest number is:::", SM
END SUB








Perform by using User defined Function:
a.       Write a function procedure to print the distance travelled by a car. The program should ask initial velocity, final velocity and time taken by the car and the program should pass them as parameter list when a function procedure is called.
DECLARE FUNCTION dis(u, uf, t)
CLS
            PRINT "Distance travelled by a Car"
            INPUT "Enter initial velocity of a car"; a
            INPUT "Enter final velocity of a car"; b       
INPUT "Enter time taken by of a car"; c
PRINT dis(a, b, c)
END
FUNCTION dis(u, uf, t)
            LET a = (uf-u)/t
            LET s =u*t+(1/2)*a*t^2
            dis=s
END FUNCTION

b.      Write a function program to check whether an input number is “Positive” or “Negative” or “Zero”. The program should ask a number from user in main module and pass it as parameter list when a function program is called.
DECLARE FUNCTION PONEZ$ (A)
CLS
              INPUT "Enter a number"; PBR
              PRINT PONEZ$(PBR)
END
SUB FUNCTION PONEZ$ (B)
              C= SGN(B)
              IF C= -1 THEN
`                        P$= "Negative"
              ELSEIF C=1 THEN
                          P$= "Positive"
              ELSE
                          P$="Zero"
              END IF
PONEZ$=P$
END FUNCTION

c.       Write a function program to decide whether an input number is even or odd. The program should ask a number from user in main module and pass it as parameter list.

DECLARE FUNCTION OE$(N)
CLS
           INPUT "Enter a Number"; FUL
           PRINT OE$(FUL)
END
FUNCTION OE$(M)
                       R=M mod 2
           IF R=0 THEN
                       H$="Even"
           ELSE
                       H$= "Odd"
           END IF
           OE$=H$
END FUNCTION


d.      Write a function procedure to print square of first 10 natural numbers.
DECLARE FUCTION SUM(I)
CLS
         FOR I = 1 to 10
                     PRINT SUM(I)
         NEXT I
END
FUNCTION SUM(I)
         SUM=I^2
END FUNCTION

e.       Write a function procedure to display the sum of digits in an input number. The program should ask a number from user in main module and pass it as parameter list when a function is called.

DECLARE FUNCTION sumdi (N)
CLS
            INPUT "Enter a multi digit number"; N
PRINT sumdi (N)
END
FUNCTION sumdi (N)
            WHILE N<>0
                        R = N MOD 10
                        S=S+R
                        N=N\10
            WEND
            sumdi = S
END FUNCTION

f.        Write a function procedure to display the sum of even digits in an input number. The program should ask a number from user in main module and pass it as parameter list when a function is called.

DECLARE FUNCTION even (N)
CLS
            INPUT "Enter a multi digit number"; N
PRINT even (N)
END
FUNCTION even (N)
            WHILE N<>0
                        R = N MOD 10
                        IF R MOD 2=0 THEN S=S+R
                        N=N\10
            WEND
            even = S
END FUNCTION


g.      Write a function procedure to display the total number of digits in an input number. The program should ask a number from user in the main module and pass it as parameter list when a function is called.

DECLARE FUNCTION sumdi (N)
CLS
            INPUT "Enter a multi digit number"; N
PRINT "Total number of digits are   ";sumdi (N)
END
FUNCTION sumdi (N)
            WHILE N<>0
                        R = N MOD 10
                        S=S+1
                        N=N\10
            WEND
            sumdi = S
END FUNCTION

h.      Write a function procedure to display the product of digits in an input number. The program should ask a number from user in the main module and pass it as parameter list when a function is called.

DECLARE FUNCTION product (R)
CLS
            INPUT "Enter a multi digit number"; N
PRINT "Total number of digits are   "; product (N)
END
FUNCTION product (N)
            LET P=1
            WHILE N<>0
                        LET R = N MOD 10
                        LET P=P*R
                        LET N=N\10
            WEND
            product = P
END FUNCTION


i.        Write a function procedure to return the sum ASCII value of each character of a word. The program should ask a word from user in main module and pass it as parameter list.

DECLARE FUNCTION asi (CH$)
CLS
            INPUT "Enter a multi digit number"; N$
PRINT "Total number of digits are   ";asi (N$)
END
FUNCTION asi (N$)
            FOR I = 1 to LEN(N$)
                        R$ = MID$(N$, I, 1)
                        A=ASC(R$)
                        S=S+A
            NEXT I
            asi = S
END FUNCTION


j.        Write a function procedure to check whether an input string is palindrome or not. The program should ask a string from user in main module and pass it as parameter list when a function is called.

DECLARE FUNCTION palin$(W$)
CLS
            INPUT "Enter a String"; ST$
PRINT palin$(ST$)
END
FUNCTION palin$(B$)
            FOR I = 1 to len(B$)
                        P$=MID$(B$, I, 1)
                        PAL$=P$+PAL$
            NEXT I
                        IF UCASE$(B$)=UCASE$(PAL$) THEN
                                    p$= "Palindrome"
                        ELSE
                                    p$= "Not Palindrome"
                        END IF
                        palin$=p$
END FUNCTION


k.      Write a function procedure to print an input word into alternate capital. For example, if input word is "SCIENCE”, the output should be “ScIeNcE”. The program should pass a string as a parameter list when a function is called.

DECLARE FUNCTION alter$(W$)
CLS
            INPUT "Enter a String"; ST$
PRINT alter$(ST$)
END
FUNCTION alter$(B$)
            FOR I = 1 to len (B$)
                        P$=MID$(B$, I, 1)
                        IF I mod 2 <>0 THEN
                                    PAL$=PAL$ + UCASE$(P$)
                        ELSE
                                    PAL$=PAL$ + LCASE$(P$)
            NEXT I
                        alter$=PAL$
END FUNCTION


l.        Write a function procedure to print an input name of a person in abbreviated form. For example, if you input a name “Sachin Kumar Malla”, the output given by the function should be SKM.

DECLARE FUNCTION abb$(W$)
CLS
            INPUT "Enter a Full Name"; ST$
PRINT abb$(ST$)
END
FUNCTION abb$(B$)
            B$=UCASE$(B$)
            P$=LEFT$(B$,1)
            FOR I = 1 to len (B$)
                        R$=MID$(B$, I, 1)
                        IF R$=" " THEN
                                    B$=B$+"."+MID$(B$, I+1,1)
                        END IF
            NEXT I
                        abb$=B$
END FUNCTION


m.    Write a function procedure to return the value of total number of consonants in a sequence. The program should ask a sentence from user in main module  and pass it as parameter list when a function is called.

DECLARE FUNCTION conso(W$)
CLS
            INPUT "Enter a string"; ST$
PRINT "Total Number of consonants are   ";conso(ST$)
END
FUNCTION conso(B$)
            B$=UCASE$(B$)
            FOR I = 1 to len (B$)
                        R$=MID$(B$, I, 1)
                        SELECT CASE R$
                                    CASE "A", "E", "I", "O", "U"
                                                V=V+1
                                    CASE ELSE
                                                C=C+1
                        END SELECT
            NEXT I
                        conso=C
END FUNCTION
n.      Write a function procedure to check whether an input number is palindrome or not. The program should ask a number from user in main module and pass it as parameter list when a function is called without using string function like LEN and MID$.


DECLARE FUNCTION palin$(W)
CLS
            INPUT "Enter a string"; ST
PRINT "Total Number of consonants are   ";palin$(ST)
END
FUNCTION palin$(B)
            B$=STR$(B)
            FOR I = 1 to len (B$)
                        P$=MID$(B$, I, 1)
                        PAL$=P$+PAL$
            NEXT I
                        IF UCASE$(B$)=UCASE$(PAL$) THEN
                                    C$ ="Palindrome"
                        ELSE
                                    C$= "Not Palindrome"
                        END IF

            NEXT I
                        palin$=C$
END FUNCTION

o.      Write a program to define user defined function to find the HCF of any two numbers.

DECLARE FUNCTION HCF ( )
CLS
            PRINT "HCF of given numbers is   "; HCF
END
FUNCTION HCF
            INPUT "Enter a numbers   "; P, Q
            WHILE  P MOD Q <> 0
                        S = P MOD Q
                        P = Q
                        Q = S
            WEND
            HCF = Q
END FUNCTION


No comments:

Post a Comment

MCQ Question Bank Microsoft PowerPoint