Assignment of Numan Jalal of Programming of Section d
Assignment of Numan Jalal of Programming of Section d
DEPARTMENT OF ENGLISH
Question:
Programming Concepts Exam
Q1.
i. Explain the differences between for and while loops. Provide a simple syntax example for each.
}
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;
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.
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:
3. Real-World Example:
Storing the daily temperature of a week:
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:
Example: matrix[0][1] = 5;
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}.