while loop tutorial – Part 4. (You can view Part 3 here.)
Several simple programs using while loop to display numbers are given here.








Some programs using while loop in C to display numbers:
Q.1. Display numbers from 1 to 6. Using while loop.
Answer.
//Program to print numbers from 1 to 6 using while loop. <stdio.h> //needed for the printf statement int main() {int i = 1; //start printing from this number
while (i <= 6) //print till this number { printf("%d \n", i); //print this number i++; //increment i by 1. }printf("Thank you! \n"); //we are now outside the loop. return 0;
}output: 123456Thank you!
Explanation-
We need to print all numbers from 1 to 6. We start the while loop at 1. (That is, loop counter i is 1.) In each iteration of the while loop, we print the current value of i, and we increment i by 1. We continue this until i reaches 6. Once i goes beyond 6, we exit the loop.
Q.2. Display odd numbers from 1 to 6 using while loop.
Answer.
//Program to print odd numbers from 1 to 6 using while loop. <stdio.h>int main(){ int i = 1; //start printing from this number while (i <= 6) //need to print numbers till 6 { printf("%d \n", i); //print the number i += 2; //increment by 2 since we only want to //print odd numbers (skip even numbers) } printf("Thank you! \n"); //this is outside the loop return 0;}Output: 135Thank you!
Explanation:
– we need to print odd numbers from 1 to 6. We start the loop at 1 (that is, the value of the loop counter i is 1.) In each iteration of the loop, we print the value of i, and then we increment i by 2. (To get to the next odd number, we need to add 2 to the present number. Hence, we increment i by 2.) We continue this till i is <= 6. Once i goes beyond 6, we exit the loop.
Q.3. Same as Question 2
Answer.
Here, we use a different logic to solve the same problem.
//Program to print odd numbers from 1 to 6 using while loop.//This is different from the above program//Here, we iterate through all values of i, and check each time//whether the number is odd or not.//if it's odd, we print it. <stdio.h>int main(){ int i = 1; //start printing from this number while (i <= 6) //need to print numbers till 6 { if (i % 2 == 1) //check if i is odd (that is, //check the remainder on division by 2) printf("%d \n", i); //print the number i++; //increment i by 1 } printf("Thank you! \n"); //this is outside the loop return 0;}Output: 135Thank you!
Explanation:
– We need to print odd numbers from 1 to 6.
– How to check if a number is odd or even? See what is the remainder when the number is divided by 2. If the number is even, remainder on division by 2 is 0. If the number is odd, the remainder on division by 2 is 1. (Example, 10 is an even number, and 10, on being divided by 2, gives 0 as remainder. 11 is an odd number. When 11 is divided by 2, the remainder is 1.)
– % – the remainder or module operator in C gives the remainder when one number is divided by another.
– We need to print from 1. Hence we start the loop counter i as 1. Each time we are inside the loop, we check whether i is odd. If i is odd, we print it. Else, we simply move on to the next value of i (we increment i by 1). We continue this until i reaches 6. When i goes beyond 6, we exit the loop.
Q.4. Again, same as Question 2.
Answer.
This time we use yet another method to solve the same problem.
//Program to print odd numbers from 1 to 6 using while loop.//This is different from the above program//Here, we iterate through all values of i, and check each time//whether the number is odd or not.//if it's even, we simply skip that iteration, using continue//for other values of i, we print it. <stdio.h>int main(){ int i = 1; //start printing from this number while (i <= 6) //need to print numbers till 6 { if (i % 2 == 0) //check if i is even (that is, //check the remainder on division by 2) { i++; //increment i continue; //skip this iteration. No need to print even number. } printf("%d \n", i); //print the number. (This number is necessarily odd.) i++; //increment i by 1 } printf("Thank you! \n"); //this is outside the loop return 0;}
output: 135Thank you!
Explanation:
We use the continue statement in this program. When continue is encountered in any iteration, the remaining statements in that iteration (below the continue statement) are skipped, and the computer goes on to the next iteration.
Hence, here whenever we encounter an even number, we use the continue statement (to skip the task of printing it).
If the number is odd, we print it.
Q.5. Write a program to display all multiples of 3 in the range from 4 to 20. Use while loop.
Answer.
//Program to display multiples of 3 from 4 to 20. Using while loop. <stdio.h>int main(){ int i = 6; //start printing from this; this is the //1st multiple of 3 in the interval from 4 to 20. while (i <= 20) //need to print numbers till 20 { printf("%d \n", i); //print the number. i += 3; //increment i by 3; go to the next multiple of 3. } printf("Thank you! \n"); //this is outside the loop return 0;}output: 69121518Thank you!
Explanation:
– We need to print multiples of 3 (that is, numbers which are divisible by 3) starting from 4 till 20.
– In this range, the 1st number which is a multiple of 3 – is 6. (Neither 4, nor 5, are multiples of 3.)
– So we start our loop with the loop counter i as 6.
– In each iteration of the loop, we print the value of i.
– Then, we need to get to the next multiple of 3. To do that, we need to increment the value of i by 3. Therefore, in each iteration, we increment the value of i by 3.
– At the end of the iteration, we go back to the while loop condition, and check that condition. We need to print till 20. Hence, we’ve put the condition as i <= 20. Once i goes beyond 20, we need not print it; we exit the loop.
Q.6. Same as Question 5.
Answer.
We solve the same problem in a different way here.
// Program to display multiples of 3 from 4 to 20. Using while loop. <stdio.h>int main(){ int i = 4; //start printing from this while (i <= 20) //need to print numbers till 20 { //We'll only print multiples of 3. if (i % 3 == 0) //if the number is a multiple of 3. //(check remainder on division by 3) printf("%d \n", i); //print the number. i++; //increment i by 1. } printf("Thank you! \n"); //this is outside the loop return 0;}output: 69121518Thank you!
Explanation:
We need to print multiples of 3 from 4 to 20. Therefore, we start the while loop with the loop counter i as 4.
In each iteration of the loop, we’ll check if the number is a multiple of 3 or not. (we do this using the modulo operator.)
If the number is a multiple of 3, we print it. (If the number is not a multiple of 3, we don’t print it.)
We increment the value of i by 1. In this manner, we continue until i reaches 20. (Each time, if i turns out to be a multiple of 3, we print it.)
Once i crosses 20, we exit the loop.
Q.7. Write a program to display numbers from 4 to 20, which are not multiples of 3. Use while loop.
Answer.
The program here is very similar to the answer to Q.6. The difference is that in each iteration, we print the number if it is not divisible by 3. (In A.6., we were printing the number if it was divisible by 3.)
// Program to display numbers from 4 to 20, which are not divisible by 3.// Using while loop. <stdio.h>int main(){ int i = 4; //start printing from this while (i <= 20) //need to print numbers till 20 { //We'll print a number only if it is not divisible by 3. if ((i % 3 == 1) || (i % 3 == 2)) //if the number is not divisible by 3 //(check remainder on division by 3) printf("%d \n", i); //print the number. i++; //increment i by 1. } printf("Thank you! \n"); //this is outside the loop return 0;}output: 45781011131416171920Thank you!
Explanation:
We need to print a number only if it is not divisible by 3.
Using the modulo operator (%), we check what is the remainder when the number is divided by 3. If the remainder is 1, or if the remainder is 2, it means that the number is not divisible by 3. (The possible remainders when a number is divided by 3 are 0, 1 and 2. If the remainder is 0, it means that the number is divisible by 3. The other remainders indicate that the number is not divisible by 3.)
If the number is not divisible by 3, we print it.
Leave a comment