0% found this document useful (0 votes)
3 views

BSc_2ndSem_PS_using_C

The document outlines the fundamentals of computer science and C programming, covering topics such as the definition and characteristics of computers, components of a computer, hardware and software distinctions, algorithms, and flowcharts. It includes detailed sections on decision-making, looping statements, arrays, strings, functions, pointers, dynamic memory management, structures, and unions in C. Additionally, it provides model question papers for practice.

Uploaded by

sudheerkuc.songs
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)
3 views

BSc_2ndSem_PS_using_C

The document outlines the fundamentals of computer science and C programming, covering topics such as the definition and characteristics of computers, components of a computer, hardware and software distinctions, algorithms, and flowcharts. It includes detailed sections on decision-making, looping statements, arrays, strings, functions, pointers, dynamic memory management, structures, and unions in C. Additionally, it provides model question papers for practice.

Uploaded by

sudheerkuc.songs
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/ 62

Problem Solving using C 1

Unit 1 : Introduction to Computer & Programming,


Fundamentals of C Programming

01. Define computer. Write about Characteristics & Limitations of computers. - 03


02. What are the components of a computer? (or) Draw a block diagram of computer. - 04
03. Define: Hardware & Software. - 05
04. Define Algorithm. What are the features of Algorithms? - 08
05. Write about Flow charts. - 10
06. Write about Concepts of Machine level, Assembly level and high-level programming. - 11
07. Write about History & Features of C Programming Language. - 13
08. Write about the structure of C program. - 15
09. What are the keywords & identifiers in C programs? (or) Write about C-Tokens. - 17
10. Write about data types in C. - 18
11. How to declare variables in C? What are the rules to construct variable names? - 19
12. Write about I/O statements in C. - 20
13. What are the operators used in a C program? - 21
14. C Programming Examples. - 25

Unit 2 : Decision making & Looping statements,


Jump control statements (Break, Continue & Goto).

01. Write about conditional statements in C. [decision control statements] - 26


02. Write about iterative statements in C. [looping statements] - 29
03. Write about nested loops in C. - 31
04. How to use break, continue & goto statements in a C program? - 32

Unit 3 : Arrays & Strings


01. How to declare & access elements of an Array? - 34
02. Write about different operations used on Arrays. - 35
03. Write about two-dimensional arrays. - 36
04. Write a C program to add two matrices. - 38
05. Write a C program to multiply two matrices. - 39
06. Write about ‘string functions’ in C. - 40
07. Write about ‘Character handling functions’ in C. - 42

Problem Solving using C – Sudheer Kumar


2
Unit 4 : Functions, Storage classes, Pointers

01. Write about the declaration & definition of functions in C. - 45


02. How to call functions? Write about ‘return’ statement in functions. - 46
03. How to pass parameters to the functions? (or) Explain Call By Value & Call By Reference. - 46
04. Write about the scope of variables in a C program. - 48
05. Write about ‘recursive functions’ (or) ‘Recursion’ in C. - 49
06. Write about ‘storage classes’ in C. - 50
07. How to use pointer variables in C? - 51
08. Write about pointer arithmetic. - 51
09. How to pass pointer variables as arguments to the functions? - 53
10. Write about using Pointers in Arrays. - 53

Unit 5 : Dynamic memory management, Structures, Unions

01. Write about Dynamic memory allocation in C. - 55


02. Write about ‘structures’ in C. - 57
03. Write about ‘unions’ in C. - 58

Model Question paper 1 - 59


Model Question paper 2 - 60

Problem Solving using C – Sudheer Kumar


Unit 1 : Introduction to Computer & Programming,
3
Fundamentals of C programming
01. Define computer. Write about Characteristics & Limitations of computers.

Computer:

 Computer is an electro – mechanical device, which takes information from the user, processes it
and gives results.
 The information which the user gives to the computer is called Input.
 The devices which give input to the computer are called Input devices.
Ex: Keyboard, Mouse, Joystick, Scanner.
 The result which the computer gives to the user is called Output.
 The devices which give output to the user are called Output devices.
Ex: Monitor, Printer, Speaker

Characteristics of computers:

SPEED :
A computer can process the data and gives output in fraction of seconds.

ACCURACY :
Inspite of processing data at high speed, computer gives accurate results.

RELIABILITY :
The output generated by the computer is reliable. It is reliable only when the data given to the
computer and the instructions to process the data are reliable.

STORAGE CAPACITY :
Computer has capacity to store huge amount of data using storage devices.

AUTOMATION :
Once the instructions are stored in the computer, it works automatically without human
intervention.

DILIGENCE :
Computer does not suffer from tiredness or losing concentration.

Limitations of computers:

 Computer does not work on itself. It requires set of instructions to be provided.


 Computers can’t take decisions on its own. We need to instruct the computer to take an action.
 Computers can’t learn by experience.
 Computers can’t be operated without power.

Problem Solving using C – Sudheer Kumar


02. What are the components of a computer? (or) Draw a block diagram of computer.
4
Components of a computer:

 There are mainly four units in a computer.

CPU
Arithmetic &
Input Unit Logic Unit Output Unit

Control Unit

Memory Unit

Storage Unit

Input Unit:

 The information which the user gives to the computer is called Input.
 The devices which give input to the computer are called Input devices.
 Ex: Keyboard, Mouse, Joysticks, Scanner, Barcode readers, Optical Mark Readers.

Output Unit:

 The result which the computer gives to the user is called Output.
 The devices which give output to the user are called Output devices.
 Ex: Monitor, Printer, Speaker, Projector

The CPU (Central Processing Unit):

 CPU stands for Central Processing Unit.


 All the operations for processing data will be done in CPU.
 There are three parts in the CPU.

 ALU (Arithmetic & Logic Unit):


All the Arithmetic operations ( + , - , * , / ) & Logical operations will be done here.

 CU (Control Unit):
Data transmission between I/O devices and processor is the responsibility of the control
unit.

 MU (Memory Unit):
All the data & instructions for executing the program will be stored here.

Storage Unit:

 The storage unit stores the data permanently into the disk.
 Ex: Hard disk, Floppy disc, CD, DVD, Magnetic tapes.

Problem Solving using C – Sudheer Kumar


03. Define: Hardware & Software.
5
Computer Hardware:

 Hardware is the physical components of a computer which we can touch and feel.
 These are the primary electronic devices used to build up the computer.
 Examples of hardware in a computer are the Processor, Memory Devices, Monitor, Printer, Keyboard,
Mouse etc.

Following are the different types of Computer Hardware:

1. Input Devices:
Input Devices are those devices through which user enters data and information into the Computer or
simply, User interacts with the Computer.
Examples of Input Devices are Keyboard, Mouse, Scanner, etc.

2. Output Devices:
Output Devices are used to show the result of the task performed by the user.
Examples of Output Devices are Monitors, Printers, Speakers, etc.

3. Storage Devices:
Storage Devices are used to store data and they are also known as Secondary Storage Devices.
Examples of Storage Devices are Memory cards, Pen drives, CDs, DVDs, Hard Disk etc.

4. Internal Components:
Internal Components consists of important hardware devices present in the System.
Examples of Internal Components are the Processor, Motherboard, RAM chips etc.

Computer Software:

 Software is a collection of related programs that performs different tasks on a computer system.
 We can say that Software is a programming code executed on a computer processor.
 The code can be machine-level code or code written for an operating system.
 Examples of software are MS- Word, Excel, PowerPoint, Google Chrome, Photoshop etc.

Types of Computer Software

 S/w can be divided into two types: System Software & Application Software

1. System Software:
 System s/w is used to interact with computer h/w.
 System s/w is also used to run Application s/w.
 Following are some of the mostly used System s/w.

Problem Solving using C – Sudheer Kumar


Computer BIOS [Basic Input Output System]:
 The computer BIOS provide basic functionality to operate the h/w connected to the computer. 6
 When the computer starts, the BIOS identifies the system devices & initialize them.
 The code written in the BIOS runs to make sure that the system devices are working correctly.
 The BIOS is stored in ROM chip.
 The BIOS loads the operating system, located in the hard disk. This process is known as booting.

Operating System:
 The operating system provide the interface for the user to interact with the computer.
 The operating system provide icons to open many applications.
 Ex: opening a web browser, open paint application, open word application for creating documents
 etc.
 The operating system monitors that the system resources are utilized correctly.
 Some of the popular operating systems are DOS, Windows, UNIX, and Linux.

Utility Software:
 Utility s/w is used to analyse & maintain the computer. Some of the utility programs are shown
 below.
 Disk Checkers are used to scan the hard disk to find corrupted areas & eliminate them.
 Disk Cleaners are used to find the files which are not required to run the computer.
 Disk defragmenters are used to move the parts of a file to one location.
 Disk partitions are used to divide the hard disk into multiple drives.
 Anti-virus utilities are used to scan the computer for viruses.

Compiler:
 Compilers are used to convert the source code written in a programming language into the
machine language.
 If the source code contain errors, then the program can’t be compiled.
 These errors are called ‘syntax errors’. Spelling mistakes & typing mistakes are syntax errors.
 Another type of error is logic error. When the program does not give correct result, it is logic error.
 Compilers work as shown in following figure:

Interpreter:
 An interpreter is a program that directly executes the instructions in a high-level language, without
converting it into machine code.
 In programming, we can execute a program in two ways. Firstly, through compilation and secondly, through
an interpreter.
 Interpreters work as shown in following figure:

Linker & Loader:


 Linkers are used to combine the parts of the program to form an executable program.
 In a large program, the code is divided into many groups for simplify the task.
 Linkers are used by the compilers.
 Loaders are used to copy programs from a storage device to the main memory.
 The programs are executed after loading into the main memory.

Problem Solving using C – Sudheer Kumar


Application software:
7
 Application s/w is a set of programs, used to run particular application.
 Application s/w use system s/w to interact with computer h/w.
 Ex: MS Paint, MS Word, Games, Media players, MP3 players etc.
 Application s/w can be classified as follows.

MS Office:

 MS Office is a s/w package provided by Microsoft to create documents, spread sheets, databases &
presentations. There are mainly 4 applications available in MS Office package.
 These are MS Word, MS Excel, MS Access & MS Powerpoint.
 MS Word is used to create documents.
Ex: Letters, Resumes, study materials can be created in MS Word.
 MS Excel is used to create the data which is in the form of rows & columns.
 MS Access is used to create databases. Ex: Employee’s details, Student’s marks.
 MS Powerpoint is used to create presentations on any topic.

Multimedia s/w:

 Multimedia s/w can be in the form audio, video, images (or) animation.
 Media players, MP3 players, VLC players are examples for multimedia s/w.
 Voice recognition s/w is used to recognize the voice which was given as input.
 In a multimedia keyboard, there are buttons to up the volume, down the volume, pause (or) stop
the multimedia running in the computer.

Graphics s/w:

 Graphics s/w (or) Image editing s/w is a program that allows users to create & edit images.
Ex: MS Paint, Adobe photoshop etc.
 Paint programs are used to create drawings. These images are stored as BMP files (Bitmap Images)
& can be easily be edited when required.
 Animation s/w (2D & 3D) is used to create animations.
 CAD s/w is used to create engineering drawings.

DBMS s/w:

 Database Management Systems are used to store & manipulate the data in the database.
 DBMS are used in ATMs & Railway reservations etc.
 Using DBMS, we can get required information from the database.
Ex 1: SELECT sname FROM students WHERE marks>90.
Ex 2: SELECT emp_name FROM employees WHERE salary>8000.
 MS Access, SQL server & Oracle are popular database management systems.

Problem Solving using C – Sudheer Kumar


04. Define Algorithm. What are the features of Algorithms?
8
Algorithm:
Algorithm is an ordered sequence of well-defined steps, that when executed always produce results &
should be terminated in a finite amount of time.

Features of Good Algorithm:


 Input and Output should be defined precisely.
 Each step in algorithm should be clear & unambiguous.
 Algorithm should be most effective among many different ways to solve a problem.
 An algorithm shouldn’t have computer code. It should be written in such a way that, it can be used in
programming languages.

Examples:

Ex 1: Algorithm to add two numbers entered by the User.

Step 1: Start
Step 2: Declare variables a, b and sum.
Step 3: Read values a and b.
Step 4: Add a and b and store the result in sum.
Sum ← a + b
Step 5: Display sum
Step 6: Stop

Ex2: Algorithm to find the biggest among three different numbers entered by the User.

Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b
If a>c
Display a is the biggest number.
Else
Display c is the biggest number.
Else
If b>c
Display b is the biggest number.
Else
Display c is the biggest number.
Step 5: Stop

Ex 3: Algorithm to find the factorial of a number entered by User.

Step 1: Start
Step 2: Declare variables n, i, result.
Step 3: Initialize variables
result ← 1
i←1
Step 4: Read value of n
Step 5: Repeat the steps until i<=n
5.1: result ← i * result
5.2: i ← i+1
Step 6: Display result
Step 7: Stop

Problem Solving using C – Sudheer Kumar


Ex 4: Algorithm to check whether a number entered by user is prime or not.
9
Step 1: Start
Step 2: Declare variables n, i
Step 3: Initialize variable
i←2
Step 4: Read value for n
Step 5: if n=0 or n=1 display ‘neither prime nor composite’
goto Step 8
Step 6: Repeat the steps until (i < n)
6.1 If remainder of (n÷i) equals 0
display ‘not prime’
goto Step 8
6.2 i ← i+1
Step 7: Display n is prime
Step 8: Stop

Ex 5: Write an algorithm to find the Fibonacci series till term ≤ 500.

Step 1: Start
Step 2: Declare variables first, second and temp.
Step 3: Initialize variables first ← 0 & second ← 1
Step 4: Display first and second
Step 5: Repeat the steps until second≤500
5.1: temp ← first + second
5.2: display temp
5.3: first ← second
5.4: second ← temp
Step 6: Stop

Problem Solving using C – Sudheer Kumar


05. Write about Flow charts.
10
Flow charts:

 A flowchart is a graphical representation of an algorithm.


 Each step is placed in a box of the appropriate shape, and arrows decide the flow of the
boxes.
 Given below are the set of shapes used for flowcharts.

SYMBOL DESCRIPTION

Start or End symbol

Input/Output symbol

Control flow

Decision symbol

Processing step

 Start and End symbols are also known as the terminal symbols and are represented as
rounded rectangles.
 These symbols are the first and last symbols in a flowchart.

 Arrows represent the flow of control in the program.

 Processing step is also called an activity and is represented by a rectangle. Activities


include instructions such as ‘add a to b’, ‘save the result’ etc.

 Input/output symbols are represented using parallelogram.

 These are used to get input from user & display the result.

 Conditional or decision symbol is represented using a diamond.

Significance of Flowcharts:

 Flowcharts are very important in the programming of a problem as they help the
programmers to understand the logic of complicated and lengthy problems.

 Once a flowchart is drawn, it becomes easy for the programmers to write the program in
any high-level languages.

Problem Solving using C – Sudheer Kumar


Example flowcharts:
11
1. Addition of two numbers 2. Find the largest of three numbers

06. Write about Concepts of Machine level, Assembly level and high-level programming.

Programming Languages:

 Over the years, computer languages have been evolved from Low-Level to High-Level
Languages. In the earliest days of computers, only Binary Language was used to write
programs.
 The computer languages are classified as follows:

Problem Solving using C – Sudheer Kumar


Low Level (or) Machine Language :
12
 Low-Level language is the only language which can be understood by the computer.
 Low-level language is also known as Machine Language.
 The machine language contains only two symbols 1 & 0.
 All the instructions of machine language are written in the form of binary numbers 1's & 0's.
 A computer can directly understand the machine language.

Middle Level (or) Assembly Language :

 Middle-level language is a computer language in which the instructions are created using
symbols such as letters, digits and special characters.
 Assembly language is an example of middle-level language. In assembly language, we use
predefined words called mnemonics.
 Binary code instructions in low-level language are replaced with mnemonics and operands in
middle-level language.
 The computer cannot understand these mnemonics in middle-level language, so it needs to be
translated into a low-level language to make it understandable by the computer. Assembler is
used to translate middle-level language into low-level language.

High Level Language :

 High-level language is a computer language which can be understood by the users.


 The high-level language is very similar to human languages and has a set of grammar rules that
are used to make instructions more easily.
 Every high-level language has a set of predefined words known as Keywords and a set of rules
known as Syntax to create instructions.
 The high-level language is easier to understand for the users but the computer can not
understand it.
 High-level language needs to be converted into the low-level language to make it
understandable by the computer. We use Compiler or interpreter to convert high-level language
to low-level language.
 Languages like FORTRAN,C, C++, JAVA, Python, etc. are examples of high-level languages.
 The instructions in these programming languages are converted to low-level language by the
compiler or interpreter so that it can be understood by the computer.

Problem Solving using C – Sudheer Kumar


07. Write about History & Features of C Programming Language.
13
History of C Programming Language:

 C programming language was developed in 1972 by Dennis Ritchie at Bell laboratories


of AT&T (American Telephone & Telegraph), located in the U.S.A.
 Dennis Ritchie is known as the founder of the c language.
 It was developed to overcome the problems of previous languages such as B, BCPL, etc.
 Initially, C language was developed to be used in UNIX operating system. It inherits many
features of previous languages such as B and BCPL.
 The first version of C was called "A System Programming Language" and was released in
1972. The language quickly gained popularity and was used to write the Unix operating
system and other system software.
 In 1978, Brian Kernighan and Dennis Ritchie published a book titled "The C Programming
Language", which is still widely used as a reference book for C programming today. The
book defined the language's syntax and libraries and provided guidelines for writing efficient
and readable code.
 The history of C Language can be summarized as in the following table:

Created By Dennis Ritchie

First version released in 1972


Programming language for UNIX
Purpose
Operating System
The ‘C Programming Language’
Published in 1978 by founders
Book
ISO and ANSI standardized C
Standardization
language in 1989
First standard version ISO/IEC 9899:1990

Latest standard version ISO/IEC 9899:2024

Latest version of C C23

Problem Solving using C – Sudheer Kumar


Features of C Programming Language:
14
C is the widely used language. It provides many features that are given below.

Simple
C is a simple language in the sense that it provides a structured approach (to break the
problem into parts), the rich set of library functions, data types, etc.

Machine Independent or Portable


Unlike assembly language, c programs can be executed on different machines with some
machine specific changes. Therefore, C is a machine independent language.

Mid-level programming language


Although, C is intended to do low-level programming. It also supports the features of a
high-level language. That is why it is known as mid-level language.

Structured programming language


We can break the C program into parts using functions. So, it is easy to understand and
modify. Functions also provide code reusability.

Rich Library
C provides a lot of inbuilt functions that make the development fast.

Memory Management
It supports the feature of dynamic memory allocation. In C language, we can free the allocated
memory at any time by calling the free() function.

Speed
The compilation and execution time of C language is fast since there are lesser inbuilt functions.

Pointer
In C, we can directly interact with the memory by using the pointers.

Recursion
In C, we can call the function within the same function. It provides code reusability.

Problem Solving using C – Sudheer Kumar


08. Write about the structure of C program.
15
Structure of C program:

- The basic structure of C program will be as shown below:

- Document Section: This section consists of set of comment lines which include name of the program,
author name, creation date and other information.

- Links Section (File): It is used to link the required system libraries (or) header files to execute a
program.

- Definition Section: It is used to define or set values to variables.

- Global variable declaration Section: It is used to declare global (or) public variables.

- void main(): Used to start of actual C program. It includes two parts as declaration part and
executable part.

- Local variable declaration section: Used to declare local (or) private variables.

- Function declaration section: Used to declare functions of program from which we get required
output.

- Executable statements are placed for execution.

- Function definition section: Used to define functions which are to be called from main().

Problem Solving using C – Sudheer Kumar


Example C program to demonstrate all sections in Structure of C program:
16

Output:

Area of Circle : 50.240002


Area of Square : 25

Problem Solving using C – Sudheer Kumar


09. What are the keywords & identifiers in C programs? (or) Write about C-Tokens.
17
Keywords:

 Keywords are preserved words that have special meaning in C language.


 The meaning of the keywords has already been described to the C compiler. There are total 32
keywords in C language.

if else switch case


default while do for
break continue goto void
return int short long
float double char signed
unsigned enum struct union
sizeof typedef register extern
auto const static volatile

Identifiers

 In C language, identifiers are the names given to variables, constants, functions and user-
defined data.
 These identifiers are defined using a set of rules.

Rules for an Identifier

 An Identifier can only have alphanumeric characters( a-z , A-Z , 0-9 ) and underscore( _ ).
 The first character of an identifier can only contain alphabet( a-z , A-Z ) or underscore ( _ ).
 Identifiers are also case sensitive in C. For example name and Name are two different
identifiers.
 Keywords are not allowed to be used as Identifiers.
 No special characters, such as semicolon, period, whitespaces, slash or comma are permitted to
be used in or as Identifier.

Character set

 In C language characters are grouped into the following categories:


 Letters(all alphabets a to z & A to Z).
 Digits (all digits 0 to 9).
 Special characters, such as colon(, semicolon(;), period(.), underscore(_), white spaces,
ampersand(&) etc.

Problem Solving using C – Sudheer Kumar


10. Write about data types in C.
18
Data types in C Language

Data types specify how we enter data into our programs and what type of data we enter.
C language has some predefined set of data types to handle various kinds of data that we use in our
program.

C language supports 2 different categories of data types:

Primary data types:


These are fundamental data types in C namely integer(int), floating(float), character(char) and void.

Derived data types:


Derived data types are like array, function, structure, union and pointer.

Integer type
Integers are used to store whole numbers.

Type Size (bytes)


short int (or) signed short int 1
unsigned short int 1
int (or) signed int 2
unsigned int 2
long int (or) signed long int 4
unsigned long int 4

Floating type
Floating types are used to store real numbers.

Type Size (bytes)


float 4
double 8
long double 10

Character type
Character types are used to store characters value.

Type Size(bytes)
char (or) signed char 1
unsigned char 1

void type
void type means no value. This is usually used to specify the type of functions.

Problem Solving using C – Sudheer Kumar


11. How to declare variables in C? What are the rules to construct variable names?
19
Variables in C Language:

 When we want to store any information, we store it in an address of the computer.


 We name that address. The naming of an address is known as variable.
 Variable is the name of memory location.
 We can change the value of a variable during execution of a program.
 A programmer can choose a meaningful variable name.
Example: average, height, age, total etc.

Rules to define variable name:

 Variable name must not start with a digit.


 Variable name can consist of alphabets, digits and special symbols like underscore _.
 Spaces are not allowed in variable name.
 Keywords are not allowed as variable name.

Declaration and Definition of variable:

 Declaration of variables must be done before they are used in the program.
 Declaration does two things.
 It tells the compiler what the variable name is.
 It specifies what type of data the variable will hold.
 Definition of variable means to assign a value to a variable. A variable must be declared before
it can be defined.

#include<stdio.h>
void main(){
int a,b,sum; // variable declaration
a=10; // variable definition
b=20; // variable definition
sum=a+b;
printf("Sum is %d",sum);
}

Output:
Sum is 30

Problem Solving using C – Sudheer Kumar


12. Write about I/O statements in C.
20
Input / output functions in C:
 C programming language provides many of the built-in functions to read given input and
write data on screen, printer or in any file.

scanf() and printf() functions:


 In C programming language, scanf() function is used to read character, string, numeric
data from keyboard.
 In the below example program, user enters a character. This value is assigned to the
variable “ch” and then displayed.
 Then, user enters a string and this value is assigned to the variable “str” and then
displayed.

#include<stdio.h>
void main(){
char ch;
char str[50];
printf("Enter a character ");
scanf("%c",&ch);
printf("Entered character is %c\n",ch);
printf("Enter a string ");
scanf("%s",&str);
printf("Entered string is %s",str);
}

Output:
Enter a character S
Entered character is S
Enter a string SUDHEER
Entered string is SUDHEER

 In C programming language, 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.

getchar() & putchar() functions:

 The getchar() function reads a character from the input device. This function reads only single
character at a time. We can use this method in the loop if we want to read more than one character.
 The putchar() function prints the character on the screen. This function puts only single character at
a time. In case you want to display more than one characters, use putchar() method in the loop.

#include <stdio.h>
void main( ){
int c;
printf("Enter a character");
c=getchar();
putchar(c);
}
 When we execute the above code, it will ask us to enter a character. When we enter the character, it
will display the entered character.

Problem Solving using C – Sudheer Kumar


gets() & puts() functions
21
 The gets() function reads a line from stdin into the buffer until either a terminating newline or EOF
(end of file) occurs.
 The puts() function writes the string to stdout.

#include<stdio.h>
void main(){
char str[100];
printf("Enter a string");
gets(str);
puts(str);
}

 When we execute the above code, it will ask us to enter a string. When we enter the string, it will
display the entered string.

Difference between scanf() and gets()

 The main difference between these two functions is that scanf() stops reading characters when it
encounters a space, but gets() reads space character also.
 If you enter name as SUDHEER KUMAR using scanf() it will only read and store SUDHEER and will
leave the part after space. But gets() function will read it completely.

13. What are the operators used in a C program?

Operators in C Language:

 C language supports many built-in operators.


 An operator is a symbol that tells the compiler to perform certain mathematical or logical operation.
 Operators are used in program to manipulate data and variables.
 In C language, we use different types of operators as shown below:

Arithmetic operators

 C supports all the basic arithmetic operators.

OPERATOR DESCRIPTION
+ Adds two operands
- Subtract second operand from first
* Multiply two operands
/ Divide the numerator by denominator
% remainder of division

Problem Solving using C – Sudheer Kumar


Assignment Operators
Following assignment operators are supported in C language. 22

OPERATOR DESCRIPTION Example

assigns value of right operand to left


= a=b
operand

adds both operands and assigns to


+= a+=b is same as a=a+b
left operand

subtracts right operand from left and


-= a-=b is same as a=a-b
assigns to left operand

multiply both operands and assigns


*= a*=b is same as a=a*b
to left

divides left operand with right


/= a/=b is same as a=a/b
operand and assigns to left

divides left operand with right


%= a%=b is same as a=a%b
operand, assigns remainder to left

Relational operators
The following table shows all relational operators supported by C.

OPERATOR DESCRIPTION

== check whether two operands are equal

!= check whether two operands are not equal

check whether left operand is greater than right


>
operand

check whether left operand is smaller than right


<
operand

check whether left operand is greater than (or)


>=
equal to right operand

check whether left operand is smaller than (or)


<=
equal to right operand

Logical operators
C language supports following 3 logical operators.
Suppose a=TRUE and b=FALSE.

OPERATOR DESCRIPTION Example


&& Logical AND a && b is FALSE

|| Logical OR a || b is TRUE

! Logical NOT !a is FALSE

Note:
&& is TRUE only if both operands are TRUE. In all other cases, && will be FALSE.
|| is FALSE only if both operands are FALSE. In all other cases, || will be TRUE

Problem Solving using C – Sudheer Kumar


Bitwise operators
Bitwise operators perform manipulations of data at bit level. 23
These operators also perform shifting of bits.

OPERATOR DESCRIPTION
& Bitwise AND

| Bitwise OR

<< Left shift


>> Right shift

Truth table for bitwise & and |

a b a&b a|b
0 0 0 0

0 1 0 1

1 0 0 1
1 1 1 1

 The bitwise shift operators shifts the bit value. The left operand specifies the value to be shifted and the
right operand specifies the number of positions that the bits to be shifted.
Ex:
a = 0001000
b =2
a << b = 0100000
a >> b = 0000010

Conditional operator (Ternary operator)

 The conditional operator in C is also called as ternary operator.


 The syntax of a conditional operator is:
variable = expression1 ? expression2 : expression3

Explanation:
The question mark "?" in the syntax represents the if part.
The first expression (expression1) is used to check TRUE or FALSE.
If that condition (expression1) is TRUE then expression2 is assigned to the variable.
If that condition (expression1) is FALSE then expression3 is assigned to the variable.

Ex:
#include<stdio.h>
void main(){
int n1=5, n2=10, max;
max = (n1>n2) ? n1 : n2;
printf(“Biggest number is %d”,max);
}

Output:
Biggest number is 10

Problem Solving using C – Sudheer Kumar


Increment/Decrement operators:
24
 Increment operators are used to increase the value of the variable by one and decrement operators are
used to decrease the value of the variable by one in C programs.

 Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: --var_name; (or) var_name--;

 Example:
Increment operator : ++i ; i++ ;
Decrement operator : ––i ; i–– ;

Ex:

#include <stdio.h>
void main(){
int i=1;
while(i<10){
printf("%d ",i);
i++;
}
}

Output:
123456789

Special operators

Following are the special operators used in C:

OPERATOR DESCRIPTION
sizeof size of the variable

& address of the variable

* pointer to the variable

Ex:

#include <stdio.h>
int main() {
char ch = 'D';
int *p = &ch;
printf("size : %d byte\n",sizeof(ch));
printf("Address : %d\n",&ch);
printf("Value : %c",*p);
return 0;
}

Output:
size : 1 byte
Address : 505952423
Value : D

Problem Solving using C – Sudheer Kumar


14. C Programming Examples.
25
Program to find Factorial of a Number
Following is the program to find factorial of a number using for loop.

#include<stdio.h>
void main(){
int i,n,fact=1;
printf("Enter the number");
scanf("%d", &n);
for(i = 1; i <= n; i++){
fact = fact*i;
}
printf("Factorial is %d", fact);
}

Output
Enter the number 5
Factorial of 5 is 120

Program to Reverse a String


Following is the program to Reverse a String using for loop.

#include<stdio.h>
void main(){
int i=0, j, k;
char str[100];
char rev[100];
printf("Enter a string ");
scanf("%s", str);
while(str[i]!='\0') i++;
k=i-1;
for(j = 0; j < i; j++){
rev[j] = str[k--];
}
printf("The reverse string is %s\n", rev);
}

Output
Enter a string university
The reverse string is ytisrevinu

Program to Check whether a Number is Palindrome

#include<stdio.h>
void main(){
int n, temp, rev=0, rem;
printf("Enter a number\t");
scanf("%d", &n);
temp = n;
while(n > 0){
rem = n%10;
rev = (rev*10)+rem;
n = n/10;
}
if(rev == temp){
printf("The number is a palindrome");
}
else{
printf("The number is not a palindrome");
}
}

Output:
Enter a number 12321
The number is a palindrome

Problem Solving using C – Sudheer Kumar


Unit 2 : Decision making & Looping statements,
26
Jump control statements (Break, Continue & Goto).
01. Write about conditional statements in C. [or] Decision control statements.

Decision control (or) Conditional branching statements in C:

 Decision making is about deciding the order of execution of statements based on given
condition.
 C language handles decision-making by supporting the following statements.
 if statement
 switch statement

Decision making with if statement:

 The if statement may be implemented in different forms depending on the complexity of


conditions to be tested.
 The different forms are:
 Simple if statement
 If....else statement
 Nested if....else statement

Simple if statement:

 The general form of a simple if statement is,


if(expression){
statement-inside;
}
statement-outside;

 If the expression is true, then 'statement-inside' it will be executed, otherwise 'statement-


inside' is skipped and only 'statement-outside' is executed.
Ex:
#include <stdio.h>
void main(){
int x=15,y=13;
if (x>y){
printf("x is greater than y");
}
}
Output:
x is greater than y

if...else statement:
 The general form of ‘if...else’ statement is,
if(expression){
statement-block1;
}
else{
statement-block2;
}

If the 'expression' is true, the 'statement-block1' is executed, else 'statement-block1' is


skipped and 'statement-block2' is executed.

Problem Solving using C – Sudheer Kumar


Ex:
#include<stdio.h> 27
void main(){
int x=15,y=18;
if (x>y){
printf("x is greater than y");
}
else{
printf("x is not greater than y");
}
}

Output:
x is not greater than y

Nested if....else statement:


The general form of a ‘nested if...else’ statement is,

if(expression1){
if(expression2){
statement block1;
}
else{
statement block2;
}
}
else{
statement block3;
}

if 'expression1' is false the 'statement-block3' will be executed, otherwise it continues to perform the test
for 'expression2' . If the 'expression2' is true the 'statement-block1' is executed otherwise 'statement-
block2' is executed.

Ex:
#include<stdio.h>
void main(){
int a,b,c;
printf("enter 3 number");
scanf("%d%d%d",&a,&b,&c);
if(a>b){
if( a > c){
printf("a is greatest");
}
else{
printf("c is greatest");
}
}
else{
if( b>c){
printf("b is greatest");
}
else{
printf("c is greatest");
}
}
}

Problem Solving using C – Sudheer Kumar


Switch statement:
28
 A switch statement allows a variable to be tested for equality against a list of values.
 Each value is called a case, and the variable is checked for each switch case.

The general form of switch statement is :-

switch(expression){
case value-1:
block-1;
break;
case value-2:
block-2;
break;
case value-3:
block-3;
break;
case value-4:
block-4;
break;
default:
default-block;
break;
}

Rules for using switch:

 The case labels must be unique.


 The case labels must end with a colon(:)
 The following line, after case statement, can be any valid C statement.
 break statements are used to exit the switch block. It isn't necessary to use break after each
block.

Ex:
#include<stdio.h>
void main(){
int value = 2;
clrscr();
switch(value){
case 1: printf("A"); break;
case 2: printf("B"); break;
case 3: printf("C"); break;
}
}

Output:
B

Problem Solving using C – Sudheer Kumar


02. Write about iterative statements in C. [or] Looping statements.
29
Looping statements in C Language:

 Loops are used to execute a set of statements repeatedly until a particular condition is satisfied.
 This sequence of statements to be executed is kept inside the curly braces { } known as the Loop body.
 After every execution of loop body, condition is verified, and if it is found to be true the loop body is
executed again.
 When the condition check returns false, the loop body is not executed.

There are 3 type of Loops in C language


1. while loop
2. for loop
3. do-while loop

while loop:

while loop is called as entry control loop.

Syntax:
variable initialization;
while (condition){
statements ;
variable increment or decrement ;
}

Example : Program to print first ten multiples of 5.

#include<stdio.h>
void main(){
int a=5,i=1;
while(i<=10){
printf("%d ",a*i);
i++;
}
}

Output:
5 10 15 20 25 30 35 40 45 50

for loop:

for loop is used to execute a set of statements repeatedly until a particular condition is satisfied.

Syntax:
for(initialization; condition ; increment/decrement){
statement-block;
}

The for loop is executed as follows:

1. It first evaluates the initialization code.


2. Then it checks the condition expression.
3. If it is true, it executes the for-loop body.
4. Then it evaluate the increment/decrement condition and again follows from step 2.
5. When the condition expression becomes false, it exits the loop.

Problem Solving using C – Sudheer Kumar


Example : Program to print first ten multiples of 5
#include<stdio.h> 30
void main( ){
int i;
for(i=1;i<=10;i++){
printf("%d ",i*5);
}
}

Output
5 10 15 20 25 30 35 40 45 50

do while loop

 do while loop is called exit control loop.


 do statement evaluates the body of the loop first and at the end, the condition is checked using while
statement.
 It means that the body of the loop will be executed at least once.

Syntax:
do{
....
....
}while(condition)

Example : Program to print first ten multiples of 5.

#include<stdio.h>
void main(){
int a=5,i=1;
do{
printf("%d ",a*i);
i++;
}while(i <= 10);
}

Output
5 10 15 20 25 30 35 40 45 50

Problem Solving using C – Sudheer Kumar


03. Write about nested loops in C.
31
Nested Loops in C:

C programming allows us to use one loop inside another loop.

Syntax:

The syntax for a nested for loop statement in C is as follows −

for ( initialize; condition; increment ) {


for ( initialize; condition; increment ) {
statement(s);
}
statement(s);
}

The syntax for a nested while loop statement in C is as follows −

while(condition) {
while(condition) {
statement(s);
}
statement(s);
}

The syntax for a nested do...while loop statement in C is as follows −

do {
statement(s);
do {
statement(s);
}while( condition );
}while( condition );

Ex:
The following program uses a nested for loop.

#include<stdio.h>
void main(){
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++)
printf("%d ",j);
printf("\n");
}
}

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Problem Solving using C – Sudheer Kumar


04. How to use break, continue & goto statements in a C program?
32
Jumping Out of Loops:
Sometimes, while executing a loop, it becomes necessary to skip a part of the loop or to leave the loop as
soon as certain condition becomes true. This is known as jumping out of loop.

1) break statement:
break statement is used to immediately exit the loop, the program continues with the statement
immediately after the loop.

Example:

#include<stdio.h>
void main(){
int i;
for(i = 0; i<10; i++){
if(i == 5)
break;
printf("%d ",i);
}
printf("came outside of loop");
}

Output:
0 1 2 3 4 came outside of loop

2) continue statement
continue statement is used to leave the current cycle of loop, and starts with the next cycle.

Problem Solving using C – Sudheer Kumar


Example:

#include<stdio.h>
33
void main(){
int i;
for(i = 0; i<10; i++){
if(i == 5)
continue;
printf("%d ",i);
}
printf("came outside of loop");
}

Output:
0 1 2 3 4 6 7 8 9 came outside of loop

3) goto statement:

A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled statement
in the same function.

Flow Diagram: Example:

#include <stdio.h>
int main(){
int num=6,i=1;
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10) goto table;
}

Output:

6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60

Problem Solving using C – Sudheer Kumar


Unit 3 : Arrays & Strings
34
01. How to declare & access elements of an Array?

Arrays:

- Arrays can store a multiple elements of the same data type.


- Instead of declaring individual variables, such as number0, number1, ... and number99, we can declare
one array variable such as number and use number[0], number[1], … and numbers[99] to represent
individual variables.
- A specific element in an array is accessed by an index.
- All arrays consist of continuous memory locations. The lowest address corresponds to the first element and
the highest address to the last element.

Declaring Arrays

- To declare an array in C, we need to specify the data type and the number of elements required by an
array.
type arrayName [arraySize];
- This is called a single-dimensional array. The arraySize must be an integer greater than zero and type can
be any valid C data type.
- For example, to declare a 10-element array called balance of type integer, use this statement:
- int balance[10];
- Here, balance is an array variable which is sufficient to hold up to 10 integers.

Initializing Arrays

- We can initialize an array in C either one by one (or) using a single statement as follows:
int balance[5] = {27, 5, 19, 43, 29};
- The number of values between braces { } cannot be larger than the number of elements that we declare
for the array between square brackets [ ].
- If we not specify the size of the array, an array just big enough to hold the initialization is created.
Therefore, we can write:
int balance[] = {27, 5, 19, 43, 29};
- All arrays have 0 as the index of their first element, and the last index of an array will be total size of the
array minus 1.
- Below is the pictorial representation of the array we discussed above:

- Following is an example to assign a single element of the array:


balance[3] = 57;
- The above statement assigns the 4th element in the array with a value of 57.

Problem Solving using C – Sudheer Kumar


Accessing Array Elements
35
- An Array element is accessed using index.
- This is done by placing the index of the element within square brackets after the name of the array.
For example:
int salary = balance[3];
- The above statement will take the 4th element from the array and assign the value to the variable ‘salary’.
- The following example Shows how to use arrays in a ‘C’ program:

#include <stdio.h>
void main(){
int n[5]; // n is an array of 5 integers //
int i,j;
for ( i = 0; i<5; i++ ) {
n[ i ] = i + 100;
}
for (j = 0; j < 5; j++ ) {
printf("Element[%d] = %d \n", j, n[j] );
}
}

Output:

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104

02. Write about different operations used on Arrays.

There are number of operations that can be performed on arrays. These operations include:

1. Traversal: Traversing means accessing every element of the array.

Step 1: START
Step 2: initialize i  0
Step 3: Repeat until i<Array_Size
Apply process to A[i]
Set i  i+1
Step 4: STOP

2. Insertion: Inserting an element in the array means adding a new data element in an array. We can
insert the element at the end of the array or in the middle of the array.

Inserting element at the end of the array: (N is Array Size)

Step 1: START
Step 2: set N = N+1
Step 3: set A[N-1] = value
Step 4: EXIT

Inserting element in the middle of the array: (N is Array size)

Step 1: START
Step 2: initialize i=N-1
Step 3: Repeat until i>=pos
A[i+1]  A[i]
i i-1
Step 4: N  N+1
Step 5: A[pos]  val
Step 6: STOP

Problem Solving using C – Sudheer Kumar


3. Deletion: Deleting an element in the array means removing a data element from an array. We can
delete the element at the end of the array or from the middle of the array. 36
Deleting element at the end of the array: (N is Array Size)

Step 1: START
Step 2: N  N-1
Step 3: STOP

Deleting element from the middle of the array: (N is Array Size)

Step 1: START
Step 2: initialize ipos
Step 3: Repeat until i<N-1
A[i]  A[i+1]
i  i+1
Step 4: N  N-1
Step 5: STOP

4. Search: Searching means to find whether a particular value is present in the array or not.

Step 1: START
Step 2: initialize i0
Step 3: Repeat until i<N
if A[i] = value display “Element found at i”
goto Step 5
i  i+1
Step 4: display “ Element not found ”
Step 5: STOP

5. Merging:

To merge any two arrays, start appending each and every element of the first array to the third array (the
merged array). Then start appending each and every element of the second array to the third array (the
merged array)

6. Sorting:

Sorting means arranging the elements of the array in some order which may either be ascending (or)
descending.

Following are the popular sorting techniques:


Bubble sort, Selection sort, Insertion sort, Merge sort, Heap sort, Quick sort.

03. Write about two-dimensional arrays.

Multi dimensional arrays:

- C programming language allows multi dimensional arrays.


- Here is the general form of a multidimensional array declaration:
type name[size1][size2]...[sizeN];
- For example, the following declaration creates a three dimensional integer array:
int A[5][10][4];

Two dimensional arrays:

- The simplest form of multi dimensional array is the two-dimensional array.


- To declare a two-dimensional integer array of size [x][y], we should write as follows –
type arrayName [x][y];
- Where type can be any valid C data type and arrayName will be a valid C identifier.
- A two-dimensional array can be considered as a table which will have x number of rows and y number of
columns.

Problem Solving using C – Sudheer Kumar


- A two-dimensional array a, which contains 3 rows and 4 columns can be shown as follows:
37

- Every element in the array a is identified by a[ i ][ j ], where ‘a’ is the name of the array, and ‘i’ and ‘j’
are the subscripts that uniquely identify each element in ‘a’.

Initializing Two-Dimensional Arrays

- Two-dimensional arrays may be initialized by specifying bracketed values for each row.
- Following is an array with 3 rows and each row has 4 columns.
int a[3][4] = { {0, 1, 2, 3} , {4, 5, 6, 7} , {8, 9, 10, 11} };
- The following initialization is equivalent to the previous example:
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Accessing Two-Dimensional Array Elements

- An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column
index of the array. For example:
int val = a[2][3];
- The above statement will take the 4th element from the 3rd row of the array.
- The following program used a nested for loop to handle a two-dimensional array:

#include <stdio.h>
void main(){
int a[3][2] = {{0,0}, {1,2}, {2,4}};
int i, j;
for ( i = 0; i < 3; i++ ) {
for ( j = 0; j < 2; j++ ) {
printf(“a[%d][%d] = %d \n”, i, j, a[i][j] );
}
}
}

Output:

a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4

Problem Solving using C – Sudheer Kumar


04. Write a C program to add two matrices.
38
C program to perform Addition of Two Matrices.

#include<stdio.h>
#include<stdlib.h>
void main(){
int i, j,r1,c1,r2,c2;
int a[10][10], b[10][10], c[10][10];
printf("Enter First Matrix Size:");
scanf("%d %d",&r1,&c1);
printf("Enter Second Matrix Size:");
scanf("%d %d",&r2,&c2);
if(r1!=r2 || c1!=c2){
printf("Matrix Addition is not possible\n");
exit(0);
}
printf("\nEnter First Matrix Elements:");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
scanf("%d",&a[i][j]);
}
}
printf("\nEnter Second Matrix Elements:");
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\nAddition of Two Matrices:\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
printf(" %d",c[i][j]);
}
printf("\n");
}
}

Output:

Enter First Matrix Size : 2 3


Enter Second Matrix Size : 2 3

Enter First Matrix Elements : 1 2 3


4 5 6

Enter Second Matrix Elements : 5 10 15


20 25 30

Addition of Two Matrices : 6 12 18


24 30 36

Problem Solving using C – Sudheer Kumar


05. Write a C program to multiply two matrices.
39
C program to perform Multiplication of Two Matrices.

#include<stdio.h>
#include<stdlib.h>
void main(){
int i, j,k,r1,c1,r2,c2;
int a[10][10], b[10][10], c[10][10];
printf(“Enter First Matrix Size:”);
scanf(“%d %d”,&r1,&c1);
printf(“Enter Second Matrix Size:”);
scanf(“%d %d”,&r2,&c2);
if(c1!=r2){
printf(“Matrix Multiplication is not possible\n”);
exit(0);
}
printf(“\nEnter First Matrix Elements:”);
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
scanf(“%d”,&a[i][j]);
}
}
printf(“\nEnter Second Matrix Elements:”);
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf(“\nMultiplication of Two Matrices:\n”);
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
printf(“ %d”,c[i][j]);
}
printf(“\n”);
}
}

Output:

Enter First Matrix Size : 2 2


Enter Second Matrix Size : 2 3
Enter First Matrix Elements : 1 2
3 4
Enter Second Matrix Elements : 3 1 5
5 6 2
Multiplication of Two Matrices : 13 13 9
29 27 23

Problem Solving using C – Sudheer Kumar


06. Write about ‘string functions’ in C.
40
Strings:

- Strings are one-dimensional array of characters terminated by a null character '\0'.


- The following declaration and initialization create a string consisting of the word "Hello".
char str[5] = {'H', 'E', 'L', 'L', 'O'};
- We can write the above statement as follows:
char str[5] = "Hello";
- The C compiler automatically places the '\0' at the end of the string.

Example:

#include<stdio.h>
void main(){
char str[5]={'H','E','L','L','O'};
printf("%s",str);
}

Output:
HELLO

String Functions:

C supports a wide range of functions that manipulate strings. To use string functions in ‘C’ program, we
need to include the header file ‘string.h’.

S.No Function Description

01 strcpy(s1,s2) copies the string s2 into string s1

02 strcat(s1,s2) concatenates string s2 at the end of string s1

03 strlen(s1) returns the length of string s1

returns 0 if s1, s2 are same


04 strcmp(s1,s2) returns less than 0 if s1<s2
returns greater than 0 if s1>s2

returns a pointer to the first occurrence of character


05 strchr(s1,ch)
ch in string s1

returns a pointer to the first occurrence of string s2


06 strstr(s1,s2)
in string s1

Problem Solving using C – Sudheer Kumar


Example programs to demonstrate string functions:
41
Example 1:

#include<stdio.h>
#include<string.h>
void main(){
char s1[12] = "Hello";
char s2[12] = "World";
char s3[12];
strcpy(s3, s1);
printf("s3 : %s\n", s3 );
strcat( s1, s2);
printf("s1 : %s\n", s1 );
printf("Length of s1 : %d\n", strlen(s1) );
}

Output:
s3 : Hello
s1 : HelloWorld
Length of s1 : 10

Example 2:

#include<stdio.h>
#include<string.h>
void main(){
char s1[12] = "Hello";
char s2[12] = "World";
char s3[20] = "Life is Beautiful";
int result = strcmp(s1, s2);
printf("result : %d\n", result);
printf("%s\n",strchr(s1,'e'));
printf("%s",strstr(s3,"is"));
}

Output:
result : -15
ello
is Beautiful

Problem Solving using C – Sudheer Kumar


07. Write about ‘Character handling functions’ in C.
42
Character Handling Functions:

 C has functions to test or manipulate individual characters.


 The functions are all defined in the ctype.h header file.
 To use the CTYPE functions, this header file must be included in your source code:
#include <ctype.h>
 CTYPE functions are of two categories: testing and manipulation.
 The following are some of the testing functions.

isalnum(ch) : This function returns non zero value if given character is alphabet or number &
returns zero if given character is not alphabet or number.

Example:

#include<stdio.h>
#include<ctype.h>
int main(){
char ch = 'S';
if (isalnum(ch)!=0)
printf("Given character is Alphabet or Number");
else
printf("Given character is Not Alphabet or Number");
return 0;
}

Output:
Given character is Alphabet or Number

isalpha(ch) : This function returns non zero value if given character is alphabet & returns zero
if given character is not alphabet.

Example:

#include<stdio.h>
#include<ctype.h>
int main(){
char ch = '6';
if (isalpha(ch)!=0)
printf("Given character is Alphabet");
else
printf("Given character is Not Alphabet");
return 0;
}

Output:
Given character is Not Alphabet

Problem Solving using C – Sudheer Kumar


isdigit(ch) : This function returns non zero value if given character is digit & returns zero if
given character is not digit. 43
Example:
#include<stdio.h>
#include<ctype.h>
int main(){
char ch = '6';
if (isdigit(ch)!=0)
printf("Given character is Digit");
else
printf("Given character is Not Digit");
return 0;
}

Output:
Given character is Digit

islower(ch) : This function returns non zero value if given character is lower case alphabet &
returns zero if given character is not in lower case.

Example:
#include<stdio.h>
#include<ctype.h>
int main(){
char ch = 'R';
if (islower(ch)!=0)
printf("Given character is lower case alphabet ");
else
printf("Given character is Not in lower case ");
return 0;
}

Output:
Given character is Not in lower case

isupper(ch) : This function returns non zero value if given character is upper case alphabet &
returns zero if given character is not in upper case.

Example:
#include<stdio.h>
#include<ctype.h>
int main(){
char ch = 'R';
if (isupper(ch)!=0)
printf("Given character is Upper case alphabet ");
else
printf("Given character is Not in Upper case ");
return 0;
}

Output:
Given character is Upper case alphabet

Problem Solving using C – Sudheer Kumar


 The following are some of the Manipulation functions.
44
toascii(ch) – This function returns the ASCII code value of the given character.
Example:
#include<stdio.h>
#include<ctype.h>
int main(){
char ch = 'T';
printf("ASCII code of Given character is %d",toascii(ch));
return 0;
}

Output:
ASCII code of Given character is 84

tolower(ch) – This function converts the given character into lower case.

Example:
#include<stdio.h>
#include<ctype.h>
int main(){
char ch = 'D';
printf("Given character in lower case is : %c",tolower(ch));
return 0;
}

Output:
Given character in lower case is : d

toupper(ch) – This function converts the given character into upper case.

Example:
#include<stdio.h>
#include<ctype.h>
int main(){
char ch = 'd';
printf("Given character in upper case is : %c",toupper(ch));
return 0;
}

Output:
Given character in upper case is : D

Problem Solving using C – Sudheer Kumar


Unit 4 : Functions, Storage classes, Pointers
45
01. Write about the declaration & definition of functions in C.

Functions:

 A function is a group of statements which can be used to perform a task.


 Every C program has at least one function, which is main(). We can divide the program code into
separate functions.
 A function declaration tells the compiler about a function's name, return type, and parameters.
 A function definition provides the actual body of the function.
 The C standard library provides many built-in functions that can be used in the program.
 For example, printf() & scanf() are the functions used to write data & read data.
 A function can also be called as a method or a sub-routine or a procedure etc.

Declaration of a Function:

 A function declaration tells the compiler about a function name and how to call the function. The actual
body of the function can be defined separately.
 A function declaration has the following parts:
return_type function_name( parameter list );

Ex:
To find maximum of two numbers, we can declare a function as follows:
int max(int a, int b);

Defining a Function

 The general form of a function definition in C programming language is as follows:

return_type function_name( parameter list ) {


body of the function
}

 A function definition in C programming consists of a function header and a function body.


 Here are all the parts of a function:

Return Type: A function may return a value. The return_type is the data type of the value the function
returns. If a function doesn’t return a value, then the return_type is the keyword ‘void’.

Function Name: This is the actual name of the function. The function name and the parameter list
together called the function signature.

Parameters: When a function is invoked, we need to pass a value to the parameter. This value is referred
to as actual parameter (or) argument. The parameter list refers to the type, order, and number of the
parameters of a function. A function may (or) may not contain parameters.

Function Body: The function body contains a collection of statements.

Ex: To find maximum of two numbers, the function definition will be as shown below:

int max(int a, int b){


int result;
if (a>b)
result = a;
else
result = b;
return result;
}

Problem Solving using C – Sudheer Kumar


02. How to call functions? Write about ‘return’ statement in functions.
46
Calling a Function

 To use a function, we need to call that function.


 When a program calls a function, the program control is transferred to the called function.
 A called function performs a defined task and the program control will be back to the main program.
 To call a function, you simply need to pass the required parameters along with the function name.
 If the function returns a value, then we can store the returned value.

Example:

#include<stdio.h>
int max(int a, int b); //function declaration
void main(){
int num1 = 100;
int num2 = 200;
printf( "Max value is : %d", max(num1,num2)); // calling the function
}
int max(int a, int b){ //function definition
int result;
if (a>b)
result = a;
else
result = b;
return result;
}

Output:
Max value is : 200

03. How to pass parameters to the functions? (or) Explain Call By Value & Call By
Reference.

Function Parameters (or) Arguments:

 If a function is to use arguments, it must declare variables that accept the values of the arguments. These
variables are called the formal parameters of the function.
 Formal parameters behave like other local variables inside the function and are created upon entry into the
function and destroyed upon exit.
 There are two ways in which arguments can be passed to a function:

call by value:

 The call by value method of passing arguments to a function copies the actual value of an argument into
the formal parameter of the function. In this case, changes made to the parameter inside the function
have no effect on the argument.

Problem Solving using C – Sudheer Kumar


 Example: Below C program uses swap(). 47
#include<stdio.h>
void swap(int a, int b);
void main(){
int num1 = 100;
int num2 = 300;
printf( "Before swap: num1 = %d num2 = %d \n", num1,num2);
swap(num1,num2);
printf( "After swap: num1 = %d num2 = %d \n", num1,num2);
}
void swap(int a, int b){
int temp;
temp=a;
a=b;
b=temp;
}

Output:

Before swap : num1 = 100 num2 = 300


After swap : num1 = 100 num2 = 300

 It shows that there is no change in the values, though they had been changed inside the function.

call by reference:

 The call by reference method of passing arguments to a function copies the address of an argument into
the formal parameter. Inside the function, the address is used to access the actual argument. It means the
changes made to the parameter affect the passed argument.
 To pass a value by reference, we need to declare the function parameters as pointer types.

Example: Below C program uses swap().

#include<stdio.h>
void swap(int *x, int *y);
void main(){
int num1 = 100;
int num2 = 300;
printf( "Before swap: num1 = %d num2 = %d \n", num1,num2);
swap(&num1,&num2);
printf( "After swap: num1 = %d num2 = %d \n", num1,num2);
}
void swap(int *x, int *y){
int temp;
temp=*x;
*x=*y;
*y=temp;
}

Output:

Before swap : num1 = 100 num2 = 300


After swap : num1 = 300 num2 = 100

As shown above, the values of the arguments are changed.

Problem Solving using C – Sudheer Kumar


04. Write about the scope of variables in a C program.
48
Scope of variables:

 A scope of variable is a region of the program where the variable can have its existence.
 There are mainly two places where variables can be declared in C programming language:

Local variables : Inside a function (or) a block.


Global variables : Outside of all functions.

Local Variables:

 Variables that are declared inside a function (or) block are called local variables.
 They can be used only by statements that are inside that function (or) block of code.
 Local variables can’t be accessible from outside functions.
 The following example shows how local variables are used.

#include<stdio.h>
void main(){
int a, b, c;
a = 10;
b = 20;
c = a + b;
printf ("value of a = %d, b = %d and c = %d", a, b, c);
}

Output:
Value of a = 10, b = 20 and c = 30

Global Variables

 Global variables are defined outside a function, usually on top of the main function.
 Global variables hold their values throughout the lifetime of the program and they can be accessed inside
any of the functions defined for the program.
 The following program shows how global variables are used in a program.

#include<stdio.h>
int g; // global variable
void main(){
int a, b; // local variables
a = 10;
b = 20;
g = a + b;
printf ("Value of a = %d, b = %d and g = %d", a, b, g);
}

Output:
Value of a = 10, b = 20 and g = 30

 A program can have same name for local and global variables but the value of local variable inside a
function will take preference. Here is an example −

#include<stdio.h>
int g = 20;
void main(){
int g = 10;
printf ("value of g = %d", g);
}

Output:
value of g = 10

Problem Solving using C – Sudheer Kumar


05. Write about ‘recursive functions’ (or) ‘Recursion’ in C.
49
Recursive function (or) Recursion:

- A recursive function is a function that calls itself.


- Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial
of a number, generating Fibonacci series etc.
- Working of recursion will be as shown below:

Ex1: Calculating factorial of a number using a recursive function:

#include<stdio.h>
void main(){
int i = 6;
printf("Factorial of %d is %d\n", i, factorial(i));
}
int factorial(int n) {
if(n <= 1) return 1;
else return n * factorial(n-1);
}

Output:

Factorial of 6 is 720

Ex2: Generating Fibonacci series using recursive function:

#include<stdio.h>
void main(){
int i;
for (i = 0; i < 10; i++)
printf("%d\t", fibonacci(i));
}
int fibonacci(int i) {
if(i == 0) return 0;
if(i == 1) return 1;
return fibonacci(i-1) + fibonacci(i-2);
}

Output:

0 1 1 2 3 5 8 13 21 34

Problem Solving using C – Sudheer Kumar


06. Write about ‘storage classes’ in C.
50
- Storage Classes in C: The storage class of a variable dictates how, when and where storage will be
allocated for the variable. There are four different storage classes available as shown below.

Automatic variables Register Variables

Keyword : auto Keyword : register


Storage Location : Main memory Storage Location : CPU Register
Initial Value : Garbage value Initial Value : Garbage value
Scope : Local to the block Scope : Local to the block

Syntax : auto [data_type][variable_name]; Syntax : register [data_type][variable_name];

Example: Example:

#include<stdio.h> #include<stdio.h>
void main(){ void main(){
auto int i = 10; register int i = 10;
{ {
auto int i = 20; auto int i = 20;
printf(“%d\n”,i); printf(“%d\n”,i);
} }
printf(“%d”,i); printf(“%d”,i);
} }

Output: Output:
20 20
10 10

External Variables Static Variables

Keyword : extern Keyword : static


Storage Location : Main memory Storage Location : Main memory
Initial Value : Zero Initial Value : Zero & can be initialize only
Scope : Global to the Program once
Scope : Local to the Block

Syntax : extern [data_type][variable_name]; Syntax : static [data_type][variable_name];

Example: Example:

#include<stdio.h> #include<stdio.h>
extern int i=10; void main(){
void main(){ int i;
void show(); void incre();
int i = 20; for (i=0;i<3;i++)
printf("%d\n",i); incre();
show(); }
} void incre(){
void show(){ int a=1;
printf("%d\n",i); static int b =1;
} a++;
b++;
Output: printf("a = %d ",a);
20 printf("b = %d\n",b);
10 }

Output:
a=2b=2
a=2b=3
a=2b=4

Problem Solving using C – Sudheer Kumar


07. How to use pointer variables in C? 51
Pointers in C:

 Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is
used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of
the data type such as int, float, char, double, short etc.
 Pointer Syntax : data_type *var_name; Example : int *p; char *p;
 Here, * is used to denote that “p” is pointer variable and not a normal variable.

KEY POINTS TO REMEMBER ABOUT POINTERS IN C:

 Normal variable stores the value whereas pointer variable stores the address of the variable.
 The content of the C pointer always be a whole number i.e. address.
 Always C pointer is initialized to null, i.e. int *p = null.
 The value of null pointer is 0.
 & symbol is used to get the address of the variable.
 * symbol is used to get the value of the variable that the pointer is pointing to.
 If a pointer in C is assigned to NULL, it means it is pointing to nothing.
 Two pointers can be subtracted to know how many elements are available between these two pointers.
 But, Pointer addition, multiplication, division are not allowed.
 The size of any pointer is 2 byte (for 16 bit compiler).

EXAMPLE PROGRAM FOR POINTERS IN C:

#include<stdio.h>
int main(){
int *ptr, q;
q = 50;
ptr = &q;
printf("%d", *ptr);
return 0;
}

Output:
50

08. Write about pointer arithmetic.

Pointer Arithmetic

We can perform addition and subtraction of integer constant from pointer variable.

Addition
We can perform pointer addition like ptr +1, ptr + 2 etc.

Example:
#include<stdio.h>
void main(){
int a;
int *ptr = &a;
printf("%p\n",ptr);
printf("%p\n",ptr+1);
printf("%p",ptr+2);
}

Output:
0x7ffdc7effe34
0x7ffdc7effe38
0x7ffdc7effe3c

Problem Solving using C – Sudheer Kumar


Subtraction 52
We can perform pointer subtraction like ptr-1, ptr-2 etc.
We cannot perform addition, multiplication and division operations on two pointer variables.
So, statements like ptr1 + ptr2 are not valid.

Example:

#include<stdio.h>
void main(){
int a;
int *ptr = &a;
printf("%p\n",ptr);
printf("%p\n",ptr-1);
}

Output:
0x7ffc509f2d34
0x7ffc509f2d30

Multiplication

Example:

#include<stdio.h>
void main(){
int x = 10, y = 20, z;
int *ptr1 = &x;
int *ptr2 = &y;
z = *ptr1 * *ptr2 ;
printf("z = %d",z);
}

Output:
z = 200

Division

There is a blank space between '/' and * in the below program because the symbol /*is considered as
beginning of the comment and therefore the statement fails.

#include<stdio.h>
void main(){
int a=20,b=4,c;
int *ptr1 = &a, *ptr2 = &b;
c = *ptr1/ *ptr2;
printf("c = %d", c);
}

Output:
c=5

Problem Solving using C – Sudheer Kumar


09. How to pass pointer variables as arguments to the functions?
53
Passing a pointer to a function:

 C programming allows passing a pointer to a function.


 To do so, simply declare the function parameter as a pointer type.
 When we pass a pointer as an argument instead of a variable then the address of the variable is passed
instead of the value.
 So any change made by the function using the pointer is permanently made at the address of passed
variable.
 This technique is known as call by reference in C.

Example:

#include <stdio.h>
void main(){
int salary=10000, bonus=2000;
salaryhike(&salary, bonus);
printf("Final salary: %d", salary);
}
void salaryhike(int *var, int b){
*var = *var+b;
}

Output:
Final salary: 12000

 If we try this same program without pointer, we find that the bonus amount will not be added in the
salary, this is because the change made by the function would be done to the local variables of the
function.
 When we use pointers, the value is changed at the address of variable.

10. Write about using Pointers in Arrays.

Pointer and Arrays in C

 When an array is declared, compiler allocates sufficient amount of memory to contain all the elements of
the array.
 Base address i.e. address of the first element of the array is also allocated by the compiler.
 Suppose we declare an array arr,
int arr[5] = { 1, 2, 3, 4, 5 };
Assuming that the base address of arr is 1000 and each integer requires two bytes, the five elements will
be stored as shown below:

Problem Solving using C – Sudheer Kumar


 Here variable arr will give the base address, which is a constant pointer pointing to the first element of the
array, arr[0]. Hence arr contains the address of arr[0] i.e. 1000. 54
 In short, arr has two purposes - it is the name of the array and it acts as a pointer pointing towards the
first element in the array.

arr is equal to &arr[0] by default

 We can also declare a pointer of type int to point to the array arr.

int *p;
p = arr;
// or,
p = &arr[0]; //both the statements are equivalent.
Now we can access every element of the array arr using p++ to move from one element to another.

NOTE: You cannot decrement a pointer once incremented. p-- won't work.

Pointer to Array

As studied above, we can use a pointer to point to an array, and then we can use that pointer to access
the array elements. Let’s have an example,

#include <stdio.h>
void main(){
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int*p = &a[0]
for (i = 0; i < 5; i++){
printf("%d", *p);
p++;
}
}

Output:
12345

In the above program, the pointer *p will print all the values stored in the array one by one. We can also
use the Base address (a in above case) to act as a pointer and print all the values.

Problem Solving using C – Sudheer Kumar


Unit 5 : Dynamic memory management, Structures, Unions
55
01. Write about memory allocation in C.

Dynamic memory allocation in C

 The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at
runtime.
 Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.
 malloc() - allocates single block of requested memory.
 calloc() - allocates multiple block of requested memory.
 realloc() - reallocates the memory occupied by malloc() or calloc() functions.
 free() - frees the dynamically allocated memory.

 Before learning above functions, let's understand the difference between static memory allocation and
dynamic memory allocation.

STATIC MEMORY ALLOCATION DYNAMIC MEMORY ALLOCATION


memory allocated at compile time memory allocated at run time
memory can’t be increased memory can be increased
used in Arrays used in linked lists

malloc() function in C

 The malloc() function allocates single block of requested memory.


 It doesn't initialize memory at execution time, so it has garbage value initially.
 It returns NULL if memory is not sufficient.
 The syntax of malloc() function is given below:
ptr=(cast-type*)malloc(byte-size)
 Let's see the example of malloc() function.

#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL){
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i){
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
}

Output:
Enter number of elements: 3
Enter elements of array: 12
14
16
Sum=42

Problem Solving using C – Sudheer Kumar


calloc() function in C
56
 The calloc() function allocates multiple blocks of requested memory.
 It initially initialize all bytes to zero.
 It returns NULL if memory is not sufficient.
 The syntax of calloc() function is given below:
ptr=(cast-type*)calloc(number, byte-size)
 Let's see the example of calloc() function.

#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int)); //memory allocated using calloc
if(ptr==NULL){
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum = sum + *(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
}

Output:

Enter number of elements: 3


Enter elements of array: 12 15 16
Sum=43

realloc() function in C

 If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by realloc() function.
In short, it changes the memory size.
 Syntax of realloc() function.
ptr=realloc(ptr, new-size)

free() function in C

 The memory occupied by malloc() or calloc() functions must be released by calling free() function.
Otherwise, it will consume memory until program exit.
 Syntax of free() function.
free(ptr)

Problem Solving using C – Sudheer Kumar


02. Write about ‘structures’ in C.
57
Structures:

 It may be defined as a secondary data type which can store collection of dissimilar data elements. In order
to define structure we have to use the special keyword “struct”.

Syntax:

struct structure_name{
datatype variablename;
datatype variablename;
datatype variablename;
}structure_variable;

Example:
struct employee{
int eno;
char name[20]; 26 bytes elements/members of structure
float salary;
};
struct employee e1,e2;  each structure variables occupies 26 bytes of memory.

Initialization of Structure:

 Structure can be initialized by placing the elements within a pair of braces and separated by comma.
struct employee e1={101,”SUDHEER”,3500.00};

Accessibility of structure elements:

 Structure elements can be accessed with the help of dot (or) arrow operators.
 Ex: e.eno,e.name,e.salary;
 Ex: eeno,ename,esalary;

Example program to demonstrate structures:

#include<stdio.h>
void main(){
struct employee{
int eno;
char name[20];
float salary;
} e1;
printf("Enter Employee Details:");
scanf("%d%s%f",&e1.eno,&e1.name,&e1.salary);
printf("******** EMPLOYEE DETAILS *******");
printf("\n Employee No = %d", e1.eno);
printf("\n Employee Name = %s", e1.name);
printf("\n Salary = %f", e1.salary);
}

Output:

Enter Employee Details:102 Sudheer 20000

******** EMPLOYEE DETAILS *******


Employee No = 102
Employee Name = Sudheer
Salary = 20000.000000

Problem Solving using C – Sudheer Kumar


03. Write about ‘unions’ in C.
58
C Unions

 Unions are conceptually similar to structures.


 The syntax to declare union is also similar to that of a structure. The only difference is in terms of storage.
 In structure each member has its own storage location, whereas all members of union uses a single
shared memory location which is equal to the size of its largest data member.

 This implies that although a union may contain many members of different types, it cannot handle all
the members at the same time. A union is declared using the union keyword.

Accessing a Member of a Union:


 In C program, members of union can be accessed using dot (.) or by using arrow ().
 To access the fields of a union, use the dot operator(.). That is the union variable name followed by the
dot operator followed by the member name.

Initializing Unions:
 A difference between a structure and a union is that in case of a union, the fields share the same memory
space, so fresh data replaces any existing data.

#include<stdio.h>
union employee{
int eno;
char name[20];
float salary;
} e1;
void main(){
printf("Enter Employee Details:");
scanf("%d%s%f",&e1.eno,&e1.name,&e1.salary);
printf("******** EMPLOYEE DETAILS *******");
printf("\n Employee No = %d", e1.eno);
printf("\n Employee Name = %s", e1.name);
printf("\n Salary = %f", e1.salary);
}

Output:
Enter Employee Details:102 Sudheer 20000

******** EMPLOYEE DETAILS *******


Employee No = 1184645120
Employee Name =
Salary = 20000.000000

 In the output of the above program, only salary is displayed correctly, because the data entered for salary
replaced already entered data for employee number & name.
Problem Solving using C – Sudheer Kumar
59
B.Sc (Honours) Computer Science
(Examination at the end of Second Semester)
Problem Solving using C
(Regulation : 2023-24) – Model paper 1

Time : Three hours Maximum : 70 marks

SECTION A – (5 x 4 = 20 marks)
Answer any FIVE of the following questions.

1. Characteristics & Limitations of computers.


2. Types of software.
3. Features of Algorithm.
4. Features of C Programming Language.
5. How to use break & continue statements?
6. Explain Call by value & Call by reference.
7. Write about the scope of variables.
8. Write about pointer arithmetic.

SECTION B – (5 x 10 = 50 marks)
Answer following questions.

UNIT 1 UNIT IV

9. a) Draw a block diagram of computer. 12. a) Write about ‘recursive functions’in C.


(or) (or)
b) Write about the structure of C program. b) Write about ‘storage classes’ in C.

UNIT II UNIT V

10. a) Write about conditional statements in C. 13. a) Explain Dynamic memory allocation in C.
(or) (or)
b) Write about iterative statements in C. b) Write about ‘structures’ in C.

UNIT III

11. a) Write about different operations used on


Arrays.
(or)
b) Write about ‘string functions’ in C.

Problem Solving using C – Sudheer Kumar


60
B.Sc (Honours) Computer Science
(Examination at the end of Second Semester)
Problem Solving using C
(Regulation : 2023-24) – Model paper 2

Time : Three hours Maximum : 70 marks

SECTION A – (5 x 4 = 20 marks)
Answer any FIVE of the following questions.

1. Data types in C.
2. C-tokens.
3. Write about flow charts.
4. History of C Programming Language.
5. How to use switch statement.
6. Rules to construct variable names.
7. Pointer variables in C.
8. How to use pointers in Arrays.

SECTION B – (5 x 10 = 50 marks)
Answer following questions.

UNIT 1 UNIT IV

9. a) Write about I/O statements in C. 12. a) Write about functions in C.


(or) (or)
b) Operators used in a C program. b) How to pass pointer variables as arguments
to the functions?

UNIT II UNIT V

10. a) Write about nested loops in C. 13. a) Explain : malloc(), calloc() & realloc() in C.
(or) (or)
b) Break, continue & goto statements b) Write about ‘unions’ in C.

UNIT III

11. a) Write about two-dimensional arrays.


(or)
b) Write about ‘character functions’ in C.

Problem Solving using C – Sudheer Kumar

You might also like