Do – While Loops in C

Looping constructs is an important programming language technique to repeat a process or calculation for a set or collection of inputs or storage based on a condition. Do – WhileĀ is a looping technique when you want to execute the required process for at least once and then check the condition for the rest of the input collection. Below are some simple C programs using do-while looping.

Example 1: C program to find the sum of digits in a given integer

      main(){
             int n, nsave, a, sum =0;
   
             printf("\nInput any integer of your choice>>");

             scanf("%d",&n);

             nsave = n;

             do{
                a = n % 10;/*right most digit*/
                sum += a;
                n /= 10;
             }
             while(n > 0);

             printf("Sum of digits of %d = %d \n", nsave, sum);
      }

Example 2: C program to print an integer back-wards

      main(){
             int n, a;
   
             printf("\nInput any integer of your choice>>");

             scanf("%d",&n);

             printf("%d written backwards is>> ");
             
             do{
                a = n % 10;/*right most digit*/
                printf("%d",a);
                n /= 10;
             }
             while(n > 0);
             printf("\n");
             
      }

 

Add a Comment

Your email address will not be published. Required fields are marked *