SEC-S20W5 Iteration in Programming: For, While, and Do-While Loops

in Healthy Steemlast month (edited)

White Simple Programming Course Video_20241014_063710_0000.png

Designed in Canva

Homework:

Q1. What are loops in programming for? Which type of loop did you like better?

Loops in programming are used to repeat a set of instructions multiple times without having to rewrite the samee code repeatedly. So they are useful when performing tasks such as counting, processing lists, or repeating tasks until a condition is met.

As for the type of loop I prefer, I like the for loop because It’s structured and predictable, make it ideal when we know the number of repetitions in advance. I also appreciate the flexibility of the while loop in my code , especially when the number of iterations depends on changing conditions.


Q2. In problem number 8, the condition for determining whether the houses are on the same street should have been written as if(n % 2 == m % 2) instead of if(n % 2 == 0 && m % 2 == 0 || n % 2 == 1 && m % 2 == 1). Explain both conditions in words. Which is better?


Condition 1: n % 2 == m % 2

So this condition checks if both numbers, n and m, are either even or odd. If both have the same remainder when divided by 2, they are of the same type (they can bee either both even or both odd).

Condition 2: n % 2 == 0 && m % 2 == 0 || n % 2 == 1 && m % 2 == 1

This is a longer way of saying the same thing. It checks two possibilities:
Both n and m are even (i.e., when divided by 2, both have a remainder of 0).
Both n and m are odd (i.e., when divided by 2, both have a remainder of 1.

Which is better?
The first condition (n % 2 == m % 2) is better because:

• It’s shorter and simpler.
• It directly expresses the intent: “Check if both numbers are of the same type (either both even or both odd).”
• The second condition is longer and repetitive, even though it does the same thing.


Q3. Output 33 times the number 33 with each kind of loop?

1. For Loop:


for(int i = 0; i < 33; i++) {
    std::cout << 33 << std::endl;
}

2. While Loop:


int i = 0;
while(i < 33) {
    std::cout << 33 << std::endl;
    i++;
}

3. Do-While Loop:


int i = 0;
do {
    std::cout << 33 << std::endl;
    i++;
} while(i < 33);

Q4. Output the numbers from 11 to 111 but skip the number 55, just in one cycle?


for(int i = 11; i <= 111; i += 10) {
    if(i != 55) {
        std::cout << i << std::endl;
    }
}

So the loop starts at 11 and increments by 10 each time. The condition if(i != 55) ensures that the number 55 is skipped.


Q5. Output the multiplication table of a random number?


#include <iostream>
#include <cstdlib>
#include <ctime>

int main() {
    srand(time(0)); // Initialize random seed
    int randomNumber = rand() % 10 + 1;  // Generate random number between 1 and 10

    std::cout << "Multiplication Table for " << randomNumber << ":\n";

    for (int i = 1; i <= 10; i++) {
        std::cout << randomNumber << " x " << i << " = " << randomNumber * i << std::endl;
    }

    return 0;
}


Q6. Output the squares of the numbers 1, 4, 9, 16,... up to 400?


#include <iostream>

int main() {
    for (int i = 1; i <= 20; i++) {
        std::cout << i * i << std::endl;  // Print the square of each number
    }
    return 0;
}


The loop runs from 1 to 20, and in each iteration, the square of the number is printed.


Q7. Derive the powers of some numbers (1, 2, 4, 8, 16, 32,... less than 3000)?


#include <iostream>

int main() {
    int n = 1;  // Start with 2^0 = 1
    while (n < 3000) {
        std::cout << n << std::endl;
        n *= 2;  // Multiply by 2 to get the next power
    }
    return 0;
}

The loop starts at 1 and keeps multiplying n by 2 until it is greater than or equal to 3000. Each power is printed before doubling the value.


Q8. How many even four-digit numbers are not divisible by 3?

So there are 4500 even four-digit numbers (from 1000 to 9998).

Among these, 1500 are divisible by 3 (specifically divisible by 6).

To find how many are not divisible by 3, we subtract:

4500 - 1500 = 3000

So there are 3000 even four-digit numbers that are not divisible by 3.


Thank you mam for teaching us about Loops. I tried my best to attempt this task. For that I consult with my teacher in University and my friends. I learned it and then I solve my task. In the last, I invite my friends @suboohi, @faran-nabeel and @neelofar. Thank you!

Best Regards,
Aalia Rubab

Sort:  
Loading...
 last month 

Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.

replace the image for your post, I don't approve of using an image from mine post

 last month (edited)

I have replaced the image. ✅
Thank you.

regarding the first question 95% - Probability AI generated

image.png

the submitted code should be provided in full, and its implementation should be demonstrated with screenshots

 last month (edited)

Hi my dear, if you check with other tool it will show 100 percent Human.

Screenshot_2024_1014_062859.png

Screenshot_2024_1014_062849.png

the submitted code should be provided in full, and its implementation should be demonstrated with screenshots.

Ok next time I'll be add screenshots.

Thank you so much.