CWhile.md - brainchildservices/curriculum GitHub Wiki
SLIDE-1
C# While Loop
The while statement executes a statement or a block of statements while a specified Boolean expression evaluates to true
SLIDE-2
When using the while loop, initialization should be done before the loop starts, and increment or decrement steps should be inside the loop. The condition should be made such that it never goes into an infinite loop.
SLIDE-3
Example-1 (https://dotnetfiddle.net/zQzzyd)
using System;
public class Program
{
public static void Main()
{
int i = 0; //Initialization
while (i < 10) //(i<10) is the condition
{
Console.WriteLine(i);
i++; //Incrementing the i value
}
}
}
SLIDE-3(DOWNWARDS)
Example-2 (https://dotnetfiddle.net/ITB0As)
using System;
public class Program
{
public static void Main()
{
int i=1;
int sum=0;
while(i<=10)
{
Console.WriteLine(i);
sum=sum+i;
i++;
}
Console.WriteLine(sum);
}
}
SLIDE-4
C# - do while Loop
The do while loop is the same as while loop except that it executes the code block at least once.
SLIDE-5
SLIDE-6
Example-1 (https://dotnetfiddle.net/6SiXfu)
using System;
public class Program
{
public static void Main()
{
int i = 0; //Initialization
do
{
Console.WriteLine(i);
i++; //Incrementing the i value
}
while (i < 0); //(i<0) is the condition
}
}
SLIDE-7
Simple Exercises :
- Write a program in C# Sharp to display the first 100 natural numbers.
Expected Output : 1 2 3 4 5 6 7 8 9 10 . .. . . 99 100 - Write a program in C# Sharp to display the even numbers in the first 100 natural numbers.
Expected Output : 2 4 6 8 10 ... ..... 98 100 - Write a program in C# Sharp to display the even numbers in the first 100 natural numbers.
Expected Output : 1 3 4 6 7 ........97 99 - Write a program in C# Sharp to display Hello World 25 times.
- Write a program in C# Sharp to display numbers from 100 to 1
Expected Output : 100 99 98 .. .......2 1
SLIDE-7(downwards)
- Write a program in C# Sharp to display even numbers from 100 to 1
Expected Output : 100 98 .. .......4 2 - Write a program in C# Sharp to display odd numbers from 100 to 1
Expected Output : 99 97 95 .. .......3 1 - Write a program in C# Sharp to display numbers from 25 to 50
Expected Output : 25 26 27 ....... 49 50 - Write a program in C# Sharp to display numbers multiples of 3 between 25 and 50
Expected Output : 27 30 33 .... 45 48 - Write a program in C# Sharp to display numbers multiples of 5 from 200 to 120
Expected Output : 200 195 190 ....... 125 120
SLIDE-8
Exercises:
Q1. Write a program in C# Sharp to display the first 10 natural numbers.
Expected Output :
1 2 3 4 5 6 7 8 9 10
Q2. Write a C# Sharp program to find the product of 10 natural numbers starting from 20.
Expected Output :
The product is : 1168737792
Q3: Write a program to read 5 numbers from keyboard (use while loop) and find their sum and average.
Test Data :
Input the 10 numbers :
Number-1 :2
Number-2 :2
Number-3 :2
Number-4 :2
Number-5 :2
Sum of numbers entered is 10
Average of numbers entered is 2
SLIDE-9
Q4: Write a program to show the multiplication table of a given integer.
Test Data :
Input the number (Table to be calculated) : 12
Expected Output :
12 X 1 = 12
12 X 2 = 24
12 X 3 = 36
...
...
12 X 10 = 120
Q5.1: Write a program to print a pattern like one below using asterisk *
Expected output:
Q5.2 Write a program to print a right angled triangle like one below using asterisk *
SLIDE-10
Q6: Write a program to print a right angled triangle like one below using asterisk *
Q7. Write a program to print following pattern.
1
22
333
4444
Q8. Write a program to print following pattern.
1
2 3
4 5 6
7 8 9 10
SLIDE-11
Q9. Write a C# Sharp program to calculate the factorial of a given number.
Test Data :
Input the number : 5
Expected Output :
The Factorial of 5 is: 120
Hint: 5! means 1x2x3x4x5
Q10. Write a C# program to take an input number from the user. Print odd numbers from 1 to the number entered by the user.
Test Data :
Input the number : 9
Expected Output :
The odd numbers upto 9 are: 1 3 5 7 9