Loops Flashcards
Print all numbers from 1-100 using each type of loop
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);
Print all even numbers from 1-100
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);
Print all odd numbers from 1-100
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);
Print the sum of all numbers from 1-100
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);
Print the sum of even numbers from 1-100
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}”);
Print the sum of odd numbers from 1-100
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}”);
Print 1-100 in descending order
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);
Print this:
****
****
**
**
*
for(int i=5;i>=1;i–){
for(int j=1;j<=i;j++){
Console.Write(“*”);
}
Console.WriteLine();
}
Print this:
*
**
**
**
**
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
Console.Write(“*”);
}
Console.WriteLine();
}
Print an array
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);
Print a diamond
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(); }