0% found this document useful (0 votes)
7 views16 pages

Adobe Scan Feb 16, 2025

Report SCR

Uploaded by

gaganrai2005.05
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)
7 views16 pages

Adobe Scan Feb 16, 2025

Report SCR

Uploaded by

gaganrai2005.05
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/ 16

Formatted I/0 Functions

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.

Formatted Output: printf


The printf function is used to display text and variables. It uses format specifiers to determine
how the output should be formatted.

Syntax:

printf("control string", argl, arg2., ..,. argn):


or

printf("format string". variables):

Example:

BCA 6

MODULE-03

PROBLEM SOLVING USINGC PROGRAMMING (BCA103)


tinclude <stdio.h>

int main()

int num =10:

float pi = 3.14159:

char letter ='A':

char namel] = "John":

printf("Integer: %dn", num):

printf("Float: %.2f\n". pi): / Print float with 2 decimal places


printf("Character: %cin", letter):

printf("String: %sn", name):

return 0:

Output:
Integer: 10
Float: 3.14

Character: A

String: John
(BCA103)
Difference between While and do while
while
do-whie

Condition is checked first then


is executed. stalement(s) Sutement(s) is
executed
thereafter condition is checked.atleast once,
It might occur statement(s) is executed zero
times, If condition is false. At least once the
statement(s) is executed.
No
semicolon at the end of while.
while(condition) Semicolon at the end of while.
while(condition):
Variable in condition is initialized before the
execution of loop. variable may be initialized before or within
the loop.

while loop is entry controlled loop.


do-while loop is exit controlled loop.

while(condition) (statement(s): 1 do statement(s): while(condition):

BREAK AND CONTINUE STATEMENTS


Break statement In C. the break statement is used
to terminate the execution of the ncarest
enclosing loop in which it appears. When the compiler encounters a break statement, the
control passes to the statement that follows the loop in which the break statement appears. Its
syntax is quite simple, just type keyword break followed with a
semicolon.
In switch statement if the break statement is missing then every case from the matched
case
label till the end of the switch, including the default, is executed.

Example:
#include<stdio.h>

BCA 31
If statement:

The if statement is used to control the flow of execution of statements. It is basically a


two-way decision statement and is used in conjuction with an expression.

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

The 'statement-block' may be a single statement or a group of statements. If the test


expression is true, the statement-block will be executed; otherwise, the statement-block
will be skipped and the execution will jump to the statement-x.
Flowchart of if statements

False
Condition

True

r Condition

username code

BCA 10

MODULE-03

PROBLEM SOLVING USING CPROGRAMMING (BCA103)

Example program: Check Whether the Number is Even or Odd.


#include<stdio.h>

void main()

int num, rem:

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:

Do while Loop Start

Execute Loop Body

Check/Test False
Condition

True

Do white Loop End

" 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

PROBLEM SOLVING USING CPROGRAMMING (BCA103)


tne conaton is tue, ueTOWu conuol Junps DuCKup to uo, anu uie staementst
in the loop execute again. This process repeats until the given condition becomes
false.

Example: sum of n numbers using do while loop.


#include<stdio.h>

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.

int *pl; //pointer to integer type


float *p2: l/pointer to float type
char *p3; /ipointer to character type
1.While loop

A while loop statement in C programming language repeatedly executes a target statement


as long as a given condition is true.

Syntax:
while(condition)
Module -03.pdf
stalement(s):

Flowchart

Whilee oop

False
Condition?

True

Execute loop body

while loop ends)

Here, statement(s) may be a single statement or u block of statements.

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

PROBLEM SOLVING USING CPROGRAMMING (BCA103)

be skipped and the first


When the condition is tested and the result is false, the loop body will
statement after the while loop will be executed.

Example: Program to find the sum ofn natural numbers.


#include <stdio.h>

void main()

int n, sum =0,i= 1:

printf("Enter the value of n: "):


25 of 35

Scanf( "%d", &n):

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

PROBLEM SOLVING USING CPROGRAMMING (BCA103)

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

Aflowchart is a pictorial representation of an algorithm. Ii specifies the solution procedure.


the relevant computations, points of decisions, and other infornation required for the solution.
Flowcharts are created by using special geometric symbols. Each symbol represents an
operation.

A flowchart is a graphical or symbolic representation of a process. It is basically used to


design and docume virtually complex processes to help the viewers to visualize the logic of
the process, so that they can gain a better understanding of the process and find flaws,
bottlenecks and other less obvious features within it.

The symbols are joined by arrows to obtain a compkete solution.


Advantages of flowcharts
" Fowcharts form a good visual aid to represent the logic for the problem solution.
" It is a form of program documentation.
" With the help of the flowchart. program could be coded efficiently.
" It facilitates orderly debugging and testing of programs.
" It provides efficient program maintenance in that the programmer can concentrate only on
that part which is to be modified.
Rules for writing Nowcharts
" Flowcharts are drawn from top to bottom or left to right.
" A flowchart always begins with START symbol and ends with STOP symbol.
Flow lines ure used to join the symbols.
There should be at least one STOP symbol in any flow chart

Symbols used in Flow-Charts


The symbols that we make use while drawing flowcharts as given below are as per
conventions folbwed by International Standard Organization (ISO).
a. Oval: Rectangle with rounded sides is used to indicate either START/ STOP of the
program.

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()

int nl, n2;

clrscr():
printf("Enter the First number:");
scanf("%d", &nl);
printf("Enter the Second number:");
scanf("%d", &n2);
if (nl > n2)

printf("The largest number is: %dn", nl);

Else

printf("The largest number is: %d\n", n2);

getch():

Output

Enter the first number: 64

Enter the second number: 55

The largest number is: 64


1.One dimensional array:
An array with only one subscript is tened one-dimensional aray or 1-D aray. It is
used to store a list of values, all of which share a common name (-D array name) and are
distinguishable by subscript value.
Declaration of onedimensional array:
data-tyepe variable-name(size]:
example: int a<5]:
Wrile a program to input valves into an array and display them/
#include<stdio.h>

int main()

BCA

MODULE-04

PROBLEM SOLVING USING CPROGRAMMING (BCA103)


int arr|5).3
fori-0;i<S;j++)

printf('enter a valucs of array \n"i):


scanf("%dn"&ar[il):

printf(he arruy elements are: \n");


for (i-0;i<5;i++)

print•("%d\"anrfi):

return O

...
OUTPUT:

45

59

98

21

The array elements are 12 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.

Its syntax is: Data-type arruy name[row][column]:

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

Example: -int a(2|31:

Total no of elements=row*colunn is 2+3 =6

It means the matrix COnsist of 2 rows and 3 columns

For example: -

2027

83 15

Positions of 2-D aTay elements in an aray are as below


00 0102

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

Accessing 2-d array /processing 2-d arrays

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

For displaying value: -


for(i-0;i<4;i++)

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;

printf(" The Sum of Even Numbers is = %d", es);


printf("The Sum of Odd Numbers is = %d ",os);
getch);

Output:
Enter the Size of an Array :

Enter the Array Elements


6. The switch statement: -

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.

The general form is :

Syntax:

switch(expresion)

case value-1:

block-1:

break:

case value-2:

block-2:
break:

default: default-block:
break;

BCA 18

MODULE-03

PROBLEM SOLVING USING CPROGRAMMING (BCA103)

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.

Character arithmetic is used to implement arithmetic operations like addition, subtraction,


multiplication, and division on characters in C language.
In character arithmetic character converts into an integer value to perform the task. For this
ASCIl alue is used.
It is used to perform actions on the strings.
C program to demonstrate character arithmetic.
#include <stdio.h>

int main()

char chl - 125, ch2- 10;

chl = chl + ch2:

printf("%dn", chl):

BCA 20

MODULE-04

PROBLEM SOLVING USING C PROGRAMMING (BCA103)

printf("%cn", chl -ch2- 4):


return 0;

Output
-121

chl = 125 + 10;

chl now becomes 135. However, since char can typically hold values only within the range
-128 to 127, integer overflow occurs

135 - 256 = -121 (wrapping around using modular arithmetic).


Internally. this value is treated as -121, but when using %d for printing. it interprets it as
121 (as it maps to the unsigned equivalent)

User defined functions:

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 add(int a, int b)

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

PROBLEM SOLVING USING CPROGRAMMING (BCA103)


member 2:

]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

char name (10]:


float salary:
struct dob

int d, m, y:
dl:

}:
int main()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy