0% found this document useful (0 votes)
38 views22 pages

PSC Unit 2

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)
38 views22 pages

PSC Unit 2

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/ 22

SVCE TIRUPATI BTECH_IT_SEM

41

COURSE MATERIAL

SUBJECT (CS20AES101) PROBLEM SOLVING USING C

UNIT 2

COURSE I B.TECH

DEPARTMENT COMPUTER SCIENCE & ENGINEERING

SEMESTER 11

PREPARED BY A.REVATHI
(Faculty Name/s) Senior Assistant Professor

VERSION V-5

PREPARED / REVISED DATE 05-3-2021

1 B. TECH_CSE-SEM 1
SVCE TIRUPATI BTECH_IT_SEM
41

TABLE OF CONTENTS – UNIT 2


SNo Contents Page No.
1 Course Objectives 1
2 Prerequisites 1
3 Syllabus 1
4 Course Outcomes 2
5 CO - PO/PSO Mapping 3
6 Lecture Plan 3
7 Activity Based Learning 4
8 1.1 5
Selection Statements
1.2 9
Loop/ Iteration Statements
1.3 11
Jump Statements/ Control Transfer Statement
9 Practice Quiz 16
10 Assignments 17
11 Part A Questions & Answers (2 Marks Questions) 19
12 Part B Questions 20
13 Supportive Online Certification Courses 20
14 Real Time Applications 20
15 Contents Beyond the Syllabus 20
16 Prescribed Text Books & Reference Books 20
17 Mini Project Suggestion 21

1 B. TECH_CSE-SEM 1
SVCE TIRUPATI BTECH_IT_SEM
41

1. Course Objectives
The objectives of this course are to
 To learn how to solve a given problem.
 To illustrate the basic concepts of C programming language.
 To discuss the concepts of Functions, Arrays, Pointers and Structures.
 To familiar with Dynamic memory allocation concepts.
 To apply concepts of structures and files to solve real word problems.

2. Prerequisites
Mathematics(Basics like prime number, factorial)

3. Syllabus
UNIT-II:
Control Statements:
Selection Statements- if and switch statements.
Iterative Statements: for, while and do-while statements.
Jump Statements: break and continue statements.

4. Courseoutcomes
The students will be able to

1. Implement C program using Conditional statements (L2).

2. Implement C program using Iterative statements (L2)

1|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41

5. Co-PO / PSOMapping

PSC PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2

CO1
3 2 2

CO2
3 2 3 3

CO3 3
2 3 2 3 2 3

6. Lecture Plan
Lecture No. Weeks Topics to be covered References

1 Introduction to Control Statements: T1

2 Selection Statements- if and if-else, programs using if and if-else TB1/RB2


1 Nested if, else-if statement, programs using nested if and
3 TB1/RB2
else if
4 switch statements TB1/RB2

5 Iterative Statements: for statement TB1/RB2

6 2 while and do-while statements TB1/RB2

7 Programs using Iterative statements TB1/RB2

8 3 Jump Statements: break and continue statements. TB1/RB2

7. Activity BasedLearning

1. Check Whether the Entered Year is Leap Year or not


2. Check whether a character is a vowel or consonant.
3. Find all roots of a quadratic equation.
4. Generate multiplication table.
5. Create a simple calculator.

2|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41

8. Lecture Notes
A statement is a part of your program that can be executed. That is, a statement
specifies an action. Statements generally contain expressions and end with a
semicolon. Statements that are written individually are called Single/Simple
statements. Statements that are written as a block are called Block/Compound
statements. A block begins with an open brace {and ends with a closing brace}.

C categorizes statements into these groups:


 Selection / Branch / Decision making / Conditional statements.
 Loop/ Iteration/ Repetitive statements
 Jump / Control transfer statements.

1.1 SELECTION STATEMENTS:


A selection statement checks the given condition and decides the execution of
statements after the success or failure of the condition.

C supports two selection control statements if and switch.


i) If statement: The if statement is a decision-making statement that allows the
computer to evaluate an expression (condition) first and depending on the
result of the condition i.e. true or false, it transfers the control to a particular
statement.

The if statement has the following forms


a) Simple if Statement
b) if...else Statement
c) Nested if Statement
d) if..else Ladder Statement

a) Simple if Statement
The simple if statement contains only one condition, if the condition is true, it will
execute the statements thatare present between opening and closing braces.
Otherwise it will not execute those statements

3|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41

Syntax: Example:
if(condition) /* A program to check whether a number is Less than 100
Single-statement or not */

OR #include<stdio.h>

if(condition) #include<conio.h>

{ void main()

Statement-block {

} int a;
clrscr();
printf("enter an integer ");
scanf("%d",&a);
if(a<=10)
printf("%d is less than 100",a);
getch();
}
b) if...else Statement If the condition is true, the block of if is executed; otherwise, the
block of else is executed

Syntax: Example:
if (condition) Write a program to check whether the given
{ number is a positive number or a negative
number
True Block Statements.
#include<stdio.h>
}
#include<conio.h>
else
void main()
{
{
FalseBlock Statements
int n;
}
clrscr();
printf("enter any number ");
scanf("%d",&n);
if(n>=0)
{
printf("\n %d is a positive number",n);
}
else
{
printf("\n %d is a negative number",n);
}
4|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
getch();
}

c) Nested if –else Statement


When an if statement is placed in another if statement or in else statement, then it
is called nested if statement.
The nested if-else is used when a series of decisions are involved. In a nested if-
else, an else statement always refers to the nearest if statement which is within the
same block as the else and that is not already associated with an else.

Syntax: Example:
if (condition) Write a program to finding greatest among
{ three numbers

if(condition) #include<stdio.h>

{ #include<conio.h>

True Block Statements. void main()


{

} int a,b;

} clrscr();

else printf("enter any two number ");

{ scanf("%d%d",&a,&b);

False Block Statements; if(a>b)

} {
if(a>c)
printf(“ %d is greatest”,a);
else
printf(“ %d is greatest”,c);
}
else
{
if(b>c)
printf(“ %d is greatest”,b);
else
printf(“ %d is greatest”,c);
}

5|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41

.
d) if..else Ladder Statement
The if-else ladder is used when multipath (multiple) decisions are involved.A
multipath decision is a chain of if-elsein which the statement associated with each
else is an if-statement.

Syntax: Example:
Write a program to test whether the given
number is a single digit or a double digit or a
if(condtion1) trible digit or more than three digits.
{
#include<stdio.h>
#include<conio.h>
statements1; void main()
} {
int n;
else if(condition2) clrscr();
{ printf("enter any number");
scanf("%d",&n);
statements2;
if(n>=0 && n<=9)
} printf("\n %d is a single digit number",n);
else if(condition3) else if(n>=10 && n<=99)
printf("\n %d is a double digit number",n);
{
else if(n>=100 && n<=999)
statements3; printf("\n %d is a trible digit number",n);
} else
printf("\n %d is more than three digits",n);
:
getch();
: }
else if(conditionN) }
{
statementsN;
}
else
{
statements;
}

6|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41

ii) Switch Statement


C has a built-in multiple-branch selection statement, called switch, which
successively tests the value of an expression against a list of integer or character
constants. When a match is found, the statements associated with that constant are
executed.
Syntax Example:
switch(expression) Write a program to check whether the
{ given character is a vowel or not using
switch case
case value1:
statements1;
#include<stdio.h>
break;
#include<conio.h>
case value2:
void main()
statements2;
{
break;
char ch;
.
clrscr();
.
printf("enter any single character:");
.
scanf("%c",&ch);
case valueN:
switch(ch)
{
statementsN;
case 'a':
break;
printf("%c is a vowel",ch);
default:
break;
statements;
case 'e':
}
printf("%c is a vowel",ch);
break;
case 'i':
printf("%c is a vowel",ch);
break;

7|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41

case 'o':
printf("%c is a vowel ",ch);
break;

case 'u':
printf("%c is a vowel",ch);
break;
default:
printf("\n %c is not a
vowel",ch);
}
getch();
}
The expression must evaluate to an integer type. Thus, you can use character or
integer values, but floating- point expressions, for example, are not allowed. The
value of expression is tested against the values, one after another, of the constants
specified in the case statements. When a match is found, the statement sequence
associated with that case is executed until the break statement or the end of the
switch statement is reached. The default statement is executed if no matches are
found. The default is optional, and if it is not present, no action takes place if all
matches fail. Technically, the break statements inside the switch statement are
optional. They terminate the statement sequence associated with each constant. If
the break statement is omitted, execution will continue into the next case's
statements until either a break or the end of the switch is reached.
Difference between if & Switch
If statement Switch statement
Definition Depending on the condition in The user will decide which
the 'if' statement, 'if' and 'else' statement is to be executed.
blocks are executed.
Expression It contains either logical or It contains a single expression
equality expression. which can be either a
character or integer variable.
Evaluation It evaluates all types of data, It evaluates either an integer, or
such as integer, floating-point, character.
character or Boolean.
Sequence of First, the condition is checked. It executes one case after
8|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
execution If the condition is true then 'if' another till the break keyword is
block is executed otherwise not found, or the default
'else' block statement is executed.
Default If the condition is not true, If the value does not match
execution then by default, else block will with any case, then by default,
be executed. default statement is executed.
Editing Editing is not easy in the 'if-else' Cases in a switch statement are
statement. easy to maintain and modify.
Therefore, we can say that the
removal or editing of any case
will not interrupt the execution
of other cases.
Speed If there are multiple choices If we have multiple choices
implemented through 'if-else', then the switch statement is the
then the speed of the best option as the speed of the
execution will be slow. execution will be much higher
than 'if-else'.

1.2 LOOP/ ITERATION STATEMENTS


In C, and all other modern programming languages, iteration statements (also called
loops) allow a set of
instructions to be repeatedly executed until a certain condition is reached. Based on
position of loop, loop statements are classified into two types:
1. entry-controlled loop (pre-test loop)- while, for
2. exit-controlled loop (post-test loop)- do while
1. While loop
While is pre tested/ entry controlled loop statement i.e first condition is checked &
body of loop is executed.
Syntax: Example:
Write a program to print first 25 natural
initialization; numbers
while(test condition) #include<stdio.h>
{ #include<conio.h>
Statements; void main()
Increment / Decrement operation; {
} int n; clrscr(); n=1;
while(n<=25)
{
printf("%3d",n);
n=n+1;
}

9|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
getch();
}

The initialization is an assignment statement that is used to set the loop control
variable. The condition is a relational expression that determines when the loop exits.
The increment/decrement defines how the loop control variable changes each time
the loop is repeated.
2. do while loop
Do While is post tested/ exit controlled loop statement i.e first body of loop is
executed & finally condition is
checked. Even though condition is false, it will execute the body of loop statements
at least once.
Syntax: Example:
Write a program to print first 25 natural
Initialization; numbers
do ##include<stdio.h>
{ #include<conio.h>
void main()
Statements; {
Increment/Decrement operation; int n; clrscr(); n=1;
} while(test condition) ; do
{
printf("%3d",n);
n=n+1;
} while(n<=25);
getch();
}

Difference between while/for & do while:


In while, if the condition is false, it will never execute the loop statements.
But in do-while, even though condition is false, it will execute the loop statements at
least once.
While Example: Example:
Write a program to illustrate while. Write a program to illustrate do while
##include<stdio.h> ##include<stdio.h>
#include<conio.h> #include<conio.h>
10|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
void main() void main()
{ {
int n; clrscr( ); n=1; while(n>10) int n;
{ clrscr( );
printf(“%d\n”,n); n=1;
n=n+1; do
} {
getch( ); printf("%d",n);
} n=n+1;
Output: No output because condition is } while(n>10);
getch();
false
}
output: it prints 1 even though condition
is false

3. For loop
for is pre tested/ entry controlled loop statement i.e first condition is checked & body
of loop is executed.
Syntax: Example:
for(initialization; condition; increment / Write a program to print first 10 numbers
decrement operation;) #include<stdio.h>
{ #include<conio.h>
list of statements void main()
} {
int i;
clrscr();
for(i=1; i<=10; i++)
{
printf(“%d\t”,i);
}
getch();
}

However, if condition section is omitted, the for loop becomes an endless loop,
which is called an infinite loop. When the condition is absent, it is assumed to be true.
The for statement may have an initialization and increment/decrement sections. But
C programmers more commonly usefor ( ; ; )
1.3 JUMP STATEMENTS/ CONTROL TRANSFER STATEMENTS
C has four statements that perform either a conditional or unconditional branch:
return, goto, break, andcontinue. Of these, we can use return and goto
anywhere inside a function and the break and continue statements in
conjunction with any of the loop statements. We can also use break with switch.
1. return Statement
A return may or may not have a value associated with it. A return with a value can
11|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
be used only in a function
with a non-void return type. In this case, the value associated with return becomes
the return value of the function. A return without a value is used to return (exit) from a
void function.
The general form of the return statement is
return; (OR) return expression;

The expression may be a constant, variable, or an expression. The expression is


present only in non-void function. In this case, the value of expression will become
the return value of the function. The expression is not present in void function.
2. break Statement
The break statement has two uses
a) to terminate a case in the switch statement.
b) to force immediate termination of a loop, bypassing the normal loop conditional
test. The general form of the break statement is break keyword followed by
semicolon
break;
3. continue Statement.
During the loop operations, it may be necessary to skip a part of the body of the
loop under certain conditions.
Like the break statement, C supports another similar statement called the continue
statement. However, unlike the break which causes the loop to be terminated, the
continue causes the loop to be continued with the next iteration after skipping any
statements in between.
In while and do-while loops, continue causes the control to go to directly to the test
condition and then to continue the iteration process. In the case of for loop,
continue causes the control to go to the increment/decrement section of the loop
and then to test condition.
The general form of the continue statement is continue keyword followed by
semicolon
continue; Example:
Example: Write a program to illustrate continue
Write a program to illustrate break #include<stdio.h>
#include<stdio.h> #include<conio.h>
#include<conio.h> void main()
void main() {
{ int i;
int i; clrscr();

12|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
clrscr(); for(i=1; i<=10; i++)
for(i=1; i<=10; i++) {
{ if(i==6) continue; printf(“%d\t”,i);
if(i==6) break; printf(“%d\t”,i); }
} getch();
getch(); }
} output: 1 2 3 4 5 7 8 9 10
output: 1 2 3 4 5

4. goto Statement
The goto statement is used to branch from one point (statement) to another in the
program.
The goto statement requires a label in order to identify the place where the branch is
to be made. A label is a valid identifier followed by a colon. The label is placed
immediately before the statement where the control is to be transferred.
Furthermore, the label must be in the same function as the goto that uses it – we
can’t jump between functions.
The general form of goto is
label: goto label;
------- --------
-------- -------
goto label;
label:

The goto breaks the normal sequential execution of the program.


If the label is before the goto statement, a loop will be formed and some statements
will be executed repeatedly. Such a jump is known as a backward jump. If the label
is placed after the goto statement, some statements will be skipped and the jump is
known as a forward jump.
Example:
Write a program to illustrate goto statement
#include<stdio.h>
#include<conio.h>
void main()
{
int i;

13|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
clrscr();
for(i=1; i<=10; i++)
{
if(i==6) goto xyz; printf(“%d\t”,i);
}
xyz: printf(“\nthankyou\n”);
getch();
}
output: 1 2 3 4 5 thankyou

1.3.3 Nested loops


Nesting of loops is the feature in C that allows the looping of statements
inside another loop.
Any number of loops can be defined inside another loop, i.e., there is no
restriction for defining any number of loops. The nesting level can be defined
at n times.
Syntax
Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}
Outer_loop and Inner_loop are the valid loops that can be a 'for' loop, 'while' loop or
'do-while' loop.
Nested for loop
The nested for loop means any type of loop which is defined inside the 'for' loop.
for (initialization; condition; inc/d 1. #include <stdio.h>
ec) 2. int main()
{ 3. {
for(initialization; condition; updat 4. int n;// variable declaration
e)
5. printf("Enter the value of n :");
{
14|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41

// inner loop statemen 6. // Displaying the n tables.


ts. 7. for(int i=1;i<=n;i++) // outer loop
} 8. {
// outer loop statements. 9. for(int j=1;j<=10;j++) // inner loop
1. } 10. {
11. printf("%d\t",(i*j)); // printing the valu
e.
12. }
13. printf("\n");
14. }

9. Practice Quiz

1) Choose a right C Statement.


A) Loops or Repetition block executes a group of statements repeatedly.
B) Loop is usually executed as long as a condition is met.
C) Loops usually take advantage of Loop Counter
D) All the above.
2) What is the output of C Program.?
int main()
{
while(true)
{
printf("RABBIT");
break;
}
return 0;
}
A) RABBIT
B) RABBIT is printed unlimited number of times.
C) No output
D) Compiler error.
3) What is the output of C Program.?
15|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
int main()
{
int a=25;

while(a <= 27)


{
printf("%d ", a);
a++;
}

return 0;
}
A) 25 25 25
B) 25 26 27
C) 27 27 27
D) Compiler error
4. The continue statment cannot be used with
A. for
B. while
C. do while
D. switch
5. Which loop is guaranteed to execute at least one time.
A. for
B. while
C. do while
D. None of the above

10. Assignments
S.No Question BL CO

1 Expalin all the control statements with example programs 1 2


16|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
2 Explain all the loop statements with example programs 2 3

3 Write a program to find the result of the student based on 2 3


percentage.
4 Write a program to reverse the given number 2 3
5 Write a program to print prime numbers between 1 to 20

11. PART-A 2Marks QUESTIONS& Answers


S.No Question& Answers BL CO
1 Define the term variable and constant.
Variable:
A variable is a data name that is used to store a
value. A variable may take different values at different
times. A variable can be chosen by the programmer in a
meaningful way. So as to reflect its function or nature in
the program.
Eg: 1 3
city college a1
Total_mark Name Avg
Constant:
A constant is a value that does not change during the
program execution. A constant used in C does not
occupy memory.

2 What is an expression in C language?

Expression is defined as a combination of operand and operator to


1 3
obtain some computation. Operands represent variable or values and
the operator tells what operation is to be performed.

3 What is an unary operator? Give example.


The operators that act upon a single operand to produce a new value
1 3
is known as unary operator.
Eg: minus operator(-) ,Increment operator (++)
4 Specify the use of printf( ) and scanf( ) functions.

The function scanf() is used for formatted input from the standard input
and provides many of the conversion facilities. 1 3
It is used for formatted output to standard output device,( screen) . The
format specification contains string and the data to be output, as the
arguments(parameter) to the printf() function.

17|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
5 What are keywords? Give example.
Keywords have fixed meaning and these meaning cannot be
changed. These keywords can be used only for their intended purpose,
they cannot be used as programmer-defined identifiers.
1 3
The following are examples of few keywords are predefined by C
auto double int struct
break else if switch
case enum do while
6 Specify the syntax used for ‘for’ statement.
The for loop is entry controlled loop the provides a more concise loop
control structure. The general format of the loop is
1 3
for(intilialization;tescondition;increment/decrement)
{
Body of the loop;
}
7 What is a nested loop?

A nested loop is a loop that runs within another loop. Put it in another
sense, you have an inner loop that is inside an outer loop. In this 2 3
scenario, the inner loop is performed a number of times as specified by
the outer loop. For each turn on the outer loop, the inner loop is first
performed.
8 When is a "switch" statement preferable over an "if" statement?

The switch statement is best used when dealing with selections


2 3
based on a single variable or expression. However, switch
statements can only evaluate integer and character data
types.
9 Distinguish between while and do..while statement in C.
While Do-while
(i) Executes the statements within (i) Executes the statements within
the while block if only the the while block at least once 2 3
condition is true.
(ii) The condition is checked at (ii) The condition is checked at
the starting of the loop the end of the loop
10 Mention the use of ‘break’ and ‘continue’ statements.
The break statement is used to terminate the loop containing it. 2 3
The continue statement does not terminate the entire loop but is used

18|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
to terminate the current iteration of the loop.

12. PART-B QUESTIONS


S.No Question BL CO
1. Explain the two way selection (if, if-else, nested if-else, 1 2
cascaded ifelse) in C language with syntax
2. Design and develop a C program to read a year as an input 1 3
and find whether it is leap year or not
3. Explain the switch statement with syntax and example. 2 2
4. Explain the different types of loops in C with syntax and 1 3
example.
5. Explain the use of break and continue statement in loops with 1 3
example
6. Design and develop a C program to reverse of an integer 1 3
number NUM and check whether it is PALINDROME or NOT.
13. Supportive Online Certification Courses:
 https://nptel.ac.in/
 https://www.coursera.org/
 https://www.udemy.com/
 https://www.edureka.co/
14. Real Time Applications:
 Bank Management System
 Library Management System
 Calendar using C
15. Content beyond the Syllabus:
 Debugging with gdb
 Type-generic math library.
 C threading libraries
16. Prescribed Text Books and Reference Books:
Text Books:
1. Reema Thareja, Programming in C, Oxford University Press, AICTE Edition,
2018.
2. R.G. Dromey, “How to Solve it by Computer”. 2014, Pearson.
Reference Books:
1. Jeri R. Hanly, Ellot B. Koffman, Problem Solving and Program Design in C, 5/e,
Pearson

19|P S C - U N I T - 2

BTECH_CSE-SEM 11
SVCE TIRUPATI BTECH_IT_SEM
41
2. B. A. Forouzan and R. F. Gilberg, Computer Science: A Structured
Programming Approach Using C, 3/e, Cengage Learning, 2007.
3. Brian W Kernighan and Dennis M Ritchie, The C Programming Language,
Second Edition, Prentice Hall Publication.
4. Paul Deitel, Harvey Deitel -C How to Program with an introduction to C++,
Eighth Edition

17. Mini Project Suggestion:


 Telecom Billing System
 Snake Game

20|P S C - U N I T - 2

BTECH_CSE-SEM 11

You might also like