Adobe Scan Feb 16, 2025
Adobe Scan Feb 16, 2025
Formatted VO functions are used to take various inputs from the user and display multiple
outputs to the user. These types of VO functions can help to display the output to the user in
different formats using the format specifiers. These I/O supports all datatypes like int, float, char,
and many more.
These functions are called formatted /O functions because we can use format specifiers in these
functions and hence, we can format these functions according to our needs.
Ex: 15.57 123 John
The line contains three pieces of data, arranged in a particular form. Such data has to be read
conforming to the format of its appearance. For example, the first part of the data should be read
into a variable float. the second into int, and the third part into char. This is possible in C using
the scanf function.
In C, formatted input/output statements are used lo display data to the screen (printf) or accept
input from the user (scanf). These functions are part of the standard inpuvoutput library, so you
need to inciude <stdio.h> at the beginning of your program.
Syntax:
Example:
BCA 6
MODULE-03
int main()
float pi = 3.14159:
return 0:
Output:
Integer: 10
Float: 3.14
Character: A
String: John
(BCA103)
Difference between While and do while
while
do-whie
Example:
#include<stdio.h>
BCA 31
If statement:
if(test expression)
it allows the computer to evaluate the expression first and then depending on whether
the value of the expression is true or false, it transfers the control to a particular
statement.
Syntax:
if(expression)
llcode to be executed
False
Condition
True
r Condition
username code
BCA 10
MODULE-03
void main()
printf("cnter a numberin");
scanf("%d".&num);
rem=num9%2:
if(rem=-0)
printf(od is even"num);
if(rem!)
printf("%d is odd".num):
getch():
3.Do while loop
at the top of the loop. the do...while
Unlike for and while loops, which test the loop condition
at the bottom of the loop.
checks its condition
loop in C programming language
except that a do...while loop is guaranteed to
A do...while loop is similar to a while loop,
execute at least one time.
Syntax:
do
statement(s):
while(condition );
Flowchart:
Check/Test False
Condition
True
" Notice that the conditional expression appears at the end of the loop, so the
statement(s) in the loop execute once before the condition is tested.
BCA 29
MODULE-03
void main()
intn,i,sum:
printf("enter a number";
scanf("%d"&n);
i=1;
sum-0;
do
sum=sum+i;
STRUCTURE UNION
Keyword The keyword structis usedto define astrucure The keyword union is usedto define a union.
Size When a vanable is associated with a stucure, the when avartable is assocdated with a union, the compiler
Compiler allocates the memoryfor each member. The allocates the memory by considetngthe size of the
size of strucure is greater than or equal to the sum of largestmemory. So, size of union is equal to the size
sizes of its members. oflargest member.
Memory Each member within astucure is assigned unique Memory aliocated is shared by individual members of
storage area of location. union
Valve Alering the value of a member will not atfed other Atlering the value of any of the member will ater other
Alering members of the structure. member values.
Accessing Individual member can be accessed ata time. Only one member can be accessed at atime.
members
nitialization Several members of a structure can intialize at once Onty the first member of aunion can be inibalized
af Members
POINTER
A pointer is a variable that store memory address or that contains address of another
variable where addresses are the location number always contains whole number. So, pointer
contain always the whole number. It is called pointer because it points to a particular location
in memory by storing address of that location.
Syntax
Data type *pointer name;
Here * before pointer indicate the compiler that variable declared as a pointe.
e-g.
Syntax:
while(condition)
Module -03.pdf
stalement(s):
Flowchart
Whilee oop
False
Condition?
True
The condition may be any expression, und true is any nonzero value. DThe loop
iterates while the condition is true.
When the condition becomes false. program control passes to the line immediately
following the loop.
Here, key point of the while loop is that the loop might not ever run.
BCA 25
MODULE-03
void main()
while (i<cn)
sum = sum 4:
2.For loop
A for loopis a repetition control structure that allows you to efficiently write a loop that needs
to execute a specific number of times.
Like the while and do-while loops, the for loop provides a mechanism to repeat a task until
a particular condition is true. For loop is usually known as a determinate or definite loop
because the programmer knows exactly how many times the loop will repeat. The number
BCA 26
MODULE-03
of times the loop has to be executed can be determined mathematically by checking the logic
of the loop.
Syntax:
for (initialization; test expression; action-after-each-iteration/updation)
Statement(s):
Flowchart
START
initialisation
Check/Test False
Condition
True
Block of Statements
Updation
Stop
Flowcharts
b. Input and output indicators: Parallelograms are used to represent input and output
operations. Statements like INPUT, READ and PRINT are represented in these
Parallelograms.
BCA Page 6
Module-1
PROBLEM SOLVING USING C PROGRAMMIMG
c. Process Indicators: Rectangle is used to indicate any set of processing operation such
as for storing arithmetic operations.
d. Decision Makers: The diamond is used for indicating the step of decision making and
there fore known as decision box. Decision boxes are used to test the eonditions
ask
questions and depending upon the answers, the appropriate actions are taken by the
computer.
6 of 13
Slow lines indicate the d°ection oesfollowed in the fkowchart. n a Flowchart, every
line must have an arrow on it to dicate tin The arrows muy be in any
direction.
. On- Page connectors: Circles are used to join the different parts of a flowchart and these
circles are called on-page connectors. The uses of these connectors give a neat shape to
the flowcharts. Ina complicated problems, a flowchart may run in to several pages. The
parts of the flowchart on difíerent pages are to be joined with each other. The parts to be
2. Program to read two numbers and find the largest (demonstration on if
else).
#include<stdio.h>
Void main()
clrscr():
printf("Enter the First number:");
scanf("%d", &nl);
printf("Enter the Second number:");
scanf("%d", &n2);
if (nl > n2)
Else
getch():
Output
int main()
BCA
MODULE-04
print•("%d\"anrfi):
return O
...
OUTPUT:
45
59
98
21
while initializing a single dimensional aray, it is optional to specify the size of array.
If the size is omitted during initialization then the compiler assumes the size of array equal
to the number of initializers.
For example:
int marks || =(99,78,50,45,67,89):
If during the initialization of the numter 4 of 35sis less then size of array, then all the
remaining elements of array are assign HezEI
BCA
MODULE-04
MODULE-04
PROBLEM SOLVING USING CPROGRAMMING (BCA103)
Two dimensional arrays
Two-dimensional array is known as matrix. The array declaration in both the array i.e.in
single dimensional array single subscript is used and in two-dimensional aray two
subscripts are used.
Or we can say 2-d aray is a collection of 1-D array placed one below the other.
Total no. of elements in 2-D array is calculated as rowcolumn
For example: -
2027
83 15
10 11 12
Position
a (0|[0] a [0|[0] a [0|[0) a [00] a (0|[0] a[0|[0)
Elements 20 7 8 3 15
Memory allocations 2000 2002 2004 2006 2008 2010
For processing 2-d aray,. we use two nested for loops. The outer for loop corresponds to the
row and the inner for loop corresponds to the column.
For example:
int a (4||5]:
BCA
MODULE-04
PROBLEM SOLVING USINGC PROGRAMMING (BCA103)
Tor reading vaue:
fori-0;i<4:i++)
forj-0:j<5j++)
scanf("%d",&a[iljl): 6 of 35
fori-0-i
1. Print the value ofy for given x=2 & z=4 and analyze the output.
a. y=X++ + ++X,
b. y= ++x + +x;
C. y= ++x + +X+++x:
d. y=O; Cprogram.pdf
e. y= »z? x:z;
.y= x&z
#include<stdio.h>
#include <conio.h>
void main()
int x=2:
int z=4;
int y:
clrscr():
y=X++ + ++X;
printf("a.y=%d, x=9%d\n".y.x);
y=++x + ++X;
printf("b.y=%d, x=%d\n"y,x);
y=++ x + ++x + ++x;
printf("c.y=%d, x=%dn".y.x):
y=x>z:
printf("d.y=od\n"y):
y=x»z?x:z:
printf("e.y-%d\n".y):
y=x&z:
printf("£y-odn" y):
getch():
Output
a.y-6,x=4
b.y=12,x=6 1of 3
c.y=27,x=9
d.y=l
e.y=9
f.y-0
5. Write aprogram to print sums of even numbers and sum of odd
numbers from array of positive integers (demonstration of ld array).
#include<stdio.h>
#include<conio.h>
void main()
int n, i, a[10],es=0,os=0;
clrscr():
printf("Enter the Size of an Aray :\n");
scanf("% d", &n);
printf("Enter the Aray Elements\n"):
for(i=0; i<n; i++)
scanf("%d", &a[il);
for(i=0; i<n; i++)
if(a<i] % 2 ==0)
es = es + alu):
else
OS = Os + ali;
Output:
Enter the Size of an Array :
Chas a built-in multiway decision stalement known as a switch. The switch statement tests the
value of a given variable (or expression) against a list of case value s& when a match is found,
a block of statements announced with that case is executed.
Syntax:
switch(expresion)
case value-1:
block-1:
break:
case value-2:
block-2:
break:
default: default-block:
break;
BCA 18
MODULE-03
Statement-x:
The switch case statement compures the value of the variable given in the switch statement with
the value of each case statement that follows. When the value of the switeh and the case
statement matches, the statement block of that purticular case is executed. Default is also a case
that is executed when the value of the variable does not match with any of the values of the case
stalement, i.e., the default case is executed when no match is found between the values of switch
und case statements and thus there are no statenents to be executed. Although the default case
is optional, it is always recommended to include it as it handles any unexpected cases. In the
syntax of the switch case statement, we have used another keyword break. The break statement
must be used at the end of each case because if it is not used, then the case that matched and
the following cases will be executed.
Flowchart:
Switeh
True itstrsest
Beak
Faise
Trase
False
True
False
True
True taterrent
faute Rreakt
Arithmetic operations on characters
Callows us to manipulate characters the same way we do with numbers. Whenevera
character constant or character variable is used in an expression, it is automatically converted
into an integer value by the system. The integer value depends on the local character set of
the system.
int main()
printf("%dn", chl):
BCA 20
MODULE-04
Output
-121
chl now becomes 135. However, since char can typically hold values only within the range
-128 to 127, integer overflow occurs
The user defined functions defined by the user according to its requirement. Built-in function
can't be modificd, it can only read and can be used.
Syntax: -
Return type name of function (type I arg 1, type2 arg2. type3 arg3)
The return_type can be any valid C data type, and the function _name can be any valid
identifier. The parameter list specifies the arguments that the function takes as input, and the
body of the function contains the statements that are executed when the function is called.
Example program:
#include <stdio.h>
int sum=a+ b:
return sum:
Structures within structures
A structure within a structure is known as nested structure in C. When one of the elements in
the definition of a struct type is of another stuct type, then we call it a nested structure in C.
Nested structures are defined when one of the elements of a struct type is itself a composite
representation of one or more types.
When a structure is within another structure, it is called Nested structure. A structure variable
can be a member of another structure and it is represented as
Syntax:
struct structl
type varl:
type var2:
struct struct2 strvar,
struct student
element 1;
element 2:
struct student1
member 1:
BCA 10
MODULE-05
]variable l:
element n;
Jvariable 2:
It is possible to define structure outside & declare its variable inside other structure.
Example program:
#include <stdio.h>
#include <string.h>
struct employee
int d, m, y:
dl:
}:
int main()