Unit 2 c Language

Download as pdf or txt
Download as pdf or txt
You are on page 1of 89

Royal Education Society's

COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

UNIT II

Introduction to Programming Languages

A programming language is a set of symbols, grammars and rules with the help of which
one is able to translate algorithms to programs that will be executed by the computer. The
programmer communicates with a machine using programming languages. Most of the
programs have a highly structured set of rules. The primary classifications of programming
languages are: Machine Languages. Assembly Languages. High level Languages.

What is Language?

Language is a mode of communication that is used to share ideas, opinions with each
other. For example, if we want to teach someone, we need a language that is
understandable by both communicators.

What is a Programming Language?

A programming language is a computer language that is used by programmers


(developers) to communicate with computers. It is a set of instructions written in any
specific language ( C, C++, Java, Python) to perform a specific task.

A programming language is mainly used to develop desktop applications, websites, and


mobile applications.

What is the need for programming languages?

 Several software packages are made using programming languages, together with:
 Operating structures
 Web browsers
 Mobile apps
 Desktop packages
 Video games
 General Software program
 Business-related software programs
 Embedded structures

Machine Language

Machine language is a collection of binary digits or bits that the computer reads and
interprets. Machine language is the only language a computer is capable of understanding.
Machine level language is a language that supports the machine side of the programming
or does not provide human side of the programming. It consists of (binary) zeros and ones.
Each instruction in a program is represented by a numeric code, and numerical addresses

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

are used throughout the program to refer to memory locations in the computer’s memory.
Microcode allows for the expression of some of the more powerful machine level
instructions in terms of a set of basic machine instructions.

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. But the computer cannot understand mnemonics, so we use a
translator called Assembler to translate mnemonics into machine language.

Assembler is a translator which takes assembly code as input and produces machine code
as output. That means, the computer cannot understand 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. Today,
assembly language is still used for systems programming, device driver development, and
other low-level programming tasks.

Example: %macro ADD_TWO_NUMBERS

mov al, 7; //al=7

mov bl, 3; //bl=3

add al, bl; //al = al+bl

sub al, bl //al = al-bl

%endmacro

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 cannot
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. All these programming languages use human-understandable language like

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

English to write program instructions. These instructions are converted to low-level


language by the compiler or interpreter so that it can be understood by the computer.

Compilation

The compiler program translates the instructions of a high level language to a machine
level language. A separate compiler is required for every high level language. High level
language is simply a programmer’s convenience and cannot be executed in their source.
The actual high - level program is called a source program. It is compiled (translated) to
machine level language program called object program for that machine by the compiler.
Such compilers are called self-resident compilers. Compiler compiles the full program and
reports the errors at the end

Compilation Process

The compilation and execution process of C can be divided in to multiple steps:

 Preprocessing Using a Preprocessor program to convert C source code in expanded


source code. "#include" and "#define" statements will be processed and replaced actually
source codes in this step.

 Compilation Using a Compiler program to convert C expanded source to assembly source


code.

 Assembly Using a Assembler program to convert assembly source code to object code.

 Linking Using a Linker program to convert object code to executable code. Multiple units
of object codes are linked to together in this step.

 Loading Using a Loader program to load the executable code into CPU for execution.
Compilation

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Linking

 After all of the files are compiled, they must be "merged together" to produce a single
executable file that the user use to run the program.

 In C, most compiled programs produce results only with the help of some standard
programs, known as library files that reside in the computer. This process is called
linking.

 The result obtained after linking is called the executable file.

 The linker′s primary function is to bind symbolic names to memory addresses.

 To do this, it first scans the files and concatenates the related file sections to form one
large file. Then, it makes a second pass on the resulting file to bind symbol names to real
memory addresses.

 Loading is loading the executable into memory prior to execution.

Loading

 After the files are compiled and linked the executable file is loaded in the computer′s
memory for executing by the loader. This process is called Loading.

 Program loading is basically copying a program from secondary storage into main
memory so it ′s ready to run.

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

 In some cases, loading us just not copying the data from disk to memory, but also
setting protection bits, or arranging for virtual memory map virtual addresses to disk
pages.

First Generation (1940s - 1950s)

The first generation of programming languages is machine language, the most basic form
of programming. Machine language consists of binary code - ones and zeros - that is
directly executed by a computer's CPU. It is highly specific to the architecture of the
computer hardware.

 Characteristics: Purely binary, difficult to read and write for humans.


 Examples: No named examples, as it varies by hardware.
 Impact: Laid the groundwork for computer programming, but was cumbersome
and error-prone.

Second Generation (1950s - 1960s)


Assembly language is a step above machine language, providing a slight abstraction. It
uses mnemonic codes or symbols to represent machine language instructions, making it
slightly easier for humans to understand and write.

 Characteristics: Still hardware-specific, but more readable than binary code.


 Examples: NASM, MASM.
 Impact: Made programming more accessible, yet still required deep hardware
knowledge.

Third Generation (1960s - 1970s)


The third generation introduced high-level programming languages, which are closer to
human languages and abstract away much of the hardware-specific details.

 Characteristics: Syntax resembles human languages, with powerful constructs that


allow for complex operations with simple statements.
 Examples: C, Fortran, COBOL, Pascal, BASIC.
 Impact: Significantly increased productivity and accessibility in programming.
Enabled the development of complex software systems.

Fourth Generation Languages (1980s - 1990s)


Fourth-generation languages (4GLs) are even more abstracted and aim at higher
productivity and simplicity in solving specific problems. They are often used in database
querying, report generation, and data manipulation.

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

 Characteristics: Focus on reducing programming effort and closer to natural


language.
 Examples: SQL, MATLAB, SAS, Visual Basic.
 Impact: Enhanced productivity in specific domains, such as data analysis and
database management.

Fifth Generation: (1980s - Present)

Fifth-generation languages focus on problem-solving using constraints rather than


algorithms. They are used in developing artificial intelligence, expert systems, and natural
language understanding.

 Characteristics: Use of logic and declarative programming paradigms.


 Examples: Prolog, Lisp, Mercury.
 Impact: Pushed forward the development of AI and complex problem-solving
systems.

History of C Language

C is a programming language developed at AT & T’s Bell Laborites of USA in 1970. It


was designed and written by a man named Dennis Ritchie. In the late seventies C began to
replace the more familiar languages of that time like PL/I, ALGOL etc. No one pushed C.
it wasn’t made the official Bell Labs language. Thus, without any advertisement C’s
reputation spread and its pool of users grew. Ritchie seems to have been rather surprised
that so many programmers preferred C to older languages like FORTRAN or PL/I or the
newer ones like Pascal and APL.
Possibly why C seems so popular is because it is reliable, simple and easy to use. Out of
the dozens of languages available, the prize of purity is often given to PASCAL.

C is a general-purpose language which has been closely associated with the UNIX
operating system for which it was developed - since the system and most of the programs
that run it are written in C. COBOL was being used for commercial applications,
FORTRAN for Engineering and Scientific Applications and so on. At this stage people
started thinking that instead of learning and using so many languages, each for a different
purpose, why not use only one language. Which can program all possible applications.
Therefore an international committee came out with a language called ALGOL 60.
However, ALGOL 60 never really became popular because it seemed too abstract , too
general. To reduce this abstractness and generality, a new language called combined
Programming Language (CPL) was developed at Cambridge University. CPL was an
attempt to bring ALGOL 60 down to earth. However, CPL turned out to be so big, having
so many features, that it was hard to learn and difficult to implement.

Basic Combined Programming Language (BCPL), developed by Martin Richards at


Cambridge University aimed to solve this problem by brining CPL down to its basic good
features. But unfortunately it turned out to too less powerful and too specific. Around same

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

time a language called B was written by Ken Thompson at AT & T’s Bell Labs, as a further
simplification of CPL. But like BCPL, B too and BCPL, added some of his own and
developed C. Ritchie’s main achievement is the restoration of the lost generality in BCPL
and B, and still keeping it powerful.

Structure of C Programming

1. Documentation Section
2. Header Section [link section]
3. Definition Section
4. Global Declaration
5. Main Section
6. Sub Program Section

Documentation Section
We can write title of program in that section using comments line there are
two types of comment line

Single line comment:-it is starting with // [double slash] this type of comment line are used
for single line comment.
Multi line Comment /* */:-This type of comment line is used to give
additional information about the program in two or more then two lines this type of
comment line is started with /* & end with ‘*/’ there is no effect on the program by using
comment line.

Header Section[link section]


In this section we can use the Header file such as # ‘stdio.h’ & ‘conio.h’ , string.h, such as
Header file are used to give c linking support to the functions used in c program.

Stdio.h stands for slandered I/O file. Which support to standard I/O function as printf() &
scanf().

Similarly conio.h is stand for console I/O hidder file which support to console I/O functions
such as clrscr(); & getch().

Definition Section
In definition section we define constant value by using #defines ex:-#define pie 3.14 In
above example to define constant value 3.142 variable.

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Global Declaration Section


There are some variables that are used in more than one function such variables are called
global variable they are declared in the global declaration section that in outside of all
function.

Main() Section
Every C program have only one main() function is start with ({ ) & close with ( } ) main
function have main two part i.e declaration part & execution part.
Declaration part:-
In this part we declare the variables before using program. In declaration part we declare
variables with their particular data types such as int, float, char etc.
Execution Part:-
In this part of main() section we use execution statements in main() section every statement
& function are terminated by (;).
Sub program section:-
In sub program section we can use user defined function. User defined function are function
which are defined by user.

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Simple C Program

/* This is my first ‘c’ program */

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Wel come C programming”);
getch();
}

//Write a program two addition of two numbers


#include<stdio.h>
#include<conio.h>
Void main()
{
Clrscr()
Printf(“The addition of two number is %d”,20+30);
Getch();
}

//Write A Program to display the name of college.

Algorithm:-

1.Start
2.Print statement “cocsit”
3.stop

Flowchart:-

START

PRINT
STATEMENT
“cocsit”

STOP

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Program:-

/* Write A Program to display the name of college.*/

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“*****out/put****”);
printf("Hello cocsit");

getch();
}

*****out/put****
Hello cocsit

The Character set


Every language has it’s own character set ex;- In English language there are 26
alphabets are used to create word, sentence, paragraph.
Similarly ‘C’ language has it’s own character sets. The character set of ‘C’ language is
nothing but combination of alphabets, digits & special symbols ‘C’ inserts the uppercase
letter A—Z lower case latter a---z the digits 0----9 & special character. The alphabets &
digits together are called as alphanumeric character.
These characters are used as building blocks from basic program element.
Following table shows the valid alphabets, digits & special symbol allowed in ‘C’
Alphabets
Uppercase -
A B C --------------------- Z.
Lowercase -
a b C --------- Z
Digits:-
0 2 3 4 5 6 7 8 9

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Special Characters
, Comma

; Semi column

: Column

? Question Mark

“ Double quotation

| Vertical Bar

\ Back Slash

/ Slash

~ Tile

_ Under score

$ Dollar sign

# Hash

% Percentage

& Ampere Sign

* Astric

- Minus

+ Plus

< Less sign/ opening angle

> Grater sign/ closing angle

( Left parentheses

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

) Right parentheses

[ Left Bracket

] Right Bracket

{ Left Brace

} Right Brace

White spacious
The ‘C’ compiler ignores white space unless they are a part of string constant white
space may be used to separate words, white space include blank space, horizontal tab.

Horizontal tab - /t
New line - /n
By using this character set we can create program, function name, variable name.
‘C’ uses certain combination of characters such as, /b,/n,/t. to represent special condition
such as back space, new line & horizontal tab these characters combination are known as
escape sequence.

IDENTIFIERS AND KEYWORDS


Identifers
 Identifiers are names that are given to various program elements such as variables,
function & array name. there are certain rules we should be follow while running
& identifiers are
 Identifier consists of letters & digits in any order except the first character should
be letter
 Both uppercase & lowercase letters are permitted although lowercase letter are
commonly used.
 Uppercase & lowercase letters are not interchangeable [an uppercase letters is not
equivalent to corresponding lowercase letter
 The underscore [_] character can be included & it is considered as a letter.
 An identifiers name up to 8 characters many compilers allow an identifiers name
up to 31 characters.
 Following names are valid identifiers x, xyz, sum, nau, basic_sal.
First character must be latters.
The following names are valid identifiers

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

X y12 sum_1 _tempera


Names area text_rate TABLE
The following names are not valid identifiers
7h The first character must be a letter
X” Illegal characters (“).
Rder-no Illegal charaters (-).
Rror flag Illegal characters (blank space).

C Keywords
Keywords are the words whose meaning has already been explained to the C compiler. The
keywords cannot be used as variable names because if we do so we are trying to assign a
new meaning to the keyword, which is not allowed by the computer.

Auto Break Case Char Const Continue


Default do Double Else Enum extern
Float far For Goto If Int
Long near Register Return Short Signed
Static struct Switch Typedef Union Unsigned
Void while

There are 32 keywords used in C all keywords must be written in lower case.

Some compilers may also include some or all of the following keywords.

Ada Far Near Asm Fortran Pascal


Entry Huge

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Variables

A variables is data name that may be used to store the data value these variables name are
giving to location in the memory of computer where we are storing the data there are some
rules available for creation and deceleration of variables.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“Enter the number”);
scanf(“%d”,&a,&b);
c=a+b
printf(“The addition is %d“,c);
getch();
}

Types of variables
In C, a quantity which may change during program execution is called a variable. Variable
names are names given to locations in the memory of computer where different constants
are stored. This location can contains integer, real or character constants. This is because a
constant stored in a location with a particular type of variable name can hold only that type
of constant. Ex:- a constant stored in a memory location with an integer variable name must
be an integer constant. One stored in location with a real variable name must be a real
constant and the one stored in location with a character variable name must be a character
constant.

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Rules for Constructing Variable Name.


a) A variable name is any combination of 1 to 8 alphabets, digits or
underscores. Some compilers allow variable names whose length could
be up to 40 characters.
b) The first character in the variable name must be an alphabet.
c) No commas or blanks are allowed within a variable name.
d) No special symbol other than an underscore (as in gross_sal) can be used
in a variable name.
Ex:- int I,net_sal;

Float a;
Char code;-

Data type
C language is rich in data type like other languages ‘C’ language has also it’s own
data
types data types are used to define or declare the type [data type] of particular variable,
constant, function etc.
In C language there are three classes of data types.
1) Primary data type.
2) Derived data type.
3) User-defined data types.

Data type are always written in small letters each data types has it’s own feature depending
upon a condition the particular data types is used in the name of data type we cannot use
in valid space.

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Data Type Range Size in Bite Size in Format


Byte String
Char -128 to 127 8 1 %c
Unsigned char 0 to 255 8 1 %c
Int -32768 to 32767 16 2 %i or %d
Unsigned int 0 to 65535 16 2 %u
long int -2147483648 to 32 4 %ld
2147483647
Unsigned long int 0 to 4294967295 32 4 %lu
Float 3.4e-38 to 3.4e+38 32 4 %f or %g
Double 1.7e-308 to 64 8 %lf
1.7e+308
Long double 3.4e-4932 to 1.1 80 10 %lf
e+4932

C Data Types

User Defined datatype Derived type

- structure - array
- union Built in type - function
- pointer

Integral type void Floating point

int char float double

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

1) Primary Data Type:- it is also known as standard of or built in data type all
compiler support fundamental data type. Primary data type is divided into 3 type.

Int [integer]:-
Integer are defined by keyword ‘int’ integer is used to store numeric value we cannot
store decimal point no in that data type integer occupies one word of storage i.e 2 bytes
or 16 bits. The range of integer data type is -32768 to 32767 ‘C’ has support to classes
of integer storage namely int shor int & long int variable than they increases the range
of value as will as size of it. 32 bit word length can store an integer ranging from -
2,147,483,648to 2147483647.
Ex:- Int a=5; int b=6;

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
a=10;
b=20;
c=a+b;
printf(“\n A addition of two number is %d”,c);
getch();
}
Float
The real no are known as floating value. These no contain functions part. This is defined
by keyword ‘float’ float value requires 4 bytes or 32 beats. The range of float is 3.4e-38 to
3.4e38.
Ex:- float pie=3.14;

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

#include<stdio.h>
#include<conio.h>
{
float a,b,c;
clrscr();
a=3.2;
b=1..5;
c=a+b;
printf(“\n Addition of two number %f”,c);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int sub1=40,sub2=50,sub3=60,sum;
float avg;
clrscr();
sum=sub1+sub2+sub3;
printf(“\n Addition of three subject is %d”,sum);
avg=sum/3;
printf(“\n average of three subject is %f”,avg);
getch();
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Character (char)
Generally those data types are used when we have to storage a single character or string to
variable. It requires 1 byte or 8 beats to store in memory. It is defined by keyword as ‘char’
the range of char is -128 to 127.
Ex:-char name;
Name=’a’;
Char name[15]=”Bhosle”;
Double
It is very similar to float data type the major difference between float & double data type
is size & range of the double data type is always greater than float data type size of double
data type is 64 beats, 8 bytes range of double data type is 1.7e-308 to 1.7e +308 .
Void Type
The Void type has no value. This is usually used to specify the type of functions. The type
of a function is said to be void when it does not return any value to the calling function. It
can also play the role of a generic type, meaning that it can represent any of the other
standard types.
User defined data type
These data type are defined by using keyword ‘typedef’ it takes following format.
Syntax :- typedef type identifier name

Ex:- typedef int mark


The example will demonstrate the use of user defined data type.
#include<stdio.h>
#include<conio.h>
typedef int mark
Void main()
{
mark sub1,sub2,sub3,sum,avg;
printf(“\nEnter the mark of three subject”);
scanf(“%d%d%d”,&sub1,&sub2,&sub3);

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

sum=sub1+sub2+sub3;
avg=sum/3;
printf(“\nThe total of three subject is%d”,sum);
printf(“\nThe average of three subject is %d”,avg);
getch();
}
here marks indicate integer data type but sub1, sub2,sub3 indirectly marks referees to
standard data type int.
Derived data type
Some time these derived data type are also called user defined data type which is derived
from fundamental data type. The derived data type are
ex:- array, pointer, function.
Escape Sequences

These are the special characters starting with ‘\’ which makes the computer to line out of
the normal sequence.
Computer has some default sequences e.g computer always prints data in a line
unless we ask the computer to create a new line etc. the following are the escape sequences.
Constant Meaning Constant Meaning
\n New line \r Carriage return
\t Horizontal tab \0 Null
\v Vertical tab \’ Single quote
\a Alert (bell) \” Double qutoe
\b Back space \? Question mark
\f Form feed \\ Back slash

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

//Write a program to swap two value by using third variable

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b,c;
clrscr();
printf(“\nEnter the First number’);
scanf(“%d”,&a);
printf(“\nEnter the Second Number”);
scanf(“%d”,&b);
c=a;
a=b;
b=c;
printf(“\n After swap A=%d”,a);
printf(“\n After swap B=%d”,b);
getch();
}
*****out/put****
Enter the First number 23
Enter the Second Number 55
After swap A= 55
After swap B= 23

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

//Write a program to swap two value by using third variable


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“\nEnter the First number’);
scanf(“%d”,&a);
printf(“\nEnter the Second Number”);
scanf(“%d”,&b);
a=a+b;
b=a-b;
a=a-b;
printf(“\n After swap A=%d”,a);
printf(“\n After swap B=%d”,b);
getch();
}
*****out/put****
Enter the First number 23
Enter the Second Number 55
After swap A= 55
After swap B= 23

/*write a program to reveres the given number*/


#include<stdio.h>
#include<conio.h>
void main()

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

{
int no,rev,a1,a2,a3;
clrscr();
printf("Enter the number");
scanf("%d",&no);
a1=no%10;
no=no/10;
a2=no%10;
no=no/10;
a3=no;
rev=100*a1+10*a2+a3;
printf("Revers number is %d",rev);
getch();
}
*****out/put****
Enter the number 123
Entered number is 321

Operators :-
An operators is a symbol which tells the computer to perform mathematic or logical
operation C supports following 8 types of operators
1) Arithmetic operators
2) Relational operators
3) Logical operators
4) Assignment operators
5) Increment & Decrement operators
6) Conditional operators
7) Bit-wise operators
8) Special operators

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

1) Arithmetic operators

C Language supports all arithmetic operators this operators provides same function
as in other languages this operators can operate on any built in data type in C
following list gives the arithmetic operators with meaning.
+,-,*,%
Integer division ignores frication parts in the result c doesn’t support an operators
for exponential
A+B
A and b is operands
//Addition of two number
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
float b,c;
clrscr();
printf("\nEnter the two number");
scanf("%d%f",&a,&b);
c=a+b;
printf("Addition of two number is %f",c);
getch();
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

2) Relational operators
If we want to compare two values then Relational operators are used C supports six
Relational operators
1 >
2 >=
3 <
4 <=
5 ==
6 !=
The simple format of Relational operators as follows

Expression Relational operator’s expression


The value of real expression is dissention statement if
//Write a program to use various relational operators and display their return values.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\nCondition : Return Values \n”);
printf(“\n10!=10 : %5d”,10!=10);
printf(“\n10==10 : %5d”,10==10);
printf(“\n10>=10 : %5d”,10>=10);
printf(“\n10<=10 : %5d”,10<=10);
printf(“\n10!=9 : %5d”,10!=9);
getch();
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

3) logical operators

C language support three logical operators

&& Logical And


|| Logical or
! Logical Not
a<b && x==30
a>b || b>c
Logical ‘AND’ (&&)
Condition1 Condition 2 Result
True False False
False True False
True True True
False False False

Logical ‘OR’ (&&)


Condition1 Condition 2 Result
True False True
False True True
True True True
False False False

Logical ‘NOT’ (!)


True False
False True

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

4) Assignment Operators
The assignment operators are used to assign values or result of the expression to
variables C support for the assignment operators that is
=
ex:-a=10;
5) Increment and Decrement operators

Prefix Increment and Decrement operators


In the prefix operators the values of variables is Increment and Decrement First
and then assigned to the expression

++a --a
a=3
y=++a
y=4
postfix operators
a post fix operators first assign the values to variables on left side and then
increment the operand
a=5;
y=a++;
y=6
6) Conditional operators

Conditional operators
Expirations ? expression2 : expression3

?:
a=3;
b=4;
x=(a<b)? a:b;

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

//write a program to find out the given number greater or not by using
conditional operators.
#include<stdio.h>
#include<conio.h>
Void main()
{
Int a,b;
Printf(“Enter two number”);
Scanf(“%d%d”,&a,&b);

a>b?printf(“A is greater number”):Printf(“B is greater number”);

getch();
}

in above expression a is less then b if this condition is true then value of a will be
to assign to the expression and if given condition is false then value of b it will be
assign to the expression
7) Bit-wise operators
There operators are used for manipulation of data at bit level we can use these
operators for testing the bits, sift right and left we can’t apply the bit wise
operators with float and double following list gives the operators and there
meaning

This operator move bit patterns either to the left or to the right. The shift operators
are represented by Left shift << and Right shift >>.

<< Left

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

10
0 0 0 0 1 0 1 0
0 0 1 0 1 0 0 0
>>Right
10
0 0 0 0 1 0 1 0
0 0 0 0 1 0

8) Special operators

Comma operators this operator is used to link or connect the related expression
Values(a=3,b=4,a+b)
X=7
3 is assign to variables a 4 will be assign to b after that x=7 addition of two
numbers
// Write a program to use of comma (,) operator

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“ Subtraction = %d\n Addition =%d”,5-3,5+6);
getch();
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Input And Output Statement


Introduction
Reading the data from the input devices and displaying the results on the screen,
are the two main tasks of any program. To perform these tasks user friendly C has a number
of input and output functions. When a program needs data, it takes the data through the
input functions and sends results obtained through the output functions. The input/output
functions are the link between the user and the VDU.
There are number of I/O functions in C based on the data types. The input/output functions
are classified in to two types
1) Formatted functions
2) Unformatted function.

1) Formatted Function:-

The formatted input/output functions read and write all types of data values. They
require conversion symbol to identify the data type. They can be used for both reading
and writing of all data values. The formatted functions return the values after execution.
The return value is equal to the number of variables successfully read/ written.
2) Unformatted function:-

The formatted input/output functions only work with the character data type. There
is no need to convert the data. In case values of other data types are passed to these
functions, they are treated as the character data. The unformatted function also return
values, but the return value of unformatted function is always the same.

Input and Output Functions

Formatted Function Unformatted Function

Input Output Input Output

scanf() printf() getchar() putchar()

gets() puts()

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Formatted Function:-

printf():-
This function is used to display result on the screen it can be used to display any
combination of numerical value as will as char or string. It requires conversion symbol and
variable names to print the data. The conversion symbol and variable names should be
same in number.
Syntax:-

printf(“<Format string>” ,arg1,arg2,….);

Ex:-
void main()
{
int a=3;
float b=5;
char c=’b’;
printf(“%d %f %c”,a,b,c);
}

//display the ascii value


#include<stdio.h>
#include<conio.h>
void main()
{
int y=65;
clrscr()
printf(“%c %d”,y,y);
getch();

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

}
scanf():-
The input data or information can be enter to the computer through a standard input
device by using scanf() function. This function can be used to enter any combination of
numerical value, single char, or strings this function return the number of data item that
have been successful we can declare the scanf() function as follow.

scanf(“<Format string>” ,&arg1,&arg2,….);

In the format string various format specifies are used

Ex:- scanf(“%d”,&a);

The argument1,argument2,……are the argument at that represent that indusial data


item in the format string these argument are return of variables. The scanf() statement
requires ‘&’ operator called address operator. The address operators print the memory
location of the variable. Here in the scanf() statement the role of ‘&’ operator is to indicate
the memory location of the variable. So that value read would be placed at that location.

Ex:-

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“%d”,c);

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

getch();
}
Getchar() Function:-

Single character can be entered into the computer using ‘c’ library function
getchar(); the get character function is a part of standard I/O function. It written a single
character from a standard input device [keyboard] the function does not require any
argument a pair of empty parentheses must follow the word getchar(). The getchar take
following form

Variable name=getchar();

Variable name is a valid C name that has been declared as char type. When this
statement is encountered, the computer waits until a key is pressed and then assign this
character as a value to getchar function. Since getchar is used on the right-hand side of an
assignment statement,

Syntax:-
Char name;
Name=getchar();

Ex:-
Void main()
{
char c;
clrscr();
c=getchar();
putchar(c);
getch();
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Putchar() Function:-

This function prints one character on the screen at a time which is read by the
standard input device such as keyboard.

Syntax:-

putchar(name);

Ex:-
Void main()
{
char c;
clrscr();
c=getchar();
putchar(c);
getch();
}
puts() function:-

This function is used to display the string or collection of character to the output
device such as monitor.
Syntax:-

Char name[20];
puts(name);

Ex:-

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
clrscr();
printf(“\nEnter the name”);
gets(name);
printf(“My name is %s”,name);
getch();
}

gets() function:-
This function is used to accepting any string or collection of character from input
device such as keyboard.
Syntax:-

Char name[20];
Name=gets();

Ex:-
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
clrscr();
printf(“\nEnter the name”);

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

gets(name);
printf(“\n My name is “);
puts(name);
getch();
}
Conversion Specifiers used in c

Data type Conversion Symbol


Integer %d or %i
Unsigned %u
Long signed %ld
Long unsigned %ld
Unsigned hexadecimal %x
Unsigned octal %o
Float %f or %g
Double %lf
Char %c
Unsigned char %c
String %s

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Decision Making Statement


Introduction
The statements in a program are executed line by line similarly in the way we read
a text book. The method to read a textbook is straight forward, which starts from first
line to the end of line and from top to bottom.
Similarly, in the program the statements are read and executed sequentially line-
by-line unto the end of program. In some time, we may select one of the options from
the available options by testing some condition. Depending on the problem, the flow
of the program is fixed.
C Language supports decision making to control the flow and execution of
statements in any order. Such statements are decision or control statements. Decision-
making is one of the important aspects of the computer. For making decisions computer
needs a condition i.e logical expression and decision-making statement. For building
conditions, we need the operators.
Expressions in decision-making statements normally consist of arithmetic,
relational, logical or conditional operators.
Decision making structures
 The if statement
 The if- else statement
 Nested if – else statement

The if statement
If it is an keyword, The if statement is a powerful decision making statement used
to control the flow of execution of statements. First condition is checked. If condition
is true than it execute the true statement block control transfer to statement x. If the
condition is false then without executing the true block the control is goes directly to
the next statement x. The if statement always used in conjunction with expressions.
These expressions evaluate true of false.
The general format of a simple if statement is
if (test expression)
{
body of if statement;
}
Next statement x;

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

The body of if may have one statement or group of statement where each statement
is separated by semicolon. If the test expression is true, the true statement block will be
executed otherwise computer will skip the block of statement and the execution will
jump to the next statement immediately after the end of if statement.

Start

True
Text

Expression?

False Body of if statement;

Statement X

End

Simple If statement flow chart


If ( a>b)
{
Printf(%d\n”,a);
}
Void main()
{
Int a,b;
Printf(“\nEnter the Number a & b”);
Scanf(“%d%d”,&a,&b);

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

if(a>b)
Printf(“A is grater number”);
If(b>a)
Printf(“B is grater number”);
Getch();
}

 The test expression (condition) should not end with semicolon(;).


 The test expression without expression arguments is not acceptable.
 The test expression must be always enclosed within a pair of brackets().

If – else statement
The if – else statement is an extension of the simple if statement. In this statement
first check the condition if condition is true then body of if (true) statement is executed
otherwise body of else (false) statement is executed. In this statement either body of if
or body of else is executed and then only the control moves to the next statement.
Syntax:-

if (condition)
{
Body of true statement block;
}
else
{
Body of false statement block;
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Start

False True
Text

Expression?

False Statement Block True Statement block

Statement X

End

Flow Chart of if – else Statement

Ex:-
if (a>b)
{
Printf(“\n A is grater then B”);
}
else
{
Printf(“\nA is less then B”);
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

This statement provides the alternates in both the situations, if condition is true the
body of if is executed if condition is false the body of else is executed.
Here computer can not proceed without executing either body of if or body of else.

//Write a program to find grater number within two numbers


void main()
{ int a,b;
printf(“\nEnter the Number a & b”);
scanf(“%d%d”,&a,&b);
if(a>b)
{
printf(“A is grater number”);
}
else
{
printf(“B is grater number”);
}getch();
}
// Write a program to find given number is even or odd
void main()
{ int num;
printf(“\nEnter the any number”);
scanf(“%d”,&num);
if(num%2==0)
{
printf(“The given number is even number”);
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

else
{
printf(“The given number is odd number”);
} getch();
}
//Write a program to find given year is leap year or not
void main()
{ int year;
printf(“\nEnter the year”);
scanf(“%d”,&year);
if(year%4==0)
{
printf(“The given year is leap year”);
}
else
{
printf(“The given year is not leap year”);
}getch();
}
Nested if –else statement
It is perfectly all right if we write an entire if-else construct within either the body
of the if statement or the body of an else statement. This is called as nesting of if. In
this kind of statement no of logical condition are checked. For executing various
statement. Nested if-else can be chained with one another If the condition is check if
condition is false then control passes to else statement block where condition is again
checked with if statement. This process continue become condition is true if condition
is true then execute the true statement block otherwise last else statement block will be
execute.
Syntax:-
if(Condition 1)

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

{
if(Condition 2)
{
Body of True Statement Block
}
else
{
Body of False Statement Block
}
}
else
{
if(Condition 3)
{
Body of True Statement Block
}
else
{
Body of False Statement Block
}
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Start

False True
Condition 1

False True False True


Condition 3 Condition 2

False Statement True Statement False Statement True Statement

Statement X

End

Flow Chart of Nested if – else Statement

Simple If ..else statement flow chart

Ex- }
if(a>b) else
{ {
if(a>c printf(c is grater”);
{ }
printf(“a is grater”); }

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

}
else else{
{ printf(“c is grater”);
if(b>c) }
{ }
printf(“B is grater”);

In that first check the condition. If the given condition1 is true then again check the
condition 2 if the given condition 2 is true then execute the true statement block and control
transfer to statement x. if the given condition 2 is false then without executing the true
statement block execute the false statement block of condition 2 and control transfer to
statement x. if given condition 1 is false then control transfer to else statement again check
the condition 3 if the condition 3 is true then execute the true statement and control transfer
to statement x. if the given condition 3 is false then execute the false statement block and
control transfer to statement x.
When this structure gets executed and as soon as any one of the statement gets
executed, immediately the chain breaks & computer goes next statement. There are many
possible forms of nesting. It always depends on our problem.

Write a program that display the class obtained by student if percentage of the
student is input through the keyboard.

void main()
{
float per;
printf(“\nEnter the percentage”);
scanf(“%f”,&per);
If(per<0 && per>100)
{
printf(“Invalid input”);
exit();

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

}
if(per<40)
printf(“Fail”);
else
{
if(per<50)
printf(“Pass”);
else
{
if(per<60)
printf(“Second class class”);
else
printf(“First class”);
}
}
Write a program find out entered key is upper case or lower case.
#include<stdio.h>
#include<conio.h>
void main()
{
char x;
clrscr();
printf("\nEnter any char");
scanf("%c",&x);
if((x>=65&&x<=90)||(x>=97&&x<=122))
{
if(x>=65&&x<=90)
printf("it is an upper case alphabet");

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

else
printf("It is lower case alphabet");
}
else
printf("It is not alphabet");
getch();
}
Positive number and negitive number
#include<stdio.h>
#include<conio.h>
void main ()
{
int num;
printf("\nenter any number");
scanf("%d",&num);
if(num>0)

{
printf("given number is positive");
}
else
{
if(num<0)
{
printf("given num is negative");
}
else
{

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

printf("Zero");
}
}
getch();}
else—if ladder

There is a negative side to the program. Even if the first condition turns out to be true, still
all other condition are checked. This will increase the time of execution of the program.
This can be avoided using the else if clause.
When multiple decision are taken then else if is used when we used else if ladder, can else
is associated with if.
Syntax
If(Condition 1)
statement 1;
else if(condition 2)
statement 2;
else if(condition 3)
statement 3;
else if(condition n)
statement n;
else
default statement;
statement x;

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Condition 1
False Flow chart for else if ladder

Condition 2 False
True
Statement 1
True Condition 3 False
Statement 2

True False
Condition n
Statement 3

Default
True
Statement 3 Statement

End
To find the grade
Void main()
{
Float per;
Printf(“Enter the value”);
Scanf(“%f”,&per);
If(per>80)
{
Printf(“grade is merit”);
}
Else if(per>70)
{

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Printf(“Distinction”);
}
Else if(per>60)
{
Printf(“Grade A”);
}

else if(per>50)
{
printf(“Grade is B”);
}
else if(per>=35)
{
printf(“Grade is C”);
}
else
{
printf(“Failed”);
}
printf(“try try but don’t cry, one day you will success”);
getch();
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Write a program to Larger number within five number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3,n4,n5,larg,small;
clrscr();
printf("\nEnter the number");
scanf("%d%d%d%d%d",&n1,&n2,&n3,&n4,&n5);
larg=n1;
if(n1>larg)
larg=n1;
if(n2>larg)
larg=n2;
if(n3>larg)
larg=n3;
if(n4>larg)
larg=n4;
if(n5>larg)
larg=n5;
printf("\nthe largest number within five number is %d",larg);
small=n1;
if(n1<small)
small=n1;
if(n2<small)
small=n2;

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

if(n3<small)
small=n3;
if(n4<small)
small=n4;
if(n5<small)
small=n5;
printf("\nthe smallest number within five number is %d",small);
getch();}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Switch statement

By using switch statement, which have multiple collection but (alternative increases)
complexity increase and program become easy to read and debug. Switch statement is
multi-case statement. The keyword switch statement value of condition match with label
Syntax:

Start
switch(expression)
{
True
case value 1: Case 1
statement 1; Statement 1
False
break;
case value 2: True
Case 2
statement 2;
Statement 2
break; False
Default: True
Case 3
Default block;
} Statement 3
False
True
Case n

Statement n
False
Switch flow chart
End

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

If condition is doesn’t match with label of case (1) than it checks condition value is match
with label of case (2). If that value is match than execute statement block of case (2) and
control transfer to statement x and so on---
We have to remember that, case labels are single character constants.
Explanation of switch statement
Expression: it is an integer expression or character.
Value1, value 2 : this are constants or constants expression known as case label.
Block 1, block 2: this are statement which may content one or more statement.
Case label must be end with colon(:).
default: the default is an optional case which it will execute expression value, doesn’t match
with any case value.
 In a switch there can be either variable or expression
 If it is a variable it must be either integer or character
 If it is an expression it must be an arithmetic expression.
 There can be any number of cases.
 Every case should have an unique value.
 These cases can be written in any sequence.
 In a case there can be any number of statement.
 It is possible to have the nested switch.
 After every case break statement is compulsory.
 Default is a case which gets selected when none of the case value matches with
the result of expression.
 Default case is optional.

Write a program that reads a number between 1 to 7 and display the day name

main()
{
Int day;
Printf(“Enter a number between 1 to 7 \n”);
Scanf(“%d”,&day);
Switch(day)
{

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Case 1:printf(“Monday\n”);
Break;
Case 2:printf(“Tuesday\n”);
Break;

Case 3:printf(“Wednesday\n”);
Break;

Case 4:printf(“Thursday\n”);
Break;

Case 5:printf(“Friday\n”);
Break;
Case 6:printf(“Saturday\n”);
Break;
Case 7:printf(“Sunday\n”);
Break;
Default:printf(“Monday\n”);
}

Write a program add,sub,multi & division of two number

void main()
{
int a,b,n,p;
flat c;
clrscr();
xyz:printf(“\nenter two number”);

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

scanf(“%d%d”,&a,&b);
printf(“\n 1 addition of two number”);
printf(“\n 2 subtraction of two number”);
printf(“\n 3 multiplication of two number”);
printf(“\n 4 division of two number”);
printf(“\nenter your choice”);
scanf(“%d”,&n);

switch(n)
{
case 1:c=a+b;
printf(“\naddition of two number is%f”,c);
break;
case 2:c+a-b;
printf(“\subtraction of two number is%f”,c);
break;

case 3:c+a*b;
printf(“\multiplication of two number is%f”,c);
break;
case 4:c+a-b;
printf(“\division of two number is%f”,c);
break;
default:
printf(“\nentered choice incorrect”);
break;
}
printf(“\ndo you want to continue”);

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

printf(“\nenter 6 yes 7: no”);


scanf(“%d”,&p);

swatch(p)
{
case 6:goto xyz;
case 7:exit();
}
getch();
}

#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int a,b,c,n,d;
clrscr();
xyz:printf("\nEnter the number a");
scanf("%d",&a);
printf("\nEnter the number b");
scanf("%d",&b);
printf("\n\n 1 Addition");
printf("\n\n 2 Subtraction");
printf("\n\n 3 Multiplication");
printf("\n\n 4 Divission");
printf("\n\n Enter the choice");
scanf("%d",&n);

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

switch(n)
{
case 1:c=a+b;
printf("\n\nAddition of two number is%d",c);
break;
case 2:
c=a-b;
printf("\n\nsubtraction of two number is%d",c);
break;
case 3:
c=a*b;
printf("\n\nmulti of two number is%d",c);
break;
default:
c=a/b;
printf("\n\ndivision of two number is%d",c);
}
printf("\n\nEnter 6 to Yes and enter 7 No");
scanf("%d",&d);
switch(d)
{
case 6:goto xyz;
case 7:exit(0);
break;
}
getch();
}
The output of the following code is ____________

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

#include <stdio.h>
int main()
{
int c=1;
switch( c ){
case 1:
printf(“1”);
default:
printf(“0”);
}
return 0;
}
Answer: 10
Looping Statement
In the program so many times we come across the situation where we want to
execute some part of the program again and again. For some particular number of time. In
such situations we make use of loop statements. These are the statements which can execute
a block of statements for some particular number of time.
The loops are used for repetitive execution of statement in block of loop as long as
the condition is true, all statements in a block of loop are executed repeatedly. When
looping condition become false. The control moves to the next statement immediately after
the end of loop block. Here the condition is a valid logical statement. The logical statement
determines the condition under which the loop should continue repeating.
So when we want to repeat some part or procedure for some particular number of
times then we use loops.
In this language there are three loops
 while loop
 do-while loop
 for loop
 break statement, continue statement, goto statement

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Entry control loop

In this loop, first condition is checked, if the condition is true, than it executes the
body of loop. Again the condition is checked, if the condition is true, it execute body of
loop . So on , this processes continues until the given condition become false. Once
condition become false, than it transfer control to statement x without executing the body
of loop

Ex -While loop,For loop

Exit control loop

In this loop, first body of loop is executed, then the condition is checked. If the
condition is true, then it executes body of loop. Again condition is checked, if it is true the
body of execute. So on this process continue until the given condition become false. Once
condition become false. It control transfer to statement x without executing the body of
loop.
In exit control loop, the condition may true or false, at least once the body of loop is
executed

Ex-do-while
While loop

the while loop is one of the entry control loop. In this loop the condition is given at the top.
First the check condition and if the condition is true then execute body of loop. The body
of while may have one statement or more than one statement, if there is a single statement
no need of brackets’{‘’}’. If there are more one statement then brackets are compulsory.
Again the check condition and if it is true then execute body of loop this process still
continues of condition become false if condition become false does not execute the body
of loop and control goes to statement x. This loop should be used when condition is more
important. Syntax:

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

While(condition)
{
Body true statement block;

Start

Initialize

Text False
Condition

End
True

Body of loop

Increment

flow chart for while loop

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Ex:- void main()


{
int i=1;
clrscr();
while(i<=100)
{
printf(“%d\n”,i);
i=i+1;
}getch();
}
void main()
{
int i=2;
clrscr();
while(i<=100)
{
printf(“%d\n”,i);
i=i+2;
}
getch();
}
Do while
There is a minor difference between the working of while and do-while loops this
difference is the place where the condition is tested. The while tests the condition before
executing any of the statements within the while loop. The do-while tests the condition
after having executed the statements within the loop.

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Syntax:-

Do
{
Body of the loop
}
While(test-condition);
Since the test condition is execute at the bottom of the loop, the do –while construct
provides an exit controlled loop and therefore the body of the loop is always executed
at least once.
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int n,k,m;
clrscr();
scanf("%d",&k);
n=1;
do
{
m=pow(n,2);
printf("%d\n",m);
n++;
} while(m<=(pow(k,2)));
getch();
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

//display 5,10,15----
#include<stdio.h>
#include<conio.h>
void main()
{
int n,k;
clrscr();
scanf("%d",&k);
n=5;
while(n<=k)
{
printf("%d\n",n);
n=n+5;
}
getch();
}
//display 7,14,21----
#include<stdio.h>
#include<conio.h>
void main()
{
int n=7;
clrscr();
while(n<=70)
{
printf("%d\n",n);
n=n+7;
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

getch();
}
//display 10,20,30----
#include<stdio.h>
#include<conio.h>
void main()
{
int n,k;
clrscr();
scanf("%d",&k);
n=10;
while(n<=k)
{
printf("%d\n",n);
n=n+10;
} getch();
}
//display 1,2,4,7,11----
#include<stdio.h>
#include<conio.h>
void main()
{
int n,k,d;
clrscr();
scanf("%d",&k);
n=1;
d=1;
while(n<=k)

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

{
printf("%d\n",n);
n=n+d;
d=d+1;
} getch();
}
//display 1,2,4,8----
#include<stdio.h>
#include<conio.h>
void main()
{ int n,k;
clrscr();
scanf("%d",&k);
n=1;
while(n<=k)
{
printf("%d\n",n);
n=n*2;
}
getch(); }
//sum of 1 to 10 number
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,sum=0;
clrscr();
while(i<=10)

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

{
printf("%d\n",i);
sum=sum+i;
i++
}
Printf(“sum of I to 10 number%d”,sum);
getch();
}
//multiplication table
#include<stdio.h>
#include<conio.h>
void main()
{ int I,j;
clrscr();
while(i<=10)
{
j=1;
while(j<=10)
{
printf("%d\t",i);
j++
}
printf(“\n”);
i++;
}
Printf(“sum of I to 10 number%d”,sum);
getch();
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Difference between while and do-while

While Do—while

Condition is at the top Condition is at the bottom.

No necessity of brackets if there is Brackets are compulsory even if


single statement in body. there is a single statement.

There is no semicolon at the end of The semicolon is compulsory at eh


while. end do-while.

Computer executes the body if and Computer executes the body at least
only if condition is true. once even if condition is false

This should be used when condition This should be used when the
is more important. process is important.

This loop is also refered as entry This loop is also refered as exit
controlled loop. controlled loop.

while(n<=10) do
{ {
printf(“%d\n”,n); printf(“%d\n”,n);
n++; n++;
} }while(n<=100);

void main()
{
int i=1;
clrscr();
do

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

{
printf(“%d\n”,i);
i=i++;
} while(i<=100);
getch();
}
//display 5,10,15----
#include<stdio.h>
#include<conio.h>
void main()
{
int n,k;
clrscr();
scanf("%d",&k);
n=5;
do
{
printf("%d\n",n);
n=n+5;
} while(n<=k);
getch();
}

//display 7,14,21----
#include<stdio.h>
#include<conio.h>
void main()
{
int n=7;

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

clrscr();
do
{
printf("%d\n",n);
n=n+7;
} while(n<=70);
getch();
}

//display 10,20,30----
#include<stdio.h>
#include<conio.h>
void main()
{
int n,k;
clrscr();
scanf("%d",&k);
n=10;
do
{
printf("%d\n",n);
n=n+10;
} while(n<=k);
getch();
}

//display 1,2,4,7,11----
#include<stdio.h>
#include<conio.h>

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

void main()
{
int n,k,d;
clrscr();
scanf("%d",&k);
n=1;
d=1;
do
{
printf("%d\n",n);
n=n+d;
d=d+1;
} while(n<=k);
getch();
}

//display 1,2,4,8----
#include<stdio.h>
#include<conio.h>
void main()
{
int n,k;
clrscr();
scanf("%d",&k);
n=1;
do
{
printf("%d\n",n);
n=n*2;

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

} while(n<=k);
getch();
}

//display 1,2,4,8----
#include<stdio.h>
#include<conio.h>
void main()
{
int n,km;
clrscr();
scanf("%d",&k);
n=1;
do
{
m=pow(n,2);
printf("%d\n",m);
n++;
} while(m<=(pow(k,2)));
getch();
}

//multiplaction table
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,j;

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

clrscr();
while(i<=5)
{
j=1;
while(j<=5)
{
printf("%d\t",i*j);
j++;
}
printf("\n\n");
i++;
}
getch();
}
Display output as
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,j;
clrscr();
while(i<=5)
{
j=1;
while(j<=i)
{
printf("*\t");
j++;
}
printf("\n\n");

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

i++;
}
getch();
}

*
*
***
****

For loop

This statement is used when calculations are to be carried out between initial and final
value with step. The for loop condition consist of three actions namely initialization, testing
and incrementing/ decrementing. Each action should be separated by semicolon(;) also this
loop statement is used when number of execution time is known in advance.

It is an entry control loop having syntax as follow.


Syntax-

for(initial value;condition;increment/decrement)
{
body of for loop;
}

In this loop the initial value, condition, increment/decrement is specified in the same line.
Here computer starts with the initial value, checks the condition. If the condition is true

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

computer executes the body and automatically goes up, it takes the increment or decrement
automatically and it continues. Remember, if the body of for continue a single statement, no
need of brackets. But if there are more than one statement in the body then they must be
enclosed between brackets.
Here every thing is specified in the same line and hence we can tell the number of
repetitions it can take. When we know the number of repetitions in advance than we should go
for the for loop.

Start

Flow Chart
Initialization

False

Condition

Stop
True

Body of loop

Increment

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=10;i++)
{

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

printf(“%d”,i);
}
getch()
}
Break Statement

The break statement is used for two different purpose it can be used to terminated a case in the
switch statement that is to terminate the given loop immediately and it by pass the normal loop
condition test.
we often come across situations where we want to jump out of a loop instantly, without wetting
to get back to the conditional test, the keyword break allows us to do this. when break is
encountered inside any loop, control automatically passes to the first statement after the loop.
A break is usually associated.
Ex:-
Switch()
{
Case 1:
Break;
Case 2:
Break;
Default:
}
ex:- void main()
{
int I;
for(i=1;i<=10;i++)
{
if(i==5)
{
break;
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

printf(“%d”,i);
}
getch();
}
Continue Statement:-

This statement is used to when execution of further statement. This stops execution of next
statement ?& transfer control to the start of loop for further execution of loop. This statement
should be used only within the loop.

Ex:- program to determine continue statement.

Void main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==5)
{
continue;
}
printf(“\n%d”,i);
getch();
}
in the above ex I is initialized to 1 then it will check (i==5) if it is true it will execute printf()
statement. After execution I is incremented by 1 & again executes printf() statement. This
process continues till the condition i<=10 becomes true.

Goto Statement:
This statement transfers the control from one statement to other statement in the program
which may not be in the sequence. The general form of the goto statement is

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

goto label;
--------
--------
label:

towards jump

label: statement

---------
---------
goto label;

backward jump

where goto requires a level in order to identify the place where the branch is to be made
label.
It is an valid variable name & must be followed by a (:) the program either before or after
the goto label statement as shown below.

Forward jump:

If the label is placed after goto label statement will be skipped such a jump is known as
forward jump.

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Backward jump:

If the label is before the goto statement a loop will form & some statement will be executed
repeatedly such a jump is known as backward jump.

#include<stdio.h>
#include<conio.h>
#include<process.h>
void main ()
{
int a,b;
clrscr();
xyz:printf("enter the number");
scanf("%d",&a);
if(a%2==0)
{
printf("given num is even ");
}
else
{
printf("\nthe given num is odd");
}
printf("\nif you want to continue");
printf("\n Enter 6 to yes 7 to no");
scanf("%d",&b);
if(b==6) goto xyz;
if(b==7) exit(0);
getch();
}

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Algorithm in Programming

In programming, algorithm is a set of well defined instructions in sequence to solve the


problem.

Qualities of a good algorithm

Input and output should be defined precisely.

Each steps in algorithm should be clear and unambiguous.

Algorithm should be most effective among many different ways to solve a problem.

An algorithm shouldn't have computer code. Instead, the algorithm should be written in such a
way that, it can be used in similar programming languages.

An algorithm is a description of a procedure which terminates with a result. Algorithm


is a step-by-step method of solving a problem.

Properties of an Algorithm:

1) Finiteness: - An algorithm terminates after a finite numbers of steps.

2) Definiteness: - Each step in algorithm is unambiguous. This means that the action
specified by the step cannot be interpreted (explain the meaning of) in multiple ways & can be
performed without any confusion.

3) Input: - An algorithm accepts zero or more inputs

4) Output:- An algorithm should produce at least one output. Effectiveness: - It consists


of basic instructions that are realizable. This means that the instructions can be performed by
using the given inputs in a finite amount of time.

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Writing an Pseudocode

An Pseudocode can be written in English, like sentences and using mathematical formulas.
Sometimes algorithm written in English like language is Pseudo code. Example 1:Finding the
average of three numbers

1. Let a,b,c are three integers

2. Let d is float

3. Display the message “Enter any three integers:”

4. Read three integers and stores in a,b,c

5. Compute the d = (a+b+c)/3.0

6. Display “The avg is:” , d

7. End.

Example 2: Write an algorithm to determine a student‟s final grade and indicate whether it is
passing or failing. The final grade is calculated as the average of four marks

Step 1: Input M1,M2,M3,M4

Step 2: GRADE (M1+M2+M3+M4)/4

Step 3: if (GRADE < 50) then

Print “FAIL”

else

Print “FAIL”

Endif

Q1. Create a program to compute the volume of a sphere. Use the formula: V = (4/3) *pi*r3
where pi is equal to 3.1416 approximately. The r is the radius of sphere. Display the result.

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Q2. Write a program the converts the input Celsius degree into its equivalent
Fahrenheit degree. Use the formula: F = (9/5) *C+32.

Q3. Write a program that converts an input inch(es) into its equivalent
centimeters. Take note that one inch is equivalent to 2.54cms

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Q4. Write a program that exchanges the value of two variables: x and y. The output
must be: the value of variable y will become the value of variable x, and vice versa.

Write an algorithm to add two numbers entered by user.

Step 1: Start

Step 2: Declare variables num1, num2 and sum.

Step 3: Read values num1 and num2.

Step 4: Add num1 and num2 and assign the result to sum.

sum←num1+num2

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Step 5: Display sum

Step 6: Stop

Problems Involving Iteration and Nesting:

Displaying Different Patterns and Shapes Using Symbols and Numbers

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Fibonacci and Other Sequences :-


The Fibonacci series is the sequence where each number is the sum of the
previous two numbers of the sequence. The first two numbers are 0 and 1 which
are used to generate the whole series.
Example
Input: n = 5
Output: 0 1 1 2 3
Explanation: The first 5 terms of the Fibonacci series are 0, 1, 1, 2, 3.
#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf(“Enter the number of terms: “);
scanf(“%d”, &n);
printf(“Fibonacci Series: %d, %d, “, t1, t2);
for (int i = 1; i <= n – 2; ++i) {
nextTerm = t1 + t2;
printf(“%d, “, nextTerm);
t1 = t2;
t2 = nextTerm;

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

}
return 0;

ASCII VS UNICODE

The two-character encoding schemes that are currently most widely utilized around
the world are Unicode and ASCII.
As opposed to ASCII, which is used to represent text in computers as symbols,
characters, and numbers, Unicode is a character encoding that may be used to process,
store, and exchange text data in
any language.

UNICODE ASCII

For electronic communication, it is a standard


A wider range of characters than ASCII is
for character encoding. The American Standard
represented by Unicode, including letters from
Code for Information Interchange (ASCII) was
languages like English, Arabic, Greek, and
first published in 1963. ASCII codes are used to
others, mathematical symbols, historical
represent text in computers and other electronic
scripts, and emoji.
devices.

Lowercase letters (a-z), uppercase letters (A-Z),


Different character encodings, such as UTF-8,
numerals (0-9), and symbols like parenthesis,
UTF-16, and UTF-32, can be used to define
dollars, the ampersand, and others are
Unicode.
represented by the ASCII code.

Only 128 different characters can be encoded


Unicode encrypts 154 written scripts.
using ASCII using a 7-bit range.

UNICODE is a superset of ASCII. ASCII is a proper subset of UNICODE

Example
Here is a program to print the ASCII value of the character variable.
#include <stdio.h>
int main()
{
char c;

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

printf("Enter any character ");


scanf("%c",&c);
printf("\nThe ASCII value of the entered character is: %d", c);
return 0;
}

Output
Enter any character d

The ASCII value of the entered character is: 100

Write an algorithm to find the largest among three different numbers entered by 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 largest number.

Else

Display c is the largest number.

Else

If b>c

Display b is the largest number.

Else

Display c is the greatest number.

Step 5: Stop

B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR

Flow Chart
 Flowchart is the diagrammatic or pictorial representation of the algorithm for solving
the problem. This is a tool used in the design of program. Flowchart consist of pictorial
symbols. Flowchart helps a programmer to plan the procedure for the solution of a
problem.
 Flowchart indicates the direction of flow of process, relevant operations and
computations, point of decision and other information which is part of the solution.
 The symbols and their meaning used in flowchart are shown in the following table

Symbol Meaning

Start, Stop (begin, end)

Input, Output

Processing program instructions

Decision

Direction of flow

Connector

Loop

Function symbol

Flowchart are not related to any programming language. Hence they can be implemented in
any programming language.
Flowchart are easy to understand. The data flow of the program can be easily understood by
flowcharts and errors in it can be easily recovered.
When we need to incorporate some more facilities within program, then it is easy to incorporate
them through flowchart.

B.C.A FY A1 Batch

You might also like