C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
Module II
Managing Input and output operations, Conditional Branching
and Loops
STATEMENT
> A statement is also called instruction.
> As the name indicates, instruction is used to instruct computer to perform a particular
task like adding two numbers reading data from keyboard.
Example:
sum=atb;
scanf ("sd ", &n);
In, the semicolon (;) is a statement terminator.
‘Compound Statement or Block:
‘The sequence of statement enclosed within a pair of braces { and } is called a compound
statement,
Example:
t
a=l*b;
printf ("area =d", a);
3
Managing Input and output operations
‘There are 2 types of I/O Functions as shown below:
Console Input/Output functions ]
Formatted functions ‘Unformatted functions
Type | Input | Ourpur Type | Input — | Output
char] seanfi) | printf() char [eeteh() | puteh()
getche() | putchar( )
getchar()
int | seanfi) | printf) int - 5
float | seanf() | printf) float = 5
String | seanfi) | printf) string [ ges) __| pu)
UNFORMATTED VO.
Unformatted 1/0 refers to an /O data has not been arranged in any order and user don’t have
control on the arrangement of data.
Dept of CSE, Canara Engineering College. Page 60C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
Reading a character
getchart ):
> Read a character from the keyboard and store this character into a memory-location,
‘You have to press ENTER key after typing a character.
Syntax
variable_name=getchar( );
char name;
name= getchart );
Example:
Writing a character
putchar( ):
> Displays a character stored in the memory-location on the screen,
Syntax
‘putchar(variable_name);
putchar(name );
Example:
Program example to read a character and display it.
#include
Void main()
t
char x;
printf("enter one letter terminated by ENTER key \n”);
x = getchar();
putchar(‘\n’);
putchar (x) ;
3
Output:
enter one letter terminated by ENTER key
a
a
Disadvantage of Unformatted 1/0
It is not possible to read/print any other data except characters ie. it is not possible to
read/print integer numbers, floating point numbers ete,
The “ctype.h” supports for character with many inbuilt function and the table 4.1 lists the
some of the functions available in it.
Dept of CSE, Canara Engineering College. Page 61C Programming for Problem Solving (17CPS13/23):Module Il __ Santhosh Kumar D K
Table 4.1 Character Test Functions
ere Is c an alphanumeric character?
isal Is can alphabetic character?
isdigit(c) Isca digit?
islower(c) Isc lower case letter?
'sprint(c) \s ca printable character?
ispunct(c) oe ‘| ls ca punctuation mark?
isspace(c) _ | Is ca white space character?
isupper(c) ___| Is can upper case letter?
FORMATTED VO
scanf():
The scanf( ) standard library function is used to read one or more values from the standard
input (keyboard) and assign them to specified variables.
Syntax:
seanf(“ControlString or format specifier
Example:
sant "“fod fF Ge", &ex, &y, &z);
Table 4.2 Commonly used scanf Format Codes
read a single character,
read a decimal integer
read a floating point value
read a floating point value
read an unsigned decimal integer
read a hexadecimal integer
read a string of word(s)
The following letters may be used as prefix for certain conversion characters.
for short integers
| for long integers or double
for long double
> A scant) always contains a control string or format string in quotation marks.
> Each value to be printed needs a conversion specification like Yed to hold its place in
the control string
Dept of CSE, Canara Engineering College. Page 62C Programming for Problem Solving (17CPS13/23):Module Il __ Santhosh Kumar D K
> There should be a comma ( , ), between the control string and the addresses of
variables.
printf()
‘The printf () standard library function is used to print the values of expressions on standard output
(ie, display) in a specified format.
Syntax:
18 li
les or express
Example
printf("%ed %ef Se", x, y, 2.95
printf ( jalways contains a control string or format string in quotation marks.
The control string may or may not be followed by some variables or expressions
whose value we want printed.
> Each value to be printed needs a conversion specification like %d to hold its place in
the control string,
> There should be a comma (,) between the control string and the list of variables.
> The symbol “\n” (called newline character) in the control string tells the machine to
jump to a new line, If there are variables or expressions to be printed, commas are
used to separate them form the control string and each other.
vv
Table 4.3 Commonly used printf Format Codes
print a single character
print a decimal integer
print a floting point value in exponent form
print a floating point value without exponent
print floating point vale either e-type or type depending on
i print a signed decimal integer
%0 print an octal integer, without leading zero
%s print sting
| %u print an unsigned decimal integer
x ____| print 2 hexadecimal integer, without leading Ox
Dept of CSE, Canara Engineering College. Page 63C Programming for Problem Solving (17CPS13/23):Module Il __ Santhosh Kumar D K
VARIATIONS IN OUTPUT FUNCTION
Table 4.4 Commonly used Output Format Flags
See Ssracemaccoesece
one
oe
oo
Causes a decimal pointto be presentin all floating point numbers, even if itis whole
# (with 0 oF x)
# (with e, forg)
Format
printie%67-4F-y)
printie%7-2F-y)
printi%-7.2F.y)
printes6Fy)
printf(%10.2e",y) -
printi-%11.40"-y) Et 2]
printt(“%-10.2e"y) T-Tetetel-lo17|
print%e"y) CI a
‘specification
Format Output
printt(‘%d”, 9876) s[8 [7 [5 ad
printf(“%6d", 9876) o[s|7l|sé
printf("%2d", 9876) 9[s|[7|6
printf(“%06d" 9876) PARSE e
printf("%06d" 9876) 0. | 0: [29s | Semler
Page 64
Dept of CSE, Canara Engineering College.C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
CONTROL STRUCTURES
A program is nothing but the execution of sequence of one or more instructions.
> Quite often, it is desirable to change the order of execution of statements based on
certain conditions or
This involves a kind of decision making to see whether a particular condition has
occurred or not and direct the computer to execute certain statements accordingly.
Based on application, it is necessary / essential
To alter the flow of a program
Test the logical conditions
Control the flow of execution as per the selection these conditions can be placed in
the program using decision-making statements.
v
vv ov
vv
C SUPPORTS MAINLY FOUR TYPES OF CONTROL STATEMENTS
1. Decision making statements
i. if-statement
ii, if else statement
nested if statement
else if ladder
¥. switch statement
2. Loop control statements
i. while loop
ii, for loop
iii, do-while loop
3. Conditional control (jump) statements
i. break statement
ii, continue statement
4. goto statement (unconditional jump)
BASIC CONCEPT OF DECISION STATEMENTS
> Decision making is critical to computer programming.
> There will be many situations when you will be given 2 or more options and you will
have to select an option based on the given conditions.
> The following flow diagram shows how conditions work in C.
Dept of CSE, Canara Engineering College. Page 65C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
1. Decision making statements
i, THE if STATEMENT
is is basically a “one-way” decision statement.
isis used when we have only one alternative.
The syntax is shown below:
if (expression)
t
True part
statement;
2
Rest of the code;
> Firstly, the expression is evaluated to true or false. Only if the expression is true
then true part statements will be executed.
The flow diagram is shown below:
false statement
rest of the code
Program to illustrate the use of if statement.
#include
void main()
{
int n;
printf ("Enter any non-zero integer: \n”) ;
scanf("$d”, &n);
if (n>0)
printf ("Number is positive number ”);
4£(n<0)
printf ("Number is negative number “);
J
Output :
Enter any non-zero integer:
7
Number is positive number
Dept of CSE, Canara Engineering College. Page 66C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
‘THE if-else STATEMENT
> This is basically a “two-way” decision statement.
> This is used when we must choose between two alternatives.
‘The syntax is shown below:
if (expression)
£
True part
Statement1;
}
else
f
False part
Statement2;
,
Rest of the code;
> Firstly, the expression is evaluated to true or false. Only if the expression is true then
true part statements will be executed otherwise false part statements are executed.
The flow diagram is shown below:
¥
~ true
<< expression >
;_——
statement2 statement
rest of the code
Program to illustrate the use of if else statement.
#include
void main()
t
int n;
print£("Enter any non-zero integer: \n") ;
scanf("$d”, &n)
if(n>0)
printf (“Number is positive number”)
else
printf ("Number is negative number”);
J
Output:
Enter any non-zero integer: 7
Number is positive number
Dept of CSE, Canara Engineering College. Page 67C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
THE nested if STATEMENT
> Anif-else statement within another if-else statement is called nested if statement.
> This is used when an action has to be performed based on many decisions. Hence, it is
called as multi-way decision statement.
The syntax is shown below:
if (expr)
t
if (expr2)
statement1;
else
statement2;
}
else
f
if (expr3)
‘statement3;
else
statement4;
2
Rest of the code;
> Firstly, the expression is evaluated to true or false. Only if the expri is true then true
part statements will be executed where again it checks for expr? otherwise false part
statements are executed.
rent of code
Program to select and print the largest of the 3 numbers using
nested “if-else” statements.
void main()
f
int a,b,c;
Dept of CSE, Canara Engineering College. Page 68C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
printf ("Enter Three Values: \n”);
scanf("$d td td”, 6a, &b, &¢);
printf ("Largest Value is: ") ;
if (a>b)
€
if (arc)
print£(" 8d”, a);
else
print£(" $d”, ¢);
2
else
£
4f(b>c)
print£(" $d", b);
else
printf (" $d", ¢);
2
}
output:
Enter Three Values:
786
Largest Value is: 8
THE else if LADDER STATEMENT.
> This is basically a “multi-way” decision statement.
> This is used when we must choose among many alternatives.
The syntax is shown below:
if (expression1)
statement1;
else if (expression2)
statement2;
else if (expression3)
statement3
else if (expression4)
statement4;
else
default statements;
‘The expressions are evaluated in order (ie. top to bottom).
oy
If an expression is evaluated to true, then
statement associated with the expression is executed &
control comes out of the entire else if ladder
For ex, if exprression! is evaluated to true, then statement! is executed.
vvvv
If all the expressions are evaluated to false, the last statement4 (default case) is
executed.
Dept of CSE, Canara Engineering College. Page 69C Programming for Problem Solving (17CPS13/23): Module _ Santhosh Kumar D K
er
[Nextsrarement |
Program to illustrate the use of else-if-ladder statement.
void main ( )
t
int n;
printf("Enter any integer:”) ;
scanf("$d", &n)
if(n>0)
printf ("Number is Positive");
else if(n< 0)
printf ("Number is Negative");
else if(n== 0)
printf ("Number is Zero");
else
printf ("Invalid input");
2
output :
Enter any integer: 7
Number is Positive
‘THE switch STATEMENT
> This is basically a “multi-way” decision statement.
> This is used when we must choose among many alternatives.
> Here, choice can be either any integer value or a character.
Dept of CSE, Canara Engineering College. Page 70C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
> Based on this integer value, the control is transferred to a particular case-value
where necessary statements are executed.
> During executing, if break statement is encountered, then the control comes out of
the switch block.
» If the value of the choice does not match with any of the case values (i.e. valuel,
value2, value3) then control goes to default label.
‘must be different.
\gram is shown below:
> All case-valu
The syntax & flow
switch (choice)
t
case 1: statement1; Soe
break;
case 2: statement2; ast mes
break;
case 3: statement3; oe
— statement
case i: statementi;
break; S83 sotement
case N: statementN;
break; defeat [1
default: default statemetns; com
/* Program to simulate simple calculator by performing arithmatic
operations*/
#include
void main()
t
int op1, op2, ans;
char op;
print£("\n Enter an arithmatic expression");
scanf ("$dictd", €op1, 6p, 6op2) ;
switch (op)
t
case '+!:ans=opltop2;
printf ("\n Sum=$d", ans);
breal
:ans=op1-op2;
print£("\n Difference=%a",ans) ;
break;
sans=opl*op2;
printf ("\n Product=$d", ans) ;
break;
148 (op2==0)
f
print£("\n Division by zero is not possible");
break;
Dept of CSE, Canara Engineering College. Page 71C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
,
ans=op1/op2;
print£("\n Quotient=$d", ans) ;
break;
case '8':ans=opltop2;
printf ("\n Remainder=td", ans) ;
break;
default :printf("\n Invalid operator") ;
}
i
BASIC CONCEPT OF LOOP
> Let's consider a situation when you want to write same message N times.
> C language provides a concept called loop, which helps in executing one or more
statements up to desired number of times.
> Loops are used to execute one or more statements repeatedl
> Every loop statements has three main part in it, ie. Loop init
condition and Loop update.
‘The flow diagram is shown below:
Start
lization, Loop test-
Program continues.
THE while LOOP
A while loop statement can be used to execute a set of statements repeatedly as long as a
given condition is true.
‘The syntax is shown below:
Loop-initialization;
while (test-condition)
f
Loop-update;
statement;
statement2;
Firstly, the expression is evaluated to true or false.
If the expression is evaluated to false, the control comes out of the loop without
executing the body of the loop.
> If the expression is evaluated to true, the body of the loop (ie. statement) is
executed.
vv
Dept of CSE, Canara Engineering College. Page 72C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
> After executing the body of the loop, control goes back to the beginning of the
while statement and expression is again evaluated to true or false. This cycle
continues until expression becomes false.
‘The flow diagram is shown below:
1
Test
Expression
free
Body of white loop
|
Program to display N natural numbers.
#include
void main()
t
aint i=1, n;
print£("\n Enter the W value");
scanf("$d", &n);
while (i<=n)
f
printé("sd\t", i);
#¢_{ Exit while loop
itt;
}
,
Output:
Enter the N value 5
12 3 4 «5
THE do whi
When we do not know exactly how many times a set of statements have to be repeated, do-
while statement can be used,
‘The syntax is shown below:
STATEMENT
Loop-initialization;
do
{
Loop-upda'
statement1;
Jwhile (test-condition) ;
Dept of CSE, Canara Engineering College. Page 73C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
> Firstly, the body of the loop is executed. ie. the body of the loop is executed at
least once.
> Then, the expression is evaluated to true or false.
> If the expression is evaluated to true, the body of the loop (i.e. statement!) is
executed
> After executing the body of the loop, the expression is again evaluated to true or
false. This eycle continues until expression becomes false.
‘The flow diagram is shown below:
Body of loop
Test False
Expression (Exit white loop
True
Program to find HCF (GCD) and LCM (Euclidian Technique)
#include
void main()
{
int a, b, x, y, t, ged, lem;
printf ("Enter two integers\n");
scanf("$did", &x, &y);
asx
bay
while (b != 0)
f
t=b;
beat;
ast;
}
ged =a;
Jem = (x*y)/ged;
printf ("Greatest common divisor of 8d and #d = %d\n", x, y, ged);
printf ("Least common multiple of $d and 8d = d\n", x, y, lem);
2
output:
Enter two integers 9 24
Greatest common divisor of 9 and 24 = 3
Least common multiple of 9 and 24 = 72
Dept of CSE, Canara Engineering College. Page 74C Programming for Problem Solving (17CPS13/23): Module _ Santhosh Kumar D K
THE for LOOP
> A for loop statement can be used to execute s set of statements repeatedly as long
as a given condition is true.
The syntax is shown below:
for (Loop-initialization; test-condition; Loop-update)
t
statement1;
Firstly, Loop-initialization is evaluated. It is executed only once
‘Then, fest-condition is evaluated to true or false.
If loop-update is evaluated to the control comes out of the loop without
executing the body of the loop.
If test-condition is evaluated to true, the body of the loop (ie. statementl) is
executed.
After executing the body of the loop, loop-update is evaluated.
‘Then test-condition is again evaluated to true or false. This cycle continues until
expression becomes false.
‘The flow diagram is shown below:
vvyv
v
vv
ntthlization
‘statement
[statenent just
[below for
Ht
Program to find the factorial of N number.
#include
void main()
t
int i, n, fact=1;
print£("\n Enter the N value");
scanf("$d", &n);
for (i=l; i The break statement is jump statement which can be used in
loops.
> ‘The break statement works as shown below:
> If break is executed in a switch block, the control comes out of the switch block
and the statement following the switch block will be executed.
> If break is executed in a loop, the control comes out of the loop and the statement
following the loop will be executed.
at
white (test expression) ( statenent/s
atenent/# iF (test expression) (
AF (eest expression) ( aed
% ‘statenent/s
Statenent/s
> nite (tote expression)
Ly
for (Antiat expression; test expression; update expression) (
seatonent/s
Af (east expression) (
breaks
statenents/
)
L,
Program to find the given number is prime or not.
#include
void main()
{
int i, n, prime=1;
printf("\n Enter the N value");
scanf("$d", &n);
for (i=2; ixen/2
€
if(n8i == 0)
itt)
if( prime
printf("\n Given number $d is a prime number", n);
else
printf("\n Given number $d is not a prime number", n);
}
Output:
Enter the N value 5
Given number 5 is a prime number
Dept of CSE, Canara Engineering College. Page 77C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
THE continue STATEMENT
> During execution of a loop, it may be necessary to skip a part of the loop based on
some condition. In such cases, we use continue statement
> The continue statement is used only in the loop to terminate the current iteration.
‘The syntax is shown below:
4
[vile (test expression) ( ‘seatenent/s
statenent/ AF (Lest expression) (
4€ (cost expression) (> continue}
continues
‘statenent/s
‘Statenent/s )
) Lipcniie (tose expression);
[> for (Antiat expression; test expression; update expression) (
EF (eet expression) (
Seatenents/
>
Program to read and add only positive numbers using continue
statement.
#include
void main()
t
int i=1, num, sum =0;
for(i=0; i < 5; i +4)
t
printf(* Enter an integer:
scanf( “sd”, énum) ;
if (num < 0)
£
printf("you have entered a negative number \n”);
continue ; // skip the remaining part of loop
}
sum=sum+num;
,
printf ("The sum of the Positive Integers Entered = $d”, sum);
2
output:
Enter an
Enter an
You have entered a negative number
Enter an 15
Enter an -100
You have entered a negative number
Enter an integer: 30
The sum of the positive integers entered = 55
Dept of CSE, Canara Engineering College. Page 78C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
THE goto statement
> goto statement can be used to branch unconditionally from one point to another in
the program,
> The goto requires a label in order to identify the place where the branch is to be
made,
> A label is any valid variable name and must be followed by a colon( : ).
> The label is placed immediately before the statement where the control is to be
transferred.
‘The syntax is shown below:
goto label;
Program to find sum of N numbers using goto statement.
#include
void main()
t
int n, num, sum
print£("\n Enter the N value");
scanf("$d", &n);
REPEATE: if (n>0)
t
print£(" Enter an integer:”)
scanf( “8d”, énum);
sum+=num;
goto REPEATE;
1
print£(" \n Sum =$d", sum);
y
Output:
Enter the N value 3
Enter an integer: 10
Enter an integer: 15
Enter an integer: 20
Sum = 45
Dept of CSE, Canara Engineering College. Page 79C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
PROGRAM EXAMPLES
1. Program to find roots of the quadratic equation
#include
#include
void main()
t
float a,b,c,D, 11,12;
print£("\n Enter three coefficients");
scanf ("$£8£8£", 6a, &b, &c) ;
if (a==0/| b==0]/ c==0)
print£("\n Invalid coefficients try again!!");
else
f
(b*b) ~(44atc) ;
4 £(D==0)
t
print£("\n Roots are equal");
xl=(-b)/ (24a);
print£("\n Root1=$£ \n Root2=tf \n",r1,r1);
i;
else if(D>0)
£
print£("\n Roots are real and distinct");
(-b) +sqrt (D))/ (24a) ;
(-b) sqrt (D))/(2*a) ;
print£("\n Root1=$f \n Root2=%f \n",r1,r2);
,
else
t
print£("\n Roots are real and imaginary");
1=(-b)/(2*a) ;//Real part
r2=sqrt (fabs (D))/(2*a) ;//imaginary part
print£("\n Root1=8£+i8£ \n",r1,r2);
print£("\n Root2=8£-i8f \n",r1,r2);
,
,
J
2. Program to compute binomial coefficients
#include
define MAX 10
Void main()
t
int m, x, binom;
print£(" mx");
for (m= 0; m <= MAX ; +4m)
printé("s4d", m);
Dept of CSE, Canara Engineering College. Page 80C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
print£("\n~ “\n") 5
m= 0;
do
€
print£("$2d_", m);
x = 0; binom = 1;
while (x <= m)
f
if(m == 0 || x
printé ("34
else
f
inom = binom * (m- x + 1)/x;
printf("$4d", binom) ;
binom);
x=xtl
)
print£("\n");
m=m+1;
,
while (m MAX) ;
print£("
}
output:
Durpur mx a 4
3. Program to Plot Pascal’s triangle
#include
void main()
t
int i, j,k), c7
printf("\n Enter the limit
scanf("$d", én) ;
print£("\n ");
for (i=0; icn; i++)
t
Dept of CSE, Canara Engineering College. Page 81C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
yj ken~i ; kt++)
printe(" ");
,
fox (5-0; J<=d; j++)
t
print£(" $d ",c);
fo* (4-5) / (541) );
}
print£("\n");
Q
4. program to find the given number is palindrome or not
#include
void main()
t
int num, temp, rem, rev=0;
print£(" Enter a number");
scant ("$d", enum) ;
if (num<=0)
printf ("Please enter a positive number");
else
€
temp=num;
while (num!=0)
€
rem=num?10;
rev=(rev*10) +rem;
num=num/10;
}
if (temp==rev)
printf("\n td is a palindrome", temp);
else
print£("\n %d is not a palindrome", temp);
5. Program to calculate electricity charges
#include
void main()
t
int unit;
float charge=100;
char name [30];
printf("\n Enter Consumer Name and number of units consumed\n");
scanf("Ss%d", name, gunit) ;
if (unit<=200)
charge+=0. 8*unit;
else if (unit<=300)
charget=
(0. 8*200) +0. 9* (unit-200) ;
Dept of CSE, Canara Engineering College. Page 82C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
else
charge+=(0.8*200) + (0. 9* (unit-200)) + (unit-300) ;
Af (charge>400)
charge+=(charge*0.15);
print£("\n Consumer Name Number of Units Charges");
print£("\n$s\t\t\td\t\ts.2£\n", name, unit, charge) ;
J
6. Program to find the square root of a number without using
library function.
#include
void main()
t
float n, root, temp=0;
printf("\nEnter a number")
scanf ("$£", én);
4f(n<0)
print£("\nPlease enter a positive number "
else
f
root=n/2;
while (root !=temp)
f
temp=root ;
(n/root+root) /2;
print£("\n Square root of %f is 8£",n, root);
y
7. Program to compute sinx using Taylors series
#include
#include
void main()
t
int i, degree;
float x, sum=0,term, numer, deno;
print£("\nEnter the values in degree");
scanf ("id", °ree) ;
x=degree* (3.142/180) ;
numer=x;
Dept of CSE, Canara Engineering College. Page 83C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
deno=deno*i* (i#1);
sum=sum+term;
while (fabs (term) >=0.00001) ;
print£("\nThe value of sin(x) without using library function is
Sin ($d) =8£\n", degree, sum) ;
printf("\nThe value of sin(x) using library function is
Sin($d)=8£\n", degree, sin (x)) ;
p
8. Program to display Fibonacci series
#include
void main()
t
int i, n, tl = 0, t2 = 1, nextTerm;
printf ("Enter the number of terms: ");
seanf("%d", én);
print£ ("Fibonacci Series: ");
for (i = 1; i <= nj +i)
f
print£("$d, ", t1);
nextTerm = t1 + t2;
tl = t2;
t2 = next Term;
1
J
Dept of CSE, Canara Engineering College. Page 84C Programming for Problem Solving (17CPS13/23): Module Il__ Santhosh Kumar D K
Question Bank
1. Explain the Syntax of nested if...else statement. Write a C program to find largest of
three numbers using nested if....else statement.
2. Explain the syntax of do-while statement. Write a C program to find the factorial of a
number using while loop, where the number n is entered by the user.
3. Write a calculator program in C language to do simple operations like addition,
subtraction, multiplication and division. Use switch statement in your program,
4, Write a C program to find the roots of quadratic equation,
5. Explain syntax of while statement. Write a C program to check the given number is
palindrome or not.
6. Explain break and continue statements with respect to do-whil
with suitable examples.
7. Print the following series:
1
12
123
1234
8. Explain ternary operator with suitable example.
9. Write a program to compute sinx using Taylors series.
10. Write a program to find the square root of a number without using library function,
11. Write a program to calculate electricity charges.
12, Write a program to find the given number is palindrome or not.
13, Write a program to Plot Pascal’s triangle.
14, Write a program to compute binomial coefficients.
15. Explain break, continue and goto statements with example.
16. Write the difference between while and do-while statement,
17. Write a program to find GCD and LCM of two numbers.
18, Write a program to generate Fibonacci series.
19. Explain looping statements with example.
. while and for loop
Referenei
1, E Balaguruswamy, Programming in ANSI C, 7th Edition, Tata McGraw-Hill
Education.
For more Program Examples refer my website:
hutps://sites. google.com/view/dksbin/subjects/e-programming-for-problem-
solving
Dept of CSE, Canara Engineering College. Page 85