Decision Making and Looping
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
#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
1 0 0 0
2 0 1 1
3 0 1 1
4 1 1 2
5 1 2 3
• 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
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.
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
printf(“%d”,c--);
} while( c);
A) 2 1 B) 2 1 0 C) 2210 D) 2211
B) At least once
C) Atmost once
D) None of these
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?
From dec-jan 09
ADDITIONAL FEATURES OF FOR LOOP
• 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
*****
****
***
**
*
#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;