Download as pdf or txt
Download as pdf or txt
You are on page 1of 59

Control Statements

in C
Presented by: P4
Dr. Rakesh Rathi
Assistant Professor & Head
Department of Computer Science and IT
Govt. Engineering College, Ajmer
Introduction
▪ A Program contains several statements, and statements are executed by the compiler.

▪ The sequence of execution of the statements by the compiler is called the execution flow.

▪ The execution flow is of two types:


✔ Sequential Execution
✔ Random Execution

Control Statements in C by Dr. Rakesh Rathi 2


Sequential Execution
▪ Executing the statements one by one in the sequential manner is called sequential execution.

▪ This type of execution is suitable for writing simple programs.

Control Statements in C by Dr. Rakesh Rathi 3


Sequential Execution
Program 1: s=(a+b+c)/2;
To calculate the area of a triangle where the /* Find the area of a Triangle */
measurements of three sides of the triangle are given.
area=sqr(s*(s-a)*(s-b)*(s-c));
#include<stdio.h>
printf(“\nArea of Triangle: %f”, area);
#include<conio.h>
getch();
#include<math.h>
}
void main() Output
{
float a, b, c, s, area;
/* accept the measurement of all 3 sides */ Output
printf(“\n Enter all three sides:”); Domain error, because
values entered are wrong
scanf(“%f%f%f”, &a, &b, &c); and final result is negative,
and which is unacceptable.
/* find the value of s */ That means argument is less
than zero.

Fundamental Concepts in C by Dr. Rakesh Rathi 4


Random Execution
▪ Executing the statements randomly and repeatedly is called random execution.
▪ It is useful to create better and complex program.
▪ It is possible by using control statements.
▪ Control statements are the statements which change the flow of execution in such a way that the
programmer can execute the statements as he likes.
▪ The following are the control statements available in C:
✔ if…else statement
✔ do…while loop
✔ while loop
✔ for loop
✔ switch loop
✔ break statement
✔ continue statement
✔ goto statement
✔ return statement

Note:
▪ A statement is executed only once where as a loop executes repeatedly several times.

Control Statements in C by Dr. Rakesh Rathi 5


Random Execution
if…else statement:
▪This statement is useful to execute a task (one or more statements) depending upon whether a condition is
true or not.
Syntax: if(condition)
statements1;
[else statements2;]
▪Syntax means the correct format of writing a statement.
▪The else part written inside the square braces [], is optional.
▪It means we can write if statement without using else part.
▪If statement can be written in different ways:

if(condition1) if(condition1)
statements1; if(condition2)
else if(condition2) if(condition3)
statements2; statements1;
else if(condition3) else statements2;
statements3; else statements3;
else statements4; else statements4;

Control Statements in C by Dr. Rakesh Rathi 6


Random Execution
Program 2: printf(“\n%d is positive.”,num);
Write a program to test whether a given number is else printf(“\n%d is negative.”,num);
positive or negative.
getch();
#include<stdio.h>
}
#include<conio.h>
void main()
{
int num;
printf(“\n Type an Integer Number:”);
scanf(“%d”, &num);
if(num==0)
printf(“\n It is zero.”);
else if(num>0)
Output

Fundamental Concepts in C by Dr. Rakesh Rathi 7


Random Execution
Program 3: printf(“\n%d is a leap year”, year);
Write a program to decide whether a given year is leap else printf(“\n%d is not a leap year”, year);
year or not.
getch();
#include<stdio.h>
}
#include<conio.h>
void main()
{
int year;
printf(“\n Enter a year:”);
scanf(“%d”, &year);
/* if the year is not century year and divisible by 4 or if the
year is century year and divisible by 400, then it is leap
year */
if(year%100!=0 && year%4==0 || year%400==0) Output

Fundamental Concepts in C by Dr. Rakesh Rathi 8


Random Execution
Program 4: if(d>0)
Write a program to find the roots of a quadratic equation. {
#include<stdio.h> printf(“\nRoots are unequal and real”);
#include<conio.h> x1=(-b+sqrt(d))/(2*a);
#include<math.h> x2=(-b-sqrt(d))/(2*a);
void main() printf(“\nRoot1:%.2f”, x1);
{ printf(“\nRoot1:%.2f”, x2);
int a, b, c; }
float x1, x2, x, d, r, r1; else if(d==0)
printf(“\n Type the values of a, b, c:”); {
scanf(“%d%d%d”, &a, &b, &c); printf(“\nRoots are equal and real”);
/* find the discriminator*/ x=-b/(2*a);
d=b*b-4*a*c; printf(“\nRoot1:%.2f”, x);

Fundamental Concepts in C by Dr. Rakesh Rathi 9


Random Execution
printf(“\nRoot2:%.2f”, x);
}
else
{
printf(“\nRoots are complex and imaginary”);
d= -d;
r= -b/(2*a);
r1= sqrt(d)/(2*a);
printf(“\nRoot1:%.2f+%.2fi”, r, r1);
printf(“\nRoot2:%.2f-%.2fi”, r, r1); Output

}
getch();
}

Fundamental Concepts in C by Dr. Rakesh Rathi 10


Random Execution
do…while loop:
▪This loop is useful to execute a group of statements repeatedly as long as a condition is true. Once the
condition becomes false, the loop terminates.
Syntax: do{
statements;
}while(condition);
▪First of all the compiler executes the statements and then tests the condition.
▪If the condition is true, then it goes back and executes the statements the second time.
▪In this way the statements are executed repeatedly as long as the condition is true.
▪Once the condition becomes false, the loop terminates.
Example: int digit = 0;
do
printf(“%d\t”, digit+=2);
while(digit<10);

Output: 2, 4, 6, 8, 10

Control Statements in C by Dr. Rakesh Rathi 11


Random Execution
Program 5: num/= 10; /* remove rightmost digit from num*/
Write a program to find the sum of digits of a number. }
#include<stdio.h> while(num>0);
#include<conio.h> printf(“\nThe sum of digits:%d”, sum);
void main() getch();
{ }
int num, digit, sum = 0;
printf(“\n Enter a number:”);
scanf(“%d”, &num);
do
{
digit = num%10; /* extract rightmost digit */
sum+= digit; /* add the digit to sum */ Output

Fundamental Concepts in C by Dr. Rakesh Rathi 12


Random Execution
while loop:
▪while loop is also same as do…while loop.
▪In while loop, the condition is tested first and then only the statements are executed.
▪while loop executes a group of statements repeatedly as long as a condition is true just like do…while.
Syntax: while(condition)
{
statements;
}
▪Compiler tests the condition first.
▪If the condition is true, then the statements in the while loop are executed repeatedly.
Example: char response = ‘y’;
while(response == ‘Y’ || response == ‘y’)
{
puts(“Hello”);
puts(“want to continue?”);
response = getchar();
}

Control Statements in C by Dr. Rakesh Rathi 13


Random Execution
Program 6: fact*=i;
Write a program to find the factorial value of a number. i++;
#include<stdio.h> }
#include<conio.h> printf(“\nFactorial of %d is %ld.”, n, fact);
void main() getch();
{ }
int i, n;
long int fact= 1;
printf(“\n Enter a positive integer:”);
scanf(“%d”, &n);
i=1;
while(i<=n)
{ Output

Fundamental Concepts in C by Dr. Rakesh Rathi 14


y
X =X*X*X*X*X……. Up to Y times

Result=Result * X

Fundamental Concepts in C by Dr. Rakesh Rathi 15


Random Execution
Program 7: /* finding power value of a no. using while loop */
Write a program to calculates the power value using our while(counter<=power)
own logic as well as the pow() function.
{
#include<stdio.h>
result1*=number;
#include<conio.h>
++counter;
#include<math.h>
}
void main()
printf(“\n\n Result using while loop\n”);
{
printf(“\n%d raised to the power of %d:%d”, number,
int number, power, counter=1; power, result1);
long int result1=1; /* finding power value of a no. using pow( ) function
*/
long int result2=2;
result2= pow(number, power);
printf(“\n Enter number, and its power:”);
printf(“\n\n Result using pow function \n”);
scanf(“%d%d”, &number, &power);

Fundamental Concepts in C by Dr. Rakesh Rathi 16


Random Execution
printf(“\n %d raised to the power of %d:%d”, number, power, result2);
getch();
}

OUTPUT

Fundamental Concepts in C by Dr. Rakesh Rathi 17


Random Execution
for loop:
▪for loop is useful to execute a group of statements as long as a condition is true.
▪This loop is more compact and complex than do…while or while loops.
▪Programmer knows how many times exactly the statements should be executed.
▪Statements are executed fixed numbers of times.
Syntax: for(expression1; expression2; expression3)
{
statements;
}

Example: for(i=1; i<=10; i++)


{
printf(“\n%d”, i);
}
Note:
▪expression1 is called ‘initialization expression’, expression2 is called ‘conditional expression’, and
expression3 is called ‘modifying expression’.

Control Statements in C by Dr. Rakesh Rathi 18


Random Execution
Program 8:
Write a program to display numbers from 1 to 10 using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1; i<=10; i++)
printf(“\n%d”, i);
getch();
}

OUTPUT

Fundamental Concepts in C by Dr. Rakesh Rathi 19


Random Execution
▪ It is possible to write for loop without using any one expression or any two expressions or all the three
expressions.
▪ for loop without expression1.
int i=1;
for(; i<10; i++) /* Here, i value is declared outside the for loop */
{
printf(“\n%d”, i);
}

▪ for loop without expression 1 and expression 3.

int i=1;
for(; i<10; ) /* Here, declaration of i value is done outside the for loop and increment of
i value can be done inside the loop after displaying i value */
{
printf(“\n%d”, i);
i++;
}

Control Statements in C by Dr. Rakesh Rathi 20


Random Execution
▪ for loop without all the three expressions.
int i=1;
for( ; ; ) /* expression2 not present, which gives condition, so loop goes to infinite loop */
{
printf(“\n%d”, i);
i++;
}

infinite loop:
▪ An infinite loop is a loop which executes forever.
▪ Infinite loops can be created using for, do…while or while loops also.
▪ In do…while or while loops, infinite loops created by taking 1 in the place of the condition.
▪ in for loops, infinite loops created by not providing any condition in expression2.

Using for() Using do…while Using while loop


for ( ; ; ) do { while(1)
{ statements; {
statements; }while(1); statements;
} }
Control Statements in C by Dr. Rakesh Rathi 21
Random Execution
▪ infinite loops can be terminated by using break statement.
▪ break statement can be used in for, while, do…while loops to terminate the loops and come out of them.

int i=1;
for( ; ; )
{
printf(“\n%d”, i);
i++;
if(i>10) break;
}

Control Statements in C by Dr. Rakesh Rathi 22


Random Execution
Program 9:
Write a program to display a required multiplication table with 20 rows, using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, no;
printf(“\nWhich Table to display:”);
scanf(“%d”, &no);
for(i=1; i<=20; ++i)
printf(“\n%-6dX%6d\t=%6d”, i, no, i*no);
getch();
} OUTPUT

Fundamental Concepts in C by Dr. Rakesh Rathi 23


Random Execution
▪ It is possible to use more than one initialization expressions, more than one conditional expressions and
more than one modifying expressions in a for loop.

Control Statements in C by Dr. Rakesh Rathi 24


Random Execution
Program 10:
Write a program to understand that it is possible to write a for loop with more than one initialization
expressions, more than one conditional expressions and more than one modifying expressions.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
for(i=1, j=10, k=10; i<=10, j>=4; i++, j--)
printf(“\n%d\t%d\t%d”, i, j, k);
getch();
}
OUTPUT

Fundamental Concepts in C by Dr. Rakesh Rathi 25


Fibonacci number series

▪Fibonacci series of 10 no’s

▪= 0 1 1 2 3 5 8 13 21 34

Fundamental Concepts in C by Dr. Rakesh Rathi 26


Random Execution
Program 11: if(n==1) printf(“\n%lu”, f1);
Write a program to generate Fibonacci number series. else if(n==2)
#include<stdio.h> {
#include<conio.h> printf(“\n%lu”, f1);
void main() printf(“\n%lu”, f2);
{ }
int i, n; else
long unsigned int f1, f2, f; {
printf(“\n How many Fibonaccis you want:”); printf(“\n%lu”, f1);
scanf(“%d”, &n); printf(“\n%lu”, f2);
/* take first two Fibonaccis as 0 and 1 */ {
f1=0; /* add the two previous nos to get new number */
f2=1; For(i=1;i<=n-2;i++)

Fundamental Concepts in C by Dr. Rakesh Rathi 27


Random Execution
f=f1+f2;
printf(“\n%lu”, f);
f1=f2;
f2=f;
}
}
}
getch();
}

OUTPUT

Fundamental Concepts in C by Dr. Rakesh Rathi 28


Random Execution
switch statement:
▪switch statement is useful to execute a particular task from among several tasks depending on the value of a
variable or expression.
Syntax: switch(variable or expression)
{
case value1: statements1;
break;
case value2: statements2;
break;
:
case valuen: statementsn;
break;
[default: default_statements;]
}

▪Statements will be executed according to the variable value entered matches case value.
▪If no variable value matches case value, then default statements will be executed.
▪break statement in the switch block is useful to come out of the block.

Control Statements in C by Dr. Rakesh Rathi 29


Random Execution
Program 12: case 1: printf(“\nGreen Colour”); break;
Write a program to show working of switch statement, to case 2: printf(“\nBlue Colour”); break;
print color.
case 3: printf(“\nBlack Colour”); break;
#include<stdio.h>
default: printf(“\n Enter choice from 0 to 3”);
#include<conio.h>
}
void main()
getch();
{
}
int i;
printf(“\n Enter Your Choice:”);
scanf(“%d”, &i);
switch(i)
{
case 0: printf(“\nRed Colour”); break;
Output

Fundamental Concepts in C by Dr. Rakesh Rathi 30


Random Execution
▪ If break statement is not found in any block than next blocks will also get execute with that, till break
statement will found.
▪ In switch statement, while writing the variable value, it is not possible to use float, double or string type
variable, only int type or char type variables can be used.
▪ switch statement is generally used in menu driven program.
▪ Some of the menu driven functions:
✔ clrscr();
✔ gotoxy();
✔ getche();

Note:
▪ These functions will not work in UNIX C. They work only in Turbo C/C++ compiler.

Control Statements in C by Dr. Rakesh Rathi 31


Random Execution
clrscr() function:
▪clrscr() function is useful to clear the screen display.
▪This function is defined in the header file <stdlib.h>

gotoxy() function:
▪This function is useful to place the cursor at any position on the screen.
▪The screen size in DOS is 80 columns width and it accommodates 25 rows.
gotoxy(30, 5); /* goto column 30 and row 5 */
▪gotoxy() function is defined in the header file <conio.h>

getche() function:
▪This function is similar to getchar() in that it receive a character from the keyboard and stores it into a
variable.
▪In getche(), need not to press the enter button, when character is typed getche() receives it automatically.
▪It displays the character being received.
▪The ‘e’ for ‘echo’ at the end of the getche() name, ‘echo’ means ‘display’.

Control Statements in C by Dr. Rakesh Rathi 32


Random Execution
Function Header file Need to press Enter? Is character displayed?
getchar() <stdio.h> Yes Yes
getche() <conio.h> No Yes
getch() <conio.h> No No

exit(0) function:
▪Useful to terminate the program.
▪This function is defined in the header file <stdlib.h>.
▪Integer number is passed to this function, generally a 0 or non zero value.
▪A 0 represents that the program is terminated normally.
▪A non zero value like a 1 represents that the program is terminated because of some error.
▪0 or non zero value which is passed to this function is called ‘status code’, it represents the status of program
termination.

Control Statements in C by Dr. Rakesh Rathi 33


Random Execution
Program 13: gotoxy(32,5); printf(“ARITHMETIC OPERATIONS”);
Write a program to display a menu with arithmetic gotoxy(32,6); printf(“--------------------------------”);
operations and perform the required operation depending
upon the user choice. gotoxy(32,8); printf(“1. Addition”);

#include<stdio.h> gotoxy(32,9); printf(“2. Subtraction”);

#include<conio.h> gotoxy(32,10); printf(“3. Multiplication”);

#include<stdlib.h> gotoxy(32,11); printf(“4. Division”);

void main() gotoxy(32,12); printf(“5. Exit”);

{ gotoxy(32,13); printf(“--------------------------------”);

char choice; gotoxy(32,14); printf(“Your Choice:”);

float a,b; /* user choice is received as a char */

/* clear the screen */ gotoxy(48,14); choice= getche();

clrscr(); /* terminate the program if choice is 5 */

/* display the menu in the center of screen */ if(choice==‘5’) exit(0);

Fundamental Concepts in C by Dr. Rakesh Rathi 34


Random Execution
gotoxy(32,16); printf(“Enter two numbers:”); break;
gotoxy(32,17); scanf(“%f, %f, &a, &b); case ‘4’: gotoxy(32, 18);
/* perform a task depending upon user choice */ printf(“Result of division:%10.4f”, a/b);
Switch(choice) break;
{ }
case ‘1’: gotoxy(32, 18); getch();
printf(“Result of addition:%10.4f”, a+b); }
break;
case ‘2’: gotoxy(32, 18);
printf(“Result of subtraction:%10.4f”, a-b);
break;
case ‘3’: gotoxy(32, 18);
printf(“Result of multiplication:%10.4f”, a*b); OUTPUT

Fundamental Concepts in C by Dr. Rakesh Rathi 35


Random Execution
break statement:
▪break statement is used in two ways:
✔ To come out of a loop.
✔ To come out of switch block.

Control Statements in C by Dr. Rakesh Rathi 36


Random Execution
Program 14: getch();
Write a program to terminate a do…while loop using }
break.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
do{
Output
printf(“\t%d”, i);
i++;
if(i>5) break; /* if i value is 6 terminate */
} while(1); /* this is infinite loop */

Fundamental Concepts in C by Dr. Rakesh Rathi 37


Random Execution
Program 15: if(percent > 100)
Write a program to display the division obtained by a {
student depending on his percentage of marks.
printf(“\nWrong Percentage”);
#include<stdio.h>
exit(0);
#include<conio.h>
}
#include<stdlib.h>
/* depending on percentage find the divisible */
void main()
switch(percent/10)
{
{
int percent;
case 0:
/* accept percentage from the user */
case 1:
printf(“\nEnter Percentage for Division:”);
case 2: printf(“\nFail”); break;
scanf(“%d”, &percent);
case 3:
/* if percentage is above 100, terminate */
case 4: printf(“\nThird Division”); break;

Fundamental Concepts in C by Dr. Rakesh Rathi 38


Random Execution
case 5: printf(“\nSecond Division”); break;
case 6:
case 7:
case 8:
case 9:
case 10: printf(“\nFirst Division”); break;
}
getch();
}

OUTPUT

Fundamental Concepts in C by Dr. Rakesh Rathi 39


Random Execution
continue statement:
▪This statement is useful to continue with the next repetition of a loop.
▪When continue inside a loop is executed, the flow of execution goes back to the starting of the loop and next
repetation will take place.
▪When continue inside a loop is executed, the subsequent statements in the loop are not executed but the
statements preceding the continue will be executed.
syntax: continue;

Control Statements in C by Dr. Rakesh Rathi 40


Random Execution
Program 16: if(i>5)
Write a program to understand the use of continue in a continue; /* this is executed when i=10 to 6 */
for loop.
printf(“%d”, i); /* this is executed when i=5 to 1 */
#include<stdio.h>
}
#include<conio.h>
getch();
void main()
}
{
int i;
/* loop to display from 10 to 1 */
for(i=10; i>=1; i--)
{
/* it is always executed */
printf(“\nBefore continue:”);
Output

Fundamental Concepts in C by Dr. Rakesh Rathi 41


Random Execution
goto statement:
▪goto statement is useful to directly jump to a particular statement in the program.
syntax: goto LABEL;
▪LABEL represents a string constant that points to a statement where the compiler should jump.
Example: printf(“\nstatement one”);
printf(“\nstatement two”);
goto TEN; /* jump from here */
printf(“\nstatement three”);
:
:
TEN: printf(“\nstatement ten”);
/* jump to this statement */
printf(“\nstatement eleven”);

Note:
▪goto statements are not recommended in programming. This main reason is that they form infinite loops
easily which increase the complexity of a program.

Control Statements in C by Dr. Rakesh Rathi 42


Random Execution
Program 17: if(ch==‘0’) exit(0);
Write a program to test whether a given character is if(ch>=65 && ch<=90) /* test if ch is uppercase */
upper case letter or lower case letter.
printf(“\nIt is uppercase letter”);
#include<stdio.h>
else if(ch>=97 && ch<=122) /* test if ch is lowercase
#include<conio.h> */
#include<stdio.h> printf(“\nIt is lowercase letter”);
void main() else printf(“\nIt is a special character”);
{ goto START; /* jump to START and repeat */
char ch; getch();
printf(“\nEnter 0 to stop…”); }
START: printf(“\nEnter a character:”);
ch=getche();
/* if 0 entered, terminate */

Fundamental Concepts in C by Dr. Rakesh Rathi 43


Random Execution

Output

Fundamental Concepts in C by Dr. Rakesh Rathi 44


Random Execution
return statement:
▪This statement is used in two ways.
✔ To terminate the function and come back to the calling function.
syntax: return;
✔ To return a value to the calling function.
syntax: return value;
Example: return x; /* x value is returned to calling function */
return 100; /* 100 is returned */
▪Any function (like main() function) contains a group of statements.
returntype myfunction()
{
statements;
}
▪Observe the returntype before the function name.
▪If the function returns an int value, then the returntype will be ‘int’.
▪If the function returns a float type value, then the returntype will be ‘float’.
▪If the function does not return any value, then write ‘void’ before the function name, ‘void’ means ‘no value’ will
be returned by the function.
Control Statements in C by Dr. Rakesh Rathi 45
Random Execution
▪ It is possible to call one function from another function.
void m1() /* void = no value is returned by this function */
{
printf(“\n Inside m1() function.”);
return; /* terminate this function */
}
void main()
{
printf(“\n Inside main() function.”);
m1(); /* call m1() function */
printf(“\n Returned from m1() function.”);
}

▪ The output will be:


Inside main() function.
Inside m1() function.
Returned from m1() function.

Control Statements in C by Dr. Rakesh Rathi 46


Random Execution
▪ return statement also be used to return some value to the calling function.
int m1() /* int means this function returns integer value */
{
printf(“\n Inside m1() function.”);
return 100; /* terminate this function and return 100 */
}
void main()
{
int x;
printf(“\n Inside main() function.”);
/* call m1() function and get returned value into x */
x = m1();
printf(“\n Returned from m1() function.”);
printf(“\n Returned value from m1() is %d”, x);
}
▪ The Output will be:
Inside main() function.
Inside m1() function.
Returned from m1() function.
Returned value from m1() is 100
Control Statements in C by Dr. Rakesh Rathi 47
Random Execution
Program 18: {
Write a program to call a function which calculates double y, result;
square root value of a given number and return that
value. printf(“\nEnter a number:”);

#include<stdio.h> scanf(“%lf”, &y);

#include<conio.h> /* call squareroot( ) function and get square root of y


*/
#include<math.h>
result= squareroot(y);
/* This function takes y value and returns its square root*/
printf(“\nSquare root value: %lf”, result);
double squareroot(double y)
getch();
{
}
double x= sqrt(y); /* store square root of y in x */
return x; /* return square root of y */
}
Output
void main()

Fundamental Concepts in C by Dr. Rakesh Rathi 48


▪Sine Series

▪Sin (x) = x - x3/3! + x5/5! - x7/7!+……

Fundamental Concepts in C by Dr. Rakesh Rathi 49


Random Execution
Program 19: /* convert the angle from degrees into radians */
Write a program to evaluate Sine series. r=(x*3.14159)/180;
#include<stdio.h> /* this become the first term */
#include<conio.h> t=r;
#include<math.h> /* till now, find the sum */
void main() sum= r;
{ /* display the iteration number and sum */
int n, i, j=1; printf(“\nIteration:%d\tSum:%lf”, j, sum);
double x, r, t, sum; /* denominator for the second term */
printf(“\nEnter no. of iterations:”); i= 2;
scanf(“%d”, &n); /* repeat for 2nd to nth terms */
printf(“\nEnter the value of angle:”); for(j=2; j<=n; j++)
scanf(“%lf”, &x); {

Fundamental Concepts in C by Dr. Rakesh Rathi 50


Random Execution
/* find the next term t */
t=((-1)*t*r*r)/(i*(i+1));
/* add it to the sum */
sum= sum+t;
/* display iteration number and sum till then */
printf(“\nIteration:%d\tSum:%lf”, j, sum);
/* increase the i value by 2 for denominator in next term*/
i= i+2;
}
getch();
}
Output

Fundamental Concepts in C by Dr. Rakesh Rathi 51


Exponential series

x 1 2 3
e = 1 + x /1 + x /2! + x /3! + ….

Fundamental Concepts in C by Dr. Rakesh Rathi 52


Random Execution
Program 20: t=1;
Write a program to evaluate exponential series. /* till now, find the sum */
#include<stdio.h> sum= t;
#include<conio.h> /* display the iteration number and sum */
void main() printf(“\nIteration:%d\tSum:%lf”, j, sum);
{ /* repeat for 2nd to nth terms */
int n, j=1; for(j=1; j<n; j++)
double x, t, sum; {
printf(“\nEnter no. of iterations:”); /* find the next term t */
scanf(“%d”, &n); t= t*x/j;
printf(“\nEnter the power value:”); /* add it to the sum */
scanf(“%lf”, &x); sum=sum+t;
/* the first term is 1 */ /* display iteration number and sum till then */

Fundamental Concepts in C by Dr. Rakesh Rathi 53


Random Execution
printf(“\nIteration:%d\tSum:%lf”, j+1, sum);
}
getch();
}

Output

Fundamental Concepts in C by Dr. Rakesh Rathi 54


To Do yourself: Home Work
1. C program to check whether a given number is even or odd without using modulus operator.
2. Program to find the reverse of a given number.
3. Program to check whether a given number is a palindrome or not. (e.g. 121, 535, etc.)
4. Program to check whether a given number is perfect or not. (e.g. 6=1+2+3)
5. Program to print first n perfect numbers.
6. Program to check whether a given number is an Armstrong number or not. (e.g. 153=13 + 53 + 33 ).
7. Program to find sum of all odd numbers that lie between 1 and n.
8. Program to find the sum of series:
a) 1+(1+2)+(1+2+3)+(1+2+3+4)…..n terms.
b) 12 + 22 + 32 +…….n terms.
c) 1 + 1/2 + 1/3 +….n terms.

9. Print the figures by using digits and symbol ( * ) as given below:


(a) 1 (b) 1 (c) 1 2 3 4 5 (d) 1
2 3 2 2 3 1 2 3 1 2 3
3 4 5 4 3 4 5 6 1 1 2 3 4 5
4 5 6 7 6 5 4 7 8 9 10 1 2 3
1
Control Statements in C by Dr. Rakesh Rathi 55
Interview Questions?
Q1. What are control statements?
Ans. The statements which control the flow of execution are called control statements. Control statements are useful to
write complex programs.
Q2. do…while loop and while loop – which one is more efficient?
Ans. do…while loop executes the statements at least once and after that the condition is tested. While loop first tests the
condition and then only executes the statements. In case of while loop, the control is better because the programmer can
execute the statements either 0 or more times. But in case of do…while loop, the control does not come into the hands of
the programmer till it executes the statements at least once. So, while loop is more efficient than do…while.
Q3. What is the difference between exit(0) and exit(1)?
Ans. Both will terminate the program. exit(0) represents normal termination of the program. exit(1) represents that the
program is being terminated because of an error.
Q4. Why goto statements should be avoided in a program?
Ans. 1. goto statements form infinite loops which make the program un-terminated. This gives confusion for the user.
2. goto statements increase the complexity and decrease the readability (understanding) of the program. When several
gotos are used, the programmer cannot understand the flow of execution.
3. goto statements make debugging (removing the errors) very difficult.
4. gotos are not part of structured programming. This means, without goto statements it is perfectly possible to write any
C program.

Control Statements in C by Dr. Rakesh Rathi 56


Interview Questions?
Q5. What is the difference between return and exit()?
Ans. ‘return’ is a statement is useful to come out of a function back to its calling function. ‘exit()’ is a function useful to
terminate the application (or software) and reach the DOS prompt. When ‘return’ is used inside main() function then it
works like exit() and terminates the entire application.
Q6. What is the difference between break and continue statements?
Ans. The break statements will immediately jump at the end of the current block of code. The continue statement will skip
the rest of the code in the current loop and will return to the beginning of the loop.
Q7. What is Domain Error in C programs?
Ans. Domain Error is a error, which generates when any function is passing a unacceptable value. Domain error
generates in the functions like sqrt(), etc. In sqrt() function Domain Error generates when argument is less than zero.

Control Statements in C by Dr. Rakesh Rathi 57


Queries???

Control Statements in C by Dr. Rakesh Rathi 58


Thanks!!!

Control Statements in C by Dr. Rakesh Rathi 59

You might also like