ho to use while loop in c++ examples part 2

While loop is used when number of iterations is not known and increment or decrement in constant .
 
//use header files syntax according to the compiler
 
#include<conio.h>
#include<iostream.h>
 
void main()
   {
   int n,r,i=1;
   int sum=0;
   int s;
   cout<<"enter a three digit number";
   cin>>n;
   s=n;
    while(i<=3)
     {
         r=n%10;
         sum=sum+r;
         n=n/10;
         i++;
      }
    cout<<"the sum of "<<s<<"= "<<sum;
    getch();
    } 
 
=========================================================== 
 
               SUM OF A NUMBER WHOSE LENGTH IS NOT KNOWN
 
#include<conio.h>
#include<iostream.h>
 
void main()
   {
   int n,r,s;
   int sum=0;
   cout<<"enter any number";
   cin>>n;
   s=n
    while(n>=1)
     {
         r=n%10;
         sum=sum+r;
         n=n/10;
      }
    cout<<"the sum of "<<s<<"= "<<sum;
    getch();
    } 
//If you don't know the length of the input no .ie. the no. enter may be 2 digits lengthy or it may be 5 digits lengthy. The above code is for any number.

===========================================================
//Reverse of a number

void main()
   {
   int n,r,i=1;
   int sum=0;
   int s;
   cout<<"enter a three digit number";
   cin>>n;
   s=n;
    while(i<=3)
     {
         r=n%10;
         sum=sum*10+r;
         n=n/10;
         i++;
      }
    cout<<"the reverse of "<<s<<"= "<< sum;
   getch();
 }