Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Thursday, 10 March 2022

28.While iterative control structure

While iterative control structure:

It is the entry controlled iterative control structure.

Condition is checked while entering into the loop.

Body of the loop executes continuously until the condition is false.

It is the most commonly used control structure in any language.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Specification1:

Print the natural numbers from 1 to n

 

Program:

#include<stdio.h>

In case of do-while

 

i=1;

do{                 

   printf("%10d",i);

   i=i+1;

   }while(i<=n);

 

void main()

{

int n,i;

 

printf("Enter the limit:");

scanf("%d",&n);

i=1;

while(i<=n)    

{

    printf("%10d",i);

    i=i+1;

}

 

}

 

Execution:

Enter the limit: 5

1          2          3          4          5

 

Specification2:

Print the natural numbers from n to 1

 

Program:

#include<stdio.h>

void main()

In case of do-while

 

i=n;

do{                 

   printf("%10d",i);

   i=i-1;

   }while(i>=1);

 

{

int n,i;

 

printf("Enter the limit:");

scanf("%d",i);

i=n;

while(i>=1)

{

   printf("%10d",i);

   i=i-1;

}

 

}         

 

Execution:

Enter the limit: 5

5          4          3          2          1

 

Specification3:

Print the natural number with in the range.

 

Program:

#include<stdio.h>

void main()

{

int ll,ul,i;

 

printf("Enter the lower limit:");

scanf("%d",&ll);

In case of do-while

 

i=ll;

while(i<=ul)

{                      

   printf("%10d",i);

   i++;

}

 

printf("Enter the upper limit:");

scanf("%d",&ul);

i=ll;

while(i<=ul)

{

    printf("%10d",i);

    i++;

}

 

}         

 

Execution:

Enter the lower limit: 5

Enter the upper limit: 9

5          6          7          8          9

 

Specification4:

Print the sum of natural numbers from 1 to n

Program:

#include<stdio.h>

In case of do-while

 

sum=0;

i=1;

do{                 

   sum=sum+i;

   i=i+1;

   }while(i<=n);

 

void main()

{

int n,i,sum;

 

printf("Enter the limit:");

scanf("%d",&n);

sum=0;

i=1;

while(i<=n)    

{

    sum=sum+i;

    i++;

}

printf("The sum of natural numbers %d",sum);

 

}

 

Execution:

Enter the limit: 5

The sum of natural numbers 15

 

Example explained:

Using i=1, i<=n, i=i+1 we are generating natural numbers from 1 to n.

In order to find the sum of natural numbers, adding "i" to another variable "sum" every time the value of "i" is changed.

Finally variable "sum" has the sum of natural numbers that we are printing as output.

Specification5:

Accept any number and print its factorial

 

Program:

#include<stdio.h>

void main()

{

int i,n;

long int fact;

 

printf("Enter an integer:");

scanf("%d",&n);

fact=1;

i=1;

while(i<=n)

{

    fact=fact*i;

    i++;

}

printf("Factorial of  %d is %ld",n,fact);

 

}

 

Execution:

Enter an integer: 5

Factorial of 5 is 120

 

Example explained:

Using i=1, i<=n, i++ we are generating natural numbers from 1 to n.

In order to find the factorial multiplying "i" to another variable "fact" every time the value of "i" is changed.

Finally variable "fact" has the factorial that we are printing.

 

Specification6:

Print the even numbers form 1 to n

 

 

Program:

#include<stdio.h>

void main()

{

int n,i;

 

printf("Enter the limit:");

scanf("%d",&n);

i=1;

while(i<=n)

{

     if(i%2==0)

         printf("%10d",i);

    i++;

}

 

}

 

Execution:

Enter the limit: 10

2          4          6          8          10

 

Example explained:

Using i=1, i<=n, i++ we are generating natural numbers from 1 to n.

But printing only when the value of "i" is an even number by checking if(i%2==0).

 

Specification7:

Accept any number and print its factors.

 

Program:

#include<stdio.h>

void main()

{

int i,n;

 

printf("Enter any integer:");

scanf("%d",&n);

i=1;

while(i<=n)

{

     if(n%i==0)

        printf("%10d",i);

    i=i+1;

}

 

}

 

Execution:

Enter any integer: 15

1          3          5          15

 

Example explained:

Using i=1, i<=n, i++ we are generating natural numbers from 1 to n.

But printing only when "i" divides "n" with reminder zero (factor)

 

Specification8:

Accept any number and print the sum of all the digits.

 

Logic:

The modulo division operator (%) return the last digit when we divide by 10;

n=n/10 removes the last digit. For example if n is 345, n/10 is 34.5 and assigning to integer variable "n" results 34 is stored into "n".

It is only possible if "n" is of integer type.

 

Example1: if "n" is a three digit number.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Example2: if "n" is a four digit number.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Summery:

Extracting individual digits one by one using n%10, n=n/10 until "n" becomes 0

while(n!=0)

{

      n%10;

 n=n/10;

}

 

Program:

#include<stdio.h>

void main()

{

int n,sum;

 

printf("Enter any integer:");

scanf("%d",&n);

sum=0;

while(n!=0)

{

    sum=sum+n%10;

    n=n/10;

}

printf("Sum of all the digits %d",sum);

 

}

 

Execution1:

Enter any integer: 345

Sum of all the digits 12

 

Execution2:

Enter any integer: 4536

Sum of all the digits 18

 

Specification9:

Accept any number and print it in reverse.

 

Logic:

 

rev=0;

while(n!=0)

{

    rev=rev*10+n%10;

    n=n/10;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Program:

#include<stdio.h>

void main()

{

int n,rev;

 

printf("Enter any number:");

scanf("%d",&n);

rev=0;

while(n!=0)

{

    rev=rev*10+n%10;

    n=n/10;

}

printf("Reverse number %d",rev);

 

}

 

Execution1:

Enter any number: 4567

Reverse number 7654 

0 comments:

Post a Comment