Loops Flashcards

1
Q

Print all numbers from 1-100 using each type of loop

A

for(int i=1;i<=100;i++){
Console.WriteLine(i);
}

int i=1;
while(i<=100){
Console.WriteLine(i);
i++;
}

int i=1;
do{
Console.WriteLine(i);
i++;
} while(i<=100);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Print all even numbers from 1-100

A

for(int i=2; i<=100;i=i+2){
Console.WriteLine(i);
}

int i=2;
while(i<=100){
Console.WriteLine(i);
i=i+2;
}

int i=2;
do{
Console.WriteLine(i);
i=i+2;
} while(i<=100);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Print all odd numbers from 1-100

A

for(int i=1;i<=100;i=i+2){
Console.WriteLine(i);
}

int i=1;
while(i<=100){
Console.WriteLine(i);
i=i+2;
}

int i=1;
do{
Console.WriteLine(i);
i=i+2;
} while(i<=100);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Print the sum of all numbers from 1-100

A

int sum=0;
for(int i=1;i<=100;i++){
sum+=i;
}
Console.WriteLine(sum);

int i=1;
int sum=0;
while(i<=100){
sum+=i;
i++;
}
Console.WriteLine(sum);

int i=1;
int sum=0;
do{
sum+=i;
i++;
} while(i<=100);
Console.WriteLine(sum);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Print the sum of even numbers from 1-100

A

int sum = 0;
for(int i=2; i<=100; i=i+2) {
sum+=i;
}
Console.WriteLine($”{sum}”);

int i=2;
int sum=0;
while(i<=100) {
sum+=i;
i=i+2;
}
Console.WriteLine($”{sum}”);

int i = 2;
int sum = 0;
do{
sum+=i;
i=i+2;
} while(i<=100);
Console.WriteLine($”{sum}”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Print the sum of odd numbers from 1-100

A

int sum = 0;
for(int i=1; i<=100; i=i+2) {
sum+=i;
}
Console.WriteLine($”{sum}”);

int i=1;
int sum=0;
while(i<=100) {
sum+=i;
i=i+2;
}
Console.WriteLine($”{sum}”);

int i = 1;
int sum = 0;
do{
sum+=i;
i=i+2;
} while(i<=100);
Console.WriteLine($”{sum}”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Print 1-100 in descending order

A

for(int i=100;i>=1;i–){
Console.WriteLine(i);
}

int i = 100;
while(i>=1){
Console.WriteLine(i);
i–;
}

int i = 100;
do{
Console.WriteLine(i);
i–;
} while(i>=1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Print this:
****
****
**

**
*

A

for(int i=5;i>=1;i–){
for(int j=1;j<=i;j++){
Console.Write(“*”);
}
Console.WriteLine();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Print this:
*
**
**
**
**

A

for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
Console.Write(“*”);
}
Console.WriteLine();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Print an array

A

int[] numbers = new int[2];
numbers[0] = 100;
numbers[1] = 200;

for(int i=0;i<numbers.Length;i++){
Console.WriteLine(numbers[i]);
}

int i=0;
while(i<numbers.Length){
Console.WriteLine(numbers[i]);
i++;
}

int i=0;
do{
Console.WriteLine(numbers[i]);
i++;
} while(i<numbers.Length);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Print a diamond

A

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

    Console.Write(" ");

    for(int k = 1; k <= 2 * i - 1; k++)
    
        Console.Write("*");

        Console.WriteLine();
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly