ABDUL WALI UNIVERSITY MARDAN
DEPARTMENT OF ENGLISH
Instructor Mr. Umer Tanveer Semest FA-24
er
Course ACI Progra BS –
Title m English
Assignme 4th Final s Marks 20
nt
Section: D 1st Semester Name: Numan Jalal
Question:
Programming Concepts Exam
Q1.
i. Explain the differences between for and while loops. Provide a simple syntax example for each.
Difference between for and while loop:
1) For loop:
Definition: A control structure that repeats a block of code in a fixed
number of times.
Structure: Combines a condition, and increment in a single line.
Usage: Best suited for tasks with a known iteration count.
Syntax: for( initialization; condition; increment/decrement) {// code to
execute
}
Example: for( int i= 1; i<=5; i++){
Cout<<i<<” “;
2) While loop:
Definition: A control structure that executes a block of code while a
condition is true.
Structure: Requires external initialization and increment statements.
Usage: Ideal for tasks with unknown or condition -based iteration counts.
Syntax: initialization;
While(condition){
// code to execute
Increment /decrement;
Example: int i=2;
While(i<=10){
Cout<<i<<” “; i+=2;
}Write a program using a for loop to print the multiplication table of 12 in descending order, starting
from
12 × 10 to 12 × 1.
Q2.
i. Why and when do we use nested if conditions? Write a program that checks if a number is divisible
by both 3and 5 using nested if statement.
Why use nested-if statements:
• Nested if- else is useful when multiple related conditions must be checked.
• It reduces repetition by allowing decisions to be grouped inside other
conditions.
• Helps in creating more precise decision -making structures.
• Simplifies situations where multiple independent conditions lead to
different outcomes.
• It is often used for error handling, complex validation, or decision -making.
Program:
ii. Write a program to check whether a number is positive, negative, or zero using if-else
statements.
Q3.
Write a program that displays the sum of numbers from 1 to 20 using do while loop.
Write a program that checks the sum of first five numbers using while loop.
Q4. What is the difference between 1D and 2 D array in terms of declaration and usage. Provide an
example from real world
1D Array:
1. Declaration:
A 1D array is declared as: Datatype
array name[size];
Example:
Int scores[5]; // Array to store 5 integer values
2. Usage:
Example: scores[0] = 90;
Typically used for lists or linear data, such as storing scores, names, or IDs.
3. Real-World Example:
Storing the daily temperature of a week:
Float temperatures[7] = {30.5, 32.0, 31.8, 29.4, 28.0, 27.5, 30.0};
2D Array:
1. Declaration:
A 2D array is declared as: Datatype
array name[rows][columns];
Example:
Int matrix[3][4]; // 3 rows and 4 columns
2. Usage:
Access elements using two indices: matrix[row][column].
Example: matrix[0][1] = 5;
Used to represent tabular data, grids, or matrices.
3. Real-World Example:
Representing a seating chart in a cinema:
Int seatingChart[5][10] = {
{1, 0, 1, 1, 0, 0, 1, 1, 1, 0},
{1, 1, 1, 0, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 1, 1, 1, 0, 1, 1, 0},
{1, 1, 0, 0, 1, 1, 1, 1, 0, 0},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1}
}; ii. Write a program to calculate the average of five numbers stored in a 1D array.
Q5.
i. Write a program to create and display a 2D array with the following values:
10 20 30
40 50 60
70 80 90
ii. Write a program that sums element in a 1D array containing the elements {3, 8, 1, 7, 4}.