Monday, September 2, 2013

TOPIC 2: LOGIC BUILDING USING PSEUDO CODE

2.1 Flowchart and Pseudo Code


Assignment 2.1.2: Pseudo code- Sequential pattern


Objective: Writing pseudo code for sequence pattern problems

Problem Description: The finance department of a company wants to calculate the gross pay of an employee in the company. The number of hours worked by the employee and the pay rate should be accepted from the user and the gross pay should be calculated as the below formula.

Calculate-Gross-Pay
1. input No_of_Hours, Pay_Rate
2. Gross_Pay = No_of_Hours * Pay_Rate
3. Display "Gross Pay is : ", Gross_Pay

The above given set of statements represents a pseudo code, which can be perceived as the initial stage of formal coding. It represents a step by step approach to solve any problem in plain english, instead of formal syntax used in any particular programming language. 

There are few conventions which are followed while writing a pseudo code as :
1. use of keyword 'input', whenever data is retrieved from the user
2. use of keyword 'display', whenever result is to be displayed
3. Mathematical expressions are used as it is, as we normally use them.
4. if, then, else, end if keywords are used for decision making situations
5. for, to , do , end for keywords are used when  certain steps are to repeated for a known number of times.
6.while, end while keywords are used when some of the steps are to repeated as long as certain condition remains true.


Assignment 2.1.3: Understanding Variables & Constants


Problem 1 :

Write a pseudo code to take the height of the building in variable 
“Building_Height” and display the same.





SOLUTION :

DISP_HEIGHT
1. input Building_Height
2.display "The height of the building is : ", Buildiing_height
3. end


Problem 2 :
Write a pseudo code to take the constant value 3.142 in a variable “pi” and
display the same.

SOLUTION :

DISP_PI
1. input pi = 3.142
2. display "The value assigned to pi is : " pi
3. end


Assignment 2.1.4: Understanding Operators


Problem 1 : 
Write a pseudo code to take two numbers as input in variables a and b and display
i. Product of the two numbers a and b
ii. Sum of the two numbers a and b
iii. Values of a-b and b-a
iv. Quotient and remainder for a/b and b/a

SOLUTION :

i ) 
PROD_OF_TWO_NUM
1. input a, b
2. Product = a * b
3. Display " The product of 'a' and 'b' is: ", Product
4. end

ii)
SUM_OF_TWO_NUM
1. input a, b
2. Sum = a  + b
3. Display " The sum of 'a' and 'b' is : ", Sum
4. end

iii)
DIFFERENCE_BETWEEN
1. input a, b
2. Diff_A_and_B = a - b
3. Diff_B_and_A = b-a
4. Display "the difference of 'a' and 'b' is : ", Diff_A_and_B
5. Display "the difference of 'a' and 'b' is : ", Diff_B_and_A
6. end

iv) 
QUOTIENT_REM
1. input a, b
2. Quo_A_by_B = a / b
3. Rem_A_by_B = a % b
4. Quo_B_by_A = b / a
5. Rem_B_by_A = b % a
6. Display "the quotient of 'a' by 'b' is : ", Quo_A_by_B
7. Display "the quotient of 'b' by 'a' is : ", Quo_B_by_A
8. Display "the remainder from 'a' by 'b' is : ", Rem_A_by_B
9. Display "the remainder from 'b' by 'a' is : ", Rem_B_by_A
10. end


Problem 2:

Write a pseudo code to take the height and width of a rectangle in variables

height and width respectively and display the rectangle's area and perimeter.

SOLUTION :

RECTANGLE_AREA_PERIMETER
1. input height, width
2. Area = height * width
3. Perimeter = 2*(height +width)
4. Display "The area of rectangle is : ", Area
5. Display "The perimeter of rectangle is : ", Perimeter
6. end


Problem 3:
Write a pseudo code to take the radius of a circle in variable radius and display

its area and perimeter.

SOLUTION :

CIRCLE_AREA_PERIMETER
1. input radius
2. Area = 22/7(radius * radius)
3. Perimeter = 2 *( 22/7) * radius
4. Display "The area of circle is : ", Area
5. Display "The perimeter of circle is : ", Perimeter
6. end


Problem 4:
Write a pseudo code to take the starting value, common difference and the
number of terms of an arithmetic progression in variables a, d and n respectively and display its nth term and sum of n terms.

SOLUTION :

ARITHMETIC_PROGRESSION
1. input a, d, n
2. Nth_term = a+((n-1)*d)
3. Sum_of_terms = (n/2)*((2*a)+((n-1)*d))
4. Display " The nth term is : ", Nth_term
5. Display "The sum of terms is : ", Sum_of_terms
6. end


Problem 5:
Write a pseudo code to take the radius of the sphere in variable radius and

display the volume of the sphere.

SOLUTION :

VOLUME_OF_SPHERE
1. input radius
2. Volume = (4/3)*3.14*(radius*radius*radius)
3. Display " The volume of the sphere is  : ", Volume
4. end




No comments:

Post a Comment