0% found this document useful (0 votes)
172 views104 pages

Decision Making and Looping

This document discusses different types of loops in C programming. It begins by explaining the need for repetition in programs using loops. The three common loop control statements in C - for, while, and do-while loops - are introduced. Examples are provided to illustrate how each loop works, including the syntax, initialization, test condition, and increment/decrement. The document also discusses tracing loop execution, and provides examples of using loops to solve problems like printing numbers, calculating sums, checking prime numbers, and more. Types of loops such as pre-test/post-test and event-controlled/counter-controlled loops are also covered.

Uploaded by

Aryan V
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)
172 views104 pages

Decision Making and Looping

This document discusses different types of loops in C programming. It begins by explaining the need for repetition in programs using loops. The three common loop control statements in C - for, while, and do-while loops - are introduced. Examples are provided to illustrate how each loop works, including the syntax, initialization, test condition, and increment/decrement. The document also discusses tracing loop execution, and provides examples of using loops to solve problems like printing numbers, calculating sums, checking prime numbers, and more. Types of loops such as pre-test/post-test and event-controlled/counter-controlled loops are also covered.

Uploaded by

Aryan V
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/ 104

Decision Making and Looping

REPETITION IN PROGRAMS
• In most software, the statements in the program
may need to repeat for many times.
– e.g., calculate the value of n!.
– If n = 10000, it’s not elegant to write the code
as 1*2*3*…*10000.
• Loop is a control structure that repeats a group
of statements in a program.
– Loop body stands for the repeated
statements.
• There are three C loop control statements:
– for, while and do-while.
THE FOR STATEMENT IN C
• The syntax of for statement in C:
for (initialization ; test condition ;
increment/decrement)
{
body of the loop
}
• The initialization set the initial value of the loop
control variable.
• The test condition test the value of the loop control
variable.
• The increment/decrement update the loop control
variable.
EXAMPLE FOR “FOR” LOOP
• Consider the following segment of
program:
Initialization Test Update of
Of loop control Of loop control
condition
variable variable

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


{
printf(“%d\n”,x);
}
TRACING OF LOOP
Iteration number value of x –value
1 1
2 2
3 3
4 4
5 5
1.PROGRAM TO PRINT N NATURAL
NUMBERS
1.PROGRAM TO PRINT N NATURAL
NUMBERS
main()
{
int i, N;
clrscr();
printf(“Enter the value of N \n”);
scanf(“%d”,&N);
for ( i=0 ; i<=N ; i++)
printf(“%d\n”,i);
getch();
}
2.WRITE A C –PROGRAM TO FIND SUM
OF N NATURAL NUMBERS
//c-program to find sum of N natural numbers

#include<stdio.h>
#include<conio.h>
main()
{
int N,i,sum=0;
clrscr();
printf("enter the range ( 1 to N)\n");
scanf("%d",&N);
for(i=1;i<=N;i++)
{
printf("%d\n",i);
sum=sum+i;
}
printf("\n sum of 1 to %d natural numbers is=%d",N,sum);
getch();
}
3.TO PRINT SUM OF EVEN NUMBERS
AND ODD NUMBERS FROM 1 TO N.
#include<stdio.h>
#include<conio.h>
main()
{
int N, i, esum=0,osum=0;
clrscr();
printf(“ Enter range (1 to N)\n");
scanf(“ %d", &N);
for(i=1;i<=N;i++)
{
if(i%2==0)
esum=esum+i;
else
osum=osum+i;
}
printf("\nsum of even numbers form 1 to
%d=%d\n",N,esum);
printf("\nsum of odd numbers form 1 to
%d=%d\n",N,osum);
getch();
}
4.WRITE A C-PROGRAM TO GENERATE
AND PRINT ARMSTRONG NUMBERS
BETWEEN 100 TO 999
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int N,d1,d2,d3,sum;
clrscr();
for(N=100;N<=999;N++)
{
d1=N/100;
d2=(N/10)%10;
d3=N%10;
sum=pow(d1,3)+pow(d2,3)+pow(d3,3);
if(sum==N)
printf("%d\n",N);
}
getch();
}
5.WRITE A C-PROGRAM TO FIND
FACTORIAL OF A GIVEN NUMBER N.
//c-program to find sum of N natural numbers

#include<stdio.h>
#include<conio.h>
main()
{
int N,I,fact=1;
clrscr();
printf("enter a number\n");
scanf("%d",&N);
for(i=1;i<=N;i++)
{
fact= fact * i;
}
printf("\n Factorial of given number %d=%d,N,fact);
getch();
}
WRITE A C-PROGRAMS TO EVALUATE
FOLLOWING SERIES
6. X=12 + 22 + 32 +--------+N2
7. y=22 + 42 + 62 +--------+N2
8. z=12 + 32 + 52 +--------+N2
9. P= 1+X+ X2 + X3 +------+ XN
WRITE A C-PROGRAM TO EVALUATE
FOLLOWING SERIES
X=12 + 22 + 32 +--------+N2
#include<stdio.h>
#include<conio.h>
main()
{
int x=0,N,i;
clrscr();
printf("Enter the value of N\n");
scanf("%d",&N);
for(i=1;i<=N;i++)
{
x=x+(i*i);
}
printf("sum=%d",x);
getch();
}
WRITE A C-PROGRAM TO
EVALUATE FOLLOWING SERIES

Y=22 + 42 + 62 +--------+N2
#include<stdio.h>
#include<conio.h>
main()
{
int y=0,N,i;
clrscr();
printf("Enter the value of N\n");
scanf("%d",&N);
for(i=1;i<=N;i++)
{
if(i%2==0)
y=y+(i*i);
}
printf("sum of sqaures of even numbers=%d",y);
getch();
}
WRITE A C-PROGRAM TO
EVALUATE FOLLOWING SERIES

Z=12 + 32 + 52 +--------+N2
#include<stdio.h>
#include<conio.h>
main()
{int z=0,N,i;
clrscr();
printf("Enter the value of N\n");
scanf("%d",&N);
for(i=1;i<=N;i++)
{
if(i%2!=0)
z=z+(i*i);
}
printf("sum of sqaures odd numbers=%d",z);
getch();
}
WRITE A C-PROGRAM TO EVALUATE
FOLLOWING SERIES
P= 1+X+ X2 + X3 +------+ XN
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int p=1,N,i,x;
clrscr();
printf("Enter the value of X\n");
scanf("%d",&x);
printf("Enter the value of N\n");
scanf("%d",&N);
for(i=1;i<=N;i++)
{
p=p+pow(x,i);
}
printf("sum of sqaures odd numbers=%d",p);
getch();
}
10. Write a c-program to generate and
print first – N Fibonacci series
WHO WAS FIBONACCI?

• The "greatest European mathematician of the middle ages", his full name
was Leonardo of Pisa, or Leonardo Pisano in Italian since he was born in
Pisa,Italy
FIBONACCI'S MATHEMATICAL
CONTRIBUTIONS
• Introducing the Decimal Number
system into Europe
• Arithmetic with Roman Numerals
• Invented formulae Pi using the
Fibonacci numbers
#include<stdio.h>
#include<conio.h>
main()
{
int i,N,fib1=0,fib2=1,fib3;
clrscr();
printf("\n Enter the value of N\n\t");
scanf("%d", &N);
if( N<2)
Printf(“Fibonacci series does not exist \n”);
else
{
printf(“%d \n %d \n”, fib1, fib2);
for(i=3;i<=N;i++)
{
fib3=fib2+fib1;
printf("%d\n",fib3);
fib1=fib2;
fib2=fib3;
}}
getch();
}
Enter the value of N:5

i fib1 fib2 fib3

1 0 0 0

2 0 1 1

3 0 1 1

4 1 1 2

5 1 2 3

finally fib3 gives first-N Fibonacci series


11. Write to check whether the given
number is prime or not
• What is a Prime Number?

A positive integer >1

A number that has exactly two divisors, 1 and


itself

A number that cannot be factored


#include<stdio.h>
#include<conio.h>
main()
{
int c,N,i;
clrscr();
printf("Enter value of N\n");
scanf("%d",&N);
c=0;
for(i=1;i<=N;i++)
{
if(N%i==0)
c++;
}
if(c==2)
printf("%d is a prime number",N);
else
printf("%d is not a prime number",N);
getch();
}
APPLICATIONS OF PRIME NUMBERS

• In information and network security


( encryption and decryption)
• Generation of mobile numbers
• ATM pin codes
• ATM card numbers
• Digital signature
WHILE LOOPING STATEMENT

• Basic form
while ( test condition)
{
body of the loop
}
Statement-x;
EXAMPLE FOR WHILE LOOP
i=0; Output:
0
while (i<=5) 1
2
{ 3
4
printf (“ %d\n,i); 5

i++;
}
printf(“End of while loop”);
WRITE A C-PROGRAM TO CHECK
WHETHER THE GIVEN NUMBER IS
PALINDROME OR NOT (4 DIGIT
NUMBER)
BACKGROUND: PALINDROME
NUMBER
• A palindrome number or numeral
palindrome is a 'symmetrical' number like
161, that remains the same when its digits
are reversed
• More examples:-
121,141,242,343-----etc
/* c-program to check whether the given
number is palindrome or not */
#include<stdio.h>
#include<conio.h>
main()
{
int N,rev=0,temp,digit;
clrscr();
printf("enter an integer\n");
scanf("%d",&N);
temp=N;
while(N>0)
{
digit= N%10;
rev= rev*10+digit;
N= N/10;
}
printf("given number is=%d\n",temp);
printf("its revers is=%d\n",rev);
if(temp==rev)
printf("Therefore number is a palindrome\n");
else
printf("Therefore number is not a palindrome\n");
getch();
}
WRITE A C-PROGRAM TO FIND
GCD AND LCM OF TWO
INTEGERS USING EUCLID’S
ALGORITHM. OUTPUT THE
RESULTS ALONG WITH GIVEN
INTEGERS.
EUCLID’S ALGORITHM
1. Take two integers m & n
2. store p=m and q=n
3. Find remainder m%n
4. Assign m=n
5. Assign n=m%n
6. Repeat step 3 to 5 upto n value becomes zero
7. m value will be the gcd
8. lcm= ( p*q) / gcd
m n rem= m%n

2 3 2%3=2
3 2 3%2=1
2 1 2%1

1 0
When n=0, gcd=m=1;
Lcm= ( 2*3)/gcd=6
/* c-program to find gcd and lcm of two interger
numbers */
#include<stdio.h>
#include<conio.h>
main()
{
int m,n,gcd,lcm,rem,p,q;
clrscr();
printf("enter the number m and n\n");
scanf("%d%d",&m,&n);
p=m;q=n;
while(n!=0)
{
rem=m%n;
m=n;
n=rem;
}
gcd=m;
printf("\ngcd of %d and %d is=%d\n",p,q,gcd);
lcm=(p*q)/gcd;
printf("\nlcm of %d and %d is=%d",p,q,lcm);
getch();
}
THE DO-WHILE STATEMENT

• Syntax:- do- while loop


do
{
body of the loop
}
while( test-condition);
statement-x;
EXAMPLE
• A simple example of a do-while loop
i=0;
Output:
do Enter a number 1
Enter a number 2
{ Enter a number 3
printf(“ Enter a number \n”); Enter a number 0

scanf(“%d”,&n); 4
i++;
}
while(n>0);
printf(“\n%d”,i);
TYPES OF
LOOPS
• Pre-test & post-test loops
• Event controlled and counter controlled
loops
Pretest and Post-test Loops
We need to test for the end of a loop, but where
should we check it—before or after each
iteration? We can have either a pre- or a post-test
terminating condition.

In a pretest loop , the condition is checked at


the beginning of each iteration.

In a post-test loop, the condition is checked at


the end of each iteration.
TYPES OF LOOPS

Pretest Loop
In each iteration, the control expression is tested
first. If it is true, the loop continues; otherwise,
the loop is terminated.
Post-test Loop
In each iteration, the loop actions are executed.
Then the control expression is tested. If it is true,
a new iteration is started; otherwise, the loop
terminates
PRE AND POST TEST LOOPS
EVENT AND COUNTER-CONTROLLED
LOOPS

 Event Controlled Loops: Event or action is


changes the expression from true to false.
LOGIC FLOW OF EVENT CONTROLLED
LOOPS
COUNTER CONTROLLED LOOPS
Counter Controlled Loops are used when we
know the number of times the loop is executed.
Number of times loop is executed is kept track by
the variable count.
If the loop is incremented then count will count
upwards.
If the loop is decremented the count will count
downwards.
COUNTER CONTROLLED LOOPS LOGIC
FLOW
LOOPS IN C

C has three loop statements:-While, for


and do-while.
While and do-while are used in event
Controlled Loops.
Counter controlled Loops use the for
statement.
C LOOP CONSTRUCTS
DIFFERENCES BETWEEN FOR ,
WHILE AND DO-WHILE
for while do-while
Pre-test loop, Pre-test Post-test loop,
Counter controlled loop, event Event controlled
loop controlled loop loop
Syntax:- Syntax:- Syntax:-
When the condition When the condition When the condition
becomes false for very becomes false for becomes false for
first time , body of the very first time , very first time
loop will not be body of the loop ,Body of the loop
executed. will not be will be executed at
executed. least for one time,

Example:- Example:- Example:-


NESTED LOOPS
• Nested loops consist of an outer loop with one or
more inner loops.
• e.g.,
for (i=1;i<=100;i++)
Outer loop
{
for(j=1;j<=50;j++)
{ Inner loop

}
}
• The above loop will run for 100*50 iterations.
5-63
THE FOLLOWING WHILE LOOP
PRINTS, FOR AN INITIAL VALUE OF
C=2
do
{ Output:
printf(“%d”,c); 2211

printf(“%d”,c--);
} while( c);
A) 2 1 B) 2 1 0 C) 2210 D) 2211

From June /July 2009 question paper


A DO WHILE LOOP IS USEFUL WHEN WE
WANT TO EXECUTE THE STATEMENTS
WITHIN THE LOOP TO BE EXECUTED
Output:
A) Only once At least once

B) At least once
C) Atmost once
D) None of these

From dec-jan 09 paper


WRITE A C-PROGRAM TO ADD THE
DIGITS OF ANY GIVEN NUMBER.

EX:-FOR N= 12345,
SUM= 1+2+3+4+5=15
FROM DEC-JAN 2009
#include<stdio.h>
#include<conio.h>
main()
{
int i,N,sum=0,temp,digit;
clrscr();
printf("enter an integer\n");
scanf("%d",&N);
temp=N;
while(N>0)
{
digit=N%10;
sum=sum+digit;
N=N/10;
}
printf("Sum of digits of %d=%d",temp,sum);
getch();
}
HOW MANY TIMES THE STATEMENTS
IN THE LOOP WILL BE EXECUTED?

for ( i=0; i>1;i++) C) Zero times


{
statements;
}
A) 1 time B) 2 times C) Zero times D) Many times

From dec-jan 09
ADDITIONAL FEATURES OF FOR LOOP

1. One or more variable can be initialized at


a time in the for statement
j=1;
for ( i=1; i<=100 ;i++)
Can be rewritten as
for ( i=1, j=1; i<=100; i++)
* The multiple arguments in the for
statement are separated by commas
2. Like the initialization section, the increment section
may also have more than one part.
ex:-
for ( i=1, j=1; i<=100 ; i++, j++)

3. The test condition may have compound relation and


the testing need not be limited to the loop control
variable.
ex:-
sum=0;
for (i=1; i<20 && sum<=100; i++)
{
sum = sum + i;
printf (“%d %d \n”,i,sum);
}
4. It is also permissible to use expression in the
assignment statements of initialization and
increment section .
ex:-
for ( i= ( m + n)/2 ; i>0 ; i = i/2 )
5. Another unique aspect of for loop is that one
o more section can be omitted , if necessary ,
consider the following statements
i=5;
for( ; i!=100 ; )
{
printf (“ %d \n”,m);
i++;
}
6. We can setup time delay loops using
the null statement as follows
for( i=10 ; i>0 ; i- - )
;

* this loop is executed 1000


times without producing any output,
it simply causes time delay.
UNCONDITIONAL BRANCHING STATEMNTS

• break
• continue
• goto
JUMPS IN LOOP
• Jumping out of a loop
*Using break statement , example for
unconditional branching statement
* General form of break is
break;
* When the break statement is encountered
inside a loop , the loop is immediately
terminated and program continues with the
statement immediately following the loop
EXAMPLE FOR BREAK STATEMENT
• while ( test-condition)
{
s1;
s2;
if ( condition)
break;
exit s3;
From
loop s4;
}
s5;
JUMPING FROM INNER LOOP
• for (- - - - - - - ) outer loop
{
s1;
for ( - - - - - - - - ) Inner loop
{
s2;
if ( condition )
break;
Exit }
From s3;
Inner s4;
loop }
SKIPPING A PART OF A LOOP
• Using continue statement
• General from
continue;
• The continue statement tells the complier , “
SKIP THE FOLLOWING STATEMENTS
AND CONTINUE WITH THE NEXT
ITERATION “
CONTINUE IN WHILE STATEMENT
• while ( test condition )
{
s1;
s2;
if( condition)
continue;
s3;
s4;
}
CONTINUE IN DO- WHILE STATEMENT
• do
{
s1;
s2;
if( condition)
continue;
s3;
s4;
}
while ( test condition);
WRITE A C-PROGRAM TO EVALUATE
FOLLOWING SERIES

E= 1+ (1 / 1! ) + (1 /2!) +-------+( 1 / N!)


#include<stdio.h>
#include<conio.h>
main()
{
int N,i,j,fact;
float e=1.0,term;
clrscr();
printf("Enter the value of N \n");
scanf("%d",&N); // read N value
for(i=1;i<=N;i++)
{
fact=1;
for(j=1;j<=i;j++)
{
fact=fact*j;
}
term=1.0/fact;
e=e+ term;
}
printf("e=%f",e);
getch();
}
WRITE A C-PROGRAM TO EVALUATE
FOLLOWING SERIES

E= 1+ (1 / 1! ) + (1 /2!) +-------UPTO ACC= 0.0001


#include<stdio.h> for (i=1;i<=100;i++)
{ fact=1;
#include<conio.h> for(j=1;j<=i;j++)
#define acc 0.001 {
fact=fact*j;
main()
}
{ term=1.0/fact;
int i,j,fact; e=e+term;
if(term<=acc)
float e=1.0,term; break;
clrscr(); }
printf("%d\n",i);
printf(“e=%f",e);
getch();
}
WRITE A C-PROGRAM TO
GENERATE N PRIME NUMBERS
#include<stdio.h>
#include<conio.h>
main( )
{
int i,N,c,j;
clrscr( );
printf("enter a number");
scanf("%d",&N);
printf("\nprime numbers from 1 to %d\n",N);
for ( i=1; i<=N; i++)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
printf("%d\n",i);
}
getch( );
}
WRITE A C-PROGRAM TO
GENERATE PRIME NUMBERS
FROM N1 TO N2
• #include<stdio.h>
• #include<conio.h>
• int main( )
• {
• int i,N1,N2,c,j;
• //clrscr( );
• printf("enter a number");
• scanf("%d%d",&N1,&N2);
• printf("\nprime numbers from N1=%d to N2=
%d\n",N1,N2);
• for ( i=N1; i<=N2; i++)
• {
• c=0;
• for(j=1;j<=i;j++)
• {
• if(i%j==0)
• c++;
• }
• if(c==2)
• printf("%d\n",i);
• }
• getch( );
• }
WRITE A C-PROGRAM THAT WILL
READ A POSITIVE INTEGER AND
DETERMINE AND PRINTS ITS
BINARY EQUIVALENT
#include<stdio.h>
#include<conio.h>
main()
{
long base=1,N,rem,temp,bin=0;
clrscr();
printf("Enter the deciaml number ");
scanf("%ld",&N);
temp=N;
while(N>0)
{
rem=N%2;
N=N/2;
bin=bin+(base*rem);
base=base*10;
}
printf("Binary equivalent of %ld is:%ld",temp,bin);
getch();
}
WRITE A C-PROGRAM TO PRINT
FOLLOWING OUTPUT

*****
****
***
**
*
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,rows,k,s;
clrscr();
printf("Enter the vale of N\n");
scanf("%d",&rows);
s=rows+1;
for (i=rows ; i>0 ;i--)
{
for (j=1;j<=i; j++)
{
printf ("*");
}
printf ("\n");
for(k=1; k<=s-i; k++)
printf (" ");
}
getch ();
}
To print following output
• 1
• 12
• 123
• 1234
• 12345
• #include<stdio.h>
• #include<conio.h>
• int main ()
• {
• int n,i,j;
• //clrscr();
• printf("Enter value of n:");
• scanf("%d",&n);
• for(i=1;i<=n;i++){
• for(j=1;j<=i;j++){
• printf("%d",(j));
• }
• printf("\n");
• }
• getch();
• }
To print following output
• 1
• 22
• 333
• 4444
• 55555
• #include<stdio.h>
• #include<conio.h>
• int main ()
• {
• int n,i,j;
• //clrscr();
• printf("Enter value of n:");
• scanf("%d",&n);
• for(i=1;i<=n;i++){
• for(j=1;j<=i;j++){
• printf("%d",i);
• }
• printf("\n");
• }
• getch();
• }
Pascal Triangle
• Enter the length of pascal's triangle : 5
• 1
• 121
• 12321
• 1234321
• 123454321
• #include<stdio.h>
• int main(){
• int length,i,j,k;

• //Accepting length from user


• printf("Enter the length of pascal's triangle : ");
• scanf("%d",&length);

• //Printing the pascal's triangle


• for(i=1;i<=length;i++) {
• for(j=1;j<=length-i;j++)

• printf(" ");
• for(k=1;k<i;k++)
• printf("%d",k);
• for(k=i;k>=1;k--)
• printf("%d",k);
• printf("\n");
• }
• getch();
• return 0;
• }

You might also like