Sunday, March 2, 2014

Write a program in QBASIC:

1.     Input 15 numbers and print them with their sum.
  =>
REM
CLS
DIM N(15)
FOR  I= 1 TO 15
INPUT “Enter a number”; N(I)
NEXT  I
CLS
PRINT “The entered numbers are:”
FOR J= 1 TO 15
PRINT N(J)
S=S+N(J)
NEXT J
PRINT “The sum is”; S
END

2.     Input 20 numbers and print them in reverse order.
  =>
REM
CLS
DIM N(20)
FOR I= 1 TO 20
INPUT “Enter a number”; N(I)
NEXT I
       CLS
       PRINT “The numbers in reverse order are:”
       FOR J = 20 TO 1 STEP – 1
       PRINT N(J)
       NEXT J
       END

  3.     Input 25 numbers and print only even numbers with their sum.
=>
   REM
      CLS
      
DIM N(25)
      FOR I = 1 TO 25
      INPUT ”Enter a number”; N(I)
      NEXT I
      CLS
      PRINT “The even numbers only are:”
      FOR J = 1 TO 25
      IF N(J)MOD 2=0 THEN PRINT N(J)
      S=S+N(J)
      NEXT J
      PRINT “The sum is”; S
      END

  4.    Input 20 numbers and print only even numbers.
    =>
      REM
      CLS
      DIM N(20)
      FOR I = 1 TO 20
      INPUT “Enter the numbers”; N(I)
      NEXT I
      CLS
      PRINT “The even numbers only are:”
      FOR J = 1 TO 20
      IF N(J) MOD 2=0 THEN PRINT N(J)
      NEXT J
      END


5.     Input 15 numbers and print only positive numbers  from the numbers given below:
10, 20, -30, -40, 19, 45, 32, -98, -78, 34, -53, 12, -9, -7, -8
 =>
REM
CLS
DIM N(15)
FOR I = 1 TO 15
READ N(I)
NEXT I
DATA 10, 20, -30, -40, 19
DATA 45, 32, -98, -78, 34
DATA -53, 12, -9, -7, -8
PRINT “The positive numbers are”
FOR J = 1 TO 15
IF N(J)>0 THEN PRINT N(J)
NEXT J
END


6.     Input 10 different names and   display all whose name start with “S”.
  =>
REM
CLS
DIM N(10) AS STRING
FOR I = 1 TO 10
INPUT “Enter the name”; N(I)
NEXT I
CLS
PRINT “The names whose first letter starts with “S” are”
FOR J = 1 TO 10
A$=UCASE$(LEFT$(N(J),1))
IF A$=”S” THEN PRINT N(J)
NEXT
END

7.      Input 15 different names and display only those names whose starting and ending characters are vowels.
  =>
REM
CLS
DIM N(15) AS STRING
FOR I = 1 TO 15
INPUT “Enter the name”; N(I)
NEXT I
PRINT “The names whose starting and ending characters are vowel are”
FOR J= 1 TO 15
A$=UCASE$(LEFT$(N(J), 1))
B$=UCASE$(RIGHT$(N(J),1))
IF (A$=”A” OR A$=”E” OR A$=”I” OR A$=”O” OR A$=”U”) AND (B$=”A” OR B$=”E” OR B$=”I” OR B$=O” OR B$=”U”) THEN PRINT N(J),
NEXT J
END

8.    Input 10 different numbers and display in ascending order.
  =>
REM
CLS
DIM N(10)
FOR I = 1 TO 10
INPUT” Enter the number”; N(I)
NEXT I
FOR I = 1 TO 10
FOR J = 1 TO 10-I
IF N(J) > N(J+1) THEN SWAP N(J), N(J+1)
NEXT J
NEXT I
CLS
PRINT “The numbers in ascending order are:”
FOR K = 1 TO 10
PRINT N(K)
NEXT K
END

9.     Input 10 different names and display in descending order.
  =>
REM
CLS
DIM N(10) AS STRING
FOR I = 1 TO 10
INPUT “Enter the name”; N(I)
NEXT I
FOR K = 1 TO 10
FOR J = 1 TO 10 – K
IF N(J) < N(J+1) THEN SWAP N(J), N(J+1)
NEXT J
NEXT K
CLS
PRINT “The names in Descending order is”
FOR I = 1 TO 10
PRINT N(I)
NEXT I
END

10.  Input 15 different names of capital and country and display in ascending order according to country.
      =>
        REM
        CLS
        DIM N(10) AS STRING
        DIM CN(10) AS STRING
        FOR I = 1 TO 10
        INPUT “Enter the name of the county and capital”; N(I),
        CN(I)
        NEXT I
        FOR J= 1 TO 10
        FOR K = 1 TO 10 – J
        IF N(K)>N(K+1) THEN
        SWAP N(K), N(K+1)
        SWAP CN(K), CN(K+1)
        END IF
        NEXT K
        NEXT J
        CLS
        PRINT “Name of county”, “Capital”
        FOR I = 1 TO 10
        PRINT N(I), CN(I)
        NEXT I
        END

11.  Store names and marks of 15 different students and print their names, marks and division using the following conditions:
Percentage                 Division

    >=80 and <=100         Distinction
>=60 and <80             First division
>=50 and <60             Second division
>=40 and <50             Third division
       <40                           Fail
     =>
       REM
       CLS
       DIM N(15) AS STRING
       DIM M(15)
       DIM D(15) AS STRING
       FOR I = 1 TO 15
       INPUT “Enter the name:”; N(I)
       INPUT “Enter the percentage:”; M(I)
       NEXT I
       PRINT “Name”, ”Percentage”, ”Division”
       FOR I = 1 TO 15
       IF M(I)>=80 AND M(I)<=100 THEN
       D[I]= ”Distinction”
       ELSEIF M(I)>=60 AND M(I)<80 THEN
       D[I]= “First Division”
       ELSEIF M(I)>=50 AND M(I)<60 THEN
       D[I]= “Second Division”
       ELSEIF M(I)>=40 AND M(I)<50 THEN
       D[I]= “Third Division”
       ELSE
       D[I]= “Fail”
       END IF
       PRINT N(I), M(I), D(I)
       NEXT I
       END

12. Input 10 different names and age of students, after that the user should ask the user to input name to search data. If the name is found then print the name and age of the student otherwise print “Data not found”.
      =>
        REM
        CLS
        DIM N(10) AS STRING
        DIM A(10)
        FOR I = 1 TO 10
        INPUT “Enter the name and age of the students”; N(I), A(I)
        NEXT I
        INPUT “Enter the name to search the data”; S$
        FLAG =0
        FOR I= 1 TO 10
        IF UCASE$(S$)=UCASE$(N(I)) THEN
        PRINT N(I), A(I)
        FLAG=1
        END IF
        NEXT I
        IF FLAG=0 THEN PRINT “Data not found”
        END

No comments:

Post a Comment