how to increment or decrement of number using while loop in c language ?

 

Hello Friend’s

Today we are going to learn about “while loop” and write a programe to  increase or decrease a given number .

First fall we know that what is while loop so, while loop is c programming loop which run the function until camand is going to be false ( means if camand is true the while loop run and until camand is not false it continuous  increment or decrement ) in short if camand is false then while loop is not execute .

In while loop we are initialise the variable , give condition to variable and increment or decrement of the variable



Sintex of while loop is..

while (/* condition */)
{
    /* code */
}

Now I hope so you can understand while loop.

So, now we are going to write a program using while loop to print the number upto 10 in accending order or decending order.

  1. 1.   Increment

#include<stdio.h>
int main(){
    int i=1;
    while(i<=10){
        printf("%d\n",i);
        i++;
    }
    return 0;
}
 
   OUTPUT:-
                                1
                                2
                                3
                                4
                                5
                                6
                                7
                                8
                                9
                              10

  1. 2.   Decrement

#include<stdio.h>

int main(){

    int i=10;

    while(i>=1){

        printf("%d\n",i);

       i--;

  }

    return 0;

}

  OUTPUT:- 

                                10
                                9
                                8
                                7
                                6
                                5
                                4
                                3
                                2
                                1

                                           Thank-you guys

 

 

 

2 Comments