[go: up one dir, main page]

0% found this document useful (0 votes)
1K views74 pages

CS25C01 Unit 1&2 Notes

The document outlines the course CS25C01 - Problem Solving Using C Programming at Velammal College, covering the evolution of programming languages, programming paradigms, and the importance of algorithms. It details the historical development of programming languages from machine code to modern high-level languages, and introduces various programming paradigms including imperative, procedural, object-oriented, and functional programming. Additionally, it discusses the characteristics, properties, and advantages of algorithms, as well as the use of flowcharts and pseudocode in problem-solving.
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)
1K views74 pages

CS25C01 Unit 1&2 Notes

The document outlines the course CS25C01 - Problem Solving Using C Programming at Velammal College, covering the evolution of programming languages, programming paradigms, and the importance of algorithms. It details the historical development of programming languages from machine code to modern high-level languages, and introduces various programming paradigms including imperative, procedural, object-oriented, and functional programming. Additionally, it discusses the characteristics, properties, and advantages of algorithms, as well as the use of flowcharts and pseudocode in problem-solving.
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/ 74

CS25C01- PROBLEM SOLVING USING C VCET

Regulation
PROGRAMMING 2025

Velammal College of Engineering and Technology, Madurai – 625 009


(Autonomous)
DEPARTMENT OF INFORMATION TECHNOLOGY
CS25C01- PROBLEM SOLVING USING C PROGRAMMING

UNIT I- INTRODUCTION
Evolution of Programming Languages - Programming Paradigms : Structured programming -
Object Oriented programming - Functional programming -Algorithms - Pseudo code and
Flowchart - Structure of C Program - Compilation Process -Preprocessor Directives - C Tokens.

CO1: Demonstrate the constructs of C Programming (K2)

1.1 Evolution of Programming Languages

1.1.1 Introduction:

Usually computer software may be classified into application software and system
software. Application Software – Set of programs necessary to carry out operations for specified
applications. System Software – General programs written for the system, which provide the
environment to facilitate the writing of application software.
Programming language translators are the best example of system software.
Programming Paradigm deals with the evolution of programming languages.

Why Programming Languages?

The purpose of using computers is to solve problems, which are difficult to solve
manually. So the computer user must concentrate on the development of good algorithms for
solving problems. After preparing the algorithms, the programmer or user has to concentrate the
programming language which is understandable by the computer.
A language that used to set instructions for computer is known as programming language.
It is a set of instructions that tells the computer what to do. Programming languages are used to
create software applications, websites, and other computer programs. The first programming
languages were developed in the 1940s and 1950s. They were very simple and inefficient, but
they were a necessary step in the evolution of computer programming.

I Year/I Sem Page 1


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

In the 1940s, programmers used machine-specific assembly languages to write code.


These languages were difficult to use and had many errors. The first modern programming
language like FORTRAN, was created in 1955. It was followed by COBOL, LISP, and ALGOL.
These four languages are the foundation of all modern programming languages. There are also
many applications that are still written in these languages and are still sustain from1964, BASIC
short for beginner’s all-purpose Symbolic Instruction Code was created in 1964, and C in 1969.
C is a robust that is used to write operating systems and its functions, like C is a programming
language that is widely used in the development of operating systems. UNIX and Linux are two
operating systems that have a lot of code written in C
The Evolution of programming languages can be divided into four main phases:
 The machine code era (1940s-1950s)
 The assembly language era (1950s-1960s)
 The high-level language era (1960s-present)
 The modern era (1990s-present)

1.1.2. The Machine Code Age

The era of machine code marked the beginning of programming languages' development.
The language that the hardware of the computer can understand directly is called machine code.
It is a very basic language that is challenging to write and debug. The first programming-
languages was created using machine code. These languages were very simple and not so
powerful, but they were a necessary step in the progression of computer programming.

1.1.3. The Assembly Language Age

The assembly language era followed the machine code period. Assembly language is a
higher-level language than machine code, but it is still a lower level language. Assembly
language uses to represent machine code instructions. This makes assembly language code more
user-friendly and efficient. High-level languages are becoming more popular, while assembly
language is becoming less so.

1.1.4. The High-Level Language Age

From 1960s the high-level language periods began. High level languages are easy to
write and debug compare to machine code or assembly language. High level programming
languages are more easily movable than assembly, meaning that they can be used without
rewritten. This is because high level languages are not specific to any architecture, while
assembly language is specific. Some of the most famous high level languages include:

I Year/I Sem Page 2


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 Auto Code (First Compiled Programming Language)


 FORTRAN short for Formula Translation
 ALGOL short for Algorithmic Language
 COBOL which is extended form Common Business Oriented Language
 BASIC short for Beginners All-purpose Symbolic Instruction Code
 PASCAL
 C
 C++
 JAVA
 PYTHON
 JavaScript

1.1.5. Modern Age

The modern age of programming languages started from 1990s. This era is marked by the
creation of new programming approaches, such as object-oriented and functional programming.
These languages are used to develop a wide range of applications, from web apps to mobile apps
to desktop apps to scientific computing apps.
Some of the most famous programming languages in the modern age are
 JAVA
 PYTHON
 JAVASCRIPT
 C#
 RUBY
 SWIFT
 KOTLIN
 GO
 RUST

***************

1.2. Programming Paradigms

Paradigm can also be termed as method to solve some problem or do some task.
Programming paradigm is an approach to solve problem using some programming language or
also we can say it is a method to solve a problem using tools and techniques that are available to
I Year/I Sem Page 3
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

us following some approach. There are lots for programming language that are known but all of
them need to follow some strategy when they are implemented and this methodology/strategy is

Fig1.1 Structure of Programming Paradigm

paradigm. Apart from varieties of programming language there are lots of paradigms to fulfil
each and every demand. They are discussed below

1.2.1. Imperative programming paradigm:


It is one of the oldest programming paradigms. It features close relation to machine
architecture. It is based on Von Neumann architecture. It works by changing the program state
through assignment statements. It performs step by step task by changing state. The main focus
is on how to achieve the goal. The paradigm consists of several statements and after execution of
all the result is stored.
Advantages:
 Very simple to implement
 It contains loops, variables etc.
Disadvantage:
 Complex problem cannot be solved
 Less efficient and less productive
 Parallel programming is not possible

Examples of Imperative programming paradigm:


 C : developed by Dennis Ritchie and Ken Thompson
 Fortran : developed by John Backus for IBM
 Basic : developed by John G Kemeny and Thomas E Kurtz
I Year/I Sem Page 4
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Imperative programming is divided into three broad categories: Structured, Procedural and
Object Oriented Programming (OOP). These paradigms are as follows:

1.3. Structured Programming Paradigm

Since the invention of the computer, many programming approaches have been tried.
These include techniques such as modular programming, top-down programming, bottom-up
programming and structured programming. The primary motivation in each case has been the
concern to handle the increasing. Complexity of program that is reliable and maintainable. These
techniques became popular among programmers over the last two decades.
With the advent of languages such as C, structured programming became very popular and was
the paradigm of the 1980s. Structured programming proved to be a powerful tool that enabled
programmers to write moderately complex programs fairly easily. However, as the programs
grew larger, even the structured approach failed to show the desired results in terms of bug-free,
easy-to-maintain, and reusable programs. Procedural and Object Oriented Programming (OOP)
Paradigms were derived from Structured Programming Paradigm.

**************
1.3.1. Procedural programming paradigm
This paradigm emphasizes on procedure in terms of under lying machine model. There is
no difference in between procedural and imperative approach. It has the ability to reuse the code
and it was boon at that time when it was in use because of its reusability.

Examples of Procedural programming paradigm:

 C : developed by Dennis Ritchie and Ken Thompson


 C++ : developed by Bjarne Stroustrup
 Java : developed by James Gosling at Sun Microsystems
 ColdFusion : developed by J J Allaire
 Pascal : developed by Niklaus Wirth

1.4. Object oriented programming Paradigm


The program is written as a collection of classes and object which are meant for
communication. The smallest and basic entity is object and all kind of computation is performed

I Year/I Sem Page 5


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

on the objects only. More emphasis is on data rather procedure. It can handle almost all kind of
real life problems which are today in scenario.

Advantages:
 Data security
 Inheritance
 Code reusability
 Flexible and abstraction is also present

Examples of Object Oriented programming paradigm:


 Simula : first OOP language
 Java : developed by James Gosling at Sun Microsystems
 C++ : developed by Bjarne Stroustrup
 Objective-C : designed by Brad Cox
 Visual Basic .NET : developed by Microsoft
 Python : developed by Guido van Rossum
 Ruby : developed by Yukihiro Matsumoto
 Smalltalk : developed by Alan Kay, Dan Ingalls, Adele Goldberg
**********
1.4.1. Declarative programming paradigm:
It is divided as Logic, Functional, Database. In computer science the declarative
programming is a style of building programs that expresses logic of computation without talking
about its control flow. It often considers programs as theories of some logic. It may simplify
writing parallel programs. The focus is on what needs to be done rather how it should be done
basically emphasize on what code is actually doing. It just declares the result we want rather how
it has be produced. This is the only difference between imperative (how to do) and declarative
(what to do) programming paradigms. Getting into deeper we would see logic, functional and
database.
1.4.2. Logic programming paradigm
It can be termed as abstract model of computation. It would solve logical problems like
puzzles, series etc. In logic programming we have a knowledge base which we know before and
along with the question and knowledge base which is given to machine, it produces result. In
normal programming languages, such concept of knowledge base is not available but while using
the concept of artificial intelligence, machine learning we have some models like Perception
model which is using the same mechanism. In logical programming the main emphasize is on

I Year/I Sem Page 6


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

knowledge base and the problem. The execution of the program is very much like proof of
mathematical statement, e.g. Prolog

1.5. Functional programming paradigm


The functional programming paradigm has its roots in mathematics and it is language
independent. The key principle of these paradigm is the execution of series of mathematical
functions. The central model for the abstraction is the function which is meant for some specific
computation and not the data structure. Data are loosely coupled to functions. The function hide
their implementation. Function can be replaced with their values without changing the meaning
of the program. Some of the languages like perl, JavaScript mostly uses this paradigm.
Examples of Functional programming paradigm:
 JavaScript : developed by Brendan Eich
 Haskell : developed by Lennart Augustsson, Dave Barton
 Scala : developed by Martin Odersky
 Erlang : developed by Joe Armstrong, Robert Virding
 Lisp : developed by John Mccarthy
 ML : developed by Robin Milner
 Clojure : developed by Rich Hickey

**************

1.5. Algorithm

The word Algorithm means “A set of finite rules or instructions to be followed in


calculations or other problem-solving operations” Or “A procedure for solving a mathematical
problem in a finite number of steps that frequently involves recursive operations”. Therefore
Algorithm refers to a sequence of finite steps to solve a particular problem.

I Year/I Sem Page 7


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

What is the need for algorithms?


 Algorithms are necessary for solving complex problems efficiently and effectively.
 They help to automate processes and make them more reliable, faster, and easier to
perform.
 Algorithms also enable computers to perform tasks that would be difficult or impossible
for humans to do manually.
 They are used in various fields such as mathematics, computer science, engineering,
finance, and many others to optimize processes, analyze data, make predictions, and
provide solutions to problems.

What are the Characteristics of an Algorithm?

 Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps
should be clear in all aspects and must lead to only one meaning.
 Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined
inputs. It may or may not take input.
 Well-Defined Outputs: The algorithm must clearly define what output will be yielded
and it should be well-defined as well. It should produce at least 1 output.
 Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
 Feasible: The algorithm must be simple, generic, and practical, such that it can be
executed with the available resources. It must not contain some future technology or
anything.
I Year/I Sem Page 8
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 Language Independent: The Algorithm designed must be language-independent, i.e. it


must be just plain instructions that can be implemented in any language, and yet the
output will be the same, as expected.

1.6.1. Properties of Algorithm:


 It should terminate after a finite time.
 It should produce at least one output.
 It should take zero or more input.
 It should be deterministic means giving the same output for the same input case.
 Every step in the algorithm must be effective i.e. every step should do some work.

1.6.2. Qualities of a Good algorithm


 Time
 Memory
 Accuracy
 Sequence
 Generability

1.6.3. Advantages of Algorithms:


 It is easy to understand.
 An algorithm is a step-wise representation of a solution to a given problem.
 In an Algorithm the problem is broken down into smaller pieces or steps hence, it is
easier for the programmer to convert it into an actual program.

1.6.4. Disadvantages of Algorithms:


 Writing an algorithm takes a long time so it is time-consuming.
 Understanding complex logic through algorithms can be very difficult.
 Branching and Looping statements are difficult to show in Algorithms

***********
1.7. Flowchart and Pseudo code
1.7.1. Flowchart

Flowcharts are graphical representations of data, algorithms, or processes, providing a visual


approach to understanding code.
 Flowcharts illustrate step-by-step solutions to problems, making them useful for beginner
programmers.
 Flowcharts help in debugging and troubleshooting issues.
I Year/I Sem Page 9
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 Flowchart consists of sequentially arranged boxes that depict the process flow.
Since it visually represents an algorithm or workflow, it is easier to interpret and understand.
However, to create an effective Flowchart, certain standardized rules must be followed

1.7.1.1. Flowchart Symbols

Different types of boxes are used to make Flowchart Symbols. All the different kinds of
boxes are connected by arrow lines. Arrow lines are used to display the flow of control.
Table.1.1. Flowchart Symbols

1.7.1.2. Basic guidelines for preparing flowchart


 Terminal

This box is of an oval shape which is used to indicate the start or end of the
program. Every Flowchart diagram has an oval shape that depicts the start of an
algorithm and another oval shape that depicts the end of an algorithm.

I Year/I Sem Page 10


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 Input/output

This is a parallelogram-shaped box inside which the inputs or outputs are


written. This basically depicts the information that is entering the system or algorithm
and the information that is leaving the system or algorithm. For example: if the user
wants to input a from the user and display it, the Flowchart for this would be:

 Process

This is a rectangular box inside which a programmer writes the main course of
action of the algorithm or the main logic of the program. This is the crux of the Flowchart
as the main processing codes is written inside this box. For example: if the programmer
wants to add 1 to the input given by the user, he/she would make the following
Flowchart:

 Decision

This is a rhombus-shaped box, control statements like if, condition like a > 0, etc
are written inside this box. There are 2 paths from this one which is “yes” and the other

I Year/I Sem Page 11


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

one is “no”. Every decision has either yes or no as an option, similarly, this box has these
as options. For example: if the user wants to add 1 to an even number and subtract 1 if the
number is odd, the Flowchart would be:

 On-Page Reference

This circular figure is used to depict that the Flowchart is in continuation with the
further steps. This figure comes into use when the space is less and the Flowchart is long.
Any numerical symbol is present inside this circle and that same numerical symbol will
be depicted before the continuation to make the user understand the continuation. Below
is a simple example depicting the use of On-Page Reference.

1.7.1.3. Advantages of Flowchart

 It is the most efficient way of communicating the logic of the system.


 It acts as a guide for a blueprint during the program design.

I Year/I Sem Page 12


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 It also helps in the debugging process.


 Using Flowcharts we can easily analyze the programs.
 Flowcharts are good for documentation.
1.7.1.4. Disadvantages of Flowchart
 Flowcharts are challenging to draw for large and complex programs.
 It does not contain the proper amount of details.
 Flowcharts are very difficult to reproduce.
 Flowcharts are very difficult to modify.
1.7.1.5. Solved Examples on Flowchart
Problem 1: Draw a Flowchart to find the greatest number among the 2 numbers.

Algorithm:
1. Start
2. Input 2 variables from user
3. Now check the condition If a > b, go to step 4, else go to step 5.
4. Print a is greater, go to step 6
5. Print b is greater
6. Stop

Flowchart:

Problem 2: Draw a Flowchart to check whether the input number is odd or even

Algorithm:
1. Start
2. Put input a
3. Now check the condition if a % 2 == 0, go to step 5. Else go to step 4
4. Now print(“number is odd”) and go to step 6

I Year/I Sem Page 13


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

5. print(“number is even”)
6. Stop

Flowchart:

***********
1.7.2 Pseudo code

 A Pseudocode is defined as a step-by-step description of an algorithm.


Pseudocode does not use any programming language in its representation instead
it uses the simple English language text as it is intended for human understanding
rather than machine reading.
 Pseudocode is the intermediate state between an idea and its
implementation(code) in a high-level language.

What is the need for Pseudocode?

Pseudocode is an important part of designing an algorithm, it helps the programmer in


planning the solution to the problem as well as the reader in understanding the approach to the
problem. Pseudocode is an intermediate state between algorithm and program

How to write Pseudo code?


Before writing the pseudo code of any algorithm the following points must be kept in
mind.
I Year/I Sem Page 14
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 Organize the sequence of tasks and write the pseudo code accordingly.
 At first, establishes the main goal or the aim.
Example:
This program will print first N numbers.
 Standard programming structures such as if-else, for, while, and cases should be
used. Statements if-else, for, while loops should be indented like they are
indented in a program, it helps to comprehend the decision control mechanism. It
also improves readability to a great extent.
Example:
IF “1”
print response
“I AM CASE 1”
IF “2”
print response
“I AM CASE 2”
 Appropriate naming conventions should be followed. so the naming must be
simple and distinct.
 Reserved commands or keywords must be represented in capital letters.
Example: if you are writing IF…ELSE statements then make sure IF and
ELSE be in capital letters.
 Check whether all the sections of a pseudo code are complete, finite, and clear to
understand and comprehend. Also, Mention everything in pseudo code that is
going to happen in the actual code.
 Pseudo code should not be written in a programming language. It is necessary
that the pseudo code is simple and easy to understand even for a layman or client.

1.7.2.1. Pseudocode Examples

I Year/I Sem Page 15


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Table 1.2: Pseudo code Examples

Syntax for if else: Example: Greatest of two numbers

IF (condition)THEN BEGIN

statement READ a,b

... IF (a>b) THEN

ELSE DISPLAY a is greater

statement ELSE

... DISPLAY b is greater

ENDIF END IF

END

Syntax for For: Example: Print n natural numbers

FOR( start-value to end-value) DO BEGIN

statement GET n

... INITIALIZE i=1

ENDFOR FOR (i<=n) DO

PRINT i

i=i+1

ENDFOR

END

Syntax for While: Example: Print n natural numbers

WHILE (condition) DO GET n

I Year/I Sem Page 16


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

statement BEGIN

... INITIALIZE i=1

ENDWHILE WHILE(i<=n) DO

PRINT i

i=i+1

ENDWHILE

END

1.7.2.2. Algorithm Vs Flowchart Vs Pseudo code:

Algorithm Flow Chart Pseudo Code

An Algorithm is used to provide a A Flowchart is pictorial A Pseudo code is a step-by-step description


solution to a particular problem in representation of flow of of an algorithm in code using plain English
form of a well-defined step-based an algorithm. text
form
A Flowchart uses standard
symbols for input, output
decisions and start stop
statements. Only uses
An algorithm only uses simple different shapes like box, Pseudo code uses reserved keywords like if-
English words circle and arrow. else, for, while, etc.

This is a way of visually


representing data, these are
nothing but the graphical These are fake codes as the word pseudo
representation of the means fake, using code like structure but
These are a sequence of steps of a algorithm for a better plain English text instead of programming
solution to a problem understanding of the code language

I Year/I Sem Page 17


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

There are no rules to writing There are certain rules for writing pseudo
algorithms code

It is difficult to understand and Flowcharts are good for


interpret documentation It is easy to understand and interpret

Pseudocode cannot be considered an


Algorithms can be considered
pseudocode algorithm

*************
1.7.2.3. History of ‘C’ Language

‘C’ is one of the most popular programming languages, it was developed by Dennis
Ritchie at AT & T’s Bell Laboratories at USA in 1972. It is an upgraded version of two earlier
languages, called BCPL and B, which were also developed at Bell Laboratories.
The following table illustrates the history of “C” Language.

Language Year Founder


ALGOL 1960 International Group
BCPL 1967 Martin Richards
B 1970 Ken Thompson
C 1972 Dennis Ritchie
K & RC 1978 Kernighan and Ritchie
ANSI C 1989 ANSI Committee
ANSI/ISO C 1990 ISO Committee

Is ‘C’ a Middle Level Language?

C stands in between these two categories. It is neither a low level language nor high level
language. It is a middle level language. It means, it performs, the task of low level language as
well as high level language. We can write the codes for Operating System, Application
Programs, and Assembly Language Program in ‘C’ language. UNIX Operating System is written
in ‘C’ language.

1.7.2.4. Features and Applications of ‘C’ Language

 ‘C’ is a general purpose, structured programming language.


 ‘C’ is a powerful, efficient, compact and flexible.
 ‘C’ is highly portable.

I Year/I Sem Page 18


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 ‘C’ is a robust language whose rich set of built in functions and operators can be used to
write any complex program.
 ‘C’ has the ability to extend itself.
 ‘C’ is well suited for writing system software application software.
 ‘C’ is a middle level language. i.e., the supports both the low level language and high
level language features.
 ‘C’ is widely available, commercial ‘C’ compilers are available on most PC’s.
 ‘C’ programs are fast and efficient.
 ‘C’ has got rich set of operators.
 ‘C’ can be applied in System Programming areas like compilers, interpreters and
assemblers, etc.,

1.8 Structure of a ‘C’ Program

The following illustrates the structure of “C” program:

 Documentation Section: It consists of a set of comment lines used to specify the name
of program, the author and other details etc.,

 Comments: Comments are very helpful in identifying the program features and
underlying logic of the program. The lines beginning with ‘/*’ and ending with ‘*/’ are
known as comment lines. These are not executable, the compiler is ignored anything in
between /* and */.

I Year/I Sem Page 19


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 Preprocessor Section: It is used to link system library files, for defining macros and for
defining the conditional inclusion.
Eg., #include<stdio.h> , #define A 10, #if def, #endif, …etc.

 Global Declaration Section: The variables that are used in more than one function
throughout the program are called global variables and declared outside of all the
function i.e., before main().
Every ‘C’ program must have one main() function, which specify the starting of ‘C’
program. It contains the following two parts.

 Declaration Part: This part is used to declare all the variables that are used in the
executable part of the program and these are called local variables.

 Executable Part: It contains at least one valid ‘C’ statement. The execution of a program
begins with opening brace ‘{‘ and ends with closing brace ‘}’. The closing brace of the
main function is the logical end of the program.

1.8.1 Programming Rules


The following rules should be followed while writing a ‘C’ program.
1. All statements in ‘C’ program should be written in lower case letters. Upper case letters
are only used for symbolic constants.
2. Blank spaces may be inserted between the words. It is not used while declaring a
variable, keyword, constant and function.
3. The program statement can be written anywhere between the two braces following the
declaration part.
4. The user can also write one or more statements in one line separating them with a
semicolon (;).

**************
1.9. Compilation Process

I. Execution of a C program
Execution is a process of running the program, to execute a ‘C’ program, we need to
follow the steps given below.
1. Creating the program
2. Compiling the program
3. Linking the program with system library

I Year/I Sem Page 20


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

4. Executing the program

The following illustrates the process of creating, compiling, and executing a ‘C’ program.

Creating the Program:


It means entering and editing the program in standard ‘C’ editor and save the program
with .C as an extension.

Compiling the Program:


This is the process of converting the high level language program into machine
understandable form. For this purpose, compiler is used. Usually this can be done in ‘C’
language by pressing ALT + F9 or choosing the Compile option in the menu system.
Here, there is a possibility to show errors i.e., syntax errors, which means the statements
written in program are not in proper syntax.

I Year/I Sem Page 21


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Linking the program with system library:


‘C’ program is the collection of predefined functions. These functions are already written
in some standard ‘C’ header files. Therefore before executing a ‘C’ program, we need to link it
with system library. This can be done automatically at the time of execution.

Executing the program:


This is the process of running and testing the program with sample data. At this time,
there is a possibility of showing two types of errors given below:
Logical Errors: These are the errors, in which the conditional and control statements
cannot end their match after some sequential execution.
Data Errors: These are the errors, in which the input data given is not in a proper syntax
as specified in input statements.
Usually, executing the program can be done by pressing CTRL + F9 or choose Run
option from the menu system.

1.9.1. Designing, Developing and Running a complete ‘C’ program

 Designing a ‘C’ program


Once the overall program strategy has been clearly understood, the other details of
program statements can be considered. This is generally referred as “Top-Down”
programming approach.

 Characteristics of a Program
Clarity: It refers to the readability of the program with underlying logic.
Integrity: It refers to the accuracy of the program. If the calculations are not performed
correctly, all other program enhancements will be meaningless.
Simplicity: The clarity and accuracy of the program is usually enhanced by keeping the
things as simple as possible with the overall program objectives.
Efficiency: It refers to the execution speed and efficient memory utilization of the
program.
Modularity: Many programs can be broken down into a series of identifiable sub
programs. Each sub program can be called as functions in C program. The modularity of
the program design, enhances the accuracy and clarity of the program.
Generality: Usually, the program is to be as general as possible, within reasonable units.

****************

I Year/I Sem Page 22


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

1.10. Preprocessor Directives

 Before a C program is compiled in a compiler, source code is processed by a program


called preprocessor. This process is called preprocessing.
 Commands used in preprocessor are called preprocessor directives and they begin with
“#” symbol.
 List of Preprocessor Directives:
Preprocessor Syntax/Description
Syntax: #define
Macro This macro defines constant value and can be any
of the basic data types.
Syntax: #include<file_name>
Header file inclusion The source code of the file “file_name” is included
in the main program at the specified place.
Syntax: #ifdef, #endif, #if, #else, #ifndef
Set of commands are included or excluded in
Conditional compilation
source program before compilation with respect to
the condition.
Syntax: #undef,#pragma
#undef is used to undefine a defined macro
Other directives
variable. #Pragma is used to call a function before
and after main function in a C program.

A program in C language involves into different processes. Below diagram will help you to
understand all the processes that a C program comes across.

I Year/I Sem Page 23


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

There are 4 regions of memory which are created by a compiled C program. They are,
1. First region – This is the memory region which holds the executable code of the
program.
2. 2nd region – In this memory region, global variables are stored.
3. 3rd region – stack
4. 4th region – heap
Difference between stack and heap memory:
Stack Heap
Stack is a memory region where “local Heap is a memory region which is used by
variables”, “return addresses of function calls” dynamic memory allocation functions at run
and “arguments to functions” are hold while C time.
program is executed.
CPU’s current state is saved in stack memory Linked list is an example which uses heap
memory.
Difference between compilers and interpreters:
Compilers Interpreters
Compiler reads the entire source code of the Interpreter reads the program source code one
program and converts it into binary code. This line at a time and executing that line. This
process is called compilation. process is called interpretation.
Program speed is fast. Program speed is slow.
One time execution. Interpretation occurs at every line of the
Example: C, C++ program. Example: BASIC

1.10.1. Macro and Header file Inclusion


 #define – This macro defines constant value and can be any of the basic data types.
 #include <file_name> – The source code of the file “file_name” is included in the main C
program where “#include <file_name>” is mentioned.
Example Program

I Year/I Sem Page 24


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Output

1.10.2. Conditional Compilation Directives

A) Example Program for #IFDEF, #ELSE AND #ENDIF:


 “#ifdef” directive checks whether particular macro is defined or not. If it is defined, “If”
clause statements are included in source file.
 Otherwise, “else” clause statements are included in source file for compilation and
execution.
Program

Output

B) Example Program for #IFNDEF AND #ENDIF:


 #ifndef exactly acts as reverse as #ifdef directive. If particular macro is not defined, “If”
clause statements are included in source file.
 Otherwise, else clause statements are included in source file for compilation and
execution.
Program

Output

I Year/I Sem Page 25


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

C) Example Program for #IF, #ELSE AND #ENDIF:


 “If” clause statement is included in source file if given condition is true.
 Otherwise, else clause statement is included in source file for compilation and execution.

Program

Output

1.10.3. Other directives: #undef and #pragma


 This directive undefines existing macro in the program.
Example Program

Output

 Pragma is used to call a function before and after main function in a C program.
Example Program

I Year/I Sem Page 26


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Output

******************
1.11. Tokens in C

In C programming, tokens are the smallest units in a program that have meaningful
representations. Tokens are the building blocks of a C program, and they are recognized by the C
compiler to form valid expressions and statements. Tokens can be classified into various
categories, each with specific roles in the program.

 Identifiers
 Keywords
 Operators
 Strings
 Special Characters
 Constant
Let’s look at each of these- one by one in detail:

1.11.1. Identifiers

These are used to name the arrays, functions, structures, variables, etc. The identifiers are
user-defined words in the C language. These can consist of lowercase letters, uppercase letters,
digits, or underscores.

1.11.1.1. Rules for identifiers

 The identifiers must not begin with a numerical digit.


 The first character used in an identifier should be either an underscore or an alphabet.
After that, any of the characters, underscores, or digits can follow it.
 Both- the lowercase and uppercase letters are distinct in an identifier. Thus, we can safely
say that an identifier is case-sensitive.
 We cannot use an identifier for representing the keywords.
 An identifier does not specify blank spaces or commas.
 The maximum length of an identifier is 31 characters.

I Year/I Sem Page 27


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 We must write identifiers in such a way that it is not only meaningful- but also easy to
read and short.

1.11.2. Keywords

Keywords are predefined or reserved words that have special meanings to the compiler.
These are part of the syntax and cannot be used as identifiers in the program. A list of 32
keywords in C or reserved words in the C programming language is mentioned below:

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while

1.11.3. Operators in C

The operators in C are the special symbols that we use for performing various functions.
Operands are those data items on which we apply the operators. We apply the operators in
between various operands. On the basis of the total number of operands, here is how we classify
the operators:

 Unary Operator
 Binary Operator
 Ternary Operator

Unary Operator
The unary operator in c is a type of operator that gets applied to one single operand, for example:
(–) decrement operator, (++) increment operator, (type)*, sizeof, etc.

Binary Operator
Binary operators are the types of operators that we apply between two of the operands. Here is a
list of all the binary operators that we have in the C language:

I Year/I Sem Page 28


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 Relational Operators
 Arithmetic Operators
 Logical Operators
 Shift Operators
 Conditional Operators
 Bitwise Operators
 Misc Operator
 Assignment Operator

Ternary Operator

Using this operator would require a total of three operands. For instance, we can use the ?: in
place of the if-else conditions. Example: C= a<b? a: b ;

1.11.4. Strings
Strings are nothing but an array of characters ended with a null character (‘\0’). This
null character indicates the end of the string. Strings are always enclosed in double quotes.
Whereas a character is enclosed in single quotes in C. Example: “hello”

1.11.5. Special Characters (Symbols) in C

The following special symbols are used in C having some special meaning and
thus, cannot be used for some other purpose. Some of these are listed below:

 () Simple brackets – We use these during function calling as well as during function
declaration. For instance, the function printf() is pre-defined.
 [ ] Square brackets – The closing and opening brackets represent the multidimensional
and single subscripts.
 (,) Comma – We use the comma for separating more than one statement, separating the
function parameters used in a function call, and for separating various variables when we
print the value of multiple variables using only one printf statement.
 { } Curly braces – We use it during the closing as well as opening of any code. We also
use the curly braces during the closing and opening of the loops.
 (*) Asterisk – We use this symbol for representing the pointers and we also use this
symbol as a type of operator for multiplication.
 (#) Hash/preprocessor – We use it for the preprocessor directive. This processor basically
denotes that the user is utilizing the header file.

I Year/I Sem Page 29


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 (.) Period – We use the period symbol for accessing a member of a union or a structure.
 (~) Tilde – We use this special character in the form of a destructor for free memory.

1.11.6. Constant in C

Constants are fixed values used in a C program. These values do not change during the
execution of the program. Constants can be integers, floating-point numbers, characters, or
strings.. Here are two of the ways in which we can declare a constant:

 By using a #define pre-processor


 By using a const keyword
Here is a list of the types of constants that we use in the C language:

Type of Constant Example

Floating-point constant 25.7, 87.4, 13.9, etc.

Integer constant 20, 41, 94, etc.

Hexadecimal constant 0x5x, 0x3y, 0x8z, etc.

Octal constant 033, 099, 077, 011, etc.

String constant “c++”, “.net”, “java”, etc.

Character constant ‘p’, ‘q’, ‘r’, etc.

***********

1.11.7 Data types in C

Each variable in C has an associated data type. It specifies the type of data that the
variable can store like integer, character, floating, double, etc.
Example:
int number;

I Year/I Sem Page 30


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

The above statement declares a variable with name number that can store integer values.
C is a statically type language where each variable’s type must be specified at the declaration
and once specified, it cannot be changed. This is because each data type requires different
amounts of memory and allows type specific operations.

The data types in C can be classified as follows:


Types Description Data Types

Primitive Data Primitive data types are the most basic data types that int, char, float,
Types are used for representing simple values. double, void

The data types that are derived from the primitive or


array, pointers,
built-in datatypes are referred to as Derived Data
function
Derived Types Types.

User Defined The user-defined data types are defined by the user structure, union,
Data Types himself. enum

Integer Data Type


The integer datatype in C is used to store the integer numbers (any number
including positive, negative and zero without decimal part). Octal values,
hexadecimal values, and decimal values can also be stored in int data type in C.

 Range: -2,147,483,648 to 2,147,483,647


 Size: 4 bytes
 Format Specifier: %d

Format specifiers are the symbols that are used for printing and scanning values of
given data types.
Example:
We use int keyword to declare the integer variable:

int val;

A variable of given data type can only contains the values of the same type. So, var can only
store numbers, not text or anything else
The integer data type can also be used as:

I Year/I Sem Page 31


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

1. unsigned int: It can store the data values from zero to positive numbers, but it can’t
store negative values
2. short int: It is lesser in size than the int by 2 bytes so can only store values from -
32,768 to 32,767.
3. long int: Larger version of the int datatype so can store values greater than int.

Note: The size of an integer data type is compiler dependent. We can use sizeof operator to
check the actual size of any data type. In this article, we are discussing the sizes according to
64-bit compilers.

Character Data Type


Character data type allows its variable to store only a single character. The size of the
character is 1 byte. It is the most basic data type in C. It stores a single character and requires a
single byte of memory in almost all compilers.
 Range: (-128 to 127) or (0 to 255)
 Size: 1 byte
 Format Specifier: %c

Example: char ch = 'A';

Float Data Type


In C programming, float data type is used to store single precision floating-point values. These
values are decimal and exponential numbers.
 Range: 1.2E-38 to 3.4E+38
 Size: 4 bytes
 Format Specifier: %f

Example: float val = 12.45;

Double Data Type


The double data type in C is used to store Double precision floating-point values (numbers
with floating point values) with double precision. It can easily accommodate about 16 to 17
digits after or before a decimal point.
 Range: 1.7E-308 to 1.7E+308
 Size: 8 bytes
 Format Specifier: %lf

Example: Double val = 12.45444444444444;

I Year/I Sem Page 32


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Void Data Type


The void data type in C is used to indicate the absence of a value. Variables of void data type
are not allowed. It can only be used for pointers and function return type and parameters.

Example:
void fun(int a, int b){
// function body
}

**************

1.11.8. Variables
 C variable is a named location in a memory where a program can manipulate the data.
This location is used to hold the value of the variable.
 The value of the C variable may get change in the program.

Rules for naming Variable


1. Variable name must begin with letter or underscore.
2. Variables are case sensitive
3. They can be constructed with digits, letters.
4. No special symbols are allowed other than underscore.
5. sum, height, _value are some examples for variable name

Declaration and Initialization of Variables:


 Variables should be declared in the C program before to use.
 Memory space is not allocated for a variable while declaration. It happens only on
variable definition.
 Variable initialization means assigning a value to the variable

I Year/I Sem Page 33


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Scope of Variables:
It implies the availability of variables within the program. Variables have two types of
scopes namely local variables and global variables.

Local Variables:
 The scope of local variables will be within the function only.
 These variables are declared within the function and can’t be accessed outside the
function.

Global Variables:
 The scope of global variables will be throughout the program. These variables can be
accessed from anywhere in the program.
 This variable is defined outside the main function. So that, this variable is visible to main
function and all other sub functions.
Difference between variable declaration and
definition:

**************

I Year/I Sem Page 34


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

UNIT I Question Bank


PART A
1. Define an Algorithm (K1)
The word Algorithm means “A set of finite rules or instructions to be followed in
calculations or other problem-solving operations” Or “A procedure for solving a
mathematical problem in a finite number of steps that frequently involves recursive
operations”. Therefore Algorithm refers to a sequence of finite steps to solve a particular
problem.

2. What are the characteristic of an algorithm? (K1)


Algorithms are characterized by clear instructions, finite steps, definite operations,
effective computations, and specific inputs and outputs, ensuring they are unambiguous
and can be implemented without ambiguity. They are also often designed to be feasible
and potentially cross-language compatible.

3. Distinguish between an algorithm and a flowchart. (K2)


S. No Algorithm Flowchart

An algorithm is a step-by-step procedure to A flowchart is a diagram created with different shapes to


1.
solve a problem. show the flow of data.

2. The algorithm is complex to understand. A flowchart is easy to understand.

3. In the algorithm, plain text is used. In the flowchart, symbols/shapes are used.

4. The algorithm is easy to debug. A flowchart is hard to debug.

5. The algorithm is difficult to construct. A flowchart is simple to construct.

6. The algorithm does not follow any rules. The flowchart follows rules to be constructed.

The algorithm is the pseudo-code for the


7. A flowchart is just a graphical representation of that logic.
program.

4. List the various symbols used to draw flowchart. (K1)


Flowcharts use various symbols to represent different parts of a process. The most
common symbols include ovals for start/end, rectangles for processes, and diamonds for
decisions, parallelograms for input/output, and arrows to show the flow of the process.
Other symbols may be used for specific purposes like connectors, delays, or data storage.

I Year/I Sem Page 35


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

5. Write a pseudo code to find maximum of three numbers. (K1)


START
// Input three numbers
PRINT "Enter the first number:"
READ num1
PRINT "Enter the second number:"
READ num2
PRINT "Enter the third number:"
READ num3
// Initialize largest to the first number
largest = num1
// Compare with the second number
IF num2 > largest THEN
largest = num2
ENDIF
// Compare with the third number
IF num3 > largest THEN
largest = num3
ENDIF
// Output the largest number
PRINT "The largest number is:", largest
END

6. List the various keywords used to write pseudo code. (K1)


Pseudo code utilizes a combination of keywords, often written in uppercase, to represent
programming constructs and control flow. Common keywords include IF, THEN, ELSE,
ENDIF, FOR, WHILE, DO, REPEAT, UNTIL, CASE, END CASE, INPUT, OUTPUT,
PRINT, SET, INCREMENT, DECREMENT, INITIALIZE, FUNCTION, CALL,
RETURN, PROCEDURE, and END PROCEDURE.

7. Discuss the concept of the programming paradigm. (K2)


A programming paradigm is a fundamental style or way of approaching programming
problems and structuring code. It's like a guiding philosophy or blueprint for how
developers conceptualize and solve problems using programming languages.

I Year/I Sem Page 36


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

8. Show the structure of „C‟ program. (K2)

9. List out the features of “C” (K1)

 “C‟ is a general purpose, structured programming language.


 “C‟ is a powerful, efficient, compact and flexible.
 “C‟ is highly portable.
 “C‟ is a robust language whose rich set of built in functions and
Operators can be used to write any complex program.
 “C‟ has the ability to extend itself

10. Define preprocessor directive and list out the types of preprocessor directives. (K1)
Before a C program is compiled in a compiler, source code is processed by a program
called preprocessor. This process is called preprocessing. Commands used in
preprocessor are called preprocessor directives and they begin with “#” symbol.

Macro - #define
Header File Inclusion -#include
Conditional Compilation -#if, #else, #endif
Other Directives -#pragma

I Year/I Sem Page 37


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

11. Discuss the compilation process. (K2)

12. What is execution? (K1)


Execution is a process of running the program, to execute a „C‟ program, we need to
follow the steps given below.
 Creating the program
 Compiling the program
 Linking the program with system library
 Executing the program

13. Show the difference between variable declaration and initialization. (K2)

 Variables should be declared in the C program before to use. Memory space is not
allocated for a variable while declaration. It happens only on variable definition.

 Variable initialization means assigning a value to the variable

14. What is data type? And list out the types of data type in C. (K1)

C data types are defined as the data storage format that a variable can store a data to
perform a specific operation. Data types are used to define a variable before to use in a

I Year/I Sem Page 38


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

program. Size of variable, constant and array are determined by data types. There are three
data types in C language. They are,

Types Description Data Types

Primitive Data Primitive data types are the most basic data types that int, char, float,
Types are used for representing simple values. double, void

The data types that are derived from the primitive or


array, pointers,
built-in datatypes are referred to as Derived Data
function
Derived Types Types.

User Defined The user-defined data types are defined by the user structure, union,
Data Types himself. enum

15. What is keyword? (K1)


Keywords are predefined and reserved word used in programming that has special
meanings to the compiler. Keywords are part of the syntax and they cannot be used as an
identifier. Example: int, for, while
16. Define type qualifier and list out the types of qualifiers. (K1)
The keywords which are used to modify the properties of a variable are called type
qualifiers. There are two types of qualifiers in C language. They are const and volatile.

17. Define variable and list out the rules for naming a variable. (K1)
C variable is a named location in a memory where a program can manipulate the data.
This location is used to hold the value of the variable. The value of the C variable may
get change in the program. Rules for naming Variable
 Variable name must begin with letter or underscore.
 Variables are case sensitive
 They can be constructed with digits, letters.
 No special symbols are allowed other than underscore.
 sum, height, _value are some examples for variable name

I Year/I Sem Page 39


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

18. Describe about C tokens. (K2)


In C programming, tokens are the smallest units in a program that have meaningful
representations. Tokens are the building blocks of a C program, and they are recognized
by the C compiler to form valid expressions and statements. Tokens can be classified into
various categories, each with specific roles in the program.

 Identifiers
 Keywords
 Operators
 Strings
 Special Characters
 Constant
19. What is an identifier? (K1)
It is used to name the arrays, functions, structures, variables, etc. The identifiers are user-
defined words in the C language. These can consist of lowercase letters, uppercase letters,
digits, or underscores.

20. List out Rules for identifiers. (K1)


 The identifiers must not begin with a numerical digit.
 The first character used in an identifier should be either an underscore or an alphabet.
After that, any of the characters, underscores, or digits can follow it.
 Both- the lowercase and uppercase letters are distinct in an identifier. Thus, we can
safely say that an identifier is case-sensitive.
 We cannot use an identifier for representing the keywords.
 An identifier does not specify blank spaces or commas.
 The maximum length of an identifier is 31 characters.
 We must write identifiers in such a way that it is not only meaningful- but also easy
to read and short.

21. Differentiate between variable and constant. (K2)


 A variable is a named storage location that can hold a value, and this value can
change during the program's execution.
 A constant, on the other hand, is a fixed value that cannot be modified after it is
defined.

I Year/I Sem Page 40


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

22. Define void data type. (K1)


 Void is an empty data type that has no value.
 This can be used in functions and pointers.
23. Explain Scope of Variables. (K2)
It implies the availability of variables within the program. Variables have two types of
scopes namely local variables and global variables.

Local Variables:
 The scope of local variables will be within the function only.
 These variables are declared within the function and can’t be accessed outside
the function.
Global Variables:
 The scope of global variables will be throughout the program. These variables
can be accessed from anywhere in the program.
 This variable is defined outside the main function. So that, this variable is
visible to main function and all other sub functions.

24. List out the applications of “C‟ Language. (K1)

C language is being used since the early days of computer programming and still is
used in wide variety of applications such as:
 C is used to develop core components of operating systems such as Windows,
Linux, and macOS.
 C is applied to program embedded systems in small devices such as washing
machines, microwave ovens, and printers.
 C is utilized to create efficient and quick game engines. For example, the
Doom video game engine was implemented using C.
 C is employed to construct compilers, assemblers, and interpreters. For
example, the CPython interpreter is written partially using C.
 C is applied to develop efficient database engines. The MySQL database
software is implemented using C and C++.
 C is employed to create programs for devices and sensors of Internet of
Things (IoT).

I Year/I Sem Page 41


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

25. What is modifier? And list out the types of modifier. (K1)
Modifiers are prefixed with basic data types to modify (either increase or
decrease) the amount of storage space allocated to a variable.
There are 5 modifiers available in C language. They are,
 short
 long
 signed
 unsigned
 long long

26. List out the merits of an object oriented Paradigm. (K1)


 Data security
 Inheritance
 Code reusability
 Flexible and abstraction is also present

PART B

1. i) Explain the characteristics and need of an algorithm. (6) (K2)


(ii)Write an algorithm to print the first N natural numbers.(7)
2. i) Draw a flowchart to find the max of three numbers and explain. (7) (K3)
(ii) Construct the pseudo code for finding simple interest. (6)
3. Explain the importance of pseudo code with its keywords and discuss about the
advantages and disadvantages. (K2)
4. Describe the structure of a C program with an example. (K2)
5. Define preprocessor directive and explain about the list of preprocessor directives with
suitable example. (K2)
6. Distinguish between Procedural oriented paradigm and Objected Oriented Paradigm.
(K3)
7. Summarize the concept of C Tokens with suitable examples. (K2)

I Year/I Sem Page 42


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

PART C

1. Develop algorithm, Flowchart and pseudo code to check whether a number is odd or
even. (K3)

2. Develop a pseudo code for menu driven program to perform various arithmetic
operations. (K3)

3. Develop algorithm, flowchart and pseudo code to find factorial of given number.
(K3)
4. Develop a flowchart for swapping two numbers of variables using third variable.
(K3)

Faculty In charge Course Coordinator Module Coordinator HOD/IT


Mrs. A. Periya Nayaki AP III Mrs. A. Periya Nayaki AP III Mrs. M. Prabha AP II Dr. R. Kavitha

I Year/I Sem Page 43


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

UNIT II- CONTROL FLOW STATEMENTS


Expressions –I/O Statements - Operators: Precedence and Associativity -Decision Making
Statements: if -else if ladder, Nested if, Switch statements - Iterative Statements: For, While, Do
while statements-Break, Continue Statements.

CO2: Build applications using Conditional and Looping constructs in C. (K3)

2.1. Expression in C

An expression is a combination of operators, constants and variables. An expression


may consist of one or more operands, and zero or more operators to produce a value.

Example: C= a+b;

2.1.1. Types of Expressions:

Here are four types of Expressions in C programming:

1. Arithmetic Expression
2. Relational Expression
3. Logical Expression
4. Conditional Expression

Types of Expressions in C

I Year/I Sem Page 44


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

2.1.2. Arithmetic Expression

An arithmetic expression consists of operands and arithmetic operators. It performs


arithmetic operations such as addition, subtraction, multiplication, and division. on the int, float,
or double type values.

The following types of arithmetic expressions are there:

 integer expression - an expression containing only integral operands,


 real expression - an expression containing only real operands
 mixed mode expression - an expression containing both integral and real operands

Example:
c=a+b;
c=a+b*10;

2.1.3. Relational Expression

Relational expressions use comparison operators such as '>' (greater than) and '<' (less
than) to compare two operands.
It is a condition that decides whether the action should be taken or not .The result of the
comparison is integer value i.e. 0 (false) or non-zero (true).Relational expressions are used
in decision-making statements and looping statements in the C language .

Relational
Description
Expression

The given condition checks whether the x is an even number or not. This
x%2 = = 0 relational expression shows the result as the value 1 if x is an even number
otherwise as the value 0

This relational expression is used to check if a is not equal to b and it results in


a!=b
1 if a is not equal to b otherwise 0.

It is used to check if this particular expression "a+b" is equal to the expression


a+b = = x+y
"x+y"

I Year/I Sem Page 45


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

a>=9 It is used to check if the value of a is greater than or equal to 9.

2.1.4. Logical Expression

Logical expressions in C which use logical operators are a powerful tool for controlling
the logic of the flow of the program. They are made by combining as many relational
expressions as the programmer wants. It then determines if certain statements or groups of
statements should be executed or not.

Logical Expressions Description

This logical expression is used as a test condition to check if the x is


( x > 4 ) && ( x < 6 ) greater than 4 and the x is less than 6. The result of the test condition is
true only when both conditions are true.

This logical expression is used as a test condition to check if x is greater


x > 10 || y <11 than 10 or y is less than 11. The result of the test condition is true if
either of the conditions holds true value

2.1.5. Conditional Expression

The conditional expression which uses ternary operator (?:) consists of three operands . It
returns 1 if the condition is true otherwise 0.

Example : c = a<b? a : b;
*****************
2.2. Input / Output Statements
 We know that input, process, output are the three essential features of computer program.
The program takes some input data, processes it and gives the output.
 We have two methods for providing data to the program.
o Assigning the data to the variables in a program
o By using the input/output statements.

I Year/I Sem Page 46


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 Two types of I/O statements are available in ‘C’ language and all input and output
operations are carried out through the function call. Several functions are available for
input/output operations in ‘C’. These functions are collectively known as the standard I/O
library.
o Unformatted Input / Output statements
o Formatted Input / Output statements

Formatted I/O Functions:


 Formatted Input / Output refers to input and output, that has been arranged in a particular
format.
 printf() and scanf() functions are inbuilt library functions in C programming language
which are available in C library by default. These functions are declared and related
macros are defined in “stdio.h” which is a header file in C language.
 We have to include “stdio.h” file as shown in below C program to make use of these
printf() and scanf() library functions in C language.

printf( ) function
 printf() function is used to print the “character, string, float, integer, octal and
hexadecimal values” onto the output screen.
 We use printf() function with %d format specifier to display the value of an integer
variable.
 Similarly %c is used to display character, %f for float variable, %s for string
variable, %lf for double and %x for hexadecimal variable.
 To generate a newline, we use “\n” in C printf() statement.
 Note: C language is case sensitive. For example, printf() and scanf() are different from
Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower case.

I Year/I Sem Page 47


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

scanf( ) function
 scanf() function is used to read character, string, numeric data from keyboard.
 The format specifier %d is used in scanf() statement. So that, the value entered is
received as an integer and %s for string.
 Ampersand is used before variable name “ch” in scanf() statement as &ch.

Unformatted I/O Functions:


 Unformatted input and output functions are only work with character data type.
 Unformatted input and output functions do not require any format specifiers. Because
they only work with character data type.
getchar( ) The getchar() function reads character type data from the input.
putchar( )  The putchar() function prints only one character at a time.
getch( )  It reads a single character directly from the keyboard, without echoing to the
screen.
getche( )  It reads a single character directly from the keyboard and echoes it to current text
window.
getc( )  It reads a character from the keyboard. It’s mainly used in file processing.
putc( )  It prints only one character at a time. It’s mainly used in file processing.
gets( )  The gets() function can read a full string even blank spaces presents in a string.
The gets() function is used to get any string from the user.
puts( )  The puts() function prints the character array or string on the console.

*****************
2.3. Operators in ‘C’
 The symbols which are used to perform logical and mathematical operations in a C
program are called C operators.
 These C operators join individual constants and variables to form expressions.
 Operators, functions, constants and variables are combined together to form expressions.
 Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is
constant and A + B * 5 is an expression.

Types of Operators
C language offers many types of operators. They are,
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bit wise operators

I Year/I Sem Page 48


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

6. Conditional operators (ternary operators)


7. Increment/decrement operators
8. Special operators

2.3.1. Arithmetic operator

The arithmetic operators are used to perform arithmetic/mathematical operations on


operands. Both unary and binary operations are available in C language. Unary operations
operate on a single operand.

S. No. Symbol Operator Description Syntax

1 + Plus Adds two numeric values. a+b

Subtracts right operand from left


– Minus a–b
2 operand.

3 * Multiply Multiply two numeric values. a*b

I Year/I Sem Page 49


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

S. No. Symbol Operator Description Syntax

4 / Divide Divide two numeric values. a/b

Returns the remainder after


% Modulus diving the left operand with the a%b
5 right operand.

Used to specify the positive


+ Unary Plus +a
6 values.

7 – Unary Minus Flips the sign of the value. -a

Increases the value of the


++ Increment a++
8 operand by 1.

Decreases the value of the


-- Decrement a--
9 operand by 1.

2.3.2. Assignment operator

Assignment operators are used to assign value to a variable. The left side operand of the
assignment operator is a variable and the right side operand of the assignment operator is a
value. The assignment operators can be combined with some other operators in C to provide
multiple operations using single operator. These operators are called compound operators.
S. No. Symbol Operator Description Syntax

Simple Assign the value of the right


= a=b
1 Assignment operand to the left operand.

Add the right operand and left


+= Plus and assign operand and assign this value a += b
2 to the left operand.

Subtract the right operand and


-= Minus and assign a -= b
3 left operand and assign this

I Year/I Sem Page 50


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

S. No. Symbol Operator Description Syntax

value to the left operand.

Multiply the right operand


Multiply and
*= and left operand and assign a *= b
assign
4 this value to the left operand.

Divide the left operand with


Divide and
/= the right operand and assign a /= b
assign
5 this value to the left operand.

2.3.3. Relational operator

The relational operators in C are used for the comparison of the two operands. All these
operators are binary operators that return true or false values as the result of comparison.

There are a total of 6 relational operators in C:

S. No. Symbol Operator Description Syntax

Returns true if the left operand is


< Less than less than the right operand. Else a<b
1 false

Returns true if the left operand is


> Greater than greater than the right operand. Else a>b
2 false

Returns true if the left operand is


Less than or
<= less than or equal to the right a <= b
equal to
3 operand. Else false

Greater than Returns true if the left operand is


>= a >= b
4 or equal to greater than or equal to right

I Year/I Sem Page 51


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

S. No. Symbol Operator Description Syntax

operand. Else false

Returns true if both the operands


== Equal to a == b
5 are equal.

Returns true if both the operands


!= Not equal to a != b
6 are NOT equal.

#include <stdio.h>

int main() {
int a = 25, b = 5;

// using operators and printing results


printf("a < b : %d\n", a < b);
printf("a > b : %d\n", a > b);
printf("a <= b: %d\n", a <= b);
printf("a >= b: %d\n", a >= b);
printf("a == b: %d\n", a == b);
printf("a != b : %d\n", a != b);

return 0;
}
Output
a<b :0
a>b :1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1

2.3.4. Logical operator

Logical Operators are used to combine two or more conditions. The result of the operation of a
logical operator is a Boolean value either true or false.

I Year/I Sem Page 52


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

There are 3 logical operators in C:

S. No. Symbol Operator Description Syntax

Returns true if
&& Logical AND both the operands a && b
1 are true.

Returns true if
|| Logical OR both or any of the a || b
2 operand is true.

Returns true if the


! Logical NOT !a
3 operand is false.

#include <stdio.h>

int main() {
int a = 25, b = 5;

// using operators and printing results


printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);

return 0;
}
Output
a && b : 1
a || b : 1
!a: 0

2.3.5. Bitwise Operator

The Bitwise operators are used to perform bit-level operations on the operands. The operators
are first converted to bit-level and then the calculation is performed on the operands.

I Year/I Sem Page 53


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

There are 6 bitwise operators in C:


S. No. Symbol Operator Description Syntax

Performs bit-by-bit AND operation


& Bitwise AND a&b
1 and returns the result.

Performs bit-by-bit OR operation


| Bitwise OR a|b
2 and returns the result.

Performs bit-by-bit XOR operation


^ Bitwise XOR a^b
3 and returns the result.

Bitwise First Flips all the set and unset bits on


~ ~a
4 Complement the number.

Shifts the number in binary form by


Bitwise
<< one place in the operation and a << b
Leftshift
5 returns the result.

Shifts the number in binary form by


Bitwise
>> one place in the operation and a >> b
Rightshilft
6 returns the result.

#include <stdio.h>
int main() {
int a = 25, b = 5;

// using operators and printing results


printf("a & b: %d\n", a & b);
printf("a | b: %d\n", a | b);
printf("a ^ b: %d\n", a ^ b);
printf("~a: %d\n", ~a);
printf("a >> b: %d\n", a >> b);
printf("a << b: %d\n", a << b);

return 0;
}

I Year/I Sem Page 54


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Output
a & b: 1
a | b: 29
a ^ b: 28
~a: -26
a >> b: 0
a << b: 800

2.3.6. Conditional Operator (Ternary Operator)

The conditional operator is the only ternary operator in C. It is a conditional operator that we can
use in place of if..else statements. It operates on three operands.
Syntax
expression1 ? Expression2 : Expression3;
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then we
will execute and return the result of Expression2 otherwise if the condition(Expression1)
is false then we will execute and return the result of Expression3.

2.3.7. Unary Operator

The increment and decrement operators are one of the unary operators which are very useful in C
language. It operates on single operand.

They are extensively used in for and while loops. The syntax of the operators is given below
 ++ variable name (Pre increment)
 variable name++ (Post increment)
 -–variable name
 variable name– –
The increment operator ++ adds the value 1 to the current value of operand and the decrement
operator – – subtracts the value 1 from the current value of operand. ++variable name and
variable name++ mean the same thing when they form statements independently, they behave
differently when they are used in expression on the right hand side of an assignment statement.
Consider the following
M=5;
y = ++m; (prefix)
In this case the value of y and m would be 6
Suppose if we rewrite the above statement as
m =5;

I Year/I Sem Page 55


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

y = m++; (post fix)


Then the value of y will be 5 and that of m will be 6. A prefix operator first adds 1 to the operand
and then the result is assigned to the variable on the left. On the other hand, a postfix operator
first assigns the value to the variable on the left and then increments the operand

2.3.8. Special Operator

2.3.8.1. Sizeof Operator

The operator size of gives the size of the data type or variable in terms of bytes occupied in the
memory. The operand may be a variable, a constant or a data type qualifier.

Example

m = sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);

The size of operator is normally used to determine the lengths of arrays and structures when their
sizes are not known to the programmer.

2.3.8.2. Comma Operator

The comma operator can be used to link related expressions together. A comma-linked list of
expressions is evaluated left to right and value of right most expression is the value of the
combined expression.

For example the statement

value = (x = 10, y = 5, x + y);

First assigns 10 to x and 5 to y and finally assigns 15 to value. Since comma has the lowest
precedence in operators the parenthesis is necessary.

2.3.8.3. dot (.) and arrow (->) Operators


Member Selection operators are used to reference individual members of classes, structures, and
unions.
 The dot operator is applied to the actual object.
 The arrow operator is used with a pointer to an object.
Syntax

I Year/I Sem Page 56


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

structure_variable . member;
structure_pointer -> member;

2.3.8.4. addressof (&) and Dereference (*) Operators

Addressof operator & returns the address of a variable and the dereference operator
* is a pointer to a variable

**********

2.4. Operator Precedence and Associativity

Precedence is used to determine the order in which different operators in complex


expressions are evaluated.
The operators of the same precedence evaluated either from left to right or right to left
depending on the level is known as associativity

I Year/I Sem Page 57


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Example:

#include <stdio.h>

I Year/I Sem Page 58


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

int main() {
int a = 6, b = 3, c = 4;
int res;

// Precedence and associativity applied here


res = a + b * c / 2;

printf("%d", res);

return 0;
}

Output
12
Explanation: The above expression is evaluated as 6 + ( (3 * 4) / 2) = 12, not (6 + 3) * (4 / 2) =
18. So what guided the compiler to evaluate the expression in this way? It is actually the
precedence and associativity of the operators used in this expression .

----------------------------******************-----------------------------

2.5 Decision Making Statements


 In decision control statements (if-else and nested if), group of statements are executed
when condition is true. If condition is false, then else part statements are executed.
 There are 3 types of decision making control statements in C language. They are,
1. if statements
2. if else statements
3. nested if statements

“IF”, “ELSE” AND “NESTED IF” DECISION CONTROL STATEMENTS:


Syntax for each C decision control statements are given in below table with description.

I Year/I Sem Page 59


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

2.5.1 if statement
In “if” control statement, respective block of code is executed when condition is true.
Example Program

Output

2.5.2. if…else statement


In C if else control statement, group of statements are executed when condition is true. If
condition is false, then else part statements are executed.
Example Program

I Year/I Sem Page 60


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Output

2.5.3. Nested if statement


 In “nested if” control statement, if condition 1 is false, then condition 2 is checked and
statements are executed if it is true.
 If condition 2 also gets failure, then else part is executed.
Example Program

Output

2.5.4. if else if ladder

In C, if else if ladder is an extension of if else statement and is used to test a series of


conditions sequentially, executing the code for the first true condition. A condition is checked
only if all previous ones are false. Once a condition is true, its code block executes, and the
ladder ends.
Syntax of if else if Ladder
if (condition1) {
// Statements 1
}
else if (condition2) {
// Statements 2

I Year/I Sem Page 61


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

}
else {
// Else body
}
where,
condition1, condition2: Contitions to be tested.
statements 1, statements 2: Code block corresponding to each condition.
An if-else ladder can exclude else block.
Example:

#include <stdio.h>
int main() {
int marks = 85;
// Assign grade based on marks
if (marks >= 90) {
printf("A\n");
} else if (marks >= 80) {
printf("B\n");
} else if (marks >= 70) {
printf("C\n");
} else if (marks >= 60) {
printf("D\n");
}
else
{
printf("F\n");
}
return 0;
}
Output:
B
Explanation:
The program checks the value of marks and assigns a grade based on predefined ranges.
It checks from highest to lowest, printing the grade based on the conditions met, or “F” if none
are met.

2.5.5. Switch Statements


 The statements which are used to execute only specific block of statements in a series of
blocks are called case control statements.
 Switch case statements are used to execute only specific case statements based on the
switch expression.
 Below is the syntax for switch case statement.

I Year/I Sem Page 62


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Example Program

Output

2.6. Iterative or Looping Statements


 Loop control statements in C are used to perform looping operations until the given
condition is true.
 Control comes out of the loop statements once condition becomes false.
 There are 3 types of loop control statements in C language. They are,
1. for
2. while
3. do-while

I Year/I Sem Page 63


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Syntax for each C loop control statements are given in below table with description.

2.6.1. for loop


 In for loop control statement, loop is executed until condition becomes false.

Output

2.6.2. while loop


 In while loop control statement, loop is executed until condition becomes false.

I Year/I Sem Page 64


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Output

2.6.3. do…while loop


 In do..while loop control statement, while loop is executed irrespective of the
condition for first time.
 Then 2nd time onwards, loop is executed until condition becomes false.

Output

Difference between while and do…while looping statements

while do…while
Loop is executed only when Loop is executed for first time
condition is true. irrespective of the condition. After
executing while loop for first time,
then condition is checked.

2.6.4. break statement


 Break statement is used to terminate the while loops, switch case loops and for loops
from the subsequent execution.
 Syntax: break;

I Year/I Sem Page 65


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Example Program

Output

2.6.5. Continue statement


 Continue statement is used to continue the next iteration of for loop, while loop and do-
while loops. So, the remaining statements are skipped within the loop for that particular
iteration.
 Syntax : continue;
Example Program

Output

2.6.6. goto statement


 goto statements is used to transfer the normal flow of a program to the specified label in
the program.
 Below is the syntax for goto statement in C.

I Year/I Sem Page 66


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Example Program

Output

-------------------------------------****************---------------------------------

I Year/I Sem Page 67


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

UNIT II Question Bank

PART A

1. What is operator? And list out the types of operators. (K1)


The symbols which are used to perform logical and mathematical operations in a C program
are called C operators.
C language offers many types of operators. They are,
 Arithmetic operators
 Assignment operators
 Relational operators
 Logical operators
 Bit wise operators
 Conditional operators (ternary operators)
 Increment/decrement operators
 Special operators

2. Define Expression in C. (K1)


An expression is a combination of operators, constants and variables. An expression may
consist of one or more operands, and zero or more operators to produce a value.

Example: C= a+b;

3. List out the types expressions in C. (K1)


Here are four types of Expressions in C programming:
 Arithmetic Expression
 Relational Expression
 Logical Expression
 Conditional Expression

I Year/I Sem Page 68


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

4. Differentiate Formatted I/O Function and Unformatted I/O Functions. (K2)

 Formatted Input / Output refers to input and output, that has been arranged in a particular
format. Printf() and scanf() functions are inbuilt library functions which are an example
of Formatted I/O Function Which requires format specifier
 Unformatted input and output functions are only work with character data type.
Unformatted input and output functions do not require any format specifiers. Because
they only work with character data type .Example : getchar() and purchar() functions
are inbuilt library functions which are an example of Unformatted I/O Function Which
does not require format specifier
5. How do you read the string with white space from user? (K1)
String with white space cannot be read from user with help of formatted i/p function scanf () .
Whereas this can be done with help of unformatted input function gets ().
6. Define Ternary Operator. (K1)
The conditional operator is the only ternary operator in C. It is a conditional operator that we
can use in place of if..else statements. It operates on three operands.
Syntax
expression1 ? Expression2 : Expression3;
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then
we will execute and return the result of Expression2 otherwise if the condition(Expression1)
is false then we will execute and return the result of Expression3.
7. Explain all types of Unary Operator. (K2)
The increment and decrement operators are one of the unary operators which are very useful
in C language. It operates on single operand.
They are extensively used in for and while loops. The syntax of the operators is given below
 ++ variable name (Pre increment)
 variable name++ (Post increment)
 __variable name(Pre decrement)
 variable name– – (Post decrement)
y = ++m; (prefix), y = m++; (post fix)
8. State about sizeof operator. (K2)
The operator sizeof gives the size of the data type or variable in terms of bytes occupied in
the memory. The operand may be a variable, a constant or a data type qualifier.
Example
m = sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);

I Year/I Sem Page 69


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

The size of operator is normally used to determine the lengths of arrays and structures when
their sizes are not known to the programmer.
9. Define precedence and associativity. (K1)
Precedence is used to determine the order in which different operators in complex
expressions are evaluated. The operators of the same precedence evaluated either from left to
right or right to left depending on the level is known as associativity.
10. Explain about Decision Making Statement. (K2)
In decision control statements (if-else and nested if), group of statements are executed when
condition is true. If condition is false, then else part statements are executed. There are 3
types of decision making control statements in C language. They are,
 if statements
 if else statements
 nested if statements
 Else if ladder statement
11. Define Dangling-else ambiguity. (K1)
The dangling else problem in syntactic ambiguity. It occurs when we use nested if. When
there are multiple "if" statements, the "else" part doesn't get a clear view with which "if " it
should combine. Dangling else can lead to serious problems. It can lead to wrong
interpretations by the compiler and ultimately lead to wrong results.
Initialize k=0 and o=0
if(ch>=3)
if(ch<=10)
k++;
else
o++;
12. Why Switch statement is called as multi-way decision making statement? (K1)
The switch case statement is a multi-way decision-making statement that chooses the
statement block to be executed by matching the given value with a list of case values.The
switch statement enables us to do several operations for the various possible values of a
single variable known as the switch variable.
13. Differentiate while and do…while looping statements. (K2)
while do…while
Its pre test loop Its post test loop
Body of the Loop is Body of the Loop is executed for
executed only when first time irrespective of the
condition is true. condition. After executing while
loop for first time, then condition is
checked.

I Year/I Sem Page 70


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

14. Differentiate the switch and if-else statement. (K2)


S. No. if-else Statement switch Statement

The condition which used in if-else can The expression in switch is usually an integral type or an
1
be any Boolean expression. enumerated type.

It can handle multiple conditions using Designed for evaluating a single expression against
2
else if clauses. multiple constants.

if-else doesn't have fall-through behavior switch can have fall-through behavior (case statements
3
by default. without breaks).

Optional else block for handling cases not Optional default case for handling cases not explicitly
4
explicitly covered. covered.

Suitable for complex, non-trivial More concise for handling a series of conditions based
5
conditions. on a single expression.

15. Show the differences between pre test and post test looping statements. (K2)
 Pretest loops check the condition before entering the loop's body, while posttest loops
check the condition after executing the loop's body at least once.
 Pretest loops may not run the loop's body at all if the condition is false initially, while
posttest loops always run the loop's body at least once.
16. Mention the significance of break statement in loops. (K1)
The break in C is a loop control statement that breaks out of the loop when encountered. It
can be used inside loops or switch statements to bring the control out of the block. The
break statement can only break out of a single loop at a time
17. Show the differences between break and continue statements. (K2)
 The primary difference between break and continue statement in C is that the break
statement leads to an immediate exit of the innermost switch or enclosing loop.
 On the other hand, the continue statement only skips the current iteration and begins
the next iteration of the while, enclosing for, or do loop
18. Define infinite loop. (K1)
An infinite loop (or, an endless loop) is a never-ending looping construct that executes a set
of statements forever without terminating the loop. It has a true condition that enables a
program to run continuously.

I Year/I Sem Page 71


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

While(1) for(; ;)
{ {
Statement; Statement;
} }

19. Define goto statement. (K1)


 goto statement is used to transfer the normal flow of a program to the specified label in
the program.
 Below is the syntax for goto statement in C.

20. Show the general form of if – else – if statement. (K2)


In C, if else if ladder is an extension of if else statement and is used to test a series of
conditions sequentially, executing the code for the first true condition. A condition is
checked only if all previous ones are false. Once a condition is true, its code block executes,
and the ladder ends.
Syntax of if else if Ladder
if (condition1) {
// Statements 1
}
else if (condition2) {
// Statements 2
}
else {
// Else body
}
where,
condition1, condition2: Contitions to be tested.
statements 1, statements 2: Code block corresponding to each condition.
An if-else ladder can exclude else block

I Year/I Sem Page 72


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

21. Why should we avoid using goto statement in C? (K1)


The “goto” statement in C should be avoided because it can lead to unstructured and
difficult-to-understand code. The flow of control in a program with “goto” statements can be
confusing and hard to follow, making it difficult to maintain or modify the code in the future.

22. Evaluate the following expression. (K2)


A = 100 + 200 / 10 - 3 * 10;
A = 100 + 200 / 10 - 3 * 10;//Associativity rule
= 100 + 20 - 3 * 10; //Precedence rule
= 100+20-30;//Associativity rule
= 120-30
A = 90
23. Write a program to determine whether a person is eligible to vote. (K1)
#include <stdio.h>
int main()
{
int age;
printf("Enter age : ");
scanf("%d", &age);
if (age >= 18)
printf("You can Vote!");
else
printf("You cant Vote!");
return 0;}

PART B

1. Discuss in detail about various operators used in C and its precedence with an example. (K2)
2. Discuss the various Conditional Branching Statements used in C with its syntax and flow
diagram. (K2)
3. Explain the importance of the following loop control statements with example. (K2)
(i) break statement (5)
(ii) continue statement (4)
(iii) goto statement (4)
4. Discuss the various Input and Output statements used in C with example. (K2)
5. Develop a C program for the following: (K3)
(i) To find the sum of the digits of a number. (123 =>1+2+3=6.) (7)
(ii)To find the sum of all odd / even numbers between 1 and100. (6)

I Year/I Sem Page 73


CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

6. Develop a C program for the following: (K3)


(i) To generate the first n numbers in a Fibonacci series. (7)
(ii) To find the factorial of a given number. (6)
7. Explain in detail the for, While and Do..While looping statements in C with suitable
examples. (K2)

PART C
1. Develop a C program for the following: (K3)
(i) To check whether a number is prime or not. (8)
(ii) To convert the temperature given in Fahrenheit to Celsius and vice versa. (7)
2. Develop a menu driven program in C to perform various arithmetic operations. (K3)
3. Develop a C program for the following: (K3)
4. Distinguish between branching and looping statements used in C. (K3)
(i) To find the power of a Number Using the while loop and using pow () function. (8)
(ii) To print a pattern of Floyd triangle. (7)

Faculty In charge Course Coordinator Module Coordinator HOD/IT


Mrs. A. Periya Nayaki AP III Mrs. A. Periya Nayaki AP III Mrs. M. Prabha AP II Dr. R. Kavitha

I Year/I Sem Page 74

You might also like