Print 1 to N numbers using while loop in C

Solution: In this tutorial, we will discuss how to print 1 to N numbers in C. Using loop, we can print up to n numbers which is given by the user. Here we will see how to perform while loop to print 1 to N numbers in C. This program is very easy for everyone.So let’s start it.

HINTS:

  • Declare only two variables.
  • One variable is used for taking input and other to print upto input number
  • First take an integer number input from the user.
  • Perform while loop.
#include<stdio.h>
#include<conio.h>

int main()
{

    int n,i=1;  // VARIABLES DECLARATION
    printf("\t------------------------------------\n");
    printf("\tPRINT 1 TO N NUMBER USING WHILE LOOP\n");
    printf("\t------------------------------------\n");
    printf("\n\n\tENTER N NUMBER ( INTEGER TYPE) : ");
    scanf("%d",&n);
    while(i<=n)
    {
        printf("\t%d\n",i);
        i++;    // i++ means i=i+1
    }
    getch();
    return 0;
}

OUTPUT:

EXPLANATION:

  • #include<stdio.h> is used for input and output functions working purpose.
  • #include<conio.h> is used for getch() function.
  • In while function, we use printf function to print numbers.
  • i++ is used to increment value.

2 comments

Leave a comment