Unit 2 c Language
Unit 2 c Language
Unit 2 c Language
UNIT II
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.
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.
%endmacro
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
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
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.
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
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.
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.
B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR
History of C Language
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.
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.
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
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
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Wel come C programming”);
getch();
}
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:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“*****out/put****”);
printf("Hello cocsit");
getch();
}
*****out/put****
Hello cocsit
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
* Astric
- Minus
+ Plus
( 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.
B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR
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.
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.
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
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
C Data Types
- structure - array
- union Built in type - function
- pointer
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
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
#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
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
#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
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
++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);
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
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.
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:-
Ex:-
void main()
{
int a=3;
float b=5;
char c=’b’;
printf(“%d %f %c”,a,b,c);
}
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.
Ex:- scanf(“%d”,&a);
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
B.C.A FY A1 Batch
Royal Education Society's
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY, LATUR
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?
Statement X
End
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();
}
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?
Statement X
End
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.
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
Statement X
End
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
#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”);
}
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
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
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
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
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
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
While 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.
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.
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
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.
Properties of an Algorithm:
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.
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
2. Let d is float
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
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.
Step 1: Start
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 6: Stop
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
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
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
Output
Enter any character d
Write an algorithm to find the largest among three different numbers entered by user.
Step 1: Start
Step 4: If a>b
If a>c
Else
Else
If b>c
Else
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
Input, Output
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