Print the numbers upto limit using using While Loop in C
This is a C program that prompts the user to enter two integers, 's' and 'e', and then uses a while loop to print all the integers between 's' and 'e' (inclusive).
- The #include<stdio.h> is a preprocessor directive that includes the contents of the standard input-output library in the program. The int main() function is the starting point of the program execution. Inside the main function, two integers s and e are declared. printf() is used to display the prompts for the user to enter the starting and ending values, and scanf() is used to get the input and store it in the variables 's' and 'e' respectively.
- A while loop is used to execute the code inside the loop as long as the condition s<=e is true. The first time the code is executed, the current value of 's' is printed using printf("\n%d",s). Then the value of 's' is incremented by 1. Then the condition s<=e is checked, if it's true the control jumps back to the start of the loop and the same process is repeated until the condition is false.
- Finally, the program returns 0 to indicate the successful execution of the program.
While loops are a control structure that allows you to repeat a block of code as long as a certain condition is true. They are a more structured and readable alternative to using goto statement.
Source Code
#include<stdio.h>
int main()
{
int s,e;
printf("\nEnter the starting value:");
scanf("%d",&s);
printf("\nEnter the ending value:");
scanf("%d",&e);
while(s<=e)
{
printf("\n%d",s);
s++;
}
return 0;
}
To download raw file
Click Here
Output
Enter the starting value:1
Enter the ending value:15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15