You are on page 1of 12

PROGRAMMING TECHNIQUES ASSIGNMENT Applicant name: Jasmine Goyal Applicant id- 486346

Q1. Write down an algorithm to get the weight (in Kilograms) and height (in meters) of person. Then calculate and print and the Body Mass Index (BMI) according to the formula: BMI=weight/(height*height) The BMI should be printed with 3 decimal places. You should choose an appropriate datatypes and names for your variables. Solution: START Use variables: person_Weight, person_Height, BMI of type float PRINT "Enter weight in Kilograms" READ person_Weight WHILE person_Weight NOT POSITIVE PRINT "Enter weight in Kilograms" READ person_Weight END WHILE PRINT "Enter height in Meters" READ person_Height WHILE person_Height NOT POSITIVE PRINT "Enter height in Meters" READ person_Height END WHILE COMPUTE BMI=person_Weight/(person_Height*person_Height) PRINT BMI upto 3 decimal places

STOP

2) Write down an algorithm to read in a temperature in Celsius and convert it to Fahrenheit. The relationship between Celsius and Fahrenheit is C = (F-32)*5/9. Solution: START Use variables: temp_Celsius, temp_Fahrenheit of type float PRINT "Enter temperature in celsius" READ temp_Celsius COMPUTE temp_Fahrenheit=9/5*temp_Celsius+32 PRINT "Temperature in fahrenheit is ",temp_Fahrenheit STOP

Q3. Write a algorithm to read a positive integer and test if it is prime number Solution: START Use variables: number,loop_Counter of type integer SET loop_Counter=2; PRINT "Enter the number" READ number WHILE number is NOT POSITIVE PRINT "Enter the positive number" READ number

END WHILE IF number >1 THEN SET loop_Couner:=2 WHILE loop_Counter<=number IF number%loop_Counter=0 THEN PRINT Number is not prime BREAK END IF INCREMENT loop_Counter by 1 END WHILE IF loop_Counter=number Print Number is Prime END IF ELSE PRINT 1 is neither prime nor composite END IF STOP

Q 4) Write down an algorithm to find out smallest and largest amongst 3 elements. Now, modify the above algorithm so that it will work with N elements where N is accepted from the user. Solution:

A)

Use variables: num_Of_Elements,loop_Counter,max_Element,min_Element of type integer element[] array of type integer

SET num_Of_Elements:=3 SET loop_Counter:=0 WHILE loop_Counter<num_Of_Elements READ element[loop_Counter] INCREMENT loop_Counter by 1 END WHILE SET max_Element:=element[0] SET min_Element:=element[0] SET loop_Counter:=1 WHILE loop_Counter<num_Of_Elements IF max_Element>element[loop_Counter] SET max_Element:=element[loop_Counter] END IF IF min_Element<element[loop_Counter] SET min_Element:=element[loop_Counter] END IF INCREMENT loop_Counter by 1 END WHILE PRINT "Largest element is",max_Element PRINT "Smallest element is",min_Element STOP

B)

START Use variables: num_Of_Elements,loop_Counter,max_Element,min_Element of type integer element[] array of type integer SET loop_Counter:=0 PRINT "Enter the total number of elements" READ num_Of_Elements WHILE loop_Counter<num_Of_Elements READ element[loop_Counter] INCREMENT loop_Counter by 1 END WHILE SET max_Element:=element[0] SET min_Element:=element[0] SET loop_Counter:=1 WHILE loop_Counter<num_Of_Element PRINT "Enter the element" READ element[loopCounter] INCREMENT loop_Counter by 1 END WHILE SET max_Element:=element[0] SET min_Element:=element[0] SET loop_Counter:=1

WHILE loop_Counter<num_Of_Elements IF max_Element>element[loop_Counter] SET max_Element:=element[loop_Counter] ENDIF

IF min_Element<element[loop_Counter] SET min_Element:=element[loop_Counter] END IF INCREMENT loop_Counter by 1 END WHILE PRINT "Largest element is",max_Element PRINT "Smallest element is",min_Element STOP

Q4. Write down algorithm to read the number of seconds and convert it into hours, minutes and seconds. Solution: START Use variables: hours, minutes, seconds, temp of type integer PRINT Please enter the number of seconds READ seconds SET temp:=seconds COMPUTE hours=seconds/3600 SET seconds:=secs%3600 COMPUTE minutes=seconds/60 SET seconds:=secs%60 PRINT Hours= hrs minutes= minutes seconds= seconds STOP

6) Write down an algorithm that reads in a markof a student (which is an integer between 0 and 100) and prints the corresponding grades (A-F). The mark-to-grade conversion table is as follows:

Grade Range

A 80

B 65-79

C 50-64

D 40-49

F <40

1. Modify the program so that it checks if the input is between 0 and 100. If not, it should ask the user to input again until the input is in the correct range. Use while statements.

2. Modify the program so that it repeats the above computation on 50


students. Use while statements. Solution: A) START Use variables: student_Mark of type integer PRINT Enter the marks of student READ student_Mark

WHILE student_ Mark < AND student_Mark>100 PRINT Enter the marks of student which lies between 0 and 100 READ END WHILE IF student_Mark >=80 PRINT Grade is A student_Mark

ELSE IF student_Mark>=65 AND student_Mark<80 PRINT Grade is B

ELSE IF student_Mark>=50 AND student_Mark<65 PRINT Grade is B

ELSE IF student_Mark>=40 AND student_Mark<50

PRINT ELSE PRINT END IF STOP

Grade is C

Grade is F

B) START Use variables: student_Mark, loop_Counter of type integer SET loop_Counter:=1 WHILE loop_Counter <=5 PRINT Enter the marks of student READ student_Mark

WHILE student_Mark <0 AND student_Mark>100 PRINT Enter the marks of student which lies between 0 and 100 READ END WHILE IF student_Mark >=80 PRINT Grade is A student_Mark

ELSE IF student_Mark>=65 AND student_Mark<80 PRINT Grade is B

ELSE IF student_Mark>=50 AND studentMark<65 PRINT Grade is B

ELSE IF student_Mark>=40 AND student_Mark<50 PRINT ELSE Grade is C

PRINT END IF

Grade is F

INCREMENT loop_Counter by 1 END WHILE STOP

Q7. Write down an algorithm to perform following operations on array:a) Insert the element at nth position in array b) Delete the element at a given position c) Display an array. Take Solution: START Use variables: elementArray[], loop_Counter, postion, new_Element, num_Of_Elements, option as integer SET loop_Counter:=0 PRINT Enter the number of elements READ num_Of_Element care of proper validations

WHILE loop_Counter<num_Of_Elements PRINT Enter the element READ elementArray[loop_Counter] INCREMENT loop_Counter by 1 END WHILE PRINT Enter the following option PRINT Enter 1 to insert element at given position PRINT PRINT Enter 2 to delete element at given position Enter 3 to display all elements

READ option

CASE 1:

option OF

PRINT Enter the position of new element READ position

WHILE position <1 PRINT Enter the valid position of new element READ END WHILE PRINT Enter the new element READ new_Element position

SET loop_Counter:=num_Of_Elements WHILE loop_Counter>=position elementArray[loop_Counter]=elementArray[loop_Counter-1] DECREMENT loop_Counter by 1 END WHILE ElementArray[position-1]:=new_Element INCREMENT num_Of_Element by 1 BREAK

2: PRINT Enter the position of element you want to delete READ position

WHILE position<1 AND position>num_Of_Elements PRINT Enter the position of element you want to delete READ END WHILE position

SET loop_Counter :=position-1 WHILE loop_Counter< num_Of_Elements elementArray[loop_Counter]= elementArray[loop_Counter-1] INCREMENT loop_Counter by 1 END WHILE DECREMENT num_Of_Elements by 1 BREAK

3: SET loop_Counter:=0 WHILE loop_Counter<num_Of_Elements PRINT elementArray[loop_Counter] INCREMENT loop_Counter by 1 END WHILE BREAK END CASE STOP

8) A password file contains username and password. Write an algorithm to accept the username and password and crosscheck it with the password file and display appropriate message. Solution: START STRING username, password, user_Name, pass_Word PRINT Enter username PRINT Enter password READ username

READ password OPEN file Read file SEARCH username IF username.exists THEN GET pass_Word IF password=pass_Word PRINT Successful Login ELSE PRINT Invalid Username/Password END IF ELSE PRINT Invalid Username Close file STOP

You might also like