ADVERTISEMENT
ADVERTISEMENT

C program to print numbers from 1 to N using while loop

This is an example of while loop in C programming language - In this C program, we are going to print numbers from 1 to N using while loop.
Submitted by IncludeHelp, on March 07, 2018

Here, N is the limit of the number, for example – if you want to print the numbers from 1 to 10 or 1 to 100 then 10 or 100 will be the value of N. In this C program, N will be entered by the user.

To print the numbers from 1 to N,

  • We will declare two variables 1) for loop counter (number) and 2) for limit (n).
  • We will check the condition whether loop counter is less than or equal to N, if condition is true numbers will be printed.
  • If condition is false – loop will be terminated.

Program to print numbers from 1 to N using while loop in C

#include <stdio.h>

int main()
{ 

	//loop counter declaration
	int number;
	//variable to store limit /N
	int n;
	
	
	//assign initial value 
	//from where we want to print the numbers
	number =1;
	
	//input value of N
	printf("Enter the value of N: ");
	scanf("%d",&n);
	
	//print statement
	printf("Numbers from 1 to %d: \n",n);
	
	//while loop, that will print numbers 
	//from 1 to n
	while(number <= n)
	{
		//printing the numbers
		printf("%d ",number);
		//increasing loop counter by 1
		number++;
	}
	
	return 0;
}

Output

Enter the value of N: 25 
Numbers from 1 to 25: 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
16 17 18 19 20 21 22 23 24 25 

C Looping Programs »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.