Vous êtes sur la page 1sur 148

A.V.

REDDY GROUPS

C-LANGUAGE

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Introduction to Programming
1. Computer Programming
2. Programming Techniques
3. Algorithm
4. Flow Chart
Computer Programming:Computer programs are written using one of programming
languages (FORTRAN, C and C++ and so on). A program has set of instruction written in order
to get the desired result. The method of writing the instruction to solve the given problem is
called programming.
Programming Techniques:There are of two types of programming techniques used:
1. Procedural Programming
2. Object Oriented Programming (OOPS)
Procedural Programming:In Procedural programming, for a given problem, variables
are identified and instructions are written using the variables in the correct sequence to get the
required result. Sometimes the program may require unconditional transfer of control from one
part of the program using GOTO statements. This can be avoided by writing the statement
sequence using many blocks. This is called a structured program.
The procedural programming method is used to solve scientific and engineering problems
involving variables.
Discrete results are expected as the output of the program.
Object Oriented Programming (OOP):In object oriented programming, objects which have
data related to a person or item are used. The program can be written using may many
functional blocks. The functional block contains instruction similar to procedural programming.
Object oriented programming method is used to develop software packages. C++ is one the
commonly used Object oriented programming languages.
Algorithm:In order to write computer program without any logical error, it is recommended
programmers prepare a rough writing showing the steps involved in the program. This is called
an algorithm.
OR
Algorithm is the step by step solution of the given problems is called algorithm.
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Flow Chart:Flow chart is a symbolic or diagrammatic represent the arrows to show
the direction of an algorithm. It uses several geometrical figures to represent the operations, and
arrows to show the direction of flow. Following are the commonly used symbols in flowcharts.

Symbol

Operation
START/STOP

Meaning
Represent the beginning and the end
of the flow chart.

Input/Read

Represent the values to be given


by user through the keyboard.

Processing

Represent the arithmetic operation.

Checking/Decision Making

Write/Print

Connector

Arrows

Represent the logical checking to


decide the flow sequence.

To print /write final output.

Represent the continuity of the flow


chart in another page.

Represent the direction of the


control flow

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft

Introduction to C Language:C is a popular programming language and is commonly used by Scientists and Engineers to write
programs for any specific applications. C is also a widely accepted programming language in the
software industries.
The C language is one of the powerful programming languages used to write the programs for variety
of applications. It was developed by Dennis Ritchie at Bell Laboratories during the 1972.
Language

Step to Learn

Englishh

Alphabets

C Language

Words

Tokens

Sentence

Statement

Function

Paragraph

Program

Tokens:As in passage the smallest unit in the world called its tokens. In C language program the smallest
individual units are known as C-tokens.
C-Tokens are of six types.
C-Tokens

Keywords

Constant

Variable

Identifier

Specifier

1. Keyword:#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Operator

Tecnosoft
These are certain special words in C Language which are already defined in C and have their particular
meaning are called keywords.
Important instruction while use any Keywords:1. Each keyword must be defining according syntax otherwise syntax error
will be occur.
2. All the keywords must be used in lower case latter.
3. Any built in keyword cannot be used as user defines word.
2. Constant:These are the datatype whose value fixed during execution of the program.
Constant

Numeric Constant

Integer Constant Real Constant

Character Constant

Character Constant

String Constant

Integer Constant:Integer constant consist only absolute number either in the positive or
negative.
Example:- 15,17,-18,-21.
Real Constant:Real constant also called the floating point constant and consist absolute number including the real
number.
Examples:- -15,76, 18.893
Character Constant:Character constant is enclosed within single quote.
Examples:-a, X.
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
String Constant:String constant is a group of character and enclosed in double quotes.
Examples:- OK, SORRY.
3. Variable:Variables are the datatype whose value may be changed during the execution of the program.A variable
represent a specific memory location or address which contain or hold some value and information.
Example:X=10;
X=X+2;
printf( %d,X);
Output:12
Here 10 is a constant and X is a variable. Its value is changed after execution the program. So the X is a
variable.
Rules to declare a Variable:1. Any variable can be up to 31 Character in length.
2. White space is not allowed.
3. The built in words cannot be used as variable.
4. Any variable may contain alphabets (A-Z), (a-z) digits (0-9) and
underscore (-).
5. 1st Letter of any variable
Identifier:Identifier refers to a name of the variables, function, array etc. These are define by the users.
Rules for Identifier:1. First character must be an alphabet.
2. Identifier must contain only letters digit and underscore (_).
3. Any built in keyword cannot be used as identifier.
4. Must not contain white space.
Specifier or Special Symbol:Special symbol are like the sign of +, -,*, /, %,<,>? Etc.
Operator:Variable and constant are separated by the operator.
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
ExampleX=10
Here examples X is a variable and 10 is a constant these variable and constant are separated by equal
sign Then the equal sign (=) is an operator.

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Structure of C Program
Header File
Global variable declaration;
Function;
Main Function
{

Starting Block

Local Variable Declaration;


.
Block of Statement

Ending Block

Header File:In C language mostly we used two header file are used. Which one name is stdio.h and another
header file name is conio.h.
Stdio.h:Standard Input output Header File
These files are containing input and output function. In C language scanf() is an input function
and printf() is an output function. These scanf() and printf() function are include in stdio.h
header file. .h is an extension of header file.
Conio.h:- Console Input Output Header File
In this header file contain clrscr() and getch() function. These functions are taking then used
conio.h header file.
Global variable:These variables which are defined the outside the main function.
Function:Function is a self contain of block which we perform a particular task.
Main Function:It is a main function. We take one main function is used in C language.
Local Variable:These variables which are define in inside the main function.
Rules for C Program:#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

1.
2.
3.
4.
5.

Tecnosoft
One main () function is used in every C program.
All the statements are written in lower case letter.
Every statement are closed by a semicolon.
Built in or predefine words are not define the user define word.
Every small and capital words are different. Example :- sum and SUM
are two different keywords.

Execution steps of c program

Source code
(.c)
Complier (Alt + F9)
Computer code
(.obj)

Run (Alt + F9)


Run
(.exe)

Goto Start Run

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft

OR
My Computer C Drive Tc Bin Tc
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Examples:Write a program in C to print your name.
#include<stdio.h>
void main()
{
printf(RAM);
}
Process to see output:1. Save the file with extension name. (The extension name of the C language is .c).
2. Compile the program (Alt+F9). If program show the error then remove of the error.
3. If the program is success compile then the program is Run (CTRL+F9)
When run the above program then do not show the output then show the output for this program first of
all doing the above process and press Alt+F5.
Note:- In c language the semicolon (;) is called terminator.
Complier:This is a translator which is used to change the program in high language to machine
or computer language.
Output:-:

Note: Make sure that you have copy the Folder of TC into C Drive else compiler show the error
-cannot open conio.h or stdio.h In this case You have to set the path.
GOTo
Option
Directories
Then show the this dialog box.

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft

Set the path of include directory and library directories by specifying the location of include and lib
folder respectively.
Write a program to print your friend name.
#include<stdio.h>
#include<conio.h>
void main()
{
printf(Shyam);
getch();
}

Output:
getch():This function is used to show the output. This function is taking used then attach a header file which is
name is conio.h.
Write a program to print your name but previous output is not show.
#include<stdio.h>

#include<conio.h>
void main()
{
clrscr();
printf(Shyam);
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
getch();
}
Output :

clrscr():This function is used to show the current output and attach a header file conio.h.
When both function clrscr() and getch() then attach a same header file conio.h.
Write a program to print your name and your friend name?
#include<stdio.h>
#include<conio.h>
void main()
{
printf(Ram);
printf(Shyam);
getch()
}
Output:-

Write a program print your name and your friend name in different line.
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(Ram\n);
printf(Shyam);
getch();
}
Output:-

\n:This command is used to show the output in the next line. As shown in above Examples.

Data Type
The store the value of variable and constant is known as data type.
Example:int X=10;
Here above example the X is a variable and 10 are a constant and the X and 10 both are store in
So the int is a data type

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

int.

Tecnosoft
Data Type

Preemptive or Built in Data Type

Numeric

Integer

Non-Preemptive or User De

Non-Numeric

Array

Structure

Float
Character

String

Preemptive or Built in Data Type:The basic data types are called the preemptive data type. These data type include integer, Float,
Character and double.
Basic Data Type:These data type are most used in c and c++.
Data Type

Keyword

Byte Occupy by Ram

Range

Format String

Character

Char

1 Byte

-128 to 127

%c

Integer

Int

2 Byte

-32768to 32767

%d

Float

Float

4 Byte

3.4e-38 to 3.4e+38

%f

Double

Double

8 Byte

1.7e-308 to 1.7e+308

%lf

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Example of Character: - a, b,k etc.
Example of Integer: - 1,2,78,-35,-9 etc.
Example of Float: 5.67, 6.89 etc.
Example of double: 5.670000000, 6.789800000 etc.
Additional Data Type:Some other data types which are describe as below.

Integer

Short Integer

Unsigned Short Integer

Unsigned Integer

Unsigned long Integer

Data Type
Short Integer
Unsigned short
Integer
Unsigned
integer
Long Integer

Keyword
Short int
unsigned
short int
unsigned int

Size Occupy
1 Byte
1 Byte

Range
-128 to -127
0 to 255

Format String
%d
%u

2 Byte

0 to 65535

%u

long integer

4 Byte

%ld

Unsigned Long
Integer
Long Double

Unsigned long 4 Byte


int
long double
10 Byte

-2147483648 to
2147483647
0 to 4294967295
3.4e-4932 to 3.4e+4932

%LF

Octal

octal

%o

Hexadecimal

hexadecimal

String

String

It represented an octal
value.
It represents a
hexadecimal value.
It represents a string of
group of character.

%lu

%x
%s

User Define Data Type:These data type which are define by the user according to programming
requirement.
Example: - Array, Structure and Union.
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Array:Array is a group of similar data type when able we need you store many data in a same time.
Structure:The structure data type stores the different data type and run all data type at a same type.

Union: The union data type stores the different data type. But one data type is run at a time.
Storage Class:Variable used in a C program are stored in RAM or CPU memory register. There
are four storage classes into which the variables are declared and stored.
1. Automatic Storage Class
2. Register Storage Class
3. Static Storage Class
4. External Storage Class
Depending on the type of the storage class, the scope and behavior of the variable vary in a C program.
Auto or Automatic Storage Class:Variables are declared in this class are stored in RAM.
This is the default storage class and the keyword auto is used to declare variables. Auto variables
are active in a block in which they are declared. Note that a block means the statement inside the
braces { }.
When a block in a C program is executed, the variables inside the block are created automatically
and are found active. These variables are destroyed automatically and become inactive when the
execution is transferred to another block or function in the same program. Note that the keyword
auto to declare these variables is original..
Auto storage class is commonly is used in all C program without the keyword. Auto.
Example:#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
auto int x =100;
/* consider an inner block*/
{
auto int x= 200;
printf("Value of x in inner block = %d\n",x);
}
printf("Value of x in outer block is =%d",x);
getch();
}
Output:Value of x in inner block = 200
Value of x in outer block = 100
Static Storage Class:Variables declared in this class are also stored in the RAM. The
Keyword static is used to declare thee variables. Similar to auto variables, the static variables
are also active in the block in which they are declared, and they retain the latest value. The static
variables are commonly used along with functions.
Examples:#include<stdio.h>
#include<conio.h>
void repeat();
void main()
{
clrscr();
int k =1;
while(k<=5)
{
repeat();
k++;
}
getch();
}
void repeat()
{
static int j=100;
j=j+1;
printf("\n Value of j =%d",j);
}
Output:Value of j =101
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Value of j =102
Value of j =103
Value of j =104
Value of j= 105
Note:- That the static variables in a function are initialized only once during the compilation. There
After the existing value of the variables is active; further, if it is declared as an auto variables its
value will be initialized in every function call during the runtime.
Extern Storage Class:Global variables are declared using this class and they are stored in the
RAM. The keyword extern is used to declare these variables. Note that the global variables are
also declared outside the main () function. Extern class can be used to consider a local variable in
a block as a global variable.
Example:#include<stdio.h>
#include<conio.h>
void display();
void main()
{
clrscr();
extern int k;
k=5;
display();
printf("\n value of x = %d",k);
getch();
}
void display()
{
extern int k;
printf("\n Value of X = %d",k);
k =k+5;
}
Output:Value of k = 5
Value of k =10
Solution:When this program is executed, the value of variable of x available in the main()
Block are use in the function by declaring in the extern storage class.
Note:- That the value of k can be transferred to the main program through extern declaration.
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Register Storage Class:Variable declared using this class are stored in CPU memory register.
The keyword register is used to declared these variables Only a few variables which are
Frequently used in the program are declared using this class to improve the program execution
speed.
The behavior of register variable is similar to that of auto variables except that their storage
Locantionsare different. In case of non availability of CPU memory register, these variables are
Stored stored in RAM as auto variables.
Example:#include<stdio.h>
#include<conio.h>
void main()
{
register int x = 100;
printf(\n Value of x = %d,x);
getch();
}
Output:Value of x = 100
Note:-

That the register variables of smaller size data types like short int, int or char are used.
Addresses of register variables can not be used for any type of processing in a program.

Operator:When we need of separate of variable and constant then the use of operator.
OR
Variable and constant are separate by operator. It is used to perform the mathematic operation.
Example:X=10
Here example X is a variable and 10 is constant but X and 10 are separate by equal
sign. Then the equal sign (=) is an operator
.
Operand:Variables are called as operand.
Example: X+Y
Here above example the X and Y are operand and plus sign (+) is an operator.
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Operator

Unary Operator

Binary Operator

Ternary Operator

Unary Operator:In this operator we use only one operand.


Example: -

Increment and Decrement Operator.

Increment Operator (++): Increment operator is used to increase the value of the variable and increase one value at a time. Its
symbol is ++.
Example:int a=2;
printf(a=%d,a++);
Output: - a=3
Solution: Here ++ is an increment operator and a is an operand and one value is add in the variable. So the value
of the variable a=2 and after increment operator the value of the variable. a= 3.
Decrement Operator(--):Decrement operators are used to decrease the value of the variable and decrease one value at a time. Its
symbol is --.
Example:- int a=2;
printf(a=%d,a--);
Output: - a=1
Solution: #109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Here above example -- is a decrement operator which is used to one value is decrease in the variable.
So the value of the variable a=2 and after decrement operator the value of the variable is a=1. a is an
operand and is an operator.
Binary Operator: In this operator we are used two operand.
Example:X+Y Here X and Y are two operand and plus sign (+) are an operator. So the above
examples are two operand so the operator is a binary operator

Binary Operator

Arithmetic Operator
Relation Operator

Logical Operator

Assignment Operator

Arithmetic Operator:These operators are used to perform the mathematical expression.


Operator
+
*
/
%

Meaning
Addition
Subtraction
Multiplication
Division
Modulus operator to
get remainder in integer
division

Example
X=A+B
X=A-B
X=A*B
X= A/B
X=A%B

Result
11+5=16
11-5=6
11*5=55
11/5=2
11%5=1

Solution:Here the value of A=11 and B= 5. In modulus operator we use to get remainder. So the 11 is divide by 5
and get remainder is 1.
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Relation Operator:These operators are used to compare the value of operands.
Example:
A>B
Solution: Here above example A and B are operands and greater then sign is an operator.
Operator
<
>
=
<=
>=
!=

Meaning
Less Than
Greater Than
Equal To
Less Than Equal To
Greater Than Equal To
Not Equal To

Example
A<B
A>B
A=B
A<=B
A>=B
A!=B

Logical Operator:These operators are joining more than two logical conditions.

Operator

Meaning
Logical And
Logical OR
Logical Not

&&
||
!

Example
(percentage>=60 && percentage<=70)
(Roll No==60 || Roll No==70)
!(percentage=50)

Solution: In Logical And operator percentage is greater then 60 and less than 70 OR percentage
between 60 to 70. Then the condition is true otherwise false. In Logical OR operator the Roll No =60
OR Roll No 70 when one condition is true then the condition is true.
Truth Table of And (&&): Expression1
T
T
F
F

Expression 2
T
F
T
F

Result
T
F
F
F

(T: - True)
(F: - False)
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft

Solution: In logical and operator the condition is true when all condition is true and one condition is
true and other condition is false then the output is false.
Truth Table of Logical OR(||): Expression1
T
T
F
F

Expression 2
T
F
T
F

Result
T
T
T
F

Solution:In logical OR operator when one condition is true and other condition is false then the output is true.
.
Truth Table for Logical Not (!): Expression 1
T
F

Result
F
T

Solution:In logical not operator if one condition is true then other condition is false.
Assignment Operator:These operator are used to change the value of a variable during the running of the program.
Operator
+=
-=
*=
/=
%=

Example
X+= 2
X- = 2
X*= 2
X/= 2
X%=2

Result
9
5
14
3
1

Solution: - Here the above table the value of X=7.


Ternary Operator:-

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
When this operator is executed by the compiler then the condition is evaluated. If it is true the
condition is evaluated. If it is true then the value 1 is assigned to the variable otherwise value -2 are
assigned to the variable.
Syntax:Variable= (condition)? Exp1:Exp2
Example:
H= (a>b)? a:b
Solution:In above example when a is greater then the H=a otherwise H=b.
Additional Operator:There are two other operators that can be used in a c program.
1. Size of Operator
2. Comma Operator
Size of Operator:Size of operator is used to find the number of bytes occupied by a variable in the computer memory.

Example:Int K, X[20];
X[20 ]:- declaration an array with 20 elements..
K
:- K is a variable.
Comma Operator:The comma operator is used to link related expression to make the program more compact.
Example:To swapping the value of X and Y.
T = X;
X = Y;
Y = T;
This can also be written as a single line using a comma operator. T = X, X = Y, Y = T;
W.A.P to print your name and press enter then the print another name.
#include<stdio.h>
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
#include<conio.h>
Void main()
{
clrscr()
printf( Pawan );
getch();
printf(Ram);
getch();
}
Output :Pawan
Press Enter
Ram
Solution:First getch() function is used to show the first name and then press enter than another
getch() function is used to show the another name.
Syntax:A program is a set of string of characters. The system programmer design to pure
language which streams are valid or invalid in the string.
The rules which check these strings are called the syntax of the programming language.
Semantics:The rule that get meaning to the string is called semantics of the programming
language. The semantics of the programming language is much harder to specifier than its syntax.

Example:For(K=1; K<=5;K++)
Printf(OK);
Solution:In the above example semantics of the programming language is that the loop will
execute the statement. printf(OK) is execute the Five times.
Input / Output Function:These are two input output function.
1. Printf()
2. Scanf()
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Printf():It is an output function. This function is used to print the value of the variable using the standard
output device.
It has following syntax:printf(Format String, V1,V2-----------------------Vn);
Format String:Format string represents a specific data type such as int, float, char.
In Int we use to %d format string.
In Float we use %f format string and
In Char we use %c format string.
V1,V2-----------------------Vn is represent the variable.
Scanf():This function is used to read or input value of variable using the standard Input device or
keyboard.
It has following Syntax:scanf(Format string, &V1,&V2--------------------&Vn);
Format String:Format string represent the format specify. Each format string represent a specific
data type such as int, float and char etc.
&V1,&V2-------------&Vn are variable or user define words whose values are to be accepted from the
keyboard.
&(Address Operator):It represent the memory address of defined variable.
Example:scanf(%d,&V1);
Its meaning is to read the value of integer variable of V1.
W.A.P to find the sum of two numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
printf(enter the value of a);
scanf(%d,&a);
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
printf( enter the value of b );
c=a+b;
printf(The sum is =%d,c);
getch();
}
Output:Enter the value of a 15
Enter the value of b 23
The sum is = 38
Solution:Step 1: Read a,b,c;
Step 2: c=a+b;
Step 3: Print c.
W.A.P to calculate the multiply of two numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr()
int a,b,c;
printf( Enter the value of a and b=);
scanf(%d%d,&a,&b);
c = a*b;
printf(The multiply of two numbers is=%d,c);
getch();
}
Output:Enter the value of a and b = 15
10
The multiply of two numbers = 150.
W.A.P to swap of two numbers.
Hints (a=12, b=15, after swapping a=15, b=12).
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
printf(Enter the value of a and b=);
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
scanf("%d%d, &a,&b);
c=a;
a=b;
b=c;
printf(After swapping the value of a and b= %d\n%d,a,b);
getch();
}
Output:Enter the value of a and b = 12
15
After swapping the value of a and b= 15
12
Solution:Step 1: Read a ,b and c (the value of a=12 , b=15 and c=0)
Step 2: c=a;(Here the value of c=a or c=12)
Step 3. a=b; (Here the value of a=b or a=15)
Step 4.b=c; (Here the value of b=c or b=12)
Step 5.print a and b after swapping.
W.A.P to find the sum of two floating values.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float a,b,c;
printf(Enter the values of a and b=);
scanf(%f%f,&a,&b);
c = a+b;
printf("The sum of two floating values=%f,c);
getch();
}
Output:Enter the values of a and b= 12.34
15.78
The sum of two floating values= 28.12
W.A.P to calculate the area of the circle.
#include<stdio.h>
#include<conio.h>
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
void main()
{
clrscr();
int r;
float Pi=3.14, A;
printf(Enter the radish of the circle=);
scanf("%d",&r) ;
a = Pi*r*r;
printf("The area of the circle=%f, A);
getch();
}
Output:Enter the radish of the circle= 7
The area of the circle =154.0000
W.A.P to add the two given values.
Hints :- (A=2345678901, B=123456789)
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
long int A=23456789,B=12345678,C;
C=A+B;
printf("sum is =%ld,C);
getch();
}
Output:The sum of two values = 35802467
Long Int:The Long int is used to using a large value. So the above example the value of A and B is
very large.
W.A.P to find the power of the any variable.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
{
int a,b,p;
clrscr();
printf(enter the value of a and b=);
scanf(%d%d,&a,&b);
p = pow(a,b);
printf(the power of any number = %d,p);
getch();
}
Output:Enter the value of a and b= 5
3
The power of the number= 125.
Solution:In above example the Power function is used to find the power of any variable. The Power function is
a math function. So we used to math.h header file.
Its has following Syntax:=pow(a,b);
pow:- It is a built in keywords.
a : - A variable which find the power.
b :- A another variable how many power is find.
Here a=5, and b=3 so the power of the 5=(5*5*5=125).
W.A.P to find the under root of the any number
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int a;
float c;
printf("Enter the value of a =");
scanf("%d",&a);
c=sqrt(a);
printf("The under root of the number=%f",c);
getch();
}
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Output:Enter the value of a = 9
The under root of the number = 3.000000
Solution:Under root is a math function. So this function is used to sqrt keyword.

Control Statement OR Condition


Control Statement:All statement written in a program are executed from top to bottom one by one. Control
statements are used to transfer the control from one part to another depending on a condition.
There are two types of condition statement used in c Language=>
1. If statement
2. Switch statement
If statement:If statement is a very powerful decision making statement, It is basically a two ways statement. It is a
branching statement. It has the following from.
If (test expression):Test Expression

True
False
It allows the execution of test expression first. I it is true then the true block statement is executed and
if it is condition is false then the false block statement is executed.
Types of If statement:1. Simple If statement
2. If Else Statement
3. Nested if else statement
Simple if Statement:#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
In if statement when the is condition true then show the output and if
Condition is false then dont show the output.
Syntax:If (test Condition)
{
True block of Statement
}
Flowchart of Simple If Statement:-

Test
Condition
Statement Block

Statement Block

W.A.P to read the number and check is even then print it.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
printf(Enter a number=);
scanf(%d,&n);
if(n%2==0)
{
printf(Number is even);
}
getch();
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
}
Output:Enter a number=10
Number is even.
Solution:Even numbers are those numbers which are dividing by 2 and remainder is zero. So
when the number 10 is dividing by 2 and then we get remainder is zero. So the condition is true. Then
print the number is even.
If else Statement:If else statement is a extension of simple if statement. In this statement
when the condition is the true then the control goes to the true block statement and if the condition is
false then than the control goes to the false block statement. After this control goes to the statement- X.
Syntax:If(Test condition)
{
-----------------------------------------------True Block Statement
------------------------}
else
{
----------------------------------------------False Block Statement
------------------------}
Statement - X;

Flowchart of If else Statement:-

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft

Test
Condition
False Block Statement

True Block Statement

Statement- X

W.A.P to enter two numbers and find which is greater.


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
printf(Enter the value of a and b=);
scanf(%d%d,&a,&b);
if(a>b)
{
printf(a is greater =%d,a);
}
else
printf(b is greater=%d,b);
getch();
}
Output:Enter the value of a and b= 15
70
b is greater=70
Solution:In this example first we check the condition is a greater then b than the condition is
false so the control goto the false block statement. So the false block is executed.
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
W.A.P to find wheather a number is divisible by another number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
printf(Enter the value of a=);
scanf(%d,&a);
if(a%5==0)
{
printf(a is divisible by 5);
}
else
printf(a is not divisible by 5);
getch();
}
Output:Enter the value of a= 25
a is divisible by 5.
Nested If- else Statement:The If else statement can be nested according to programming requirement.
Syntax:If(test Condition-1)
{
Statement 1;
}
else if (test condition-2)
{
Statement 2
}
else if (test condition-3)
{
Statement-3
}
else
{
Default Statement;
}
Statement X;

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Flow Chart:-

Test Condition - 1
Statement - 1

Test Condition - 2
Statement - 2

Test Condition - 3
Statement - 3
Default Statement

Statement - X

Solution:Above construction is known as Nested If else Construction. In this construction


Test condition -1 is checked and if it is the condition is true then control goes to
Statement X. OR. If condition is false than control goes to test condition - 2 and this
condition is true then the control goes to the Statement X OR. If the condition is false
so the control goes to the Test condition -3 and this condition is true than the is control
goes to the Statement- X OR If the Test condition-3 is false then the default condition is
will be executed and control goes to statement - X. Its checks the condition from top to
bottom.
W.A.P to find the greater number among three numbers.
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a, b c;
printf(Enter the value of a,b and c=);
scanf(%d%d%d,&a,&b,&c);
if(a>b && a>c)
{
printf(a is greater =%d,a)
}
else if(b>c && b>a)
{
printf(b is greater=%d,b)
}
else
printf(c is greater =%d,c);
getch();
}
Output:Enter the value of a, b and c = 12
15
25
C is greater
Solution:&& => It is a Logic And operator. The benefit of this operator is both the condition is true then the
output is true otherwise one condition is false and other is true then the output is false.
W.A.P to enter a number and print following format.
Gen =
1
SC
= 2
OBC = 3
Invalid entry = More than 3.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
printf(Enter the value of a=);
scanf(%d,&a);
if(a==1)
{
printf(Gen);
}
else if(a==2)
{
printf(Sc);
}
else if(a==3)
{
printf(OBC);
}
else
printf(Invalid entry);
getch();
}
Output:Enter the value of a= 3
OBC
Drawbacks of else If:As number of condition increases number of else-If condition also increases.
So complexity of program also increases.
Switch Statement:This statement is used to execute a block of statement depending on the
value of the variable.
Switch, case and break three keywords are used in switch case. One extra case is used which name is
default case. The default case gets executed if no cases satisfy the condition.
Syntax:Switch(Variable name)
{
Case Label 1:
{
=============
=============Statement
break;
}
Case Label 2:
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Constant

Tecnosoft
{
===========
=========== Statement
break;
}
Case Label 3:
{
=======
======= Statement
break;
}
default:
{
===========
=========== Statement
}
Break:Break statement which will transfer the control to the end of switch statement.
W.A.P to find the city according to given code.
City Code
1
2
3
4
5
More than 5

City
Rewari
Sonepat
Ambala
Delhi
Gurgaon
Other

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int code;
printf(Enter the value of code=);
scanf(%d,&code);
switch(code)
{
case 1
{
printf(Rewari);
break;
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
}
case 2:
{
printf(Sonepat);
break;
}
case 3:
{
printf(Ambala);
break;
}
case 4:
{
printf(Gurgaon);
break;
}
case 5:
{
printf(Delhi);
break;
}
default:
{
printf(Other);
}
getch();
}
Output:Enter the value of code= 3
Ambala
OR
Enter the value of code =7
Other
Solution:The default case is used when the value of code is greater than 5 then default case is execute.
W.A.P to find the value Y using given information
Y(X,n)

1+x
1+X/n
1+ nx

when n = 1
when n = 2
when n > 3 or n< 1

#include<stdio.h>
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
#include<conio.h>
void main()
{
clrscr();
int x, n;
float y;
printf(Enter the value of x and n=);
scanf(%d%d,&n,&x);
switch(n)
{
case 1:
{
y = 1+x;
break;
}
case 2:
{
y = 1+x/n;
break;
}
default:
{
y = 1*n*x;
}
printf(The value of y = %f,y);
}
getch();
}
Output:Enter the value of n and X = 2
10
The value of y = 5.0000
GOTO Statement:The Goto Statement is an unconditional branching statement. It is used to transfer the control from one
part of the program.
Example:W.A.P to find the day of week following number of day.
Number of Day
st

Number of Week Day

#109,1 floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
1.
2.
3.
4.
5.
6.
7.
More than 7

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Invalid entry

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
K:printf("enter the value of a=");
scanf("%d",&a);
switch(a)
{
case 1:
printf("Sunday");
goto K;
case 2:
printf("Monday");
goto K;
case 3:
printf("Tuesday");
goto K;
case 4:
printf("Wednesday");
goto K;
case 5:
printf("Thursday");
goto K;
case 6:
printf("Friday");
goto K;
case 7:
printf("Saturday");
goto K;
default:
printf("Invalid entry");
break;
}
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
getch();
}
Output:Enter the value of a = 2
Monday
Enter the value of a =5
Thursday
Enter the value of a = 1
Sunday
Enter the value of a =12
Invalid entry
Solution:Here K is represent the Goto statement. In above example the first time Enter the value of a= 2 then
output show Monday and press enter then renter the value of a = 5 then output is shows Thursday and
when all case are not run then not goto the outside of the program. If we are going to outside the
execute part then the value enter of a is press more than 7 then we are go to the outside the execute
part..
Loop Control Structure
Loop:Loop is defined as repetition. Loop is used when a certain set of statement are to be executed again an
again in a program. So, loop program contains two segments.
1. Body of the Loop.
2. Control statement.
Depending on the position of the control statements loop are of two types.
1. Entry control
2. Exit control

Entry control:Entry control is called as pre-test. In this control fist of all condition check so it is called entry control
or pre-test. Examples For Loop & While Loop.
Exit control:In this control the condition is checked at last so it is called exit control or past-test.
Example- do while .

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft

Entry Control

True

Exit control
C language has three types of loop.
1.
2.
3.

While.
Do - While
For Loop

Every loop has divide in three parts.


First part
:- is called initialization. In this part the value of variable is initialized.
Second part:- is called condition. In this part decides how many time loop will be executed.
Third part
:- is increment or decrement is depend on the condition.
NOTE :-

The three parts are separate by semicolon (;).

While Loop:While Loop is used when a set of statement are to executed again and again.
Syntax:#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Initialization;
While (test condition)
{
==Body of Loop;
=====;
Increment and decrement operator
}
Ex:W.A.P. to print first ten natural number.
Hints:-12345-------------10.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
a=1;
while(a<=10)
{
printf(%d,a);
a++;
}
getch();
}
Output:1 2 3 4 5 -----------------------10
Ex:W.A.P. to print the sum of first ten natural number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,sum=0;
a=1;
while(a<=10)
{
Sum=sum+a;
a++;
}
printf(the sum of natural no.%d,sum);
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
getch();
}
Output :The sum of natural no.55
Ex:W.A.P. to enter the number and check is perfect or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,num,sum=0;
printf(enter a number=);
scanf(%d,&num);
while(a<num)
{
if(num%a==0)
sum=sum+a;
a++;
}
If(num==s)
printf(perfect number);
else
printf(not perfect number);
getch();
}
Output :Enter a number=6
Perfect number
Solution:Perfect numbers are those numbers which divisible of sum is equal to the number.
Ex:6=1+2+3
6 is perfect number.
6 is divide 1, 2 and 3 and its sum is equal to 6 so the number is perfect.
Ex:W.A.P. to print the following series.
1.2 ,1.4,1.6,1.8---------------------------- 10 terms
#include<stdio.h>
#include<conio.h>
void main()
{
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
int i;
float a=1
i=1
while(i<=10)
{
a=a+.2;
printf(%f,a);
i++;
}
getch();
}
Output :1.2, 1.4, 1.6, 1.8-------------------------------10terms
Do-While Loop:Do-While Loop is also used to execute a block of statement repeat depending on a
condition. It has following syntax.
Syntax :initialization
do
=====
=====block of statement;
=====
increment or decrement operator ;
}
while(test condition);
Ex:W.A.P. to print the first tan natural number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a=1;
do
{
printf(%d,a);
a++;
}
While(a<=10);
getch();

initialization
block of statement
increment operator.
condition

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
}
Output :1 2 3 4 5 6 7 8 9 10

Ex:W.A.P. to print the squre of first tan natural number.


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
a=1;
do
{
b=a*a;
printf(%d,b);
a++;
}
While(a<=10);
getch();
}
Output :1 4 9 16 25 36-------------------------------------100
Ex:W.A.P. to print a name for ten times.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a=1;
do
{
printf(pawan \n);
a++;
}
While(a<=10);
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
getch();
}
Output :pawan
pawan
--------------------pawan

Difference Between While and Do While Loop


Do-While Loop:In Do-While loop body of the loop execute once if the condition is wrong for
first time. Because in this loop first of all statement are execute and then condition check.
While Loop:But in this loop the statement block will not be execute when the value of the
condition is flash.
For Loop:For loop is also use to execute a block of statement repeat depending on a condition. It
has following syntax.
Syntax:for ( initialization; test condition; Increment and decrement )
{
==== Block of statement
====
}
Ex:W.A.P. to calculate the sum of first ten natural number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,sum=0;
for(a=1;a<=10;a++)
{
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
sum=sum+a;
}
printf(the sum of natural no.%d,sum);
getch();
}
Output :The sum of natural no.55
Ex:W.A.P. to print all even no. between 1 to 10 and also print the sum of the even no.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
for(i=1;i<=10;i++)
{
if(i%2==0)
{
sum=sum+i;
printf(%d,i);
}
}
printf(the sum %d,sum);
getch();
}
Output :2
4
6
8
10
The sum of natural no.30
Ex:W.A.P. to print the series.
-1 +2 3 +4 -5 +6------------------------------------n
#include<stdio.h>
#include<conio.h>
void main()
{
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
int a,n;
printf(enter the number when you want=);
scanf(%d,&n);
for(a=1;a<=n; a++)
{
if(a%2==0)
printf(+%d,a);
else
printf(-%d,a);
}
getch();
}
Output :Enter the number where you want=10
-1+2-3+4-5+6-7+8-9+10
Ex:W.A.P. to count the number of digits in any number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,count=0;
printf(enter any number=);
scanf(%d,&n);
while(n>0)
{
n=n/2;
count++;
}
printf(total number of digits=%d,count);
getch();
}
Output :Enter any number =1234
Total number of digits = 4
Some Important Program:1. W.A.P to enter a number and print its reverse.
#include<stdio.h>
#include<conio.h>
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
void main()
{
clrscr();
int num,a,r=0,b;
printf("Enter a no=");
scanf("%d",&num);
while(num!=0)
{
b=num%10;
r=r*10+b;
num=num/10;
}
printf("The reverse number is = %d",r);
getch();
}
Output:Enter a number = 123
The reverse number is = 321
Solution:Step 1. Enter the number =123
Step 2. Check the condition is number is greater than zero or not
Step 3. If the number is greater than zero.
Step 4. So the value of b = num%10; Then the value of b = 123%10 then the
remainder is =3b = 123%10 then the remainder is =3 .So the value of b =3.

Step 5. Then the value of r= r*10+b So the value is giving condition r = 0*10+3 So
the value of r = 3.
Step 6. The value of num = num/10 So this condition the value of num = 123/10
=12. So that again the check condition is num greater than zero So this process again repeat So this
time the value of b= 12/%10 = 2 So the value of r = r*10+b or r= 3*10+2 = 32 So the value of num
num/10 so num 13/10= 1 So again condition is true So the value of b= 1%10 = 1 So the value of r =
32*10+1 =321 So the value of num =1/10=0 So the condition is false. So the print the reverse number
is = 321.
2. W.A.P to enter a number and print its sum.
#include<stdio.h>
#include<conio.h>
void main()
{
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
clrscr();
int num,a,r=0,b,sum=0;
printf("enter a no=");
scanf("%d",&num);
while(num!=0)
{
b=num%10;
r=r*10+b;
num =num/10;
sum= sum+b;
}
printf("The sum of number is = %d",sum);
getch();
}
Output:Enter the number = 1234
The sum of number = 10
Solution:Step 1. In this step we insert the value of number suppose the value of number =
1234.
Step 2. In this step we check the condition is num > zero if condition is true so b = 1234%10=4
So the value of sum = sum+b or sum = 0 and b= 4 so the sum = 0+4 =4. So condition is
repeat when num is not less than or equal to zero. So last time the value of sum = 1+2+3+4 =10 So the
print the ouput is the sum of digits = 10
The sum of the number = 1+2+3+4 = 10..

3. W.A.P to enter a number and check is palindrome or not.


(Hints Palindrome numbers are those number which are reverse is same.)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int num,d,b,r=0;
printf("enter a no=");
scanf("%d",&num);
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
d=num;
while(num!=0)
{
b=num%10;
r=r*10+b;
num =num/10;
}
printf("reverse is=%d",r);
if(d==r)
{
printf("number is palindrome");
}
else
printf("not palindrome");
getch();
}
Output:Enter a number = 121
Number is palindrome.
Solution:Palindrome number are those number which are revere of the number is same. For Example: the
number of 121 its reverse is = 121 So the number is palindrome.
In this program first of all we doing the process of reverse program and number is reverse so the
number is compare of when number which is enter so the both number are equal then print number is
palindrome otherwise print not palindrome.
4. W.A.P to enter a number and check is Armstrong or not.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int num,a,e,b,count=0,d,p,sum=0;
d=num;
printf("enter a no=");
scanf("%d",&num);
while(num!=0)
{
b=num%10;
count++;
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
num =num/10;
}
while(d>0)
{
e=d%10;
p=pow(e,count);
sum=sum+p;
d=d/10;
}
if(sum==num)
printf("number is armstrong
else
printf("not Armstrong);
getch();
}
Output:Enter a number = 153
Number is Armstrong.
Solution:Armstrong number are those number which are sum of power of all number is equal to
the enter number. For Example:- number is 153= (13+53+33= 1+125+27 = 153 So the number is
Armstrong.)
Step 1. In this step we put the logical which is find the count number in the digit and count the
digit.
Step 2. And In this step we use of the power function.
5. W.A.P to enter a number and check is prime or not.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num,i,count=0;
printf("Enter a number = ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
while(num%i==0)
{
count++;
i++;
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
}
}
if(count==2)
printf("Prime number");
else
printf("Not prime number");
getch();
}
Output:Enter a number = 7
Prime number
OR
Enter a number = 8
Not Prime number.
Solution:Prime numbers are those number which are divide are only one or itself. Example 7 is a
prime number because the number is dividing by only one or 7.
6. W.A.P to enter a number and print its factorial.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num,fact=1,i;
printf("enter a number = ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("the factroial of the number = %d",fact);
getch();
}
Output:Enter a number = 5
The factorial of the number = 120
Solution:Factorial means is multiply of the number to a sequence order from itself to the one.
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Example:Factorial of the number is 5 = 5*4*3*2*1 = 120 So this the ooutput.
7. W.A.P to enter a number and find out the total no of even number, odd number and zero.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x, odd=0,even=0,zero=0,r;
printf("enter number =");
scanf("%d",&x);
while(x!=0)
{
r=x%10;
if(r==0)
zero++;
else if(r%2==1)
odd++;
else
even++;
x=x/10;
}
printf("Total no of even no =%d\n",even);
printf("Total no of odd no =%d\n",odd);
printf("total no of zero = %d",zero);
getch();
}
Output:Enter a number = 1204
Total no of even no = 2
Total no of odd = 1
Total no of zero =1
8. W.A.P to print the following series.
1 11 111 ------------------------------------------------10 terms
#include<stdio.h>
#include<conio.h>
void main()
{
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
long int a=1,b;
clrscr();
printf("%ld\t",a);
for(b=1;b<10;b++)
{
a=a*10+1;
printf("%ld\t",a);
}
getch();
}
Output:1 11 111 1111 ------------------------------------------------1111111111
\t :- This command is used for as tabs or space.
9. W. A.P to find the roots of a quadratic equation ax2 + bx + c =0 for all possible combination
of a,b,c.
Solution:A quadratic equation will have two roots which are obtained using the following
expression X = -b + b2 - 4ac
2*a
When b2 4ac is called discriminant.
Note that b2 4ac >0, the roots are real and unequal.
b2- 4ac = 0 the roots are real and equal. X = -b
2*a
b2 -4ac< 0 the roots are imaginary.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int a,b,c,d;
float r,r1,r2;
printf("enter the value of a, b and c=");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
printf("The value of d = %d\n",d);
if(d>0)
{
r1= (-b+sqrt(d))/2*a;
r2 = (-b-sqrt(d))/2*a;
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
printf("The roots are real and unequal=%f\n%f",r1,r2);
}
else if(d==0)
{
r = -b/2a;
printf("roots are equal=%f",r);
}
else
printf(roots are imaginary);
getch();
}
Output:Enter the value of a, b and c= 1
6
5
The value of d = 16
The roots are real and unequal = -1.00000
-5.00000

10. W.A.P to print the following series.


0 1 1 2 3 5 8 ----------------------10 terms
#include<stdio.h>
#include<conio.h>
void main()
{
Int a=0,b=1,c,d;
clrscr();
printf(%d%d\t,a,b);
for(c=1;c<=10;c++)
{
d = a+b;
a=b;
b=d;
printf(%d\t,b);
}
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
getch();
}
Output:0 1 1 2 3 5 8 13 ------------------------------10 terms.

Break Statement :The break statement is used to transfer the control to the end of statement block
in loop.
Ex:#include<stdio.h>
#include<conio.h>
void main()
{
int a;
for(a=1;a<=10;a++)
{
if(a==5)
break;
printf(%d\n,a);
}
getch();
}

control is transferred to the end


of block / or out side the for loop.

Output :1
2
3
4

Continue Statement:The continue statement control the beginning of a statement block in a


loop.
Ex:#include<stdio.h>
#include<conio.h>
void main()
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
{
int a;
clrscr();
for(a=1;a<=5;a++)
{
if(a==3)
continue;
printf(%d\n,a);
}
getch();
}
Output :1
2
4
5
Solution:In this statement when the condition is true then the value is except and after the value is
print as show in above example.
Nested Loop :The loop can be nested according to programming requirement. In nested loop
we have taken more than one loop is used.
Nested For Loop:When a for loop is defined with in another for loop then it is called as nested for
loop. Outer loop will be execute first of all ,then inner loop will be execute desired number of times, as
maintained on condition of inner for loop.
Syntax:For( initialization; test condition-1; increment / decrement)
{
Outer loop starting block.
For( initialization; test condition-2; increment / decrement)
{
Inner loop starting block.
==============
block of statement
==============
}
close inner loop block.
}
close outer loop.

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Ex:W.A.P. to print the pattern.
1
12
123
1234
12345
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c;
for(r=1;r<=5;r++)
{
for(c=1;c<=r;c++)
{
printf(%d,c);
}
Printf(\n);
}
getch();
}
Output :1
12
123
1234
12345
Ex:W.A.P. to print the pattern .
1
23
456
78910
101112131415
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c,count=0;
for(r=1;r<=5;r++)
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
{
for(c=1;c<=r;c++)
{
count++;
printf(%d,count);
}
printf(\n);
}
getch();
}
Output :1
23
456
78910
1112131415
Ex:W.A.P. to print the pattern.
1
21
321
4321
54321
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c,space;
for(r=1;r<=5;r++)
{
for(space=5; space>=r; space--)
{
printf( );
}
for(c=r; c>=1; c--)
{
printf(%d,c);
}
printf(\n);
}
getch();
}
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Output :1
21
321
4321
54321
Nested While Loop:When a while loop is defined with in another while loop then it is called
as nested while loop. First of all outer loop will be execute, then within inner loop will be execute. It
has following syntax.

Syntax:Initialization-outer loop variable;


{
while(test condition-1)
Initialization-inner loop variable;
while(test condition-2)
==============
inner loop block of statement
==============
Increment / decrement
updating inner loop value.
}
close inner loop.
Increment / decrement
updating outer loop value.
}
Close Outer loop.
Ex:W.A.P. to print a table from 1 to 10 .
1234-----------------------------10
2468-----------------------------20
-------------------------------------------------------------------------10203040----------------------100
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c;
r=1;
while(r<=10)
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
{
c=1;
while(c<=10)
{
Printf( %d,r*c);
C++;
}
printf(\n);
r++
}
getch();
}

close inner loop.


close outer loop.

Ex:W.A.P. to print the following pattern.


*
**
***
****
*****
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c;
r=1;
while(r<=5)
{
c=1;
while(c<=r)
{
printf( *);
c++;
}
printf(\n);
r++
}
getch();
}
Output:*
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
**
***
****
*****
Ex:W.A.P. to print the following pattern.
1
22
333
4444
55555
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c;
r=1;
while(r<=5)
{
c=1;
while(c<=r)
{
printf( %d,r);
}
printf(\n);
}
getch();
}
Nested do While Loop:When a do-while loop is defined with in another do-while loop then it is
called as nested do-while loop.
Syntax:Initialization-outer loop variable;
do
{
Initialization-inner loop variable;
do
{
==============
inner loop block of statement
==============
Increment / decrement
updating inner loop.
}
close inner loop.
while(test condition-1)
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
Increment / decrement
}
close outer loop.
while(test condition-2)

updating outer loop.

Ex:W.A.P. to print the following pattern.


5
54
543
5432
54321
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c;
r=5;
do
{
c=5;
do
{
printf( %d,c);
c++
}
while(c<=r);
printf(\n);
r--;
}
while(r>=1);
getch();
}
Ex:W.A.P. to print the following pattern .
A
AB
ABC
ABCD
ABCDE
#include<stdio.h>
#include<conio.h>
void main()
#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.
Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Tecnosoft
{
char a,b;
a=A;
do
{
b=B;
do
{
printf( %c,b);
b++;
}
while(b<=a);
printf(\n);
A++;
}
while(a<=5);
getch();
}
else
{
printf(%d,c);
}
c++;
while(c<=r);
printf(\n);
r++;
}
while(r<=5);
getch();
}

#109,1st floor,Annapurna block,Aditya enclave,ameerpet,hyd-38.


Ph:9966422225,Email:tecnosoft4u@gmail.com,visit us:www.tecnosoft.in

Array
Array:An array is a group of same data type element when able we need you store many
data in a same time in different position.
Types of Array:Array is of two types1. Single or one dimensional array.
2. Two or multi dimensional array.
Single dimensional array:An array that have one variable and one subscript then that
array is known as one or single dimensional array.
Syntax:Data type variable name [size];
Data type: any data type is used in c language.
Variable name (v1):- any name of the variable.
[size]:it represent the size of array.
Declaration of Array:Example :Int n[5]
Int :- int is data type in c language.
n:- n is the variable.
[5]:- the size of the array is 5 block.
n[0]

n[1]

n[2]

n[3]

n[4]

Here n is collection of 5 integer elements first element is n[0], second element is n[1], third
element is n[2], forth element is n[3] and last or 5th element is n[4]
Note:The index of an array has always started from zero. If an array has n elements
then first element will be zero elements and last element will be n-1.
In an integer array each element will consume 2 byte of memory so the above array is
consuming 10 byte.
In a float array each element will consume 4 byte of memory.

Initialization of an array :-

An array can be initialized in two types.


1. Compile time Initialization.
2. Run time Initialization.
Compile time Initialization:Syntax:Data type variable name [size]={list of elements}
Example:-

Ex:-

int n[5] ={1,2,3,4,5};


1
2
3
4
5

N[0]

N[1]
N[2]
N[3]
N[4]

W.A.P. to print the ten value .


#include<stdio.h>
#include<conio.h>
void main()
{
int n[ ]={1,2,5,8,10,15,7,18,19,50};
int i;
for(i=0;i<=9;i++)
{
Printf(the value of array %d,n[i]);
}
getch();
}
Output :The value of an array,2,5,8,10,15,7,18,19,50.
Ex:W.A.P. to add any two tables .
x[ ]
y[ ]
10
1
5
+
2
=
2
3
7
4
#include<stdio.h>
#include<conio.h>
void main()
{
int x[ ]={10,5,2,7};

t[ ]
11
7
5
11

int y[ ]={1,2,3,4};
int t[4];k
for(k=0;k<4;k++)
{
t[k]=x[k]+y[k]
Printf( %d\n,t[k]);
}
getch();
}
Output :11
7
5
11
Run time Initialization:In thisInitialization the value are through keyboard at the
run time.
Syntax:type a1[s1]variable name[size]an[sn]
type
:- represent any valid data type such as int, float, char etc.
a1 a2 a3. an: -these represent any name of array.
s1 s2 s3. sn: - these represent the size of array.
Ex:W.A.P. to enter 10 number and display them.
#include<stdio.h>
#include<conio.h>
void main()
{
int n[10],i;
printf(enter 10 numbers)
for(i=0;i<9;i++)
{
Scanf(%d,&n[i]);
}
Printf( output array);
for(i=0;i<9;i++)
{
printf(%d,n[i]);
}
getch();
}
Output :3
6

7
8
9
15
5
7
8
2
Output array =3,6,7,8,9,15,5,7,8,2.
Solution :-

in this above example the first loop is used to enter the value and
second loop is used to show the given value.

Ex:W.A.P. to enter 5 numbers and print its sum.


#include<stdio.h>
#include<conio.h>
void main()
{
int n[5],i,sum=0;
for(i=0;i<=4;i++)
{
printf(enter values)
Scanf(%d,&n[i]);
}
printf( elements of array=);
for(i=0;i<=4;i++)
{
sum=sum+n[i];
printf(%d,n[i]);
}
printf(the sum of array is =%d,sum)
getch();
}
Output :Enter the value =
3
Enter the value =
6
Enter the value =
7
Enter the value =
8
Enter the value =
12
Element of array =3, 6, 7,8,12.
The sum of array is = 36

Ex:W.A.P. to enter 5 numbers and find the largest number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n[5],I,high=0;
for(i=0;i<=4;i++)
{
printf(enter values)
Scanf(%d,&n[i]);
}
Printf( largest element =);
for(i=0;i<=4;i++)
{
If(high<n[i])
{
High=n[i];
}
}
printf(%d,high);
getch();
}
Output :Enter the value =
Enter the value =
Enter the value =
Enter the value =
Enter the value =
The largest element is =55.

15
5
3
55
40

Ex:W.A.P. to enter 5 numbers and arrange in descending order.


#include<stdio.h>
#include<conio.h>
void main()
{
int n[5],I,j,temp;
for(i=0;i<=4;i++)
{
printf(enter values)
scanf(%d,&n[i]);
}
\\ sorting process
for(i=0;i<=4;i++)
{
for(j=0;j<=4;j++)
{
If(n[j]>n[i])

{
temp=n[i];
n[i]=n[j];
n[j]=temp
}
}
}
printf(display of elements after sorting);
for(i=0;i<=4;i++)
{
printf(%d,n[i]);
}
getch();
}
Output :Enter the value =
6
Enter the value =
8
Enter the value =
4
Enter the value =
7
Enter the value =
2
Display of elements after sorting =8 7 6 4 2.
W.A.P to enter the 5 numbers and check how many numbers are positive and negative.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,pos=0,neg=0;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter the value of array");
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
if(a>0)
pos++;
else
neg++;
}
printf("Total number of postive =%d",pos);
printf("Total number of negative = %d",neg);
getch();
}
Output:-

Enter the value of array = 12


Enter the value of array = 23
Enter the value of array = 34
Enter the value of array = -38
Enter the value of array = 28
Total number of positive = 4
Total number of negative = 1
Two Dimensional Array:Two Dimensional Array is also called the matrix. In Two
Dimensional Array have two sub subscripts. It is indicated by two indexes. First index or
subscript is represent the number of rows and second subscript represents the number of
column.
Declaration of
Array:-

Two Dimensional

Syntax:type arr1[rd1][cd1],arr2[rd2][cd2]..arrn[rdn][cdn]
Where type represent any valid data type in c such as int, float, char etc.
arr1,arr2..arrn these are represent the name of an array.
rd1,cd2.cdn
represent the size of row.
cd1cd2..cdn
represent the size of column.
Example:Int x[5][6]
X is the name of the array whose size of row is 5 and size of column is 6.
Initialization of an array :An array has initialized in two types.
1. Compile Time Initialization
2. Run time Initialization.
Compile time Initialization:Int x[2[3]={5,6,7,2,4,3};
OR
Int x[2][3]={{5,6,7},{2,4,3}};
Initialized the element of first row to 5,6,7 and second row to 2,4,3 these value are store in
computer memory
0
1
2
0
1

Here x is a collection of 6 integer value. First value is x[0][0]=5, second value is x[0][1]=6,
third value is c[0][2]=7, forth value is x[1][1]=4, fifth value is x[1][1]=3, and sixth value is
x[1][2]=2.
X[0][0]
X[0][1]
X[0][2]
X[1][0]
X[1][1]
X[2][2]

represent the first row and first column.


represent the first row and second column.
represent the first row and third column.
represent the second row and first column.
represent the second row and second column.
represent the second row and third column.

Ex:W.A.P. to print a 2*3 matrix.


#include<stdio.h>
#include<conio.h>
void main()
{
int x[2][3]={5,6,7,2,4,3};
int r,c;
for(r=0;r<2;r++)
{
for(c=0;c<3;c++)
Printf(output matrix %d \t,x[r][c]);
}
Printf(\n);
}
getch();
}
Output :Output matrix :-

67
2

Run time Initialization:In thisInitialization the value are enter through the key
board at run time.
Syntax:Type arr1[r][c],arr2[r][c]..arrn[r][c]
Where type represent any valid data type in c such as int, float, char etc.
arr1,arr2..arrn
these are represent the name of an array.
[r]
represent the size of row.
[c]
represent the size of column.
W.A.P. to print a matrix which 2 rows and 3 column and display them.
#include<stdio.h>
#include<conio.h>

void main()
{
int a[2][3],r,c;
for(r=0;r<2;r++)
{
for(c=0;c<3;c++)
{
printf("enter value");
scanf("%d",&a[r][c]);
}
}
for(r=0;r<2;r++)
{
for(c=0;c<3;c++)
{
printf("%d\t",a[r][c]);
}
printf("\n");
}
getch();
}
Output :Enter value = 4
Enter value = 5
Enter value = 6
Enter value = 3
Enter value = 7
Enter value = 12
4
3

5
7

6
12

W.A.P. to find the sum of two 2*2 matrix.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],d[2][2],r,c;
\* input value in first matrix *\
printf("enter the value of matrix\n");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
printf("enter value of a=");
scanf("%d",&a[r][c]);
}
}

\* input value in second matrix *\


printf("enter the value of sec matrix\n");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
printf("enter value of b=");
scanf("%d",&b[r][c]);
}
}
\* printing of first matrix *\
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
printf("%d",a[r][c]);
}
printf("\n");
}
\* printing of second matrix *\
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
printf("%d",b[r][c]);
}
\* a addition of two matrix in third matrix and display result *\
printf("result after addition of two matrix=\n");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
d[r][c]=a[r][c]+b[r][c];
printf("%d\t",d[r][c]);
}
printf("\n");
}
printf("\n");
}
getch();
}
Output :Enter value in first matrix
Enter value of a = 4
Enter value of a = 5
Enter value of a = 6
Enter value of a = 8

Enter value in second matrix


Enter value of a = 5
Enter value of a = 3
Enter value of a = 7
Enter value of a = 8
4
5
5
3
6
8
7
8
Result after addition of two matrix.
9
8
13
16
W.A.P. to find the product of two matrixes whose first matrix is 2*3 and second matrix
is 3*2.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][3]={1,2,3,4,5,6};
int b[3][2]={1,2,3,4,5,6};
int d[2][2],r,c,k;
clrscr();
printf("enter the value of matrix\n");
for(r=0;r<2;r++)
{
for(k=0;k<3;k++)
{
d[r][k]=0;
for(c=0;c<3;c++)
{
d[r][k]=d[r][k]+a[r][c]*b[c][k];
}
}
}
printf("output matrx \n");
printf("enter the value of sec matrix\n");
for(r=0;r<2;r++)
{
for(k=0;k<2;k++)
{
printf("%d",d[r][k]);
}
printf("\n");
}
getch();
}

Output :-

Output ofmatrix
22
49

28
64

12
45

3
6

Solution :-

1
4

22
49

+ 6
+ 15

1
3

+ 15
+ 30

2
8

2
4
5

+ 8
+ 20

+ 18
+ 36

28
64

Ex:W.A.P. to print a square matrix converts in to unit matrix.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],r,c;
\* input value in matrix *\
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
printf("enter the value of a=");
scanf("%d",&a[r][c]);
}
}
\* output value of matrix *\
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
printf("%d \t",a[r][c]);
}
printf("\n");
}
\* process for convert square matrix to unit matrix *\
for(r=0;r<3;r++)

{
for(c=0;c<3;c++)
{
if(r!=c)
{
a[r][c]=1;
printf("%d",a[r][c]);
}
else
{
a[r][c]=0;
printf("%d",a[r][c]);
}
}
printf("\n");
}
getch();
}
Output :enter the value of a
enter the value of a
enter the value of a
enter the value of a
enter the value of a
enter the value of a
enter the value of a
enter the value of a
enter the value of a
12
10
6

15
5
4

18
7
3

1
0
0

0
1
0

0
0
1

=
=
=
=
=
=
=
=
=

12
15
18
10
5
7
6
4
3

POINTER
Pointer:Pointer is a special variable which represent a storage location in memory. A
pointer variable is a variable that contain the address of another variable.

Remember that the Ram has many cells to store value. Each cell in memory is
1byte and has a unique address to identify it. The memory address is always an unsigned
integer.
Example :- 65522,6640 etc.
In pointer use of two types of operator.
1. Indirection operator
2. Address operator
Indirection Operator:This operator is denoted by * symbol and this symbol is called asterisk
( * ). Which is use to access the value of a variable through a keyboard.
Address operator (&):The symbol & (ampersand) is an address operator which is used to access the
address of a variable and assign it to pointer to initialize it.
Example:Int
a = 10
Int
*p
pointer integer
p = &a; // assign the address of a into p variable
ap
10
Value
6442
value
6442
Pointer declaration:-

address

9246

Address

The pointer variable in the declaration is preceded by the


asterisk (*) symbol.
Syntax :type *v1,*v2..*vn.
Where type represent any valid data type in c such as int, float, char etc.
*v1,*v2..*vn represent the pointer value .

Ex:W.A.P. to print the value and its address of the variable.


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

int a;
int *p;
printf("Enter the value of a=");
scanf("%d",&a);
p=&a;
printf("%d",*p);
printf("the address of a =%u",&p);
getch();
}
Output :Enter the value of a = 5
5
The address value of a = 65522.
Ex:W.A.P. to find the sum of two values using pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,t;
int *p1,*p2;
printf("enter any two values=");
scanf("%d%d",&a,&b);
p1=&a;
p2=&b;
t=*p1+*p2;
printf("the total of the two values = %d",t);
getch();
}
Output :enter any two values = 15
10
Total of two values = 25.
Ex:W.A.P. to find highest value of any given two value using pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,h;
int *p1,*p2;
printf("enter any two values=");
scanf("%d%d",&a,&b);
p1=&a;
p2=&b;

if(*p1>*p2)
{
h=*p1;
}
else
h=*p2;
printf("the highest values = %d",h);
getch();
}
Output :enter any two values = 25
20
The highest value = 25.
Ex:W.A.P. in c to swap any two given value using pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int *p1,*p2,*p3;
printf("Enter any two values=");
scanf("%d%d",&a,&b);
p1=&a;
p2=&b;
p3=&c;
\\ swapping process
*p3=*p1;
*p1=*p2;
*p2=*p3;
printf("the first value after swapping = %d",a);
printf("the second value after swapping = %d",b);
getch();
}
Output :Enter any two values = 15
12
The first value after swapping = 12.
The second value after swapping = 15.
Ex:W.A.P. in c to convert a farnehite to Celsius temperature using pointer.
#include<stdio.h>
#include<conio.h>
void main()

{
int f;
float c;
int *p1;
printf("enter the temperature farnihite");
scanf("%d",&f);
p1=&f;
c=(*p1-32)/18;
printf("convertedtemperaturin celcius =%f",c);
getch();
}
Output :Enter the temperature in far. = 86
Converted temperature in Celsius = 3.
Pointer and Array
The way there can be an array of ints or an array of floats similarly, there can be an array of
pointers.
Ex:W.A.P to enter the 5 numbers and print your sum of numbers with the help of pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
int *p,sum=0;
clrscr();
for(i=0;i<5;i++)
{
printf("enter the value of array");
scanf("%d",&a[i]);
}
p =&a[0];
for(i=0;i<5;i++)
{
sum =sum+*p;
printf("using pointer in array=%d\n",*p);
p++;
}
printf("the sum of array= %d",sum);
getch();
}
Output:Enter the value of array = 1
Enter the value of array = 2
Enter the value of array = 3

Enter the value of array = 4


Enter the value of array = 5
Using pointer in array = 1
Using pointer in array = 2
Using pointer in array = 3
Using pointer in array = 4
Using pointer in array = 5
The sum of array
= 14
W.A.P to print a 3*4 matrix using pointer
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[3][4]= {10,11,12,13,20,21,22,23,30,31,32,33};
int i,j;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%5d ",*(*(arr+i)+j));
}
printf("\n");
}
getch();
}
Output:10
20
30

11
21
31

12
22
32

13
23
33

Function
Function:Function is define as collection of statement which perform particular task.
Function facilitates division of large problem in smaller problem.
Ex:W.A.P. to print the addition of two values three times without function.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e,f,g,h,i=0;
printf("enter the value of a and b");
scanf("%d%d",&a,&b);
c=a+b;
printf("the sum is %d",c);
printf("enter the value of d and e=");
scanf("%d%d",&d,&e);
f=d+e;
printf("the sum is %d",f);
printf("enter the value of g and h");
scanf("%d%d",&g,&h);
i=g+h;
printf("the sum is %d",i);
getch();
}
Output :enter the value of a and b . = 15
10
The sum is = 25.
enter the value of d and e . = 25
15
The sum is = 40.
enter the value of g and h. = 30
50
The sum is = 80.

Ex:W.A.P. to print the addition of two values three times using function.
#include<stdio.h>
#include<conio.h>
void add();
void main()
{
add();
add();
add();
getch();
}
void add()
{
int a,b,c;

printf("enter the value of a and b");


scanf("%d%d",&a,&b);
c=a+b;
printf("the sum is %d",c);
}
Output :enter the value of a and b . = 15
10
The sum is = 25.
enter the value of a and b . = 25
15
The sum is = 40.
enter the value of a and b . = 30
50
The sum is = 80.
Need of function:Function is developed due to the need of module programming where each task
of a developed software are divided into different module.
Use of function:1. Functions facilitate handling of a complex task easily.
2. A lot of predefined function in c language provide facility of faster
development of software. These function are defined in header file.
3. Function make designing and maintain of program easily.
4. Functions are very much useful when a block of statement has to be written
/ executed again and again.
5. Function are also used to reduce the difficulties during debugging a
program
Type of function:There are main two type of function.
1. User define function.
2 .Built in function or pre defined function.
User define function:These function are define by the user according to
programming requirement.
Built in or library or per define function:These functions are already define in c
language. Each built in function are already define in header file.
Example of built in function:Pow(), sqrt() and string related function such as strlen,
strcmp etc.each function contain pair of round brackets ().

Rules of built in function:1. All the built in or library function must be used in small letter.
2. Each library function must be used according to their syntax otherwise a
syntax error will be appeared.
3. Any keyword are built in function cannot be used as user define function or
word.
Argument:Argument is used to pass information between function and main program
these are also called parameters.
Return type:In this type function is want to return the answer to main program then we use
return type. The example of return type such as int, float etc.
User define function:These functions are declared by the user according to
programming requirement.
Elements of user define function:Each user define function has divided in three parts.
1. First part is called function declaration in this part we declare the name of
function.
2. Second part is called function calling. In this part we call the function.
3. Third part is called function definition. In this part we define the work are
doing in function.
Type of user defines function:1. No argument & no return value.
2. Some argument & no return value.
3. No argument & some return value.
4. Some argument & some return value.
No argument & no return value:In this type of function we cannot use of argument and return type.
Syntax:Void function( );
Declaration
Function name( );
Calling
Void function name( )
Definition
{
=======
=======
block of statement.
}
Note:If we want to call a function is more than one then the calling of function is
more than one.

Ex.
Function name( );
Calling
Function name( );
Calling
The process of declaration and definition is same.
Ex:W.A.P. to add of two values with using function.
#include<stdio.h>
#include<conio.h>
void add();
Function Declaration
void main()
{
add();
Function Calling
getch();
}
void add()
Function Definition
{
int a,b,c;
printf("enter the value of a and b");
scanf("%d%d",&a,&b);
c=a+b;
printf("the sum is %d",c);
}
Output :enter the value of a and b . = 15
10
The sum is = 25.
Ex:W.A.P. to calculate the simple interest no argument and no return value.
#include<stdio.h>
#include<conio.h>
void simple();
void main()
{
simple();
getch();
}
void simple()
{
int p,r,t;
float si;
printf("enter the value of p r and t");
scanf("%d%d%d",&p,&r,&t);
si=(p*r*t)/100;
printf("the simple interest is %f",si);
}

Output :enter the value of p,r and t . = 1000


10
3
The simple interest is = 300.
Ex:W.A.P. to calculate the area of circle for two times using no argument and
no return value
#include<stdio.h>
#include<conio.h>
void area();
void main()
{
area();
Function Calling
area();
Function Calling
getch();
}
void area()
{
int r;
float a,pi=3.14;
printf("enter the value of radius");
scanf("%d",&r);
a=pi*r*r;
printf("the area of circle is %f",a);
}
Output :enter the value of radius. = 7
The area of circle is = 153.86000.
enter the value of radius. = 10
The area of circle is = 314.

Types of argument:The arguments are of two types.


1. Actual Argument
2. Formal Argument
Actual Argument:The argument listed in the function calling statement is represented as actual
argument. These values of actual argument are copied in formal argument.

Ex:High(a,b); \\ calling to function.


Here a and b are actual argument.
Formal Argument:The argument used in the function definition some are represent as formal
argument.
Example:void high (int x, int y)
x and y are formal argument.
The value of x and y is equal to a and b.
2. Some argument & no return value:In this type of function we are used to
argument and there is not give any return vale.
Ex:W.A.P. to add two numbers with the help of some argument and no
return value.
#include<stdio.h>
#include<conio.h>
void add(int,int);
void main()
{
int a,b;
printf("enter the value of a and b");
scanf("%d%d",&a,&b);
add(a,b);
getch();
}
void add(int x,int y)
{
int c;
c=x+y;
printf("the sum is %d",c);
}
Output :enter the value of a and b . = 15
10
The sum is = 25.
Solution:In the above example the function is called through body of main function ().the
control will be transferred to body of add function. In body of add function first of all the
value of variable a and b will be copied to the formal argument x and y. then both will be
added over theses and result will be displayed in function.

Ex:W.A.P. to calculate the area of rectangle with the help of some argument and no return
value.
#include<stdio.h>
#include<conio.h>
void area(int,int);
void main()
{
int a,b;
printf("enter the value of a and b");
scanf("%d%d",&a,&b);
area(a,b);
getch();
}
void area(int x,int y)
{
int c;
c=x*y;
printf("the area of rectangle is %d",c);
}
Output :enter the value of a and b . = 15
20
The area of rectangle is = 300.
Ex:W.A.P. to find the highest value of any given two integers.
#include<stdio.h>
#include<conio.h>
void high(int,int);
void main()
{
int a,b;
printf("enter the value of a and b");
scanf("%d%d",&a,&b);
high(a,b);
getch();
}
void high(int x,int y)
{
if(x>y)
printf("x is greater than y");
else
printf("y is greater than x");
}
Output :-

enter the value of a and b . = 25


15
X is greater than y.
Ex:W.A.P. to calculate the area of square.
#include<stdio.h>
#include<conio.h>
void area(int);
void main()
{
int a;
printf("enter the value of a=");
scanf("%d",&a);
area(a);
getch();
}
void area(int x)
{
int y;
y=x*x;
printf("the area of square %d",y);
}
Output:enter the value of a. = 10
the area of square is. = 100
Ex:W.A.P. to add and subtract of two values.
#include<stdio.h>
#include<conio.h>
void add(int,int);
void subtract(int,int);
void main()
{
int a,b;
printf("enter the value of aand b=");
scanf("%d%d",&a,&b);
add(a,b);
subtract(a,b);
getch();
}
void add(int x,int y)
{
int z;
z=x+y;

printf("the addiction is =%d",z);


}
void subtract(int x,int y)
{
int z;
z=x-y;
printf("the subtract is =%d",z);
}
Output :enter the value of a and b . = 25
15
The addition is = 40
The subtract is = 10
No Argument & some return value:In this type of function we cannot use of
argument but use only return type.
Syntax:1. Declaration
type function ();
Variable type function name;
Ex:- . int function name();
2. Calling
var = funname ();
Here var is the variable of type that is the return type of the function.
3. Definition
Int functionname()
{
---------------------Statements
----------------------}

Ex:W.A.P. to add of two values.


#include<stdio.h>
#include<conio.h>
int add();
function declaration
return type
void main()

{
int c;
c=add();
\\function calling
printf("the addition is %d",c);
getch();
}
int add()
function definition
{
int a,b,d;
printf("enter the value of a and b=");
scanf("%d%d",&a,&b);
d=a+b;
return(d);
}
Output :enter the value of a and b . = 15
10
The addition is = 25
Solution:In above example function returning variable d. Return statement is used to
return variable to function from function is called. The value of d will be copied to c in main
function.
Ex:W.A.P. to add of three floating values.
#include<stdio.h>
#include<conio.h>
float add();
void main()
{
float d;
d=add();
printf("the addition is %f",d);
getch();
}
float add()
{
float a,b,c,e;
printf("enter the value of a b and c=");
scanf("%f%f%f",&a,&b,&c);
e=a+b+c;
return(e);
}
Output :enter the value of a b and c . = 15.56

10.75
12.87
The addition is = 39.180000

Some Argument and some return value:In this type of function we can use of some
argument and some return value.
Syntax:type function name (parameter list);
Ex:- Here type is represent to any data type which is used in c.
Int add(int,int)
Here int is a type and add () is a Functionname and (int,int) is argument orparameter.
W.A.P. to add of two values with the help of some argument and some return value.
#include<stdio.h>
#include<conio.h>
int add();
void main()
{
int a,b,c;
printf("enter the value of a and b=");
scanf("%d%d",&a,&b);
c=add(a,b);
printf("the addition is %d",c);
getch();
}
int add(int x,int y)
{
int d;
d=x+y;
return(d);
}

Output :enter the value of a and b = 15


20
The addition is = 35
Solution:In above example function returning variable d. Return statement is used to
return variable to function from function is called. The value of d will be copied to c in main
function.

Ex:W.A.P. to find the factorial of the given number.


#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
int a,fact;
printf("enter the value of a =");
scanf("%d",&a);
fact=factorial(a);
printf("the factorial is %d",fact);
getch();
}
int factorial(int x)
{
int b,c=1;
for(b=1;b<=x;b++)
{
c=c*b;
}
return(c);
}
Output :enter the value of a = 5
The factorial is = 120
Ex:W.A.P. to find the reverse of a number.
#include<stdio.h>
#include<conio.h>
int reverse(int a);
void main()
{
int a,d;
printf("enter the number =");
scanf("%d",&a);
d=reverse(a);
printf("the reverse is %d",d);
getch();
}
int reverse(int x)
{
int b=0,r;
while(x!=0)
{
r=x%10;
b=b*10+r;
x=x/10;

}
return(b);
}
Output :enter the number = 315
The reverse is = 513
Ex:W.A.P. to find the number of digits in a given number.
#include<stdio.h>
#include<conio.h>
int sum(int);
void main()
{
int a,b;
printf("enter the number = ");
scanf("%d",&a);
b=sum(a);
printf("the number of digits =%d",b);
getch();
}
int sum(int x)
{
int r,count=0;
while(x!=0)
{
r=x%10;
count++;
x=x/10;
}
return(count);
}

Output :enter the number = 1234


The no. of digits = 4
Ex:-***
W.A.P. to print Fibonacci series.
0 1 2 3 5. 10 terms.
#include<stdio.h>
#include<conio.h>
void fibonacci(int,int);
void main()
{

int a=0,b=1;
clrscr();
printf("%d\t%d\t",a,b);
fibonacci(a,b);
getch();
}
void fibonacci(int x,int y)
{
int sum,d;
for(d=1;d<=10;d++)
{
sum = x+y;
x=y;
y=sum;
printf("%d\t",y);
}
}
Output :0 1 2 3 5 8 13 21 34 55 89
Draw bocks of return statement:Return cannot return more than one variable.
Ex:Return(a,b)

this is an invalid statement.

Ex:W.A.P. to addition, subtraction, multiplication and division of some argument and no


return value using function.
#include<stdio.h>
#include<conio.h>
void add(int,int);
void sub(int,int);
void mul(int,int);
void div(int,int);
void main()
{
int a,b;
printf("enter the value of a and b=");
scanf("%d%d",&a,&b);
add(a,b);
sub(a,b);
mul(a,b);
div(a,b);
getch();
}
void add(int x,int y)
{

int z;
z=x+y;
printf("the addition is =%d",z);
}
void sub(int x,int y)
{
int z;
z=x-y;
printf("the subtraction is =%d",z);
}
void mul(int x,int y)
{
int z;
z=x*y;
printf("the multiplication is =%d",z);
}
void div(int x,int y)
{
int z;
z=x/y;
printf("the division is =%d",z);
}
Output:Enter the value of a and b =15
5
The addition is = 20
The subtraction is = 10
The multiplication is = 75
The division is = 3
Types of Variable scopes:There are of two types of variable.
1. Local Variable
2. Global Variable
Local or Internal Variables:Variables declared inside the body of a function or main
program are referred as local variables. These variables are active in the block in which they
are declared.
Ex:W.A.P to add of two values.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b,c;
printf(enter the value of a and b=);

scanf(%d%d,&a,&b);
c = a+b;
printf(the sum is = %d,c);
getch();
}
Output:Enter the value of a and b = 12
5
the sum is = 17.
Here a,b,and c are called local variables because which are defined in a main function.
Global Variables or Public Variables:Variables declared outside the body of the main program are referred as global variables
Ex:W.A.P to add and subtract of two values using global variables.
#include<stdio.h>
#include<conio.h>
int a,b;
void add();
void sub();
void main()
{
add();
sub();
getch();
}
void add()
{
int c;
printf("Enter the value of a and b=");
scanf("%d%d",&a,&b);
c=a+b;
printf("The sum is = %d",c);
}
void sub()
{
int d;
d= a-b;
printf("The subtract is = %d",d);
}
Output:Enter the value of a and b = 25
15
The sum is = 40
The subtract is = 10

Here a and b are global variable. Because these variables are defined in outside the main
function and use of these variables are in each function and c and d are local variables
which are define in a function and use only this function.
Calling types of function:A function calling is two types
1. Call by value
2. Call by reference
Call by value:When function is called by passing value then it is called call by value.
In call by value actual argument are send to function its value is copied in formal argument. If
there is change in formal argument in function it will not be effect in actual argument.
Ex:#include<stdio.h>
#include<conio.h>
void total(int,int);
void main()
{
int x=10,y=75;
printf("befor function call x=%d,y=%d",x,y);
total(x,y);
printf("after function call x=%d,y=%d",x,y);
getch();
}
void total(int a,int b)
{
a=a+1;
b=b+1;
printf("inside the function a=%d,b=%d",a,b);
}
Output :befor function call x=10,y =75
inside function call x=11,y =76
after function call x=10,y =75
Solution:in above example the value of change x and y in formal argument then its not
effect on actual argument.
Ex:W.A.P. to swap any two value using call by value.
#include<stdio.h>
#include<conio.h>
void swap(int,int);

void main()
{
int a,b;
printf("enter a and b");
scanf("%d%d",&a,&b);
swap(a,b);
printf("value in main after swapping=%d%d",a,b);
getch();
}
void swap(int x,int y)
{
int z;
z=x;
x=y;
y=z;
printf("value in function after swapping=%d%d",x,y);
}
Output :Enter a and b=6
7
value in function after swapping=7
6
value in main after swapping=6
7
Solution:in above example the value of a and b is changed but there is no effect in main
function.
Ex:W.A.P. to calculate the simple interest.
#include<stdio.h>
#include<conio.h>
void simple(int,int,int);
void main()
{
int p,r,t;
printf("enter principal, rate and time");
scanf("%d%d%d",&p,&r,&t);
simple(p,r,t);
getch();
}
void simple(int x,int y,int z)
{
int s;
s=(x*y*z)/100;
printf("the simple interest is %d",s);
}

Output :Enter the principal rate and time=1000


10
3
The simple interest is = 300
Call by reference:In call by reference address of actual argument is sent to function. The
address is sent in function call using & (ampersand) or address operator. The address of
actual argument is received by pointer. In function whenever there is change it will be same
in main function as well as in function.
Ex:#include<stdio.h>
#include<conio.h>
void total(int*,int*);
void main()
{
int x=10,y=75;
printf("befor function call x=%d,y=%d",x,y);
total(&x,&y);
printf("after function call x=%d,y=%d",x,y);
getch();
}
void total(int*x1,int*y1)
{
*x1=*x1+1;
*y1=*y1+1;
printf("inside the function x1=%d,y1=%d",*x1,*y1);
}
Output :befor function call x= 10, y= 75
inside function call x= 11, y= 76
after function call x= 11,y= 75
Ex:W.A.P. to swap any two value using call by reference.
#include<stdio.h>
#include<conio.h>
void swap(int*,int*);
void main()
{
int a,b;
printf("enter a an d=");
scanf("%d%d",&a,&b);
swap(&a,&b);

printf("value in main after swapping=%d%d",a,b);


getch();
}
void swap(int*x,int*y)
{
int z;
z=*x;
*x=*y;
*y=z;
printf("the value in function after swapping%d%d",*x,*y);
}
Output :Enter a and b=5
6
value in function after swapping=6
5
value in main after swapping=6
5
Ex:W.A.P. to calculate the simple interest through call by reference.
#include<stdio.h>
#include<conio.h>
void simple(int*,int*,int*);
void main()
{
int p,r,t;
printf("enter p,r and t=");
scanf("%d%d%d",&p,&r,&t);
simple(&p,&r,&t);
getch();
}
void simple(int*x,int*y,int*z)
{
int s;
s=(*x**y**z)/100;
printf("the simple interest is %d",s);
}
Output :Enter the p, r and t=1000
10
3
The simple interest is = 300
Ex:W.A.P. to find whether the given number is palindrome or not.

#include<stdio.h>
#include<conio.h>
void poly(int*);
void main()
{
int n;
printf("enter the number=");
scanf("%d",&n);
poly(&n);
getch();
}
void poly(int*x)
{
int r,b=0,d;
d=*x;
while(*x!=0)
{
r=*x%10;
b=b*10+r;
*x=*x/10;
}
if(b==d)
printf("the number is polindrome");
else
printf("the number is not polindrome");
}
Output :Enter the number=121
the number is palindrome.
Enter the number=153
the number is not palindrome.
Palindrome :These number which reverse is same.
Examples:- The number is 121
And its reverse is 121
So the number is palindrome.
W.A.P. to find the sum of all digits of a given number using function.
#include<stdio.h>
#include<conio.h>
void sum(int*);
void main()
{
int n;
printf("enter the number=");
scanf("%d",&n);
sum(&n);

getch();
}
void sum(int*x)
{
int r,sum=0;
while(*x!=0)
{
r=*x%10;
*x=*x/10;
sum=sum+r;
}
printf("the sum of digits= %d",sum);
}
Output :Enter the number=516
Sumof digits = 12
W.A.P. to calculate the compound interest call by reference.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void compound(float*,float*,float*);
void main()
{
float p,r,t;
printf("enter the p, rate and time=");
scanf("%f%f%f",&p,&r,&t);
compound(&p,&r,&t);
getch();
}
void compound(float*x,float*y,float*z)
{
float c;
c=(*x*pow((1+*y/100),*z)-*x);
printf("the compound interest =%f",c);
}
Output :Enter the p, rate and time=1000
5
3
The compound interest is = 300
W.A.P. to enter a number and check is prime number or not using call by reference.
#include<stdio.h>
#include<conio.h>
void prime(int*);
void main()

{
int n;
printf("enter the number=");
scanf("%d",&n);
prime(&n);
getch();
}
void prime(int*x)
{
int i=1,count=0;
while(i<=*x)
{
if(*x%i==0);
{
count++;
}
i++;
}
if(count==2)
printf("the number is prime");
else
printf("the number is not prime");
}
Output:Enter the number=11
The number is prime.
Enter the number=15
The number is not prime.
Prime Number:Those numbers which are divide by one and itself.
the number is = 11
11 are dividing only one and itself.

Difference between call by value and call by reference


Call by value
Call by reference
1. This is the usual method to call a
1. In this method, the address of the
function in which only the value of
variable is passed as an argument.
variable is passed as argument.
2. Any change in the value of the
2. Any change in the value of the
argument passed is accepted in calling
argument passed is local to the
the program since change is a made
function and is not accepted in the
indirectly in the memory location
calling program.
using the pointer.
3. Memory location occupied by formal
3. Memory location occupied by formal
and actual argument is different.
and actual argument is same.
4. Since a new location is created, this
4. Since the existing memory location is

method is slow.
5. There is no possibility of wrong data
manipulation since the argument are
directly used in an expression.

used through its address this method


is fast.
5. There is possibility of wrong data
manipulation since the address are
used is an expression.

Recursion:A function calling itself again and again to compute a value is referred to
recursive function or recursion. Normally a function is called by the main program or by
some other function but in recursion the same function is called by itself repeatedly.
Ex:W.A.P. to calculate the factorial of a number.
#include<stdio.h>
#include<conio.h>
int factroial(int);
void main()
{
int a,f;
printf("enter the number=");
scanf("%d",&a);
f=factroial(a);
printf("factorial is =%d",f);
getch();
}
int factroial(int n)
{
int fact;
if(n==1)
{
return(1);
}
else
fact=n*factorial(n-1);
return(fact);
}
Output:Enter the number=5
The factorial is = 120
Solution:When this function is called the value of a is supplied by the main program to
find its factorial. Let n=5 then the sequence of the function call by itself is shown blow to
return the result.
After the first call which is from the main program

Fact=5*factorial(5-1) call the function return (fact) ; with n=4 after the second call which is
from the function fact=5*4*functional(4-1) call by function n=3
After third call return (5*4*3* functional(3-1)) call the function n=2.
After forth call return (5*4*3*2functional(2-1) call the function n=1.
After fifth call return (5*4*3*2*1);/* the result 120 is returned.
This will return the result to the main program and the repeated execution of the function is
terminated.
Use of recursion function:1. Recursion function are written with less number of statement compared
function.
2. Recursion is effective where terms are generated successively to compute a
value
3. Recursive is useful for branching processor recursive help to create short
code that would otherwise be impossible.
String
String:A string is a group of characters of any length. A string enclosed with double
quotation marks is known as a literal.
Example:Hello is a literal. The String can be stored the data in the form of array of
character in C language. The last character in a string is always \o, a Null character with
ASCII value equal to zero.
The string computer can store in an array as shown below.
C

\O

Declaration and Initialization of String:C does not support string as a data type
however it allows to represent string as character array.
Initialization of a string is divide into two parts:1. Compile time initialization
2. Run time initialization
Compile Time Initialization:Syntax:char n[] = {computer};
char:- It is a data type.
n :It is a name of variable.
[ ] :IT is represent to the size of string.

W.A.P to print your name with the help of String.


#include<stdio.h>
#include<conio.h>
void main()
{
char n[] = {Pawan};
printf(your name is = %s, n);
getch();
}
Output:Your name is = Pawan.
Run Time Initialization:In this initialization the value are enter through the keyboard at run time.
Syntax:char String name[ Size]
Example:char n[10];
Char:- It is a data type and in String only support the character data type .
n :- It is the name of the String.
[10] :-: The size of the string is 10
W.A.P to enter your name through the keyboard using String.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char n[10] ;
printf("Enter your name=");
scanf("%s",&n);
printf("your name is = %s", n);
getch();
}
Output:Enter your name = Pawan
Your name is
= Pawan
String Handling Function in C:There are seven important string handling function in
C language. Which is Strlen(), Strcpy(), Strcat(), Strrev(), Strcmp(), Strupr(), Strlwr().
Note:-

When we use a string function then we used a suitable Header file which
name is #include<string .h>

Strlen():This function is used to find the length of character String.


Syntax:L = strlen(s);
Where L is a integer data type which is find the length of the String.
Where S represent String constant or String Variable. String Variable is also called Character
Array.
W.A.P in C to find the length of a String.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char n[10] ;
int L;
printf("Enter your String=");
scanf("%s",&n);
L=strlen(n);
printf("The length of String= %d", L);
getch();
}
Output:Enter your string = Computer
Length of your string = 8.
Strcpy():This function is used to copy a string to another String variable.
Syntax:strcpy(S2,S1);
Where S1 represent source String variable.
Where S2 represent target string variable.
W.A.P to copy a string from another string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char S1[] = {"Computer"};
char S2[10];

strcpy(S2,S1);
printf("%s",S2);
getch();
}
Output:Computer
Strcmp():This function is used to compare two character Strings. It returns a zero when
two strings are equal otherwise it returns a numerical value which is the difference in the
ASCII value of the first mismatching of the string being compared.
Syntax:strcmp(S1,S2);
WhereS1 and S2 are two Strings.
W.A.P in C to compare any two given String.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char S1[] = {"Computer"};
char S2[] = {"computer"};
if(strcmp(S1,S2)==0)
printf("Both Strings are equal");
else
printf("Both Strings are not equal");
getch();
}
Output:Both Strings are not equal.
Solution:The value of C in ASCII code is = 67.
The value of c in ACII code is = 97
So both string are not equal..
W.A.P ito compare any two given String and show output Both Sting are equal.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char S1[] = {"Computer"};

char S2[] = {"Computer"};


if(strcmp(S1,S2)==0)
printf("Both Strings are equal");
else
printf("Both Strings are not equal");
getch();
}
Output:Both Strings are equal.
Solution:The value of C in ASCII code is = 67.
The value of C in ASCII code is = 67.
The firs letter of the both string is equal in ASCII code. So the both Strings are equal.
W.A.P to swap any two given Strings.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char S1[] = {"Software"};
char S2[] = {"Hardware"};
char temp[10];
printf("The value of strings s1 and S2= %s\n%s\n",S1,S2);
strcpy(temp,S1);
strcpy(S1,S2);
strcpy(S2,temp);
printf("After swapping the value of strings S1 and S2=%s\n%s",S1,S2);
getch();
}
Output:The value of string S1 and S2 = Software
Hardware
After swapping the value of string S1 and S2 = Hardware
Software
Strcat():This function is used to add of two strings.
Syntax:strcat(S1,S2);
Where S1and S2 are two Strings.
W.A.P in C to add of two Strings.
#include<stdio.h>
#include<conio.h>

#include<string.h>
void main()
{
clrscr();
char S1[] = {"New"};
char S2[] = {"Delhi"};
strcat(S1,S2);
printf("The addition of two Strings= %s",S1);
getch();
}
Output:The addition of two strings= NewDelhi.
W.A.P in C to add of two strings in reverse order.
(Hints:-The last digit is first and first digit is add on last position.)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char S1[] = {"New"};
char S2[] = {"Delhi"};
strcat(S2,S1);
printf("The addition of two Strings in reverse order= %s",S2);
getch();
}
Output:The addition of two Strings in reverse order = DelhiNew
Strupr():This function is used to convert the lower case letter into the upper case letter.
Syntax:strupr(S);
Where S is represent the String.
W.A.P to convert all alphabets in a string to upper case letter.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char S[] = {"rewari"};

strupr(S);
printf("String after convert in upper case letter=%s",S);
getch();
}
Output:String after convert in upper case letter = Rewari
Strlwr():This function is used to convert all alphabets in a string to lower case letters.
Syntax:strlwr(S);
Where S is represent the String.
W.A.P to convert all alphabets into lowercase letter.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char S[] = {"REWARI"};
strlwr(S);
printf("String after convert in lower case letter=%s",S);
getch();
}
Output:Sting after convert in lower case letter = rewari
Strrev():This function is used to reverse the string
Syntax:strrev(S);
Where S is represent the String.
W.A.P to print the reveres of the String.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{

clrscr();
char S[] = {"Rewari"};
strrev(S);
printf("After reverse the String is=%s",S);
getch();
}
Output:After reverse the String is = iraweR
W.A.P in C to rearrange any ten names in alphabetical order using case sensitive
matching.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char x[10][25] =
{"Pawan","Amit","Sonu","Narender","Monu","Mehesh","Umed","Sandeep","Neeraj","Sumit
"};
char y[25];
int k,j;
printf("List of student\n");
for(k=0;k<10;k++)
{
printf("%s\n",x[k]);
}
// Sorting Process
for(k=0;k<10;k++)
{
for(j=k+1;j<10;j++)
{
if(strcmp(x[k],x[j])>0)
{
strcpy(y,x[k]);
strcpy(x[k],x[j]);
strcpy(x[k],y);
}
}
}
printf("Alphabetical proces of names \n");
for(k=0;k<10;k++)
printf("%s\n",x[k]);
getch();
}
Output:-

List of Students= Pawan,Amit,Sonu,


Narender,Monu,Mehesh,Umed,Sandeep,Neeraj,Sumit.
Alphabetical process of names= Amit
Monu
Mehesh
Narender
Neeraj
Pawan
Sandeep
Sonu
Sumit
Umed.

Structure and union


Structure:A structure is user define compound data type which consist of data members related to any
application. It is also known record in other programming language.
Syntax:struct structure name
{
Type member var.1;
Type member var.2;
========
};
Struct :It is a keyword which we used to structure then we use to this keyword.
type:type is represent any valid data type in c.
Member var.1:- These are variables which are related to a person or item or any
application.
Example:Struct abc
{
Int roll no;
Char name[20];
Int total;
};

Structure name
member of structure
termination of structure.

Dot operator (.):This operator used to access a member variable in a structure variable
or an array element.
Struct variable. Member variable1.;
Structure variable :A structure variable represent of declared structure.

Declaration of structure variable:Structure variable. Data member


It is to be noted that the dot operator or separates the structure variable or array and the data
member.
Example:#include<stdio.h>
#include<conio.h>
void main()
{
struct abc
{
int a;
float b;
char c;
};
struct abc x;
x.a=10;
x.b=15.50;
x.c='y';
printf("%d%f%c",x.a,x.b,x.c);
getch();
}
Initialization of structure variable:The variable of structure can be assigned values during
declaration or during execution of program.
#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
int rollno;
char name[20];
char branch[10];
};
struct student s={1000,"pawan","b.c.a"};
printf("student roll no name and branch=%d\n%s\n%s",s);
getch();
}
Output :Student roll no., name and branch= 1000
Pawan
b.c.a
W.A.P in c to read and print your name age and percentage.

#include<stdio.h>
#include<conio.h>
struct student
{
int age;
char name[20];
float percentage;
};
void main()
{
struct student s;
printf("enter your name=");
scanf("%s",s.name);
printf("enter your age=");
scanf("%d",s.age);
printf("enter your percentage=");
scanf("%f",s.percentage);
getch();
}
Output :Enter your name = Pawan
Enter your age = 18
Enter your percentage = 67.35
W.A.P in c to read and print following information through key board with using
structure.
Roll no
Name
Marks :- physics, chemistry & math.
Total:- physics + chemistry + math.
Percentage:- total*100/300
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char n[20];
int phy,che,math;
};
void main()
{
struct result x=(1000,"pawan","90,85,75");
int total;
float per;
total=phy+che+math;

per=total*100/300
printf("result \n");
printf("roll=%d\n",x.roll);
printf("name=%d\n",x.name);
printf("total obtained marks=%f"x,per);
getch();
}
Output :Enter your name = Pawan
Enter your age = 18
Enter your percentage = 67.35
W.A.P to find the size of structure .
#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
int regno;
char n[20];
};
struct student x;
printf("size of structure in bytes=%d",sizeof(x));
getch();
}
Output :The size of structure in bytes = 22
W.A.P to read and print following information through structure for 5 student
1 reg. no.,2. name, and age.
#include<stdio.h>
#include<conio.h>
struct student
{
int regno;
char name[20];
int age;
};
void main()
{
struct student s;
int i;
for(i=1;i<=5;i++)
{

printf("enter your reg no.=");


scanf("%d",&s.regno);
printf("enter your name =");
scanf("%s",&s.name);
printf("enter your age=");
scanf("%d",&s.age);
}
getch();
}
Output :Enter your regno = 101
Enter your name = pawan
Enter your age = 18
Enter your regno = 102
Enter your name = satdav
Enter your age = 20
Enter your regno = 103
Enter your name = sonu
Enter your age = 18
Enter your regno = 104
Enter your name = narender
Enter your age = 18
Enter your regno = 105
Enter your name = sumit
Enter your age = 20
Unions:Similar to a structure a union also consists of data members but only one data
member is active at a time. Union use keyword union for declaration. The union is used when
memory of computer is small.
Syntax:Union union identifier
{
Type data member1;
Type data member2;
=======
=======
};
Here type is a valid data type in c.
Data members:-These are variables.
Union identifier:-Represent the name of union.
Example:union abc
{
int a;
float b;
char c;

};
Size of union:The size of union is equal to the number of bytes occupied in ram by the
largest data member in the union. Consider the following example .
union student
{
int rollno;
char name[20];
};
The size of union is equal to 20bytes because the largest data member name occupies 20
bytes in ram.
Example:W.A.P to find the size of the union.
union student
{
int rollno;
char name[20];
};
Void main( )
{
Union student x;
Printf(size of union =%dsizeof(x));
Getch( );
}
Output :The size of union is = 4
Solution:The size of integer =2 bytes
The size of float =4 bytes
The size of character =1 byte
So the size of float is high so the output is 4.
Difference between an union and a structure.
Structure
1. The keyword struct is used to declare a
structure.
2. .All the data members in a structure are
active at a time.
3. It is useful to declare a compound data
type to group data members related to a
person.
4. It is commonly used in most of the
application.

Union
1. The keyword union is used to declare an
union.
2. Only one data type is active at a time.
3. It is useful in certain cases where the
user will select any one data members in the
group of data members
4. It is not commonly used.

File Handling
Files:A File contains data/ information which are permanently in storage device. Floppy
disk and hard disk are commonly used to store the information. When large quantity of data is
required to be stored and processed, the concept of file is used .
A file stored in a storage device is always identified using a file name.
Example:List.TXT OR List. DAT
Note that a filename normally has a primary name and a secondary name which are Separated
by dot(.).
List .Txt
Program Name
Program Name
Dot Or
Separator
The primary and secondary names are assigned by the programmer. Normally the secondary
name is used to identify the nature of the file.

Types of File:The data files are classified in two categories based on the type of data
or information stored in them.
1. DAT is used to refer to Data File.
2. Txt is used to refer to Text File.
File
Data File

Text File

.DAT

. TXT

Data File:A data file contains data stored in the form of records. A record is a collection
of data related to a person or an item.
Example:101
102
103

Pawan
Dev
Sonu

18
20
19

Text File:A text file contains information stored in the form of string of characters.
The characters enter through the keyboard are stored continuously as following.
101,
102,
103,

Pawan,
Dev,
Sonu,

18
20
19

A Program is a group of instruction


and statements which perform a
certain task according to instruction.

Data File Organization:The data files organizations are classified in two categories. First
is called Sequential File Organization and second is called Random Access File
Organization.
Sequential File Organization:In a Sequential file organization the records or information is
stored sequentially one by one. The data is read in the same order in which they are stored.
Record
Reg. No
Top
1.
1005
2.
1006
3.
1007
Bottom
4.
1008
Random Access File Organization:In random file, The any record can be accessed
directly or randomly, normally a key data is used to identify the required record in a random
file processing.
File Processing:Data/ Information stored in a storage device are access using the file
pointer. The following operation:1.
2.
3.
4.
5.
6.

Creation of a File of a specific type


Readily/ processing a File
Append/ add information to a File
Modify/ edit data in a File
Delete items in a File
Update the File (Modification of the File).

File Declaration:A file is declared and the data is accessed using a file pointer. It has
following from:Syntax:FILE *FP
Where FILE refers to the type name for File declaration.
FP represent to the File pointer.
Example:FILE *a;
FILE *X,*Y;
Note that the type name FILE is always in Capital Letters. The file pointer develops a link
between the storage device and the program.
fopen () Function:fopen() Function is used to open a file and set the file pointer to the
beginning, end of a File. It has following form:Syntax:FP = fopen(File name, mode);
Where FP represent to the file pointer.
File name represent the name of the File to be opened.
Mode represents the operation mode to access data.
Example:a = fopen(LIST.TXT,w);
The following Modes are used in data processing:-

Mode
w
r
a
wt
rt
at

Operation
To open a file to write data or information.
To open a file to read data or information from the beginning of
To open a file to add data/ information at the end of an existing
a file.
To open a file for writing . Data can be read after writing using the
same mode.
To open a file for reading and writing data.
To open a file to write information at the end of a file. Data can be read
after writing using the same mode.

Example:a = fopen(STUDENT.TXT, w);


b =fopen(STUDENT .DAT,r);
fclose () Function:This function is used to close an active file.
Syntax:fclose(FP);
Where FP represent to the file pointer.
Example:fclose(a);
Handling Characters:1. getc() Function:This function is used to read a character in a file.
Syntax:ch = getc(FP);
Where ch represents to the character variable.
FP represent to the File pointer.

Example:FILE *tfile;
char c
c = getc(tFile);
Note:- that the character read from the file is assigned to the character variable.
putc() Function:This function is used to write a character to a file.
Syntax:putc(ch,FP);
Where ch represent to the variable.
FP represent to file pointer.

Example:FILE *tfile;
char c;
c= getchar();
putc(c,tfile);
Note:- That the value of the char variable C is written in the File.
feof() Function:This function is used to locate the end of a while reading data.
Syntax:feof(FP);
Where FP refers to file pointer.
This function will return a zero when the file contains data and returns a non- zero integer
when the end of a file is reached.
Exanple:File * Fptr;
If(Feof(fptr==0)
printf(\nData available in the file);
else
printf(\n End of file is reached);
Handling Integers:getw() Function:This function is used to read an integer data from a file.
Syntax:X = getw(p);
Where X represent to an integer Variable.
P represent to file pointer.
Example:FILE *P;
Int X;
X = getw(P);
putw () Function:This function is used to write an integer data in a file.
Syntax:putw(X,P);
Where X represent to an integer variable.
P represent to file pointer.
Example:FILE *P;
Int X;

putw(X,P);
Note:- That the value of integer variable X is written in the text file.
fscanf() Function:This function is used to read data from a file. It is similar to the scanf
function expand that Fscanf is used read data from the disk.
Syntax:fscanf(FP,Format String, &v1,&v2,-----&vn);
Where FP represent the File pointer.
Format string represent to control Strings which are used according to data type.
V1, V2 ----------Vn These are variable whose values are read from the disk.
Example:fscanf(P,%d%s,&regno,name);
fprintf() Function:This function is used to write data to a file. It is similar to the printf
function expect that fprintf() function used to write data to a disk.
Syntax:fprintf(FP,Format Strings,V1,V2-----Vn);
Where FP represent the File pointer.
FormatString the control strings which are used according to data type.
V1,V2-------Vn are variables whose values are write the disk.
Example:fprintf(P,%s%d,name,Regno.);
Rewind() Function:This function is used to move the file pointer to the beginning of file.
Syntax:rewind(FP);
A File called List .Txt Contains information such as RegNo, Name of Students Age,
Branch.
W.A.P to create a file to store details of n students.
#include<stdio.h>
#include<conio.h>
void main()
{
int regno, Age,k,n;
char name[20],Branch[10];
FILE *P;
P= fopen("List.TXT","w");
clrscr();
printf("how many Students");

scanf("%d",&n);
for(k=0;k<n;k++)
{
printf("\n reg no, student name, age & branch\n");
scanf("%d%s%d%s",&regno,&name,&Age,&Branch);
fprintf(P,"%5d%10s%5d%10s", regno,name,Age,Branch);
}
fclose(P);
getch();
}
Output:How many students = 2
Reg no Student Name Age & Branch
1001
Dev
20
B.c.a
Reg no Student Name Age & Branch
1002
Sonu
19
B.c.a
1001
Dev 20
B.c.a
1002

Sonu

19

B.C.a

Solution:The output is shown in List file which are made in Bin Folder.
W.A.P to read information from a File.
#include<stdio.h>
#include<conio.h>
void main()
{
int regno, Age;
char name[20],Branch[10];
FILE *P;
P= fopen("List.TXT","r");
clrscr();
while(!feof(P))
{
printf("\n reg no, student name, age & branch\n");
fscanf(P,"%d%s%d%s",&regno,&name,&Age,&Branch);
printf("%5d%10s%5d%10s", regno,name,Age,Branch);
}
fclose(P);
getch();
}

Output:Reg no
1001
Reg no
1002

Student Name
Age
Dev
20
Student Name
Age
Sonu
19

Branch
B.c.a
Branch
B.c.a

The Preprocessor
C has a special feature of preprocessor which make it different from other high level language
that dont have this type of facility. Some advantage of using preprocessor are :1. Readability of the program is increased.
2. Program modification becomes easy.
3. Makes the program portable and efficient.
There are several steps involved from the stage of writing a c program to the stage of getting
it executed. The combination of these steps is know as the Build Process.
At this stage before a C program is complied it is passed through another program called
Preprocessor. The C program is often known as Source Code. The preprocessor works on
the source code and creates Expand Source Code. The steps of the complier of the
program:Source Code

Preprocessor

Compiler

The preprocessor offers several features called preprocessor directives. The lines starting with
the symbol # are known as preprocessor directives. Some features of preprocessor directives
are1. Each preprocessor directive starts with a # symbol.
2. There can be only one directives in a line.
3. There is no semicolon at the end of the directive.
4. The preprocessor directives can be placed anywhere in a program (inside or outside
functions) but they are usually written at the beginning of a program.
5. A directive is active from the point of its appearance till the end of the program.
We would learn the following preprocessor directives here:-

1. Macro Substitution
2. File Inclusion
3. Conditional compilation
Macro Substitution:Constant value or expression can be identified using symbolic
names. At the time of preprocessing, all these symbolic names are replaced by value/
expressions. This process is referred to macro substitution.
Syntax:#define Macroname Macroexpression
Here # and define are of two operator which is used in macro.
Macroname is any valid C identifier, and it is generally taken in capital letters. The macro
expression can be any text.
Note:- That is no white space between # and define one space is necessary between the
macroname and macroexpression.
Example:W.A.P to show that macro expansion can be a string constant.
#include<stdio.h>
#include<conio.h>
#define MSG "I am Pawan Dhankhar"
void main()
{
printf(MSG);
getch();
}
Output:I am Pawan Dhankhar.
The C Preprocessor searching for the macroname MSG and replace the Message I am
Pawan Dhankhar in the printf () function.
W.A.P to print first ten natural number using macro.
#include<stdio.h>
#include<conio.h>
#define NATURAL 10
void main()
{
int i;
for(i=1;i<=NATURAL;i++)
{
printf("%d\t",i);
}
getch();
}
Output:1

10

The C preprocessor searches the macroname NATURAL and its value is 10 and its value is
replaced in for loop. The value of NATURAL is 10 which is define in macro. So the loop is
executed for 10 times.
Macro with Argument:A macro is used in the from of arguments. The #define
directive can also be used to define macros with arguments.
Syntax:#define Macroname(arg1,arg2,-----------------------------------------argn)
Here arg1,arg2,---------------------argn are the formal arguments. The macro name is replaced
with the macro expression and the formal argument are repaced by the corresponding actual
argument supplied in the macro call.
Example:#define SUM(X,Y) (X+Y)
Here SUM is a macro name and X and Y are the arguments. (X+Y) are the operation
which is performing the macro.
W.A.P to add of two values using macro with argument.
#include<stdio.h>
#include<conio.h>
#define SUM(X,Y) (X+Y)
void main()
{
int s,a,b;
printf("Enter the value of a and b= ");
scanf("%d%d",&a,&b);
s=SUM(a,b);
printf("The sum of two values = %d",s);
getch();
}
Output:Enter the value of a and b= 25
15
The sum of two values = 40
W.A.P to find the area of the circle and product of two numbers using macro.
#include<stdio.h>
#include<conio.h>
#define PRODUCT(X,Y) (X*Y)
#define AREA(X) (3.14*X*X)
void main()
{
int p,a,b,r;
float A;
clrscr();

printf("The radiush of the circle = ");


scanf("%d",&r);
A=AREA(r);
printf("The area of the Circle = %f\n",A);
printf("Enter the value of a and b= ");
scanf("%d%d",&a,&b);
p=PRODUCT(a,b);
printf("The Product of a and b = %d",p);
getch();
}
Output:Enter the radish of the Circle = 7
The area of the Circle = 153.86001
Enter the value of a and b = 15
6
The product of a and b =90
Here in AREA and PRODUCT are two macro and in AREA macro is follow the operation
(3.14 *X *x) and PRODUCT macro is follow the operation of multiply of a and b is (X*Y).

2. Including Files:The preprocessor directive # include is used to include a file into the source code. We
have already used this directive to include a header file in our program. The file name is
should be with in angle brackets or double quotes.
Syntax:#include File name
#include<File name>
If the file name is in double quotes, first it is searched in the current directory (where the
source file is present) if not found there then it is searched in the standard include directory. If
the file name is within angle brackets, then the file is searched in the standard include
directory only.
3. Conditional Compilation:Conditional compilation means compilation of a part of a
program based on some condition. These condition are checked during the preprocessing
phase. The directives used in conditional compilation are :#ifdef
#ifndef
#if
#else
#elif #endif
Every #if directive should end with a #endif directive. The working of these directives is
somewhat to that of if------else.
#if and #endif:An expression which is followed by the #if is ealuted, if result is
non-zero then the statement between #if and #endif are complied otherwise they are replaced.
Syntax:#if Constant Expression

----------------------------------------------------Statements
#endif
----------------------------------------------------- Statements
Example:
/*Program o understand the use of #if directives*/
#include<stdio.h>
#include<conio.h>
#define S 7
void main()
{
int a,b;
clrscr();
printf("Enter the value of a and b =");
scanf("%d%d",&a,&b);
#if S>=5
a=a+b;
b=a*b;
printf("Value of a variables a and b have been changed ");
#endif
printf("a =%d,b = %d\n",a,b);
printf("program completed\n");
getch();
}
Output:Enter the value of a and b= 20
5
Value of variables a and b have changed a=25 , b= 100
Program Completed.
In this program S is defined with the value of 7. Firs the constant expression S>=5 is
evaluated, since it is true, hence all the statements between #if and #endif are complied.
Suppose the value of S is changed to 4, now the constant expression S>=5 would evaluate to
false hence the statements between #if and #endif will not be complied. In this case the
output of the program would be:a =20, b=5
Program Completed.
#else and #elif:#else is used with the #if preprocessor directive. It is similar to if---------else.
Syntax:#if Constant expression
----------------------------------------------------------- Statements
#else
-------------------------------Statements
#endif

If the constant expression evaluates to non- zero them the statements between #if and #else
are complied otherwise the statements between #else and #endif are complied.
/* Program to understand the use of #else directive*/
#include<stdio.h>
#include<conio.h>
#define s 7
void main()
{
int a,b;
clrscr();
printf("Enter the value of a and b =");
scanf("%d%d",&a,&b);
#if s>=5
a=a+b;
b=a*b;
printf("Value of a variables a and b have been changed ");
#else
printf("Value of S is less than 5\n");
a=a-b;
b=a/b;
#endif
printf("a =%d,b = %d\n",a,b);
printf("program completed\n");
getch();
}
Output:Enter the value of a and b = 25
5
Value of variables a and b have changed a= 30, b=150
Program completed.
In this program S is defined with the value of 7. Firs the constant expression S>=5 is
evaluated, since it is true, hence all the statements between #if and #endif are complied.
Suppose the value of S is changed to 4, now the constant expression S>=5 would evaluate to
false hence the statements between #else and #endif will be complied. In this case the output
of the program would be:Value of S is less than 5
a =20, b=4
Program Completed.
#ifdef and #ifndef:The directives #ifndef provide an alternative short form combining #if
with defined oprator.
#if defined(macro name) is equivalent to #ifdef macro name.
#if !defined (macro name) is equivalent to #ifndef macro name.
Syntax:-

#ifdef macro_name
------------------------------------------------------ Statements
# endif
If the macro name has been defined with the #define directive, then the statements between
#ifdef and #endif will be complied. If the macro name has not been defined or was undefined
using #undef then these statements are not complied.
Syntax of #ifndef:#ifndef macro_name
----------------------------- Statements
-----------------------------#endif
If the macro_name has not been defined using #define or was undefined using #undef, then
the statements between #ifndef and #endif are complied. If the macro_name has been
defined, then these statements are not complied.
Ex:W.A.P to understand the use of #ifdef directive.
#include<stdio.h>
#include<conio.h>
#define S
void main()
{
clrscr();
int a, b;
printf("Enter the value of a and b =");
scanf("%d%d",&a,&b);
#ifdef S
printf("S is defined\n");
a++;
b++;
#endif
printf("a = %d, b =%d\n",a,b);
printf("program completed\n");
getch();
}
Output:Enter the value of a and b =12
5
S is defined
A=13, b=6
Program Compleed

The macro S has been defined. So the statements between #ifdef and #endif are complited. If
we delete the definition of macro S from the program, then these statements would not be
complied and the output of the program would be:a = 12, b=5
Program Completed.
W.A.P to understand the use of #undef and #ifdef directives.
#include<stdio.h>
#include<conio.h>
#define S
void main()
{
clrscr();
int a, b;
printf("Enter the value of a and b =");
scanf("%d%d",&a,&b);
#ifdef S
printf("S is defined\n");
a++;
b++;
#endif
#undef S
#ifdef S
printf("Preprocessor \n");
a++;
b++;
#endif
printf("a = %d, b =%d\n",a,b);
printf("program completed\n");
getch();
}
Output:Enter the value of a nd b =13
5
S is defined.
A = 14, b=6
Program Completed.
Here we have undefined the macro S in the middle of program. So the macro Flag is defined
only for the part of the program that is before the #undef directive, and is undefined for the
rest of program.
Exercise:1. What is a C preprocessor? Explain the various preprocessor directives.
2. W.A.P in c to find the circumference of the area..
3. What is the differation of macro and a function.
4. What is Conditional Compliation?

5. W.A.P to find the simple interest of a given promem.


DMA
Dynamic Memory Allocation:Dynamic memory allocation refers to the method of
allocationg a block of memory is not required at the time of running of the program.. A block
of memory can be used to store values of simple or subscripted variables, and a block of
memory can be accessed using a pointer. Following are the function used in dynamic memory
allocation. And these function are available in the header File <alloc.h>
There are four function is used in the DMA.
1. Malloc () Function
2. Calloc(() Funtion
3. Realloc Function
4. free Function
Malloc:This function is used to aloacate a single block of mempry to store values if
specifieds data type. It also addigns the address of the first byte of the allocated space to a
pointeer. It has following form:-

p=(type(*)malloc(size);Hee p refers to pointer variable.


type refers to the data btype to be stored in memory.
Size refers to number of bytes to be allotted.
Example:int *p;
p = (int *)malloc(10);
OR
p = (int *)malloc(5 *size of(int));
The allocated space can be used to store 5 int type va;ues,and this block of memory can be
acceses by the pointer papa of the same type.
Calloc () Function:This function is used to allocate memory in multipal blocks of
asame size during the program execuation. The space allotted is used to store values of an
array or structure. This function is assigns the address of the first byte of the allotted space to
a pointer. It also assigns 0 as the intial value for all bytes in the block of memory.
Syntax:p = (type *)calloc(n,m);
Where p refers to pointer variable.
type refers to the data type to be stored in memory.
n refers to number of blocks o be alloted.
m refers to number of bytes in each block of memory.
Consider the following examples to allocate 40 blocks of 4 bytes each. The total size of space
alloted is 160 bytes.
float *x;
x = (float *)calloc(40,4); To allocate 4o blocks of 4 byted each.
OR
X = (float *)calloc(40, sizeof(float));
The allocated space may be used to store 40 float values and accessed by the pointer
of X of the same type. Note that a NULL pointer is returned when there is insufficient in
memory.
Relloc () Function:This function is used to modify or reallocate the memory space which
is previously alloted. This faciliate to increase or reduce the alloted space at a later stage in a
program.
Syntax:P = realloc(p.size);
Where p refers to pointer variable of existing and new block.
Size refers to number of bytes to be allotted newly.
Example:Int *p;
p=i(nt *)malloc(50);
p=realloc(p,100);

The previously alloted space of 50 bytes to the pointer p is modified as 100 bytes at a larger
stage of the program.
Free () Function:This function is used to rlease the memory space which is alloted using
malloc() or calloc() or realloc() function.
Syntax:free(p);
Where p is the pointer which refers to the allocated space in memory.
Example:W.A.P to demo for malloc and free function.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
char *x;
int *y;
float *z;
clrscr();
x=(char *)malloc(sizeof(char));
y=(int *)malloc(sizeof(int));
z=(float *)malloc(sizeof(float));
printf("Enter a character\n");
*x = getchar();
printf("Enter an integer \n");
scanf("%d",&y);
printf("enter a float\n");
scanf("%f",&z);
printf("Entered character = %c,Integer = %d, & float =%f\n",*x,*y,*z);
free(x);
free(y);
free(z);
getch();
}
Output:Enter a character y
Enter a integer 23
Enter a float 23.4566
Enter character y, Integer 23 &Float 23.4566
DMA for Array:W.A.P to creae an array with using DMA.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>

void main()
{
int *x,n,k;
clrscr();
printf("Enter the size of array");
scanf("%d",&n);
x=(int *)malloc(sizeof(int)*n);
for(k=0;k<n;k++)
{
printf("Enter x[%d] element:",k);
scanf("%d",x+k);
}
printf("Created array \n");
for(k=0;k<n;k++)
printf("x[%d]= %d\n",k,*x+k);
free(x);
getch();
}
Output:Enter the size of array = 5
Enter x{0] element = 1
Enter x[1] element = 2
Enter x[2] element = 3
Enter x[3] element = 4
Enter x[4] element = 5
Created array
X[0] = 1
X[1] = 2
X[2] = 3
X[3] =4
X[4] = 5
Exercise:1. Explain DMA?
2. What is the purpose of realloc() function?
3. Differentiate calloc () and malloc () function.
Graphics Using C
Setting Texr Modes:A text mode with a specific number of characters can be set.
Following are the commonly used text modes
Symbol
BW 40
C40

Mode Value
0
1

Text Mode
Black and white with 25 rows and 40 colmun.
Color with 25 rows and 40 colmun

BW80
2
Black and white with 25rows and 80 colmuns.
C80
3
Color with 25 rows and 80 colmuns.
Text backgrounf () Functio:Textbackground () function is used to set the background color
in the text mode.
Syntax:textbackground(n);
Where n is an integere reffering the background color mode.
Example:textbackground(1); or textbackgroundcolor(BLUE);
Will set the background color blue.
The following is the value list corresponding to colors.
Symbol
Color Value
Symbol
Color Value
BLACK
0
LIGHTBLUE
9
BLUE
1
LIGHTGREEN
10
GREEN
2
LIGHTCYAN
11
CYAN
3
LIGHTRED
12
RED
4
LIGHTMAGENTA
13
MAGENTA
5
YELLOW
14
BROWN
6
WHITE
15
LIGHTGRAY
7
BLINK
128
DRAKGRAY
8
Note that the symbol can also be used in the function instead of color value.
Example:textbackgroundcolor(RED) ; OR textbackgroundcolor(4);
textcolor:Textcolor () function is used to set the color for the text going to be displayed.
Syntax:textcolor(n);
Where n is an integer referring the text color.
Example:textcolor(2); or textcolor(GREEN);
Will set the text color GREEN.
cputs () function:This function is used to display a text message in the monitor screen in
the graphics mode.
Syntax:cputs(text message);
W.A.P to print your name in green color.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
textcolor(GREEN);
cputs("Pawan Dhankhar");
getch();
}
Output:-

W.A.P to find the biggest of mtwo number using text mode.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int a,b;
clrscr();
textmode(C80);
textbackground(RED);
textcolor(GREEN);
cputs("Enter the first number :");
scanf("%d",&a);
cputs("Enter the second number :");
scanf("%d",&b);
textcolor(BLUE+BLINK);
if(a>b)
{
printf("%d",a);
cputs("Is the biggest number");
}
else
{
printf("%d",b);
cputs("Is the biggest number");

}
getch();
}
Output:-

Vous aimerez peut-être aussi