0% found this document useful (0 votes)
5 views9 pages

Asignment 2 Programming

Uploaded by

toonszoomi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Asignment 2 Programming

Uploaded by

toonszoomi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q1.

Answer the following questions:


a) What is a Programming Language? Why do we need programming
languages?
Ans: A programming language is a formal language used to write instructions that a
computer can understand and execute. These instructions (called programs) tell the
computer what to do, how to do it, and when to do it.
Examples of programming languages include:

 Python
 C++
 Java
 JavaScript

Each language has its own syntax (rules) and structure, but all are used to
communicate with computers.

b) Differentiate between Compiler, Interpreter and linker.

Compiler Interpriter Linker


Translates whole program Translates line by line Combines compiled files
at once
Generates executable file Does not executable file Final executable
Errors shown after full Stops at first error and Errors in linking stage
compilation shows immediately
Execution is faster execution is slower Not for execution
Used before execution Used during execution Use after execution
Example: C, C++ Example: Python, Connects code pieces
JavaScript

c) Define the following terms with examples:


• Variable
• Data Type

Ans. Variable:
A variable is a named storage location in memory used to store data that can change
during program execution.

Example

x = 10
name = "Ali"

Here, x and name are variables storing values.

Data Type:

A data type defines the type of data a variable can store, such as numbers, text, etc.

Examples:

x = 10
y = 3.14
name = "Ali"
is_true = True
d) What is an Algorithm? Write an algorithm to calculate the area of a rectangle.

Ans. An algorithm is a step-by-step procedure or set of instructions used to solve a


problem or perform a task.

Algorithm to Calculate the Area of a Rectangle:

Step 1: Start
Step 2: Input length (L)
Step 3: Input width (W)
Step 4: Calculate area = L × W
Step 5: Display the area
Step 6: Stop

Example
If:
L = 5, W = 3
Area = 5 × 3 = 15

Q2. Explain conditional statements in your own words.


Then write the syntax and one example each of:
• if
• if-else
• else-if ladder
• Nested if
Also mention where each is used in real-life decision making

Ans. Conditional statements are used to make decisions in a program.


They check a condition (true or false) and execute different parts of code based on the
result.

1. if Statement

Syntax:

if condition:
statement

Example:

age = 18
if age >= 18:
print("You can vote")

Real-Life Use:

 If it is raining → take an umbrella

2. if-else Statement

Syntax:
if condition:
statement1
else:
statement2

Example:

num = 5
if num % 2 == 0:
print("Even")
else:
print("Odd")

Real-Life Use:

 If you pass → celebrate


 Else → study again

3. else-if Ladder (elif)

Syntax:

if condition1:
statement1
elif condition2:
statement2
elif condition3:
statement3
else:
statement

Example:

marks = 75
if marks >= 90:
print("A grade")
elif marks >= 70:
print("B grade")
elif marks >= 50:
print("C grade")
else:
print("Fail")

Real-Life Use:

 Grading system (A, B, C, Fail)

4. Nested if

Syntax:

if condition1:
if condition2:
statement

Example:

age = 20
if age >= 18:
if age <= 60:
print("Eligible adult")

Real-Life Use:

 If you have a ticket


→ then check seat number

Q3. Given the following conditions:


A student’s result is decided as:
• Marks ≥ 90 → Grade A
• 75–89 → Grade B
• 50–74 → Grade C
• Below 50 → Fail
Tasks:
1. Write the conditional logic (pseudo code or program structure).
2. Find the output for:
o Marks = 92
o Marks = 76
o Marks = 48
3. Explain why only one condition executes.

1. Conditional Logic (Pseudo Code)

Start
Input marks
If marks >= 90
Print "Grade A"
Else if marks >= 75
Print "Grade B"
Else if marks >= 50
Print "Grade C"
Else
Print "Fail"
Stop
2. Outputs:
Q3. Why Only One Condition Executes?

Only one condition executes because the else-if ladder works from top to bottom:

 As soon as one condition becomes true, the program executes that block
 Then it skips all remaining conditions

Example:
If marks = 92

 First condition (≥ 90) is true → Grade A is printed


 The rest are ignored

Q4. Write an algorithm and draw a flowchart to:


Check whether a person is eligible for a scholarship:
• Marks ≥ 80 AND Attendance ≥ 75%
If eligible → Print “Scholarship Approved”
Else → Print “Not Eligible”
Also mention the Boolean expression used

Ans. Algorithm:
Step 1: Start
Step 2: Input Marks
Step 3: Input Attendance
Step 4: Check condition:
If (Marks ≥ 80 AND Attendance ≥ 75)
Print "Scholarship Approved"
Else
Print "Not Eligible"
Step 5: Stop

Boolean Expression Used:

(Marks ≥ 80) AND (Attendance ≥ 75

Flowchart (Text Form)

Start
|
v
Input Marks, Attendance
|
v
┌─────────────────────────────┐
│ Marks ≥ 80 AND Attendance ≥ 75? │
└──────────────┬──────────────┘
|
Yes | No
| | |
v | v
"Scholarship | "Not Eligible"
Approved" |
\ /
v v
Stop
Q4. A number is given. Write the conditional logic to:
• Check if the number is positive, negative, or zero
• If positive, also check whether it is even or odd
Then answer:
• Which type of conditional structure is used?
• Why is nested decision-making needed here.

1. Type of Conditional Structure Used

 Nested if (an if inside another if)


 Also uses an if-else ladder to check positive, negative, or zero

2. Why Nested Decision-Making is Needed

 Because checking even or odd only makes sense if the number is positive
 We need a condition inside another condition to handle this dependent
decision

You might also like