Bca C Unit 1
Bca C Unit 1
BCA: SEMESTER I
UNIT – II: Decision Making and Branching: Introduction, Decision Making with IF Statement, Simple If
Statement, The If....Else Statement, Nested of If....Else Statements, The else if Ladder, The Switch
Statement, The ? : Operator, The Goto Statement.
Decision Making and Looping: Introduction, The While statement, the do Statement, the For
Statement, Jumps in Loops.
Arrays: Introduction, One-dimensional Arrays, Declaration and Initialization of One dimensional Arrays,
Two-dimensional Arrays, Initializing two-dimensional Arrays, Multi-dimensional Arrays, Dynamic
Arrays.
UNIT- III Character Arrays and Strings: Introduction, Declaring and Initializing String Variables, Reading
Strings from Terminal, Writing Strings to Screen, Arithmetic Operations on Characters, Putting Strings
together, Comparison of Two Strings, String-handling Functions, Table of Strings, Other Features of
Strings.
User-Defined Functions : Introduction, Need for User-Defined Functions, A Multi-function Program,
Elements of user-defined Functions, Definition of Functions, Return Values and their Types, Function
Calls, Function Declaration, Category of Functions, No Arguments and No Return Values, Arguments
but No Return Values, Arguments with Return Values, No Arguments but Returns a Value, Functions
that Return Multiple Values, Nesting of Functions, Recursion, Passing Arrays to Functions, Passing
Strings to Functions, The Scope, Visibility and Lifetime of Variables, Multifile Programs.
UNIT- IV Pointers: Introduction, Understanding Pointers, Accessing the Address of a Variable, Declaring
Pointer Variables, Initialization of pointer Variables, Accessing a Variable through its pointer, Chain of
Pointers, Pointer Expressions, Pointer Increments and Scale Factor, Pointers and Arrays, Pointer and
Arrays, Pointers and Character Strings, Arrays of Pointers, Pointers as Function Arguments, Functions
Returning Pointers, Pointers to Functions, Pointers and Structures.
Structures and Unions : Introduction, Defining a Structure, Declaring Structure Variables, Accessing
Structure Members, Structure Initialization, Copying and Comparing Structure Variables, Operations on
Text Book: 1. Computing Fundamentals & C Programming – by E Balagurusamy, 1st Edition McGrawHill
Education.
References: 1. Spoken Tutorial on “C”, as E-resource for Learning. http://spoken-tutorial.org
*****
1. Write a c program for electricity bill tacking different categories of users, different slabs in each
category. (Using nested if else statement)
2. Write a c program to evaluate the following using loops
a) 1+x2/2!+x4/4!+...up to 5 terms b) x+x3/3!+x5/5!+...up to 5 terms
3. Write a c program to check whether the given number is a. Prime or not b. Perfect or abundant or
deficient.
4. Write a c program to find the mean, mode, median, and variance of list of values by using one
dimensional array.
5. Write a menu driven program to read a list of numbers and perform the following operations
a) Print the list, b) Delete duplicates from the list c) Reverse the list
6. Write a program to read a list of numbers and search for given number using binary search
algorithm and if found display its index otherwise display the message "element not found in the list"
using function.
7. Write a menu driven program to read two matrices and compute their sum and product using
functions
8. Write a menu driven program to read list of student names and perform the following operations
using functions. a. To print list of names b. To sort them in ascending order c. To print the list after
sorting
9. Write a c program that consists of recursive functions to find
a. Factorial of a given number b. Print the Pascal triangle using binomial theorem
10. Write a menu driven program to read list of student names and perform the following operations
using array of character pointers.
a) To insert a student name b) To delete a name c) To print the names.
*****
2. Application Software:
Application software is designed to help users perform specific tasks.
Types of Application Software:
General-purpose Software: Used for common tasks.
Examples: MS Word, Excel, PowerPoint, web browsers.
Specialized/Domain-specific Software: Used for specific industries or functions (e.g.,
accounting software like Tally, graphic design tools like Adobe Photoshop).
Productivity Software: Increases efficiency (e.g., project management tools like Trello, MS
Project).
Educational Software: Designed for learning and teaching (e.g., Duolingo, Moodle).
Multimedia Software: For creating and playing media (e.g., VLC Media Player, Adobe Premiere
Pro).
Web-based Applications: Applications that run on web browsers (e.g., Google Docs, Canva).
1. Low-Level Languages : These are close to machine language and difficult for humans to understand
but offer high performance.
a) Machine Language: Binary code (0s and 1s)
Directly executed by the CPU.
Example: 10110000 01100001
b) Assembly Language: Uses mnemonics like MOV, ADD, SUB
Requires an assembler to convert into machine code
Hardware-specific
Example: MOV AX, 1, ADD AX, 2
3. Very High-Level Languages (VHLL): Focus on reducing complexity, used in specific fields. Examples:
SQL – Database queries
MATLAB – Mathematical computing
R – Statistical computing
Scratch – Visual block-based language for education
4. Scripting Languages: Used for automating tasks; often interpreted, not compiled.
Examples: Python, Perl, Bash, JavaScript
2. Problem Solving Techniques: Problem solving in computer science involves identifying a problem,
developing an algorithm, and implementing a solution using a programming language.
Steps in Problem Solving:
Understanding the Problem
Identify inputs, outputs, and constraints.
Planning the Solution
Choose an approach (algorithm).
Techniques include:
Flowcharts-Pseudo code-Algorithms
Designing the Algorithm
Write logical steps to solve the problem.
Coding the Solution
Convert the algorithm into a programming language.
Testing and Debugging
Run the program and fix errors (bugs).
Documentation-Write notes and instructions for future reference.
Maintenance
Update or modify the program as needed.
2. Importance of C Language
Foundation Language: Base for C++, Java, Python, etc.
Portability: Can run on different hardware platforms.
Speed: Compiled and fast, close to hardware.
Structured Language: Allows use of functions and modular code.
System-Level Programming: Used for OS, compilers, embedded systems.
3. Basic Structure of a C Program
#include <stdio.h> // Preprocessor directive
// Main function - execution starts here
int main() {
printf("Hello, World!"); // Output statement
return 0; // Exit code
}
B.C.A: Programming Concept using C 6
Key Components:
Component Purpose
#include <stdio.h> Includes standard input/output library
main() Entry point of the program
{ ... } Denotes block of code
printf() Prints output to screen
return 0; Ends the program
4. Sample Programs in C
a) Print a Message
#include <stdio.h>
int main() {
printf("Welcome to C Programming!");
return 0; }
b) Add Two Numbers
#include <stdio.h>
int main() {
int a = 5, b = 7;
int sum = a + b;
printf("Sum = %d", sum);
return 0;}
5. Programming Style in C: Good programming style improves readability, debugging, and
maintainability. Guidelines:
Use meaningful variable names (e.g., totalMarks instead of tm)
Use indentation and proper spacing
Add comments to explain code
Use modular code (break into functions)
Maintain consistent brace placement
Example (Good Style):
#include <stdio.h>
// Function to add two numbers
int add(int x, int y) {
return x + y;
}
int main() {
int result = add(10, 15);
printf("Sum = %d", result);
return 0;
}
5. Tokens, Keywords and Identifiers, Constants, Variables, Data Types explain with example.
1. Character Set in C: The Character Set refers to all the valid characters that can be used in C
programming. Categories:
Character Type Examples
Letters A–Z, a–z
Digits 0–9
Special Symbols ~, !, @, #, $, %, ^, &, *, (, ), {}, [], ;, :, ,, . etc.
Whitespace Space, Tab, Newline (\n)
Escape Sequences \n, \t, \\, \', \"
2. C Tokens: Tokens are the smallest elements of a C program. The C compiler breaks the program into
these tokens. Types of C Tokens:
Token Type Example
Keywords int, return, if, else
Identifiers sum, main, total_marks
Constants 100, 'A', 3.14
Operators +, -, *, /, ==
Special Symbols (), {}, ;, []
Strings "Hello"
5. Variables in C: Variables are containers used to store data that can change during program
execution.
Rules for Declaring Variables:
Must be declared before use.
Must have a valid identifier name.
Data type must be specified.
Example: int age = 21;
float salary = 30000.50;
char grade = 'A';
6. Data Types in C: Data types define the type of data a variable can store.
Primary Data Types:
Type Description Example
int Integer numbers int a = 10;
float Floating-point numbers float b = 3.14;
char Single characters char c = 'A';
double Double precision floatdouble d = 9.81;
void No value (used in functions) void main()
2. Declaration of Storage Class: A storage class defines the scope, visibility, lifetime, and default initial
value of a variable.
Storage Classes in C:
Storage Class Scope Lifetime Default Value Keyword
auto Local (default) Inside function Garbage value auto
register CPU Register Inside function Garbage value register
static Local/Global Entire program 0 (if not initialized) static
extern Global Declared elsewhere Actual variable value extern
Example: static int count = 0;
extern int total; // declared in another file
3. Assigning Values to Variables: You can assign a value to a variable at the time of declaration or later.
Examples: int age = 25; // Declaration with assignment
age = 30; // Assignment later
float pi = 3.14;
char grade = 'A';
4. Defining Symbolic Constants: Symbolic constants are created using the #define preprocessor
directive.
Syntax: #define CONSTANT_NAME value
Example: #define PI 3.14159
#define MAX_STUDENTS 100
printf("Value of PI is %f", PI);
Note: No semicolon at the end of #define.
b) Volatile Keyword: Used to tell the compiler not to optimize a variable because its value may change
unexpectedly (e.g., hardware registers, multi-threading).
Example: volatile int timerRegister;
Useful when variable values are changed outside normal program flow (e.g., interrupt routines or
hardware).
Underflow: Occurs when a value goes below the minimum limit a data type can hold.
Example: #include <stdio.h>
int main() {
float x = 1.0e-38;
float y = x / 10.0; // Underflow may occur
printf("%e", y); // Very small number or 0
return 0;
}
Managing Input and Output Operations: Introduction, Reading a Character, Writing a Character,
Formatted Input and Output.
2. Reading a Character
Using getchar():
Reads a single character from the keyboard.
Does not require any argument.
Example: #include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
ch = getchar(); // Input
printf("You entered: ");
putchar(ch); // Output
return 0;
}
3. Writing a Character: Using putchar(), Outputs a single character to the screen.
Takes a character variable or constant as an argument.
Example: char grade = 'A';
putchar(grade);
B.C.A: Programming Concept using C 11
4. Formatted Input and Output
Formatted Input: scanf() , Used to read values of any data type from the user.
Syntax: scanf("format_specifier", &variable);
Example: int age;
scanf("%d", &age); // Reading integer
6. Increment and Decrement Operators: Used to increase or decrease the value of a variable by 1.
Operator Meaning Example
++ Increment a++, ++a
-- Decrement a--, --a
++a (Pre-increment) → Increments, then uses the value
a++ (Post-increment) → Uses the value, then increments
7. Conditional (Ternary) Operator: Used to evaluate a condition and return one of two values.
Syntax: (condition) ? expression1: expression2;
Example: int a = 5, b = 10;
int max = (a > b) ? a : b;
b) Explicit Type Conversion (Type Casting): Manually converting one data type into another using
casting operator.
Syntax: (type) expression;
Example: int a = 5, b = 2;
float result = (float)a / b; // Output: 2.5
3. Mathematical Functions:
C provides a variety of mathematical functions through the header file: #include <math.h>
Common Math Functions in C:
Function Description Example
sqrt(x) Square root of x sqrt(25) → 5.0
pow(x, y) x raised to power y pow(2, 3) → 8.0
fabs(x) Absolute value of float x fabs(-5.5) → 5.5
floor(x) Largest int ≤ x floor(5.9) → 5.0
ceil(x) Smallest int ≥ x ceil(4.1) → 5.0
log(x) Natural log of x log(2.71828) → 1
exp(x) e raised to x exp(1) → 2.71828
sin(x) Sine of x (in radians) sin(0) → 0
cos(x) Cosine of x cos(0) → 1
tan(x) Tangent of x tan(0) → 0
Note: Most functions return double values.
Example Program Using Math Functions
#include <stdio.h>
#include <math.h>
int main() {
double num = 9.0;
printf("Square root: %.2f\n", sqrt(num));
printf("Power: %.2f\n", pow(2, 3));
printf("Ceil: %.2f\n", ceil(3.4));
printf("Floor: %.2f\n", floor(3.4));
return 0;
}
B.C.A: Programming Concept using C 16
UNIT-II
UNIT – II: Decision Making and Branching: Introduction, Decision Making with IF Statement, Simple If
Statement, The If....Else Statement, Nested of If....Else Statements, The else if Ladder, The Switch
Statement, The ? : Operator, The Goto Statement.
Decision Making and Looping: Introduction, The While statement, the do Statement, the For
Statement, Jumps in Loops.
Arrays: Introduction, One-dimensional Arrays, Declaration and Initialization of One dimensional Arrays,
Two-dimensional Arrays, Initializing two-dimensional Arrays, Multi-dimensional Arrays, Dynamic
Arrays.
*****
1. Decision Making and Branching:
Simple if Statement: Executes a block of code only if a specified condition is true.
Syntax: if (condition) {
// statements to execute if condition is true
}
Example: #include <stdio.h>
int main() {
int number = 10;
if (number > 0) {
printf("The number is positive.\n");
}
return 0;
}
2. if...else Statement: Executes one block if condition is true, and another block if it is false.
Syntax: if (condition) {
// true block
} else {
// false block
}
Example: #include <stdio.h>
int main() {
int number = -5;
if (number >= 0) {
printf("The number is non-negative.\n");
} else {
printf("The number is negative.\n");
}
return 0;
}
4. else if Ladder: Allows checking multiple conditions in sequence. First true condition’s block is
executed.
Syntax: if (condition1)
{ // block1
} else if (condition2) {
// block2
} else if (condition3) {
// block3
} else {
// default block
}
Example:#include <stdio.h>
int main() {
int marks = 75;
if (marks >= 90) {
printf("Grade A\n");
} else if (marks >= 75) {
printf("Grade B\n");
} else if (marks >= 60) {
printf("Grade C\n");
} else {
printf("Fail\n");
}
return 0;
}
B.C.A: Programming Concept using C 18
The switch Statement: Used to perform different actions based on the value of a single variable or
expression. It's a good alternative to the else if ladder when there are many possible discrete values.
Syntax: switch (expression) {
case constant1:
// statements
break;
case constant2:
// statements
break;
...
default:
// statements
}
Example: #include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Note:
break is used to exit the switch block.
If break is omitted, execution continues to the next case (called fall-through).
2. The While statement, the do Statement, the For Statement, Jumps in Loops
1. The while Statement: Executes a block as long as the condition is true. The condition is checked
before executing the loop body.
Syntax: while (condition) {
// loop body
}
Example: #include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
return 0;
}
Output:
12345
2. The do...while Statement: Similar to a while, but executes at least once because the condition is
checked after the loop body.
Syntax: do {
// loop body
} while (condition);
Example:#include <stdio.h>
int main() {
int i = 1;
do {
B.C.A: Programming Concept using C 20
printf("%d ", i);
i++;
} while (i <= 5);
return 0;
}
Output:
12345
if (arr == NULL) {
printf("Memory not allocated.\n");
return 1;
}
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("You entered:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
*****