Simple programs using while loop in C. See Part 1 on while loops here.


Q.1. Write a C program to print Good Morning three times.
Solution.
// This program displays Good Morning 3 times on the screen
#include <stdio.h>
int main()
{
int i = 1; //set initial value of the loop counter
while (i <= 3) //condition that we check each time before entering the loop
{
printf("Good Morning\n"); //display Good Morning once on the screen
i++; //increment the loop counter
}
printf("Thank you\n");
return 0;
}


Output:
Good Morning
Good Morning
Good Morning
Thank you

Notes:
– This is a very simple program to introduce while loop.
– i is the loop counter. It controls how many times the loop is executed.
– This loop is executed 3 times. That is, there are 3 iterations. (Each time the loop is run, it is known as one iteration.)
– In each iteration of the loop, Good Morning is printed once.
– There are 3 iterations of the loop in all. Hence, Good Morning gets printed thrice, which is what we want.



Q.2. Print Jai Sri Ram six times. Write a program to do this.
Solution.
// This program displays Jai Sri Ram six times.
#include <stdio.h>
int main()
{
int i = 1; //initialize
while (i <= 6) //condition to be checked
{
printf("Jai Sri Ram\n"); //do the repetitive task once
i++; // increment the loop counter
}
printf("Have a good day");
return 0;
}


Output :
Jai Sri Ram
Jai Sri Ram
Jai Sri Ram
Jai Sri Ram
Jai Sri Ram
Jai Sri Ram
Have a good day

Notes:
– This program is very similar to the previous program.
– Our task is to print Jai Sri Ram six times.
– Printing Jai Sri Ram is very simple. We just need one printf statement.
– Since we need to print it 6 times, we’ll put that printf statement within a while loop, and make the loop run 6 times.
– We use the loop counter i to make sure that the loop runs exactly 6 times.
– i++; increments the value of the variable i by 1.


Q.3. What is the output of the following program –
#include <stdio.h>
int main()
{
int i = 1; //initialize
while (i <= 4) //condition to be checked
{
printf("Jai Sri Ram\n"); //do the repetitive task once
i++; // increment the loop counter
}
printf("Have a good day");
return 0;
}


Solution.
The output is the following:
Jai Sri Ram
Jai Sri Ram
Jai Sri Ram
Jai Sri Ram
Have a good day

Notes:
– The loop counter i is set to 1 initially. Each time inside the loop, i is incremented by 1. The loop is executed until i is <= 4. Hence, the loop executes 4 times.
– In each iteration of the loop, Jai Sri Ram is printed once.
– Hence, in all, Jai Sri Ram is printed 4 times.
– After the loop terminates, “Have a good day” is printed once.


Q.4. What is the output of the following program –
#include <stdio.h>
int main()
{
int i = 1; //initialize
while (i <= 4) //condition to be checked
{
printf("Jai Sri Ram\n");
printf("Jai Sri Ram\n");
i++; // increment the loop counter
}
printf("Have a good day");
return 0;
}


Answer.
Following is the output:
Jai Sri Ram
Jai Sri Ram
Jai Sri Ram
Jai Sri Ram
Jai Sri Ram
Jai Sri Ram
Jai Sri Ram
Jai Sri Ram
Have a good day

Notes:
– This is very similar to the previous program.
– As in the previous program, the loop runs 4 times. (The value of i goes from 1 to 4.)
– Each time that the loop runs, there are 2 printf statements – that is, Jai Sri Ram is printed twice in each iteration of the while loop.
– There are 4 iterations in all. Hence, Jai Sri Ram is printed 8 times.

Posted in

2 responses to “while loop tutorial – Part 2. C Programming Lessons”

  1. […] while loop tutorial – Part 2. C Programming Lessons […]

    Like

  2. […] Introducing while loop in C. Simple program with detailed explanation. (Similar post on while loop is here.) (For Part 2 of this tutorial, see here.) […]

    Like

Leave a comment

Is this your new site? Log in to activate admin features and dismiss this message
Log In