C Program To Print All The Numbers In a Given Range

Introduction

In this post, I am going to explain a c program to print all the numbers in a given range using all loops. We will read a range i.e. first value and the second value and then print all the numbers between these values. See the following expected output.

Also Read: C Program to Display Numbers From 1 to n Except 6 and 9

Expected Output

Output 1
Enter the range
1
10
1 2 3 4 5 6 7 8 9 10

Output 2
Enter the range
33
40
33 34 35 36 37 38 39 40

C Program To Print All The Numbers In a Given Range using for loop

In this program, we are using for loop.

#include<stdio.h>
int main()
{
   int fvalue, lvalue, i;
   printf("Enter the range\n");
   scanf("%d%d",&fvalue,&lvalue);
   for(i=fvalue;i<=lvalue;i++)
   {
     printf("%d ",i);
   }
   return 0;
}

Detailed Explanation

In this section, I am going to explain this program in detail line by line.

int fvalue, lvalue, i;

Here, we have declared three variables of type integer. We will store the first number in the variable fvalue and the last number in the variable lvalue. The variable i is the loop variable.

Also Read: The while loop in C Programming

printf(“Enter the range\n”);

This statement displays the message on the console. The message is to inform the user to enter the range i.e. first value and the last value.

scanf(“%d%d”,&fvalue,&lvalue);

This statement is reading first and the last number i.e. the range from the console and storing that values in the variable fvalue and lvalue respectively.

for(i=fvalue;i<=lvalue;i++)

This is the for loop. Here, i = fvalue means we are assigning the fvalue to the variable i. Next, we are comparing the value of i with the variable lvalue and if this condition is true, then we will execute the body of the for loop. The i++ means increment the value of i.

printf(“%d “,i);

This is the body of for loop. There is only single statement in this loop which we have to repeated until the value of i remains less than or equal to lvalue. Because lvalue is the last value of the given range.

Also Read: C Program to Print Multiples of 5 using do while loop

This is the small explanation for the c program to print all the numbers. Here, we have used the for loop.

In the next two sections, we have written the same program using while and do-while loop. All the explanation remains the same. Only syntax of while loop and do-while loop is changed.

C Program To Print All The Numbers In a Given Range using while loop

In this program, we are using while loop.

#include<stdio.h>
int main()
{
   int fvalue, lvalue, i;
   printf("Enter the range\n");
   scanf("%d%d",&fvalue,&lvalue);
   i=fvalue;
   while(i<=lvalue)
   {
     printf("%d ",i);
     i++;
   }
   return 0;
}

C Program To Print All The Numbers In a Given Range using do while loop

In this program, we are using do while loop.

#include<stdio.h>
int main()
{
   int fvalue, lvalue, i;
   printf("Enter the range\n");
   scanf("%d%d",&fvalue,&lvalue);
   i=fvalue;
   {
     printf("%d ",i);
     i++;
   }
   while(i<=lvalue;)
   return 0;
}

As you can see, there are three c programs which are printing all values in a given range.

Thank you for reading. If you have any queries or feedback, please feel free to contact me.

FAQs

How to write a C Program to Print numbers from 1 to 100?

#include<stdio.h>
int main()
{
int i;
for(i=1;i<=100;i++)
{
printf(“%d “,i);
}
return 0;
}

C Program to Print numbers from 10 to 1 using for loop

#include<stdio.h>
int main()
{
int i;
for(i=10; i>=1; i–)
{
printf(“%d “,i);
}
return 0;
}

C program to print all numbers between a and b using while loop

#include<stdio.h>
int main()
{
int a, b, i;
printf(“Enter the range\n”);
scanf(“%d%d”,&a, &b);
i=a;
while(i<=b)
{
printf(“%d “,i);
i++;
}
return 0;
}

How do you print even numbers in a while loop in a given range?

#include<stdio.h>
int main()
{
int a, b, i;
printf(“Enter the range\n”);
scanf(“%d%d”,&a, &b);
i=a;
while(i<=b)
{
if((i%2)==0)
printf(“%d “,i);
i++;
}
return 0;
}

How do you print odd numbers from a while loop in a given range?

#include<stdio.h>
int main()
{
int a, b, i;
printf(“Enter the range\n”);
scanf(“%d%d”,&a, &b);
i=a;
while(i<=b)
{
if((i%2)==1)
printf(“%d “,i);
i++;
}
return 0;
}

Leave a Comment