4. Loop Control Statements

 A loop is defined as a block of statements which are repeatedly  executed for a certain number of times.





There are three types of loop

  1. for loop
  2. while loop
  3. do while loop


1) for loop 

The for loop allows you to execute a set of instructions until a certain condition is satisfied .


Syntax:


for(expression 1;expression 2;expression3){
    //statement;
    }

Example

The first five numbers starting from one together  with their squares

#include<stdio.h>
void main(){
	int i;
	for (i=1;i<=5;i++){
	    printf("the number is %d it is square is %d\n ",i,i*i);
	}
    }

Output:


the number is 1 it is square is 1

 the number is 2 it is square is 4

 the number is 3 it is square is 9

 the number is 4 it is square is 16

 the number is 5 it is square is 25


Write  a program Display numbers from 1 to 15 using for loop


#include<stdio.h>
void main (){
	int i;
	for (i=1;i<=15;i++){
		printf("%d ",i  );
	}
}

Output:


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 


Write a program to display even numbers from 0 to 10


#include<stdio.h>
#include<conio.h>
void main (){
	int i=0;
	printf("Number in Ascending order ");
        for(i=0;i<=10;i++){
		printf("%3 ",i);
		}
        printf("\n\n");
	
	printf("Number is Descending order : ");
	for(i=10;i=>1;i--){
		printf("%d ",i);
		    }
}


Output :


Number in Ascending order   1   2   3   4   5   6   7   8   9  10


Number is Descending order :  10   9   8   7   6   5   4   3   2   1



Write a program to count numbers between 1 to 100 not divisible 2,3 and 5.

#include<stdio.h>
#include<conio.h>
void main(){
	int i=0,s=0;
	printf("The number from 1 to 100 not divisible by 2 3 and 5\n");
	for(i=0;i<=100;i++){
	    if(i%2!=0 && i%3!=0 && i%5!=0){
		printf("%d\t",i);
		s++;		
	        }
	    }
	printf("\ntotal number is %d\n",s);
    }

Output :


The number from 1 to 100 not divisible by 2 3 and 5

1    7       11      13      17      19      23      29      31       37      41      43      47      49      53      59      61      67    71      73      77      79      83       89      91      97     

total number is 26



2) while -loop


The test condition may be any expression .The loop statement will be executed till the condition is true i.e the test condition is evaluated and if the condition is true ,then the body of the loop is executed .when the condition becomes false the execution will be out of the loop.


Syntax :


while(test condition){

//Body of the loop

}

Example

Write a program to print the string “you are best” 5 times using while loop.


#include<stdio.h>
#include<conio.h>
void main(){
	int i=1;
	while(i<6){
            printf("you are best\n");
            i++;
            }
    }

Output :

you are best

 you are best

 you are best

 you are best

 you are best


Write a program to calculate the factorial of a given number . use a while loop.


#include<stdio.h>
#include<conio.h>
void main(){
	int a,fact=1;
	printf("Enter the numbers\n ");
	scanf("%d",&a);
	while(a>=1){
		printf("%d*",a);
		fact = fact*a;
		a--;
	}
	printf("=%d ", fact );
	printf("\nThe factorial of given number is %d ",fact );
	
    }


Output :

Enter the numbers

5

5*4*3*2*1*=120

The factorial of given number is 120



Write a program to enter a number through the keyboard and find the sum of its digits.

#include<stdio.h>
#include<conio.h>
void main (){
	int n,k=1,sum=0;
	printf("enter the numbers ");
	scanf("%d",&n);
	 
	 while(n!=0){
	 	k= n%10;
	 	sum = sum+k;
	 	k= n/10;
	 	n=k;
	 }
	printf("sum of digit %d",sum);
}

Output :

enter the numbers 112

sum of digit 4


Write a program to enter a few numbers and count the positive and negative numbers together with their sum . When o is entered the program should be terminated.

#include
#include
void main (){
	int j=1,p=0,n=0,s=0,ns=0;
	printf("enter the numbers ");
	while(j!=0){
		scanf("%d",&j);
		if(j>0){
			p++;
			s= s+j;
		}
		else if(j<0){
                    n++;
                    ns = ns +j;
                    }
            }
        printf("\nTotal positive numbers is %d\n ",p);
        printf("\nTotal negative  numbers is %d\n ",n);
        printf("\nsum of  positive numbers is %d\n ",s);
        printf("\nsum of negative  numbers is %d\n ",ns);
        }


Output : 
 enter the numbers 2 3 4 -7 -8 9 4 -2 0 
 Total positive numbers is 5 
 Total negative numbers is 3 
 sum of positive numbers is 22 
 sum of negative numbers is -17


3) do-while Loop


Whereas in the do while ,the condition is checked at the end of the loop .The do while loop will execute at least one time even if the condition is false initially .The do-while loop executes until the condition becomes false.


 Syntax :


do{
Statements ;
}
while(condition);

Example

Write a program using  do while loop and display a message “i am best ,i can do it” 5 times.


#include<stdio.h>
void main(){
int i=1;
do{
    printf("i am a best,i can do it\n ");
    i++;
   }while(i<=5);

}

Output :

i am a best,i can do it

 i am a best,i can do it

 i am a best,i can do it

 i am a best,i can do it

 i am a best,i can do it



Write a program to print the entered number in reverse order .use do while loop.Also perform a sum and multiplication with their digits


#include
void main (){
int n ,i,d=0, x=1,mul=1,sum=0;
printf("enter the number of digits ");
scanf("%d",&d);
printf("Enter the number which is to be reversed :");
scanf("%d",&n);
printf("Reversed number\n");
do{
	i = n%10;
	printf("%d",i);
	sum = sum+i;
	mul = mul*i;
	n =n/10;
	x++;
}while(x<=d);
printf("\naddition of digit : %4d\n ",sum);
printf("multiplication of digits : %4d\n ",mul);
getche();
}

Output :

enter the number of digits 4

Enter the number which is to be reversed :2356

Reversed number

6532

addition of digit : 16

multiplication of digits : 180



Write a program to check whether the given number is prime or not

#include<stdio.h>
int main(){
	int n,x=2;
	printf("enter the number\n");
	scanf("%d",&n);
	do{
	   if(n%x==0){
	        printf("the number %d is not  prime number",n);
		getch();
		exit(0);
		}
	    x++;
	    }while(x<n);
        printf("the number %d is prime number ",n);
        getche();
    }

Output :

enter the number

4

the number 4 is not  prime number



Write a program to evaluate the series such as 1+2+3…..i.use do-while loop.where i can be a finite value .its value should be read through the keyword.


#include<stdio.h>
#include<conio.h>
void main (){
	int i,a=1,s=0;
	printf("enter the number \n");
	scanf("%d",&i);
	do{
	    printf("%d +",a);
	    s = s+a;
	    a++;
	}while(a<=i);
        printf(" = %d",s);
    }

Output :

enter the number

6

1 +2 +3 +4 +5 +6 + = 21


Post a Comment

Previous Post Next Post