Generating a series of digits by using for loop in C and C++

Generating a series of digits by using for loop in C and C++,There are certain loops defined in the programming languages like While, do-while and for

Series of digits by using for loop

Loop Concept 

Basically a Loop is used to run a set of statements under certain pre-dominated conditions and it runs till the condition is true and eventually stops as the condition becomes false.
There are certain loops defined in the programming languages like While, do-while and for loop but here we gonna discuss about only for-loop.

The syntax for the for-loop is as listed below:

                  for (initialization; condition test; increment or decrement)
{
     //statements to come 
}
Below is the flow chart for a for-loop that shows how a for loop basically works in step by step manner this will help you to grab more and learn and understand easily.
Generating a series of digits by using for loop in C and C++


Basic example of code for generating a series of digits by using for loop is written below

we are taking one initial and final value to generate the series between those two number in our case let say a, b and incrementing it by only a value of one.
If you wish you can change the input values and increment by your needs like by 2 or 5.

Let’s write the required code for the problem :


Code in C++ :

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int a,b;
        cout<<"Enter value of 1st number :/n":
        cout<<"Enter value of 2nd number :/n":

	cin>>a>>b;
        cout<<"The output series of digits are :/n":
      
	for(int i=a; i<=b; i++)
	 {
	     cout<<i<<" ";
	 }
	return 0;
}

Sample input :
Enter value of 1st number : 1
Enter value of 1st number : 10

Sample output :
The output series of digits are :
1 2 3 4 5 6 7 8 9 10

Code in C :

#include<stdio.h>
int main()
{
 int a, b, i ;
printf("Enter value of 1st number : ");
printf("Enter value of 2nd number : ");
scanf("%d %d",&a,&b);
 printf("\n"); //for new line
printf("The output series of digits are :/n");

  /*  For loop
  (initialize; condition ; increment) */

 for(i=a; i<=b; i++)
 {
   printf(" %d",i);
 }
return 0;
}

Sample input :
Enter value of 1st number : 1
Enter value of 1st number : 10

Sample output :
The output series of digits are :
1 2 3 4 5 6 7 8 9 10

Both of the codes provides accurate results to generate the digits between the defined range (a, b) using the for loop concept.

If you liked this post, then share this with your friends and if anything is confusing or incorrect then let me know in the comment section.

Thanks from my side, this is Mayank, keep learning and exploring !!

Mayank Pathak is the editor and blogger at "The Coding Bro".He loves to codes, learn new technologies, and write the coding solutions and articles for the communities of learners, especiall…

Post a Comment

Please do not enter any spam link in the comments.
© THE CODING BRO. All rights reserved. Premium By The Coding Bro