[go: up one dir, main page]

0% found this document useful (0 votes)
9 views20 pages

3_third Chapter r Looping Statemnt

The document provides an overview of control statements and looping constructs in C programming, including entry control loops (for and while loops) and exit control loops (do-while loops). It includes syntax examples, program demonstrations for printing tables, Fibonacci series, and checking for prime numbers, as well as the use of break and continue keywords. Additionally, it covers nested loops and provides examples for calculating marks and identifying Armstrong numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

3_third Chapter r Looping Statemnt

The document provides an overview of control statements and looping constructs in C programming, including entry control loops (for and while loops) and exit control loops (do-while loops). It includes syntax examples, program demonstrations for printing tables, Fibonacci series, and checking for prime numbers, as well as the use of break and continue keywords. Additionally, it covers nested loops and provides examples for calculating marks and identifying Armstrong numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

3. CONTROL STATEMENT OR LOOPING STATEMNT

Control statement are use if we want to perform the some task repetitively again and again.

Following are control statement in c language.


1]Entry control loop

Entry control loop means if condition satisfy or true first time then loop will be execute
otherwise not

Types of entry control loop


I]for loop

Ii]while loop

2]Exit control loop

May be condition false first time loop will execute minimum single time is called

As.exit control loop .if condition is true then loop will execute same as entry control loop.

If we want to use the any type of loop we need three important steps.

I]Intialization: initialization means starting point of loop

Ii]condition:-how many time will be repeat or how may be repetation you want

To perform.

Iii]Increment and Decrement:- steps for increment and decrement

How to use the for loop in programme or application

Syntax:- for(Intialization;condition;increment and decrement)

{
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

Body for loop

e.g. for (i=1; i<=5; i++)

/* Intialization Condition Increment &Decrement */

Printf(“\n good morning”);

Output:-

Good morning

Good morning

Good morning

Good morning

Good morning

How to use execute above example:-

First time i=1 is less than 5 then condition is true and loop will be execute

And print the good morning message then I will increment by 1 then next

Time i=2 this is also true so loop will be execute and print the good morning

Message this process done up to the five times.

//Following programme demonstrate the use of for loop for print the table
#include<stdio.h>

#include<conio.h>

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

void main(){

int no,table,I;

clrscr();

printf(“enter the number for table \n”);

scanf(“%d”,&no);

for(i=1;i<=100;i++)

table=no*I;

printf(“%d\n”,table);

getch();

Output:-

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

How to use the while loop in programme

Syntax:- Intilization ;

While(condition)

Body of while loop

Increment and decrement

e.g. i=1;//initialization or 1 start from 1

while(i<=5)

Printf(“good morning”);

I++;

How to use the do while loop

Syntax :-Intalization;

Do

Statement block

Increment or decrement

While(condition);

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

//Following programme demonstrate the use of do while loop for display the
table
#include<stdio.h>

#include<conio.h>

void main(){

int no,table.i;

clrscr();

printf(“enter the number for print table \n”);

scanf(“%d”,&no);

i=1;

while(i<=no)
{

table =i*no;

printf(“%d\n”,table);

i++;

getch();

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

Use of break and continue keyword.

Break:-this keyword is used to stop the execution of loop in middle if programmer want.

Continue:- This keyword is used to skip some the execution step of loop if programme want.

//Programme demonstrate the use of break keyword.


#include<stdio.h>

#include<conio.h>

void main(){

int i;

clrscr();

for(i=1;i<=5;i++)

if(i==3)

break;

else

printf(“good morning”);

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

getch();

This programme geneate the output good morning two times because condition satisfy

In loop then loop will stop this executuion

Nested for loop statement

For(Intilization;condition;increment and decrement)

For(initialization ;condition ;increment and decrement)

Body of inner for loop

Body of outer for loop

//Programme demonstrate the use of nested for loop


#include<stdio.h>

#include<conio.h>

void main(){

int I,j,table;
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

clrscr();

for(i=1;i<=10;i++)
{

for(j=1;j<=10;j++)

tacle=i*j;

printf(“%d”,table);

printf(“\n”);

getch();

Output:-

//Write programme calculate the marks.


#include<stdio.h>

#include<conio.h>

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

void main(){

int marks1,marks2,marks3,sum;

char choice;

clrscr();

do{

printf(“\n enter the marks of three subjects”);

scanf(“%d%d%d”,&marks1,&marks2,&marks3);

sum=marks1+marks2+marks3;

printf(“\n sum is %d”,sum);

printf(\n cont……………(Y/N):”);

fflush(stdin);

choice=getche();

while(choice!=’n’&&choice!=’N’)

getche();

Output:

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

//Programme for fibonancci series.


#include<stdio.h>

#include<conio.h>

void main(){

long first=0,second=1,third;

int i,num;

clrscr();

printf(“how many numbers u want in the series:”);

scanf(“%d”,&num);

printf(“\n %ld\t%ld”,first,second);
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

for(i=2;i<num;i++)

third=first+second;

printf(“\t %ld”,third);

first=second;

second=third;

getche();

Output:-

//Programme for check prime or not


#include<stdio.h>

#include<conio.h>

void main(){

int num,i,rem;

clrscr();

printf(“\n enter the number”);


GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

scanf(“%d”,&num);

for(i=2;i<num/2;i++)

rem=num%i;

if(rem==0)

printf(“\n the number is not prime”);

break;

if(i==(num/2))

printf(“\n the number is prime”);

getch();

Output:-

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

//Print the star shape using nested loop


#include<stdio.h>

#include<conio.h>

void main(){

int i,j,k;

clrscr();

printf(“\n first\n”);

for(i=1;i<=5;i++)

for(j=1;j<=i;j++)

printf(“*\t”);

printf(“\n”);

printf(“\n second \n”);

for(i=1;i<=5;i++)
{

for(j=5;j>=i;j--)

printf(“*\t”);

printf(“\n”);

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

printf(“\n Third \n”);

for(i=1;i<=5;i++)

for(k=4;k>=i;k--)

printf(“\t “);

for(j=1;j<=i;j++)

printf(“*\t”);

printf(“\n”);

getch();

Output:-

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

//Reversing the number and also conting it’s number of digits


#include<stdio.h>

#include<conio.h>

void main(){

long num,rev=0,temp;

int rem,count=0,sum=0;

clrscr();

printf(“\n enter the number”);

scanf(“%ld”,&num);

temp=num;

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

while(num>0)

rem=num%10;

num=num/10;

count++;

sum+=rem;

rev=rev*10+rem;

printf(“\n the number of digits in %ld is %d “,temp,count);

printf(“\n the sum of the digits of %ld is %d”,temp,sum);

printf(“\n the reverse of %ld is %ld”,temp,rev);

getch();

Output:

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

//w.a.p.to show the good morning message.


#include<stdio.h>

#include<conio.h>

void main(){

int i;

clrscr();

for(i=1;i<=5;i++)

if(i==3)

continue;

else

printf(“good morning”);

getch();

Output:-

This program four time good morning message.if condition satisfy in loop then if will skip
one

Step of execution in loop.

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

Solved program on control statement

//Print the series of Armstrong number up to the limit.


#include<stdio.h>

#include<conio.h>

void main(){

int num,sum,temp,rem,limit;

clrscr();

printf(“\n enter the limit:”);

scanf(“%d”,&limit);

for(num=1;num<=limit;num++)

sum=0;

temp=num;

while(temp>0)

rem=temp%10;

sum=sum+rem*rem*rem;

temp/10;

if(sum==num)pPrintf(“\t %d”,sum);

getch();

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

Output:-

//Check number is Armstrong or not.


#include<stdio.h>

#include<conio.h>

void main(){

int num,sum=0,temp,rem;

clrscr();

printf(“\n enter the number”);

scanf(“%d”,&num);

temp=num;

while(num>0)

rem=num%10;

sum=sum+rem*rem*rem;

num/=10;

}
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)
GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

if(sum==temp)

printf(“\n the number is a armstrong”);

else

printf(“\n the number is not Armstrong”);

getch();

Output:-

GIRI’S TECH HUB,Pune(IT Training Institute with 100% Guaranteed Placement Support Mob:9175444433)

You might also like