0% found this document useful (0 votes)
2 views12 pages

Assignment 2 - Full Code Explanations (Program by Program)

The document contains explanations for various C++ programs, including printing a multiplication table, using loops, calculating sums and averages, and finding the largest and smallest numbers in an array. Each program is accompanied by code snippets and detailed explanations of the logic and concepts used. Additionally, it covers important C++ concepts such as arrays, loops, and input/output operations.

Uploaded by

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

Assignment 2 - Full Code Explanations (Program by Program)

The document contains explanations for various C++ programs, including printing a multiplication table, using loops, calculating sums and averages, and finding the largest and smallest numbers in an array. Each program is accompanied by code snippets and detailed explanations of the logic and concepts used. Additionally, it covers important C++ concepts such as arrays, loops, and input/output operations.

Uploaded by

khizerjani47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

✅ Assignment 2 – Full Code Explanations (Program by

Program)

🔹 Program 1: Print Table of a Number


#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter a number to print table: ";
cin >> num;

for (int i = 1; i <= 10; i++) {


cout << num << " * " << i << " = " << num * i << endl;
}

return 0;
}

📘 Explanation:
●​ cin >> num;: User se ek number liya jiska table print karna hai.​

●​ for (int i = 1; i <= 10; i++): 1 se 10 tak loop chalega.​

●​ num * i: Har line pe number ka table print karega.​


🔄 Example: If num = 5, output:​
5 * 1 = 5​
5 * 2 = 10 ... up to 5 * 10 = 50​

🔹 Program 2: Use of Do-While Loop


#include <iostream>
using namespace std;
int main() {
int i = 1;

do {
cout << i << " ";
i++;
} while (i <= 10);

return 0;
}

📘 Explanation:
●​ do...while: Pehle ek baar body run hogi, baad mein condition check hogi.​

●​ i++: i ko 1 se 10 tak badhata hai.​

●​ Output: 1 2 3 4 5 6 7 8 9 10​

🔹 Program 3: Using While Loop


#include <iostream>
using namespace std;

int main() {
int i = 1;

while (i <= 10) {


cout << i << " ";
i++;
}

return 0;
}

📘 Explanation:
●​ while (i <= 10): Jab tak i 10 se chhota ya barabar hai, loop chalta rahega.​

●​ Same output as above but with while instead of do...while.​


🔹 Program 4: Sum of Array Elements
#include <iostream>
using namespace std;

int main() {
int arr[5], sum = 0;

cout << "Enter 5 numbers:\n";


for (int i = 0; i < 5; i++) {
cin >> arr[i];
sum += arr[i];
}

cout << "Sum = " << sum;


return 0;
}

📘 Explanation:
●​ int arr[5]: Array of 5 integers.​

●​ for loop input leta hai aur sum karta hai.​

●​ sum += arr[i]: Sum mein har number add ho raha hai.​

●​ Arrays: Ek tarika ek hi naam ke neeche multiple values rakhne ka.​

🔹 Program 5: Display Array Elements


#include <iostream>
using namespace std;

int main() {
int arr[5];

cout << "Enter 5 numbers:\n";


for (int i = 0; i < 5; i++) {
cin >> arr[i];
}

cout << "You entered:\n";


for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}

return 0;
}

📘 Explanation:
●​ Yeh program user se 5 values leta hai aur fir wohi values display karta hai.​

●​ Pehla for → Input,​


Doosra for → Output​

●​ Arrays se multiple data store karna easy hota hai.​

🔹 Program 6: Average of 5 Numbers


#include <iostream>
using namespace std;

int main() {
int arr[5], sum = 0;
float avg;

cout << "Enter 5 numbers:\n";


for (int i = 0; i < 5; i++) {
cin >> arr[i];
sum += arr[i];
}

avg = sum / 5.0;


cout << "Average = " << avg;
return 0;
}
📘 Explanation:
●​ sum / 5.0: 5.0 likhna zaroori hai taake decimal (float) result aaye.​

●​ Yeh average nikalta hai 5 values ka.​

🔹 Program 7: Find Largest in Array


#include <iostream>
using namespace std;

int main() {
int arr[5], largest;

cout << "Enter 5 numbers:\n";


for (int i = 0; i < 5; i++) {
cin >> arr[i];
}

largest = arr[0];
for (int i = 1; i < 5; i++) {
if (arr[i] > largest)
largest = arr[i];
}

cout << "Largest = " << largest;


return 0;
}

📘 Explanation:
●​ largest = arr[0]: Sabse pehle value ko assume kiya ke yeh largest hai.​

●​ Fir har element check hua agar koi aur bada ho to largest update ho gaya.​

🔹 Program 8: Find Smallest in Array


#include <iostream>
using namespace std;

int main() {
int arr[5], smallest;
cout << "Enter 5 numbers:\n";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}

smallest = arr[0];
for (int i = 1; i < 5; i++) {
if (arr[i] < smallest)
smallest = arr[i];
}

cout << "Smallest = " << smallest;


return 0;
}

📘 Explanation:
●​ Similar to largest, but condition is arr[i] < smallest​

●​ Yeh program sabse chhoti value array mein find karta hai.​

📚 Important Concepts (Definitions)


Concept Urdu + English Definition

main() Har C++ program ka start point hota hai.

if Agar koi shart (condition) poori ho to ye block run hota hai.

else Agar if fail ho jaye to ye block run hota hai.

for loop Repeat karne ke liye use hota hai (jaise counting).

while Jab tak condition true hai, tab tak chalta hai.
loop

do-whil Pehle ek baar chalata hai, baad mein check karta hai.
e

array[] Ek structure jisme multiple values store ki ja sakti hain (same type).

cin User input lene ke liye.


cout Output print karne ke liye.

% Modulus Remainder nikalta hai (Even/Odd check).

Agar aap chaho to main is ka PDF version ya printable form bhi bana ke de sakta hoon.

Would you like a final combined PDF of these explanations or notes?

Great! Let’s now go one by one and explain each program in Assignment 1 in Urdu +
English. I'll cover every line of code and concepts used like main(), if, array, loops,
etc.

✅ Assignment 1 – Full Code Explanations (Program by


Program)

🔹 Program 1: Salary Bonus Calculation


#include <iostream>
using namespace std;

int main() {
float salary, bonus;
cout << "Enter salary: ";
cin >> salary;

if (salary > 10000)


bonus = salary * 0.10;
else
bonus = salary * 0.05;

cout << "Bonus: " << bonus;


return 0;
}

📘 Explanation:
●​ #include <iostream>: Yeh header file input/output ke liye hoti hai (cin/cout).​

●​ using namespace std;: Taake cout, cin use kar sakein bina std:: ke.​
●​ int main(): Program yahin se start hota hai. main() har C++ program ka entry
point hota hai.​

●​ float salary, bonus;: Do variables declare kiye salary aur bonus ke liye (float
kyunki decimal ho sakta hai).​

●​ cin >> salary;: User se salary input li.​

●​ if (salary > 10000): Agar salary 10,000 se zyada ho to 10% bonus do.​

●​ else: Warna 5% bonus do.​

●​ cout << "Bonus:" << bonus;: Final bonus print karo.​

●​ return 0;: Program successful end.​

🔹 Program 2: Find Greatest of Three Numbers


#include <iostream>
using namespace std;

int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;

if (a > b && a > c)


cout << "A is greatest";
else if (b > a && b > c)
cout << "B is greatest";
else
cout << "C is greatest";

return 0;
}

📘 Explanation:
●​ int a, b, c;: Teen integers declare kiye.​

●​ cin >> a >> b >> c;: Teenon numbers input kiye.​


●​ if (a > b && a > c): Agar a dono se bara ho → A is greatest.​

●​ else if (b > a && b > c): Agar b dono se bara ho → B is greatest.​

●​ else: Warna c sabse bara hai.​

🔹 Program 3: Simple Calculator Using Switch


#include <iostream>
using namespace std;

int main() {
char op;
float num1, num2;

cout << "Enter operator (+, -, *, /): ";


cin >> op;

cout << "Enter two numbers: ";


cin >> num1 >> num2;

switch(op) {
case '+':
cout << "Result: " << num1 + num2;
break;
case '-':
cout << "Result: " << num1 - num2;
break;
case '*':
cout << "Result: " << num1 * num2;
break;
case '/':
cout << "Result: " << num1 / num2;
break;
default:
cout << "Invalid operator";
break;
}

return 0;
}

📘 Explanation:
●​ char op;: Operator store karega (+, -, *, /)​

●​ switch(op): Operator ke mutabiq different cases run honge.​

●​ break: Har case ke baad break zaroori hai warna next case bhi run ho jata.​

🔹 Program 4: Print 1 to 10 Using Loop


#include <iostream>
using namespace std;

int main() {
for (int i = 1; i <= 10; i++) {
cout << i << " ";
}
return 0;
}

📘 Explanation:
●​ for (int i = 1; i <= 10; i++): Loop jo 1 se 10 tak chalega.​

●​ cout << i: Har bar i print hoga.​

🔹 Program 5: Sum of First 10 Natural Numbers


#include <iostream>
using namespace std;

int main() {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum = sum + i;
}
cout << "Sum = " << sum;
return 0;
}

📘 Explanation:
●​ sum = sum + i: Har iteration me i add hota ja raha hai.​

●​ Output: 55 (1+2+3+...+10)​

🔹 Program 6: Factorial of a Number


#include <iostream>
using namespace std;

int main() {
int n, factorial = 1;
cout << "Enter a number: ";
cin >> n;

for (int i = 1; i <= n; i++) {


factorial *= i;
}

cout << "Factorial = " << factorial;


return 0;
}

📘 Explanation:
●​ factorial *= i means factorial = factorial * i​

●​ Factorial of 5 = 5×4×3×2×1 = 120​

🔹 Program 7: Check Even or Odd


#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter number: ";
cin >> num;

if (num % 2 == 0)
cout << "Even";
else
cout << "Odd";
return 0;
}

📘 Explanation:
●​ num % 2 == 0: Agar remainder 0 ho to even number hai.​

🔹 Program 8: Reverse Counting from 10 to 1


#include <iostream>
using namespace std;

int main() {
for (int i = 10; i >= 1; i--) {
cout << i << " ";
}
return 0;
}

📘 Explanation:
●​ Loop 10 se start ho kar 1 tak jata hai i-- se.​

You might also like