0% found this document useful (0 votes)
8 views

C Material

For beginners c materiel

Uploaded by

mahithak040
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C Material

For beginners c materiel

Uploaded by

mahithak040
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 111

Steps in Learning English :-

Alphabets - words - sentences - paragraph

Steps in learning C language :-


Alphabets Data/Contstants
Digits Variables Instructions
Program
Special Symbols datatypes
keywords
Operators

COMPUTER / SYSTEM :- IT IS THE COMBINATION OF HARDWARE AND SOFTWARE

HARDWARE :- PHYSICAL PARTS OF THE COMPUTER. WHICH ARE SEE AND TOUCHABLE.

Ex :- KEYBOARD, MOUSE, MONITOR etc.

SOFTWARE :- SET OF PROGRAMS. WHICH ARE INVISIBLE AND UNTOUCHABLE.

Ex :- MS-OFFICE, WINDOWS, ORACLE, PHOTOSHOP etc.

NOTE :- BOTH HARDWARE & SOFTWARE ARE INTERDEPENDENT.

TYPES OF SOFTWARES
====================
1) System software (OPERATING SYSTEMS)
2) Application software
A) PACKAGES
B) LANGUAGES

OPERATING SYSTEM :- IT IS AN INTERFACE (WAY) BETWEEN USER AND COMPUTER. IT CONTROLS


AND MANAGES THE TOTAL RESOURCES OF THE COMPUTER. THROUGH OPERATING SYSTEM ONLY WE
CAN OPERATE THE SYSTEM. WITHOUT OPERATING SYSTEM WE CAN'T PERFORM EVEN SINGLE
OPERATION.

Ex :- MS-DOS, WINDOWS, UNIX, LINUX, MACINTOSH etc

PACKAGE :- IT IS A PRE-DESIGNED SOFTWARE.


ADVANTAGE : TIME SAVE.
DISADVANTAGE : WE CAN'T MAKE ANY MODIFICATIONS IN IT.
Ex:- MS-OFFICE, PHOTOSHOP, TALLY, PAGE MAKER etc

LANGUAGE :- BY USING LANGUAGES WE CAN DEVELOP NEW SOFTWARES.


ADVANTAGE :- WE CAN DEVELOP SOFTWARES AS PER OUR REQUIREMENTS.
DISADVANTAGE :- TIME WASTE.
BY USING LANGUAGES WE CAN DEVELOP NEW SOFTWARES / PROJECTS / APPLICATIONS /
PACKAGES.
Ex :- COBOL, PASCAL, C, C++, JAVA, DOT NET etc

DATA :- IT IS NOTHING BUT A KNOWN VALUE, WHICH IS FORMED WITH DIGITS, ALPHABETS AND
SPECIAL CHARACTERS.

Ex :- 101, RAMA, 3/2/2014

INFORMATION :- PROCESSED DATA.

6 + 3 : 9

IN THE ABOVE STATEMENT 6 + 3 IS CALLED DATA AND 9 IS CALLED INFORMATION.


INSTRUCTION / STATEMENT :- IT IS NOTHING BUT A REQUEST / COMMAND.

Ex :- A = 10

PROGRAM :- SET OF INSTRUCTIONS.


Ex :- A = 10
B = 20
C = A + B
PRINT C

WHY WRITE PROGRAMS? :- TO SOLVE PROBLEMS.


WHERE WRITE PROGRAMS? :- IN LANGUAGES.
SOFTWARE :- SET OF PROGRAMS.

LANGUAGE :- IT IS A WAY TO COMMUNICATE WITH OTHERS / SYSTEM.

COMPUTER LANGUAGE :- IT UNDERSTANDS ONLY MACHINE LANGUAGE, WHICH CONTAINS ONLY


ZEROS (OFF) AND ONES (ON). SYSTEM CONVERTS ANYTHING INTO ONES AND ZEROS. FOR
EXAMPLE 25 IS CONVERTED TO 11001. (DRAW FIGURE).

TYPES OF LANGUAGES
====================
1) LOW LEVEL LANGUAGES
2) HIGH LEVEL LANGUAGES

LOW LEVEL LANGUAGES :- THESE ARE SYSTEM FRIENDLY LANGUAGES. IN THESE LANGUAGES
READIBILITY (READING FACILITY) & DEBUGGING (RECTIFYING ERRORS) IS VERY DIFFICULT.
THE MAIN ADVANTAGE OF THESE LANGUAGES IS, THESE LANGUAGES INSTRUCTIONS EXECUTES
VERY FASTLY BY SYSTEM. THESE LANGUAGES ARE CLASSIFIED INTO TWO CATEGORIES.

1) MACHINE LANGUAGE

2) ASSEMBLY LANGUAGE

MACHINE LANGUAGE :- IT IS NOTHING BUT SYSTEM LANGUAGE. IT IS ALSO CALLED BINARY


LANGUAGE.

ASSEMBLY LANGUAGE :- BY USING THIS LANGUAGE WE CAN WRITE PROGRAM STATEMENTS WITH
THE HELP OF MNEMONICS (SMALL ENGLISH WORDS LIKE ADD, SUB, MUL, DIV etc).

HIGH LEVEL LANGUAGES :- THESE ARE USER FRIENDLY LANGUAGES. USING THESE LANGUAGES
WE CAN WRITE PROGRAM STATEMENTS IN SIMPLE ENGLISH LANGUAGE. THESE LANGUAGES
PROVIDES EXCELLENT READIBILITY (READING FACILITY) & DEBUGGING (RECTIFYING ERRORS)
FACILITIES. WE CAN'T COMMUNICATE DIRECTLY WITH SYSTEM THROUGH HIGH LEVEL LANGUAGES.
WE CAN COMMUNICATE WITH THE HELP OF TRANSLATORS. SO THESE LANGAUGE INSTRUCTIONS
EXECUTES SLOWLY.

Ex :- C, C++, JAVA etc.


LANGUAGE TRANSLATORS
========================
THESE ARE USED TO TRANSLATE HIGH LEVEL LANGUAGE INSTRUCTIONS INTO MACHINE LANGUAGE
AND MACHINE LANGUAGE INSTRUCTIONS INTO HIGH LEVEL LANGUAGE.

1) ASSEMBLER
2) INTERPRETER
3) COMPILER
ASSEMBLER :- IT IS USED TO TRANSLATE ASSEMBLY LANGUAGE INSTRUCTIONS INTO MACHINE
LANGUAGE.

INTERPRETER :- IT IS USED TO TRANSLATE HIGH LEVEL LANGUAGE INSTRUCTIONS INTO


MACHINE LANGUAGE. (STATEMENT BY STATEMENT, TIME WASTE).

COMPILER :- IT IS USED TO TRANSLATE HIGH LEVEL LANGUAGE INSTRUCTIONS INTO MACHINE


LANGUAGE. (TOTAL STATEMENTS AT A TIME, TIME SAVE).

'C' IS A HIGH LEVEL LANGUAGE. 'C' LANGUAGE HAS COMPILER.

HISTORY OF 'C' LANGUAGE


=========================

IN 1950's THERE ARE MAINLY AVAILABLE THREE HIGH LEVEL LANGUAGES.


1) FORTRAN
2) PASCAL
3) COBOL

FORTRAN (FORMULA TRANSLATION) :- IT IS USED TO DESIGN SCIENTIFIC APPLICATIONS. IT


IS THE FIRST HIGH LEVEL LANGUAGE.

PASCAL (SCIENTIST NAME) :- IT IS USED TO DESIGN ENGINEERING APPLICATIONS.

COBOL (COMMON BUSINESS ORIENTED LANGUAGE) :- IT IS USED TO DESIGN COMMERCIAL /


BUSINESS APPLICATIONS.

IT IS NOT POSSIBLE TO DESIGN SCIENTIFIC APPLICATION IN COBOL AND SO ON. IF A


PROGRAMMER WANTS TO DEVELOP THREE TYPES OF APPLICATIONS HE MUST LEARN ABOVE THREE
LANGUAGES. IT IS VERY DIFFICULT. AT THIS STAGE, COMPUTER EDUCATED PEOPLE TAUGHT,
WHY NOT USE A SINGLE LANGUAGE TO DEVELOP ALL TYPES OF APPLICATIONS. THEN THE
COMPUTER EDUCATED PEOPLE FORMED AS A TEAM NAMED INTERNATIONAL COMMITE. THE AIM OF
INTERNATIONAL COMMITE IS TO DEVELOP A NEW LANGUAGE, WHICH HAS ABILITY TO DEVELOP
ALL TYPES OF APPLICATIONS.

================================================
NAME YEAR DEVELOPED BY
================================================
ALGOL-60 1960 INTERNATIONAL COMMITE
(ALGORTHEMIC
LANGUAGE)

CPL 1963 CAMBRIDGE UNIVERSITY


(COMBINED
PROGRAMMING
LANGUAGE)

BCPL 1967 MARTIN RICHARDS


(BASIC CPL)

B 1970 KEN THOMPSON

C 1972 DENNIS RITCHIE

'C' WAS DEVELOPED BY DENNIS RITCHIE IN 1972 at AT & T (ADVANCED TELE COMMUNCATIONS
& TECHONOLGIES) BELL LABORATORIES OF USA. THE NAME 'C' HAS NO ABBREVATION. IT IS
JUST THE NEXT ALPHABET OF B. IT MEANS 'C' IS THE ADVANCED VERSION / NEXT VERSION OF
'B' LANGUAGE. 'C' IS A GENERAL PURPOSE HIGH LEVEL PROGRAMMING LANGUAGE.
Steps in learning C language:-
Alphabets Data/Contstants
Digits Variables Instructions
Program
Special Symbols datatypes
keywords
Operators

CHARACTER SET :- EVERY LANGUAGE HAS IT'S OWN CHARACTER SET. WE CAN FORM / DEVELOP
ANYTHING BASED ON THESE CHARACTER SET ON THAT LANGAUGE. 'C' LANGUAGE CHARACTER SET
CONTAINS THE FOLLOWING CHARACTERS.

UPPER CASE ALPHABETS : A - Z


LOWER CASE ALPHABETS : a - z
DIGITS : 0 - 9
~ : COMPLEMENT OPERATOR (TILDE)
! : NOT
# : PRE PROCESSOR DIRECTIVE (HASH)
% : MODULES OPERATOR (MOD)
^ : UNTIL (CARAT)
& : ADDRESS OPERATOR (AMPERSAND)
* : ASTERISK
( : OPENING PARANTHESIS
) : CLOSING PARANTHESIS
[ : OPENING SQUARE BRACKET
] : CLOSING SQUARE BRACKET
{ : OPENING CURELY BRACE
} : CLOSING CURELY BRACE
- : MINUS
_ : WORD CONNECTOR (UNDER SCORE)
+ : PLUS
= : ASSIGNMENT OPERATOR
| : OR OPERATOR (PIPE)
\ : BACKWARD SLASH
: : COLON
; : SEMI COLON
' : SINGLE QUOTE
" : DOUBLE QUOTE
< : LEFT ANGULAR BRACKET
> : RIGHT ANGULAR BRACKET
, : COMMA (SEPERATOR)
. : PERIOD / DOT OPERATOR
? : QUESTION MARK
/ : FORWARD SLASH

Data:- Data is a known value.


Data
Numeric Non-numeric
integer real value character
String
20 2.5 'M' "Rama"

//Variable: A quantity which may vary during the execution of a program is called
as variable.

variables:- A variable is used to store constants or data temporarily.


ex:- a=10;
a -- variable
10 --data
Rules for naming a variable:-
1.The variable name consist of alphabets,digits in any order but the first
character must be alphabet
ex:- 10=200 wrong
1st=40 wrong

ex:- a=20
sno=101
s4=56

2. Both upper case and lower case letters are different in c language, because 'C'
is case sensitive language.

3. In variable name no special symbols are allowed except underscore.


s.no (wrong)
student no (wrong)
net salary (wrong)
stuno
stu_number

4. Variable preferable length is 8 characters, the length of an variable must be


lessthan 32 characters.
ex:- a b c d e f
ex:- student_number,student_name,telugu,english,maths,science
ex:- stuno,stuname,tel,eng,mat,sci,soc,hin,tot,avg etc..

5. Variable name should not be a standard keyword or reserved word.

KEYWORDS :- THESE ARE ALSO CALLED AS RESERVED WORDS. THESE WORDS HAS PRE-DEFINED
MEANING IN 'C' LANGUAGE. WE CAN DEVELOP PROGRAMS WITH THE HELP OF KEY WORDS. WE
CAN'T USE THESE KEYWORDS AS VARIABLE NAMES. 'C' LANGUAGE PROVIDES THE FOLLOWING 32
KEYWORDS.

AUTO BREAK CASE CONST


CHAR CONTINUE DO DOUBLE DEFAULT
DEFINE ELSE EXTERN
ENUM FOR FLOAT GOTO
INT IF LONG REGISTER
RETURN SHORT SWITCH STATIC
SIGNED STRUCT TYPEDEF UNION
UNSIGNED VOLATILE VOID WHILE

Data types in C:
The kind of data that variables may hold in a programming language are called as
data types. C data types can be classifies into 3 categories.
1 Primary data types
2 Derived data types
3 User defined data types / structures

Primary data types:


All C compiler supports 4 fundamental data types. Namely int, char,
float and double.

int: It is positive, negative and whole values but not a decimal number.
Eg: 10, 20, -30, 0 etc.

char: A single character can be treated as character data type and is defined
between single quotation marks.
Eg: �r�, �H�, �5�, �*�, etc.

Note: String is also a character data type. A group of characters defined between
double quotation marks is a String.

Eg: �HHHH�, �abc985�, etc.

float: The numbers which are stored in the form of floating point representation is
called as float data type.

Eg: 10.25, 478.1234, -56.37821, etc.

double: The numbers which are stored in the form of double pr-ecision floating
point representation is called as double data type.

Eg: 1023.56789, 12345.56789, 0.456321, -567.4556 etc.

Derived data types:


These data types are created from the basic integers, characters and floating data
types.
Eg:

arrays, pointers, structures etc.

Sub classification of Primary data types, memory size, format specifiers and their
accessibility range :

Data type Format Spec Mem size Acc range


-----------------------------------------------------------------------------------
---------
char %c 1 Byte 0 to 255

int %d 2 Bytes -32768 to


32767

unsigned int %u 2 Bytes 0 to


65535

long (or) %ld 4 Bytes -2147483648


to 2147483647
long int

float %f 4 Bytes 3.4*(10 power -


38)

char [ ] (string) %s --------- [size]

int sno;
float rate; variable declaration
char gen;
char name[10];

sno=10;
rate=3.5; assignment
gen='M';
name="Raju"; not possible
int sno=10;
float rate=3.45; Initilization
char gen='M';
char name[10]="Rama";

Note:- All variables must be delcared before thier usage.


All variables Contains garbage value before assignment.

Operators:- C has a rich set of operators. They are


1. Airthmetic operators
2. Relational operators
3. Logical operators
4. Increment and decrement operators
----------------
5. Assignment operators
6. Conditional operators / Ternary operators
7. Bitwise operators
8. Comma operators

ex:- a+b
a,b are operands
+ is operator

1. Airthmetic operators
+ --> addition
- --> subtraction
* --> mlutiplication
/ --> It returns quotient
% --> It returns reminder (modulos operator)

operator Operation Result


====== ======= =====
+ 3+4 7
- 3-4 -1
* 3*4 12
/ 20/3 6
% 20%3 2

PRIORITY OF OPERATORS / OPERATORS PRECEDENCE


=================================================
OPERATORS PRIORITY IS BASED ON PRDMAS PRINCIPLE.

P ==== > PARANTHESIS ====> FIRST GROUP


R ====> REMAINDER ====> SECOND GROUP
D ====> DIVISION ====> SECOND GROUP
M ====> MULTIPLICATION ====> SECOND GROUP
A ====> ADDITION ====> THIRD GROUP
S ====> SUBTRACTION ====> THIRD GROUP

NOTE :- ASSIGNMENT OPERATOR (=) HAS LEAST PRIORITY.

2+4*5
2+20
22

a=20,b=3
c=a+b
d=a-b
e=a*b
f=a/b
g=a%b

3)20(6 quotient /
18
2 reminder %
9 even / odd no
4 r=9/2 not correct

9 even / odd no
1 store r=9%2

tot=200
avg=200%3 not correct
avg=200/3
3)200(66
198
2

total no of days 200 / 135 days


findout howmany months and days
6 mon=200/30
20 days=200%30

Relational operators:- Using these operators we can frame the condtions


> -- Greater than
< -- Less than
>= -- Greather than or equal to
<= -- Less than or equal to
== -- equal to
!= -- Not equal to

ex:- a=10,b=20
if(a>b) false
if(a<b) true
if(a=b) not possible
if(a==b) comparision
Note:- If the condition is true then the statement returns 1
If the condition is false then the statement returns 0
Ex:-
a=b Assignment
a==b Comparision
Operator Operation Result
====== ======= =====
> 3 > 4 0
< 3 < 4 1
>= 3 >= 4 0
<= 3 <= 4 1
== 3 == 4 0
!= 3 != 4 1

Logical operators :-Using these operators we can combined conditions


Operator Operation Result
====== ======= =====
&& (and) (3>4)&&(3<4) 0
|| (or) (3>4)||(3<4) 1
! (Not) !( 3 > 4) 1

Increment and Decrement operators :-


These operators can be used in two ways 1) post fix 2) pre fix
increment ex:-
a=5
a++
a=a+1
a=6
Decrement ex:-
a=5
a--
a=a-1
a=4

Post increment:-
a=7,b
b=a++
a=8
b=7
first assign ,next increment

Pre increment:-
a=7,b
b=++a
a=8
b=8
First increment,next assignment

post decrement:
a=7,b
b=a--
a=6
b=7

pre decrement :
a=7,b
b=--a
a=6
b=6
--------------------------------------
Structure of C program:-
Document section
Header file including section
main section
declaration part
executable part

DOCUEMNT SECTION:- IT IS USED PURPOSE OF THE PROGRAM IN PROGRAM.THIS DOCUMENT


SECTION MUST BE GIVEN AS COMMENTS.

COMMENTS :- COMMENTS ARE USED TO WRITE AIM OF THE PROGRAM. THESE ARE ALSO USED TO
WRITE REFERENCES. THE MAIN ADVANTAGE OF COMMENTS IS READABILITY (UNDERSTANDABLE).
THESE ARE NOT EXECUTABLE STATEMENTS. COMMENTS ARE TWO TYPES.

1) SINGLE LINE COMMENT : STARTS WITH // AND NEED NOT TO END

2) MULTI LINE COMMENT : STARTS WITH /* AND ENDS WITH */

/*
AIM : SUM OF TWO NUMBERS
AUTHOR : RANI
DATE OF WRITTEN : 03/05/2016
E-MAIL : [email protected]
MOBILE NO : 99999 99999
*/

HEADER FILES
=============
THE MAIN REASON FOR POPULARITY OF 'C' LANGUAGE IS IT'S BUILT-IN / PRE-DEFINED /
STANDARD / LIBRARY FUNCTIONS. A FUNCTION PERFORMS SOME TASK. 'C' LANGUAGE CONTAINS
MORE THAN 1500 FUNCTIONS TO PERFORM VARIOUS TASKS. IF THERE IS NO FUNCTION
AVAILABLE FOR OUR REQUIREMENTS, THEN WE CAN CREATE NEW FUNCTIONS AS PER OUR
REQUIREMENTS. THESE FUNCTIONS ARE CALLED USER-DEFINED FUNCTIONS. THE 1500 LIBRARY
FUNCTIONS ARE CLASSIFIED INTO 18 CATEGORIES. EACH CATEGORY IS CALLED A HEADER FILE.
HEADER FILES CONTAINS FUNCTIONS. SOME OF THE MOST COMMONLY USED HEADER FILES ARE
LISTED BELOW.

1) STDIO.H 2) CONIO.H 3) MATH.H 4) STRING.H

BEFORE USING ANY FUNCTION WE MUST INCLUDE RELATED HEADER FILE IN THE FOLLOWING
MANNER.

SYNTAX
=======
# include <header_file_name>
Ex:-
# include <stdio.h>
# include <conio.h>
# include <math.h>

FINDING FUNCTION RELATED HEADER FILE


========================================
TYPE FUNCTION NAME AND PLACE CURSOR ON THE FUNCTION NAME. THEN PRESS RIGHT BUTTON
(ESC).

void main( ):- IT IS THE MOST IMPORTANT FUNCTION IN 'C' PROGRAM. GENRALLY PROGRAM
CONTAINS SOME FUNCTIONS. 'C' PROGRAM EXECUTION STARTS FROM MAIN( ). ALL PROGRAM
STATEMENTS OR OTHER FUNCTIONS MUST BE PLACED INSIDE THE MAIN( ).

ex: - int main( )


ex: - main( )
ex: - void main( )

{ } :- THESE ARE CALLED CURELY BRACES. BRACES INDICATES STARTING AND ENDING OF THE
MAIN( ).

printf():- This function is used to display various types of formatted data


(int,float,char,string) on the standard output device.
syntax:-
printf("control string",arguments list);

The printf function moves the data from program to outside like monitor.

control string:- normally it contains the following categories of characters


1. Conversion characters (starts with %)
2. Escape sequences (starts with \)
3. Other characters (It displays as it is)
arguments list :- The arguments in the printf are variables

conversion characters:- These are used to specify the type of data i.e. to be
displayed with printf

%d (or) %i :- integer
%c :-character
%f :-float
%s :-string
%ld :-long int
%lf :- long float
%u :- unsigned int

Escape sequences :- Certain non printable characters are called as escape


sequences.
\a :- alert (beep sound)
\b :- backspace
\n :- new line (next line)
\t :- tab space (horizontal)
\r :- carriage return (same line begining)
\\ :- backslash will print
\" :- double quotes will print

clrscr( ) :- THIS FUNCTION IS USED TO CLEAR THE OUTPUT SCREEN.

getch( ) :- THIS FUNCTION IS USED TO VIEW OUTPUT.

NOTE :- 'C' LANGUAGE PROGRAMS MUST BE WRITEN IN LOWER CASE ALPHABETS. EVERY
STATEMENT MUST BE ENDS WITH SEMICOLON EXCEPT VOID MAIN( )

IDE :- IT STANDS FOR INTEGRATED DEVELOPMENT ENVIRONMENT. IT IS USED TO WRITE, SAVE,


COMPILE & EXECUTE PROGRAMS. MANY COMPANYS PROVIDE IDE'S FOR 'C' LANGUAGE. THE
STANDARD AND FAMOUS IDE FOR 'C' LANGUAGE IS "TURBO C++ IDE".

STEPS TO WRITE, SAVE, COMPILE & EXECUTE / RUN A 'C' PROGRAM


=================================================

1) FILE ====> NEW (Alt + F) - N

2) WRITE PROGRAM

3) SAVE PROGRAM
FILE ====> SAVE (NAME.C) (F2)

4) COMPILE PROGRAM
COMPILE ====> COMPILE (ALT + F9)

5) EXECUTE / RUN PROGRAM


RUN ====> RUN (CTRL + F9)

6) VIEW OUTPUT
WINDOW ====> USER SCREEN (ALT + F5)

7) CLOSING FILE
WINDOW ====> CLOSE (ALT + F3)

8) EXIT 'C' LANGAUGE


FILE ====> QUIT (ALT + X)
FILE ====> OPEN (F3)

ZOOM ====> F5

F6 :- to move one window to another window which are already opened

Ctrl + Y :- to delete entire line at a time


Alt+backspace :- to get back recently deleted information(like undo)
shift + Arrow keys :- to select particular area
Ctrl + Ins :- copy
Shift+Ins :- paste
Shift + delete:- cut
Alt + 0 :- Recently used file list
A SAMPLE 'C' PROGRAM
======================
#include<stdio.h>
void main( )
{
printf("Welcome to C Language");
}

OUTPUT
=======
Welcome to C Language

#include<stdio.h>
void main( )
{
printf("Learning C Language is Good");
}

OUTPUT
========
Welcome to C LanguageLearning C Language is Good

#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("Learning C Language is Good");
getch( );
}

OUTPUT
========
Learning C Language is Good

#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("One");
printf("Two");
printf("Three");
getch( );
}
OUTPUT
========
OneTwoThree

\n (BACK SLASH n) :- IT IS USED TO GENERATE NEW LINE OR NEXT LINE.

#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("One\n");
printf("Two\n");
printf("Three");
getch( );
}
OUTPUT
========
One
Two
Three

#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("One");
printf("\nTwo");
printf("\nThree");
getch( );
}

OUTPUT
========
One
Two
Three

#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("One");
printf("\nTwo\n");
printf("Three");
getch( );
}

OUTPUT
========
One
Two
Three

#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("One\nTwo\nThree");
getch( );
}

OUTPUT
========
One
Two
Three

NOTE :- DONT GIVE SPACE BETWEEN BACKSLASH AND n. DONT USE UPPER CASE N. IF YOU TYPE
\n\n THEN GENERATES ONE EXTRA EMPTY LINE.

#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("One\NTwo");
getch( );
}

OUTPUT
========
OneNTwo

#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("One\n");
printf("\nTwo");
getch( );
}

OUTPUT
========
One

Two

\t (BACK SLASH t) :- IT IS USED TO GENERATE FIVE / EIGHT SPACES.


#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("1\t");
printf("2");
getch( );
}

OUTPUT
========
1 2
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("*\n");
printf("*\t*\n");
printf("*\t*\t*");
getch( );
}
OUTPUT
=======
*
* *
* * *

#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("*\n*\t*\n*\t*\t*");
getch( );
}

OUTPUT
=======
*
* *
* * *
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
printf("\t\t*\n\t*\t*\n*\t*\t*");
getch( );
}
OUTPUT
=======
*
* *
* * *
ESCAPE SEQUENCES :- THESE ARE ALSO CALLED NON-PRINTABLE CHARARACTERS. THESE
CHARACTERS CAN'T PRINT ON SCREEN, WHICH PERFORMS SOME ACTION. THESE CHARACTERS
STARTS WITH BACK SLASH AND FOLLOWED BY A LOWER CASE ALPHABET.

[ \n, \t,\b, \r, \', \", \?, \\, \0 ]

\b : BACK SPACE

# include <stdio.h>
# include <conio.h>
void main()
{
clrscr();
printf("Jaya\b");
printf("Sree");
getch();
}

OUTPUT
=======
JaySree

\r : CARRAIGE RETURN

IT IS SIMILAR TO \n.

\n : NEXT LINE FIRST COLUMN

\r : SAME LINE FIRST COLUMN

# include <stdio.h>
# include <conio.h>
void main()
{
clrscr();
printf("Jaya\r");
printf("Sri");
getch();
}

OUTPUT
=======
Sria

# include <stdio.h>
# include <conio.h>
void main()
{
clrscr();
printf("This is Rama\'s Pen\n");
printf("This is \"Rama\" System\n");
printf("What is your name\?\n");
printf("C:\\Jasmine\\Lily\\Rose\\Lotus\n");
getch();
}

OUTPUT
=======
This is Rama's Pen
This is "Rama" System
What is your name?
C:\Jasmine\Lily\Rose\Lotus

\0 : NULL CHARACTER (STRING TERMINATOR)

WORKING WITH DATA


=====================

DATA :- IT IS NOTHING BUT A KNOWN VALUE.

VARIABLE :- WHICH STORES DATA. IT IS A MEMORY LOCATION. BEFORE USING VARAIBLES WE


MUST DECLARE / CREATE THEM AT THE STARTING OF THE PROGRAM.

DECLARING VARIABLES
======================
SYNTAX
=======

DATA_TYPE VARIABLE_NAME;

Ex :-
int htno;
float rate;
char gndr;
char name[10]; (STRING)

=======================================
SAMPLE DATA DATA TYPE SIGN /
CONVERSION CHARACTER /
FORMAT SPECIFIER / CONTROL STRING
=======================================
10 int %d (or) %i

12.45 float %f

'K' char %c

"Rama" char %s
=======================================

WORKING WITH INTEGERS


=========================
#include<stdio.h>
#include<conio.h>
void main()
{
int a=35; //intilization
clrscr();
printf("%i",a);
getch();
}
op:- 35
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c; // decalration
clrscr();
a=10; //assignment
b=20;
c=a+b;
printf("%d",c);
getch();
}
o/p:-30
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=9;
clrscr();
printf("%d",a*b);
printf("\n%d",a>b);
getch();
}
op:- 45
0
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("%d",a);
getch();
}
o/p:- garbage value

#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
clrscr();
printf("%d",A);
getch();
}
o/p:- undefined symbol A

#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=39,c=4;
clrscr();
printf("A=%d\nB=%d\nC=%d",b,c,a);
getch();
}
o/p:- A=39
B=4
C=10

#include<stdio.h>
#include<conio.h>
void main( )
{
int a; / int A; (VARIABLE DECLARATION)
a = 10; (ASSIGNMENT / STORING)
clrscr( );
printf("a\n");
printf("%d\n",a);
printf("A Value : %d\n",a);
printf("%d is A Value\n",a);
printf("Ravana Has %d Heads\n",a);
printf("A = %d",a);
getch( );
}

OUTPUT
=======
a
10
A Value : 10
10 is A Value
Ravana Has 10 Heads
A = 10

#include<stdio.h>
#include<conio.h>
void main( )
{
int htno;
htno = 101;
clrscr( );
printf("Hall Ticket Number : %d\n",htno);
printf("%d is my Hall Ticket Number",htno);
getch( );
}

OUTPUT
=======
Hall Ticket Number : 101
101 is my Hall Ticket Number
SQUARE OF A NUMBER
=====================
#include<stdio.h>
#include<conio.h>
void main( )
{
int num,sqr;
num = 6;
sqr = num * num;
clrscr( );
printf("Square : %d\n",sqr);
printf("%d square : %d\n",num,sqr);
printf("%d is the square of %d",sqr,num);
printf("%d * %d = %d",num,num,sqr);
getch( );
}

OUTPUT
=======
Square : 36
6 square : 36
36 is the square of 6
6 * 6 = 36
SUM OF TWO NUMBERS
=====================
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
a = 10;
b = 20;
c = a + b;
clrscr( );
printf("Sum : %d\n",c);
printf("%d and %d sum is %d\n",a,b,c);
printf("%d is the sum of %d and %d\n",c,a,b);
printf("%d + %d = %d",a,b,c);
getch( );
}

OUTPUT
=======
Sum : 30
10 and 20 sum is 30
30 is the sum of 10 and 20
10 + 20 = 30

#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20;
clrscr();
printf("Before swaping \nA=%d\nB=%d",a,b);
printf("\nAfter swaping \nA=%d\nB=%d",b,a);
getch();
}
Before swaping
A=10
B=20
After swaping
A=20
B=10

#include<stdio.h>
#include<conio.h>
void main()
{
int a=20,b=44,t;
clrscr();
printf("\nBefore swaping");
printf("\nA=%d",a);
printf("\nB=%d",b);
t=a;
a=b;
b=t;

a=a+b;
b=a-b; //without using third variable
a=a-b;

printf("\nAfter swaping");
printf("\nA=%d",a);
printf("\nB=%d",b);
getch();
}

a=20,b=3
o/p:
addition is 23
subtraction is 17
multiplication 60
quotient is 6
reminder is 2

#include<stdio.h>
#include<conio.h>
void main()
{
long int a=45678;
clrscr();
printf("A value: %ld",a);
getch();
}

WORKING WITH REAL NUMBERS


==============================
#include<stdio.h>
#include<conio.h>
void main( )
{
float rate;
rate = 12.75;
clrscr( );
printf("Item Cost : %f\n",rate);
printf("Item Cost : %.2f\n",rate);
printf("Item Cost : %g",rate);
getch( );
}
OUTPUT
=======
Item Cost : 12.750000
Item Cost : 12.75
Item Cost : 12.75

NOTE :- BY DEFAULT FLOAT DISPLAYS 6 DECIMALS.


#include<stdio.h>
#include<conio.h>
void main()
{
int a=9,b=2;
float c;
clrscr();
c=a/b;
printf("C val : %.2f",c);
c=(float)a/b; //type casting
printf("\nC val : %.2f",c);
getch();
}

WORKING WITH CHARACTERS


============================
#include<stdio.h>
#include<conio.h>
void main( )
{
char grd;
grd = 'A';
clrscr( );
printf("Grade : %c\n",grd);
printf("Rama got %c Grade",grd);
getch( );
}

OUTPUT
=======
Grade : A
Rama got A Grade

NOTE :- CHARACTER DATA MUST BE PLACED INSIDE THE SINGLE QUOTES.

WORKING WITH STRINGS


=======================
#include<stdio.h>
#include<conio.h>
void main( )
{
char name[10] = "Rama";
clrscr( );
printf("Name : %s\n",name);
printf("My Name : %s\n",name);
printf("Your Name : %s\n",name);
printf("%s is husband of sita\n",name);
printf("%s killed Ravana\n",name);
printf("Ravana was killed by %s",name);
getch( );
}

OUTPUT
=======
Name : Rama
My Name : Rama
Your Name : Rama
Rama is husband of sita
Rama Killed Ravana
Ravana was killed by Rama
NOTE :- STRINGS MUST BE PLACED INSIDE THE DOUBLE QUOTES.

AREA OF CIRCLE
================
#include<stdio.h>
#include<conio.h>
void main( )
{
int rds;
float area;
rds = 6;
area = 3.14 * rds * rds;
clrscr( );
printf("Area of Circle : %.2f",area);
getch( );
}

OUTPUT
=======
Area of Circle : 113.04

PERIMETER OF CIRCLE
=====================
#include<stdio.h>
#include<conio.h>
void main( )
{
int rds;
float prmtr;
rds = 6;
prmtr = 2 * 3.14 * rds;
clrscr( );
printf("Perimeter of Circle : %.2f",prmtr);
getch( );
}

OUTPUT
=======
Perimeter of Circle : 37.68

AREA OF SQUARE
================
#include<stdio.h>
#include<conio.h>
void main( )
{
int side,area;
side = 6;
area = side * side;
clrscr( );
printf("Area of Square : %d",area);
getch( );
}

OUTPUT
=======
Area of Square : 36
AREA OF RECTANGLE
====================
#include<stdio.h>
#include<conio.h>
void main( )
{
int len,brdth,area;
len = 6;
brdth = 3;
area = len * brdth;
clrscr( );
printf("Area of Rectangle : %d",area);
getch( );
}

OUTPUT
=======
Area of Rectangle : 18

AREA OF TRIANGLE
==================
#include<stdio.h>
#include<conio.h>
void main( )
{
int base,hgt;
float area;
base = 7;
hgt = 3;
area = 0.5 * base * hgt; // area = 1/2 * base * hgt;
clrscr( );
printf("Area of Triangle : %.1f",area);
getch( );
}

OUTPUT
=======
Area of Triangle : 10.5

STUDENT INFORMATION
===================
STUDENT NO : 22
STUDENT NAME : RAJESH
HALLTICKET NO : 456789
TELUGU MARKS : 89
ENGLISH MARKS : 90
MATHS MARKS : 78
TOTAL MARKS : ?
AVERAGE MARKS : ?
STUDENT GRADE : A
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int sno=22,tel=89,eng=90,mat=87,tot;
char sname[10]="RAKESH",grade='A';
long int htno=456789;
float avg;
clrscr();
printf("\t\t\tSTUDENT INFORMATION");
printf("\n\t\t\t===================");
printf("\nSTUDENT NO : %d",sno);
printf("\nSTUDENT NAME : %s",sname);
printf("\nHALL TICKET NO : %ld",htno);
printf("\nTELUGU MARKS : %d",tel);
printf("\nENGLISH MARKS : %d",eng);
printf("\nMATHS MARKS : %d",mat);
tot=tel+eng+mat;
//avg=(float)tot/3;
avg=tot/3.0;
printf("\nTOTAL MARKS : %d",tot);
printf("\nAVERAGE MARKS : %.2f",avg);
printf("\nSTUDENT GRADE : %c",grade);
getch();
}

scanf():- This function is used to input various types of data from standard input
device.

syntax:- scanf("control string",args list);

control string:- It contains only conversion characters or format specifiers (%s %d


%f %c etc..)

args list :- The arguments in the scanf are variables, all these variables
used in the scanf must be pre fixed with an ampersand (&) address operator

commonly used conversion characters:-


%d (or) %i :- integer
%c :- character
%f :- float (dont use %.2f)
%s :- string without white space character (blank,tab,enter)
%[ ] :- string including white space character
%ld :- long int
%lf :- long float

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter a value :");
scanf("%d",&a);
a++;
printf("a value : %d",a);
getch();
}
op:
Enter a value : 15
a value : 16
w a p to input two nos print the sum of two nos.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter a value :");
scanf("%d",&a);
printf("Enter b value :");
scanf("%d",&b);
c=a*b;
printf("c value is : %d",c);
getch();
}

write a program to input a no findout square and cube of no.


Enter a no 5 enter
op:-Square is 25
cube is 125

#include<stdio.h>
#include<conio.h>
void main()
{
int no,s,c;
clrscr();
printf("Enter a value :");
scanf("%d",&no);
s=no*no;
c=s*no;
printf("Square is : %d",s);
printf("\nCube is : %d",c);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter a value :");
scanf("%d",&a);
printf("Square is : %d",a*a);
printf("\nCube is : %d",a*a*a);
getch();
}
W a p to input no days find out how many days and months.
#include<stdio.h>
#include<conio.h>
void main()
{
int days;
clrscr();
printf("Enter a days :");
scanf("%d",&days);
printf("%d months %d days",days/30,days%30);
getch();
}
w a p to input monthly salary findout annual salary

#include<stdio.h>
#include<conio.h>
void main()
{
long int msal;
clrscr();
printf("Enter your monthly salary :");
scanf("%ld",&msal);
printf("Your annual salary is : %ld",msal*12);
getch();
}

w a p to input rate,qty, findout amount,discount(10%),net amount.

Enter rate 50
Enter Qty 60
output:-
Amount is 3000
Discount is 300
Net amount is 2700

#include<stdio.h>
#include<conio.h>
void main()
{
float rate,qty,amt,dis,net;
clrscr();
printf("Enter rate :");
scanf("%f",&rate);
printf("Enter qty");
scanf("%f",&qty);
amt=rate*qty;
dis=amt*15/100;
net=amt-dis;
printf("\nAmount is %.2f",amt);
printf("\nDiscount is %.2f",dis);
printf("\nNet amount is %.2f",net);
getch();
}

Note:- Scanf terminates reading data when ever it encounters any white space
character. (blank,tab,enter)
w a p to input marks in 3 subjects findout total,avg.

#include<stdio.h>
#include<conio.h>
void main()
{
int no,s1,s2,s3,tot;
char name[10];
float avg;
clrscr();
printf("Enter student no,name");
scanf("%d %s",&no,&name);
printf("Enter s1,s2,s3 values :");
scanf("%d %d %d",&s1,&s2,&s3);
tot=s1+s2+s3;
avg=tot/3.0;
printf("\nStudent no : %d",no);
printf("\nStudent name : %s",name);
printf("\ntotal marks : %d",tot);
printf("\nAvg marks : %.2f",avg);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a character");
scanf("%c",&ch);
printf("The character is %c",ch);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
clrscr();
printf("Enter a name");
scanf("%s",&name);
printf("Name is : %s",name);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20],town[20];
clrscr();
printf("Enter a name");
scanf("%s",&name);
printf("Enter a Town");
scanf("%s",&town);
printf("Hello %s Welcome to %s",name,town);
getch();
}
String including White space characters:-
To input no of words

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20],town[20];
clrscr();
printf("Enter a name");
scanf("%[^\n]",&name);
printf("Enter a Town");
scanf("%s",&town);
printf("Hello %s Welcome to %s",name,town);
getch();
}
To input no of lines

#include<stdio.h>
#include<conio.h>
void main()
{
char addr[200];
clrscr();
printf("Enter a address (ends with &).. ");
scanf("%[^&]",&addr);
printf("The address is : %s",addr);
getch();
}

PROBLEMS WHILE INPUT CHARACTERS & STRINGS


===============================================
IN 'C' LANGUAGE WE CAN'T INPUT TWO CHARACTERS / STRINGS ONE BY ONE. BECAUSE ENTER
KEY IS STORED IN SECOND CHARACTER VARIABLE. IN THIS SITUTATION FFLUSH( ) FUNCTION
IS USEFUL.

fflush( ) :- THIS FUNCTION IS USED TO CLEAR KEYBOARD MEMORY. IT MEANS CLEARS ENTER
KEY.

# include <stdio.h>
# include <conio.h>
void main()
{
char ch1,ch2;
clrscr();
puts("Enter First Character");
scanf("%c",&ch1);
fflush(stdin); // stdin : STANDARD INPUT : KEYBOARD
puts("Enter Second Character");
scanf("%c",&ch2);
printf("First Character: %c\n",ch1);
printf("Second Character: %c",ch2);
getch();
}

OUTPUT
=======
Enter First Character
k
Enter Second Character
v
First Character : k
Second Character : v

getchar( ):- THIS FUNCTION IS USED TO INPUT A CHARACTER.


syntax:- char getchar();

#include<stdio.h>
#include<conio.h>
void main()
{
char grd;
clrscr();
printf("Enter Grade\n");
grd = getchar(); //scanf("%c",&grd);
printf("Grade : %c\n",grd);
getch();
}

putchar( ):- THIS FUNCTION IS USED TO DISPLAY A CHARACTER VARIABLE / CONSTANT.


Syntax:-putchar(char);
#include<stdio.h>
#include<conio.h>
void main()
{
char grd = 'A';
clrscr();
putchar(grd);
putchar('\n');
putchar('K');
getch();
}

puts():- This function is used to dispaly a string on the standard output


device.This function generates new line by default after printing the string
syntax:- puts(string);

#include<stdio.h>
#include<conio.h>
void main()
{
char name[10]="Raju";
clrscr();
printf("One");
printf("Two");
printf("Three");
printf("%s",name);
puts("One");
puts("Two");
puts("Three");
puts("name");
puts(name);
getch();
}
OneTwoThreeRajuOne
Two
Three
name
Raju

GETS( ) IS USED TO INPUT MULTIPLE WORDS UPTO ONE LINE ONLY. BECAUSE GETS( )
TERMINATES WITH ENTER KEY. GETS( ) IS UNABLE TO INPUT MULTIPLE LINES. TO INPUT
MULTIPLE LINES WE CAN USE SCANF( ) INSTEAD OF GETS( ).
syntax:- gets(string);
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
clrscr();
printf("Enter a name");
//scanf("%s",&name); one word
//scanf("%[^\n]",&name); one line of words
gets(name); //one line of words
printf("The name is %s",name);
getch();
}
ip: raja ram mohan
op: raja ram mohan

CONTROL FLOW STATEMENTS


============================

FLOW OF EXECUTION :- THE WAY THE PROGRAM IS EXECUTED. GENERALLY PROGRAMS ARE
EXECUTED IN TWO TYPES OF FLOW OF EXECUTIONS.

1) SEQUENTIAL EXECUTION

2) RANDOM EXECUTION

SEQUENTIAL EXECUTION :- IN THIS MODEL, ALL STATEMENTS ARE EXECUTED ONE BY ONE (TIME
WASTE).

RANDOM EXECUTION :- IN THIS MODEL, WE CAN DIRECTLY EXECUTE THE REQUIRED STATEMENT
(TIME SAVE). TO APPLY RANDOM EXECUTION WE NEED CONTROL FLOW STATEMENTS.

CONTROL FLOW STATEMENTS :- THESE ARE THE STATEMENTS TO CHANGE FLOW OF EXECUTION.
THESE STATEMENTS ARE USED TO TAKE DECISIONS AND TO ITERATE (LOOP) SET OF
STATEMENTS. WITH THE HELP OF CONTROL FLOW STATEMENTS PROGRAMMER HAS BETTER CONTROL
ON HIS PROGRAM.

BRANCHING STATEMENTS :- THESE STATEMENTS ARE USED TO TAKE DECISIONS.

Ex :- IF STATEMENT, SWITCH STATEMENT

ITERATING / LOOPING STATEMENTS :- THESE STATEMENTS ARE USED TO EXECUTE SET OF


STATEMENTS REPEATEDLY.

Ex :- FOR LOOP, WHILE LOOP, DO WHILE LOOP

JUMPING STATEMENTS :- THESE STATEMENTS ARE USED TO TRANSFER CONTROL TO REQUIRED


STATEMENT.

Ex :- BREAK STATEMENT, CONTINUE STATEMENT, GOTO STATEMENT, RETURN STATEMENT

IF STATEMENT
=============

THIS STATEMENT IS USED TO CHECK CONDITIONS AND TO COMPARE VALUES. THIS STATEMENT IS
AGAIN CLASSIFIED INTO THE FOLLOWING FOUR CATEGORIES.

1) SIMPLE IF STATEMENT

2) IF ELSE STATEMENT

3) ELSE IF STATEMENT

4) NESTED IF STATEMENT

SIMPLE IF STATEMENT
=====================
BLOCK / COMPOUND STATEMENT :- SET OF STATEMENTS INSIDE THE BRACES. IF A BLOCK
CONTAINS ONLY ONE STATEMENT THEN BRACES ARE OPTIONAL. BUT PLACING BRACES IS
RECOMONDED.
THIS STATE MENT IS USED TO CHECK WHETHER A CONDITION IS TRUE OR FALSE.

SYNTAX
=======

IF(CONDITION)
{
STATEMENT 1;
STATEMENT 2;
STATEMENT 3;
}
STATEMENT X;

IF THE CONDITION IS TRUE THEN THE BLOCK (FROM STATEMENT 1 TO STATEMENT 3) IS


EXECUTED. OTHERWISE THE BLOCK IS NOT EXECUTED. STATEMENT X IS COMMON STATEMENT. IT
CAN'T BE EFFECTED BY IF STATEMENT. IT IS EXECUTED IF THE CONDITION IS EITHER TRUE
OR FALSE.

GENERALLY SIMPLE IF STATEMENT IS USED TO SELECT ONE OPTION.

# include <stdio.h>
# include <conio.h>
void main()
{
clrscr( );
if(10 > 5)
{
printf("Hai");
}
getch( );
}

OUTPUT
=======
Hai

# include <stdio.h>
# include <conio.h>
void main()
{
clrscr( );
if(10 > 50)
{
printf("Hai");
}
getch( );
}

OUTPUT
=======
BLANK

# include <stdio.h>
# include <conio.h>
void main( )
{
clrscr();
if(10 > 5)
{
printf("Hai,");
printf("How r u? ");
printf("Bye");
}
getch( );
}

OUTPUT
=======
Hai,How r u? Bye

# include <stdio.h>
# include <conio.h>
void main()
{
clrscr( );
if(10 > 50)
{
printf("Hai,");
printf("How r u? ");
printf("Bye");
}
getch( );
}

OUTPUT
=======
BLANK

# include <stdio.h>
# include <conio.h>
void main()
{
clrscr( );
if(10 > 5)
{
printf("Hai");
}
printf("Bye");
getch( );
}

OUTPUT
=======
HaiBye

# include <stdio.h>
# include <conio.h>
void main()
{
clrscr( );
if(10 > 50)
{
printf("Hai");
}
printf("Bye");
getch( );
}
OUTPUT
=======
Bye

# include <stdio.h>
# include <conio.h>
void main()
{
int m;
clrscr( );
puts("Enter Marks");
scanf("%d",&m);
if(m >= 35)
{
printf("Result : Pass");
}
getch( );
}

OUTPUT 1
=========
Enter Marks
98
Result : Pass

OUTPUT 2
=========
Enter Marks
32
BLANK

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
int n,r;
clrscr();
printf("Enter Number\n");
scanf("%d",&n);
if(n > 0)
{
r = sqrt(n);
printf("Square Root : %d\n",r);
}
getch();
}
if else :- to select one option from two options.
syntax:- if(exp/condition)
{
STATEMENTS;
}
else
{
statements;
}
statement x;

Note:- If the condition is true then the statements followed by if will be


executed, other wise the statements followed by else will be executed.Statement x
is common statement. it can not be effected by if else statement. It is executed if
the condition is true or false.

#include<stdio.h>
#include<conio.h>
void main()
{
int no;
clrscr();
printf("enter a no");
scanf("%d",&no);
if(no>0)
printf("it is positive no");
else
printf("it is negative no");
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int no,r;
clrscr();
printf("enter a no");
scanf("%d",&no); 8
r=no%2;
if(r==0)
printf("it is even no");
else
printf("it is odd no");
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int no;
clrscr();
printf("enter a no");
scanf("%d",&no);
if(no%2==0)
printf("it is even no");
else
printf("it is odd no");
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int no;
clrscr();
printf("enter a no");
scanf("%d",&no);
if(no%2)
printf("it is odd no");
else
printf("it is even no");
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int y;
clrscr();
printf("enter a year");
scanf("%d",&y);
if(y%4==0)
printf("it is leap year");
else
printf("it is not leap year");
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int cp,sp;
clrscr();
printf("enter cost of price");
scanf("%d",&cp);
printf("enter selling price");
scanf("%d",&sp);
if(sp>cp)
printf("you got profit %d Rs",sp-cp);
else
printf("you got %d Rs loss",cp-sp);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3;
clrscr();
printf("enter marks in 3 subs");
scanf("%d %d %d",&s1,&s2,&s3);
if(s1>=35 && s2>=35 && s3>=35)
printf("Result : Pass");
else
printf("Result : Fail");
getch();
}
case conversion functions:-
prototype<ctype.h>
tolower():-This function is used to convert a character in to lower case.
syntax:- char tolower(char);
toupper():- It converts a characater in to upper case.
syntax:- char toupper(char);
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("enter a character");
scanf("%c",&ch);
ch=tolower(ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
printf("it is vowel");
else
printf("it is consonant");
getch();
}
=============
else if:-It is used to select one option from no of options.
syntax:-
if(cond1)
{
statements;
}
else if(cond2)
{
statements;
}
else if(cond n)
{
statements;
}
else
{
statements;
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a,b values");
scanf("%d%d",&a,&b);
if(a>b)
printf("A is big");
else if(b>a)
printf("B is big");
else
printf("Both are same");
getch();
}
textcolor():- It is set a color. In C there 16 predefined colors available
for dispaly output in various colors. All color names must be capital letters.
for colors help type any color in capital letters like RED then press mouse
right button on color.
cprintf():- It displays the text in the color specified by textcolor.

#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
clrscr();
puts("1.Red");
puts("2.Green");
puts("3.Yellow");
printf("Enter your choice(1,2,3)..");
scanf("%d",&ch);
if(ch==1)
{
textcolor(RED);
cprintf("Informatics");
}
else if(ch==2)
{
textcolor(GREEN);
cprintf("You are Green selected");
}
else if(ch==3)
{
textcolor(YELLOW);
textbackground(GREEN);
cprintf("Institute");
}
else
{
//textcolor(LIGHTMAGENTA+BLINK);
textcolor(13+128);
cprintf("Invalid choice");
}
getch();
}
Colors - characters compare R.Red,G.Green,Y.Yellow etc..
w a p to input 3 nos findout biggest no.
w a p to input a character findout the given character is alphabet or digit
or special symbol

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("R.Red\nG.Green\nY.Yellow\n");
printf("Enter your choice(R,G,Y)..");
scanf("%c",&ch);
ch=toupper(ch);
if(ch=='R')
{
textcolor(RED);
cprintf("Informatics");
}
else if(ch=='G')
{
textcolor(GREEN);
cprintf("You are Green selected");
}
else if(ch=='Y')
{
textcolor(YELLOW);
textbackground(GREEN);
cprintf("Institute");
}
else
{
//textcolor(LIGHTMAGENTA+BLINK);
textcolor(13+128);
cprintf("Invalid choice");
}
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter a,b,c values");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
printf("A is big");
else if(b>a&&b>c)
printf("B is big");
else
printf("C is big");
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a character");
scanf("%c",&ch);
printf("The character is : %c\n",ch);
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
//if((ch>=65&&ch<=90)||(ch>=97&&ch<=122))
printf("It is alphabet");
else if(ch>='0'&&ch<='9')
printf("It is digit");
else
printf("it is special symbol");
getch();
}

w a p to input rate,qty findout amount,discount,netamount?


discount rate are:-
amt<1000 no discount
amt>=1000 dis 5%
amt>=2000 dis 10%
amt>=3000 dis 20%
amt>=5000 dis 30%
#include<stdio.h>
#include<conio.h>
void main()
{
float rate,qty,amt,dis,netamt;
clrscr();
printf("Enter rate");
scanf("%f",&rate);
printf("Enter quantity");
scanf("%f",&qty);
amt=rate*qty;
if(amt<1000)
{
dis=0;
netamt=amt-dis;
printf("\nAmount is : %.2f",amt);
printf("\nDiscount is : %.2f",dis);
printf("\nNet amount is : %.2f",netamt);
}
else if(amt>=1000&&amt<2000)
{
dis=amt*5/100;
netamt=amt-dis;
printf("\nAmount is : %.2f",amt);
printf("\nDiscount is : %.2f",dis);
printf("\nNet amount is : %.2f",netamt);
}
else if(amt>=2000&&amt<3000)
{
dis=amt*10/100;
netamt=amt-dis;
printf("\nAmount is : %.2f",amt);
printf("\nDiscount is : %.2f",dis);
printf("\nNet amount is : %.2f",netamt);
}
else if(amt>=3000&&amt<5000)
{
dis=amt*20/100;
netamt=amt-dis;
printf("\nAmount is : %.2f",amt);
printf("\nDiscount is : %.2f",dis);
printf("\nNet amount is : %.2f",netamt);
}
else
{
dis=amt*30/100;
netamt=amt-dis;
printf("\nAmount is : %.2f",amt);
printf("\nDiscount is : %.2f",dis);
printf("\nNet amount is : %.2f",netamt);
}
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
float rate,qty,amt,dis,netamt;
clrscr();
printf("Enter rate");
scanf("%f",&rate);
printf("Enter quantity");
scanf("%f",&qty);
amt=rate*qty;

if(amt>=5000)
dis=amt*30/100;
else if(amt>=3000)
dis=amt*20/100;
else if(amt>=2000)
dis=amt*.10;
else if(amt>=1000)
dis=amt*5/100;
else
dis=0;
netamt=amt-dis;
printf("\nAmount is : %.2f",amt);
printf("\nDiscount is : %.2f",dis);
printf("\nNet amount is : %.2f",netamt);
getch();
}

w a p to input consumer no,consumer name,last month reading,present month


reading findout units and bill amount using below tarrif.
0 to 50 units - 2.50 (min)
51 to 100 units - 3.75
101 to 200 units - 4.50
201 to 300 units - 6.00
above 300 units - 8.00

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int lmr,pmr,units,cno;
float bamt;
char cname[20];
clrscr();
printf("Enter consumer no");
scanf("%d",&cno);
printf("Enter consumer name");
scanf("%s",&cname);
printf("Enter last month reading");
scanf("%d",&lmr);
printf("Enter present month reading");
scanf("%d",&pmr);
if(pmr<lmr)
{
printf("Invalid reading");
getch();
exit(0);
}
units=pmr-lmr;
printf("No of units is : %d",units);
if(units<=50)
bamt=50*2.50;
else if(units<=100)
bamt=(50*2.50)+(units-50)*3.75;
else if(units<=200)
bamt=(50*2.50)+(50*3.75)+(units-100)*4.50;
else if(units<=300)
bamt=(50*2.50)+(50*3.75)+(100*4.50)+(units-200)*6.00;
else
bamt=(50*2.50)+(50*3.75)+(100*4.50)+(100*6.00)+(units-300)*8.00;

printf("\nBill amount is : %.2f",bamt);


getch();
}
nested if:- Placing a if with in another if

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("Enter age");
scanf("%d",&age);
if(age>13)
{
if(age<=19)
printf("Teenage");
else
printf("Crossed Teenage");
}
else
printf("Below Teenage");
getch();
}
w a p to input marks in 3 subjects findout total,avg,grade if student is pass

#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3,tot;
float avg;
clrscr();
printf("Enter marks in 3 subjects");
scanf("%d%d%d",&s1,&s2,&s3);
if(s1>=35&&s2>=35&&s3>=35)
{
printf("Result : Pass");
tot=s1+s2+s3;
avg=tot/3.0;
printf("\nTotal marks : %d",tot);
printf("\nAverage marks : %.2f\n",avg);
if(avg>=80)
printf("A+ Grade");
else if(avg>=60)
printf("A grade");
else if(avg>=50)
printf("B grade");
else
printf("C grade");
}
else
{
printf("Result : Fail");
}
getch();
}
SWITCH STATEMENT
==================

IT IS USED FOR COMPARISION PURPOSE. USING SWITCH STATEMENT WE CAN SELECT OPTIONS
DIRECTLY. BY USING SWITCH STATEMENT WE CAN COMPARE ONLY INT AND CHAR. SWITCH
STATEMENT HAVE SEVERAL CASES AND ONE DEFAULT OPTION. IF THE RELATED CASE IS NOT
FOUND THEN DEFAULT BLOCK IS EXECUTED. EVERY CASE MUST ENDS WITH BREAK STATEMENT.

SYNTAX
=======

SWITCH(VARIABLE)
{
CASE LABEL 1 :
STATEMENTS;
BREAK;
CASE LABEL 2 :
STATEMENTS;
BREAK;
CASE LABEL 3 :
STATEMENTS;
BREAK;
DEFAULT :
STATEMENTS;
}

THE LABEL MUST BE EITHER INT OR CHAR.

#include<conio.h>
#include<stdio.h>
void main()
{
int ch;
clrscr();
printf("1.Sunday\n2.Monday\n3.Tuesday");
printf("\nEnter your choice(1,2,3)..");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("it is sunday");
printf("\nHoliday");
break;
case 2:
printf("It is monday");
printf("\nIt is working day");
break;
case 3:
printf("It is Tuesday");
break;
default:
printf("Invalid choice");
}
getch();
}

#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("Enter a character");
scanf("%c",&ch);
ch=tolower(ch);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("It is vowel");
break;
default:
printf("It is consonant");
}
getch();
}

#include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
char ch;
clrscr();
printf("Enter a,b values");
scanf("%d%d",&a,&b);
printf("Enter your choice(+,-,*)..");
fflush(stdin);
scanf("%c",&ch);
switch(ch)
{
case '+':
printf("addition is : %d",a+b);
break;
case '-':
printf("Subtraction is : %d",a-b);
break;
case '*':
printf("Multiplication is : %d",a*b);
break;
default:
printf("Invalid choice");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3,tot,avg,per;
clrscr();
printf("Enter marks in 3 subjects");
scanf("%d%d%d",&s1,&s2,&s3);
tot=s1+s2+s3;
avg=tot/3;
printf("\nTotal marks : %d",tot);
printf("\nAverage marks : %d",avg);
per=avg/10;
switch(per)
{
case 8:
case 9:
printf("A+ grade");
break;
case 6:
case 7:
printf("A grade");
break;
case 5:
printf("B grade");
break;
default:
printf("C grade");
}
getch();
}
Looping:- It is a process of executing statements repeatedly more than once. There
are 3 types of looping statements available.
1.for
2.while
3.do while
for:- It is the most commonly used looping statement.
syntax:- for(exp1;exp2;exp3)
statement;

exp1:- It is used to initilize the value of index.


exp2:- It is used for logical testing
exp3:- It is used to alter the value of index.
Note:- The statement followed by for will be executed repeatedly until the exp2
becomes false.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=5;i++)
printf("\nWelcome");
printf("\nEnd of loop");
getch();
}

for(i=1;i<=10;i=i+3)
printf("\nWelcome"); w w w w
for(i=5;i<=25;i=i+5) 30
printf("\nWelcome"); w w w w w

for(i=1;i<=25;i=i+5) 26
printf("\nWelcome"); w w w w w

for(i=5;i<=25;i=i+3)
printf("\nWelcome"); w w w w w w w
w a p to print 1 to 10 natural nos.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
printf("\n %d",i);
printf("\nEnd of loop");
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
for( ;i<=10; )
printf("\n %d",i++);
printf("\nEnd of loop");
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=11;i<=15;i++);
printf("\n %d",i);
printf("\nEnd of loop");
getch();
}
w a p to print upper case and lower case alphabets using for loops.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=65;i<=90;i++)
printf("%c ",i);

printf("\n");

for(i='a';i<='z';i++)
printf("%c ",i);
getch();
}
w a p to print 1 to 10 even nos.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
printf("\n %d",++i);

for(i=2;i<=10;i++)
printf("\n %d",i++);

for(i=2;i<=10;i=i+2)
printf("\n %d",i);

getch();
}
w a p to print 20 to 1 reverse nos.
w a p to print 20 to 1 odd nos reverse 3 models
w a p to print 1 to n natural nos.
w a p to print n1 to n2 natural nos.
w a p to print n2 to n1 reverse nos.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();

// for(i=20;i<=1;i--) blank
// printf("\n %d",i);

// for(i=20;i<=20;i--) 20 19 18 .....1 0 -1 -2 -32768


// printf("\n %d",i);

// for(i=1;i<=20;i--) 1 0 -1 -2 -3 -32768
// printf("\n %d",i);

// for(i=20;i<=1;i++) blank
// printf("\n %d",i);

// for(i=20;i>=1;i++) 20 21 22 23 ... 32767


// printf("\n %d",i);

for(i=20;i>=1;i--)
printf("\n %d",i);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
// for(i=19;i>=1;i=i-2)
// printf("\n %d",i);
// for(i=19;i>=1;i--)
// printf("\n%d",i--);
for(i=20;i>=1;i--)
printf("\n%d",--i);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("Enter n value");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d ",i);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n1,n2;
clrscr();
printf("Enter n1,n2 values");
scanf("%d%d",&n1,&n2);
for(i=n1;i<=n2;i++)
printf("%d ",i);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter n1,n2 values");
scanf("%d%d",&n1,&n2);
for( ;n1<=n2; )
printf("%d ",n1++);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter n2,n1 values");
scanf("%d%d",&n2,&n1);
for( ;n2>=n1; )
printf("%d ",n2--);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=15;i++)
{
textcolor(i);
cprintf("Welcome ");
printf("\n");
}
getch();
}

W a p to input a no and print the multiplication table of given no.


Enter a no 5
5 * 1 = 5
5 * 2 = 10
w a p to input a no and print the following format.
1 n
to to
n 1

#include<stdio.h>
#include<conio.h>
void main()
{
int no,i;
clrscr();
printf("Enter a no");
scanf("%d",&no);
for(i=1;i<=10;i++)
printf("\n %d * %d = %d",no,i,no*i);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,j;
clrscr();
printf("Enter a no");
scanf("%d",&no);
for(i=1,j=no;i<=no,j>=1;i++,j--)
printf("\n%d\t%d",i,j);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int no,i;
clrscr();
printf("Enter a no");
scanf("%d",&no);
for(i=1;i<=no;i++)
printf("\n%d\t%d",i,(no+1)-i);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int no,i;
clrscr();
printf("Enter a no");
scanf("%d",&no);
for(i=1;i<=no;i++)
printf("\n%d\t%d",i,no--);
getch();
}
sum of 1 to n digits.
Enter a no 5
output:- Sum of 1 to 5 Digits is : 15
output:- 1+2+3+4+5=15

#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,sum=0;
clrscr();
printf("Enter a no");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
sum=sum+i;
}
printf("\nSum of 1 to %d digits %d",no,sum);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,sum=0;
clrscr();
printf("Enter a no");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
sum=sum+i;
printf("%d+",i);
}
printf("\b=%d",sum);
getch();
}

1.
Enter a no 5
5 Fact is : 120
5*4*3*2*1=120
1*2*3*4*5=120
2.
Enter base,power values 2 5 Result: 32
3 4 Result: 81
=====

#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,fact=1;
clrscr();
printf("Enter a no");
scanf("%d",&no);
//for(i=no;i>=1;i--)
for(i=1;i<=no;i++)
{
fact=fact*i;
printf("%d*",i);
}
printf("\b=%d",fact);
getch();
}

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int b,p,r;
clrscr();
printf("Enter base,power nos");
scanf("%d%d",&b,&p);
r=pow(b,p);
printf("%d power %d is : %d",b,p,r);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i,b,p,r=1;
clrscr();
printf("Enter base,power nos");
scanf("%d%d",&b,&p);
for(i=1;i<=p;i++)
{
r=r*b;
}
printf("\n%d power %d is : %d",b,p,r);
getch();
}
w a p to print n1 to n2 even nos

#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,i;
clrscr();
printf("Enter n1,n2 values");
scanf("%d%d",&n1,&n2);
if(n1%2==1)
n1++;
for(i=n1;i<=n2;i=i+2)
printf("\n %d",i);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,i;
clrscr();
printf("Enter n1,n2 values");
scanf("%d%d",&n1,&n2);
for(i=n1;i<=n2;i++)
if(i%2==0)
printf("\n %d",i);
getch();
}
w a p to print even nos,odd nos upto n.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("Enter n value");
scanf("%d",&n);
printf("Even nos are\n");
for(i=1;i<=n;i++)
if(i%2==0)
printf("%d\n",i);

printf("Odd nos are\n");


for(i=1;i<=n;i++)
if(i%2==1)
printf("%d\n",i);
getch();
}
Even nos total, Odd nos total upto n
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,os=0,es=0;
clrscr();
printf("Enter n value");
scanf("%d",&n);
printf("======== =========\n");
printf("Odd Nos Even nos\n");
printf("======== =========\n");
for(i=1;i<=n;i++)
{
if(i%2==1)
{
printf("%d\t",i);
os=os+i;
}
else
{
printf("%d\n",i);
es=es+i;
}
}
printf("======== =========\n");
printf("%d\t%d\n",os,es);
printf("======== =========\n");
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n1,n2,c=0;
clrscr();
printf("enter n1,n2 values");
scanf("%d%d",&n1,&n2);
for(i=n1;i<=n2;i++)
if(i%2==0)
{
printf("\n%d",i);
c++;
}
printf("\nNo of even nos is %d",c);
getch();
}
w a p to input a no findout the given no is prime or not.
Enter a no 5
5%1 5%2 5%3 5%4 5%5
0 1 2 1 0 = 2 zeros prime
Enter a no 6
6%1 6%2 6%3 6%4 6%5 6%6
0 0 0 2 1 0 more than 2 zeros not prime

#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,c=0;
clrscr();
printf("Enter a no");
scanf("%d",&no);
for(i=1;i<=no;i++)
if(no%i==0)
c++;

if(c==2)
printf("It is prime no");
else
printf("It is not prime no");
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,c=0;
clrscr();
printf("Enter a no");
scanf("%d",&no);
for(i=2;i<no;i++)
if(no%i==0)
c++;

if(c==0)
printf("It is prime no");
else
printf("It is not prime no");
getch();
}
break:- This statement is used to terminate from loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=20;i++)
{
printf("\n %d",i);
if(i==5)
break;
}
printf("\n End of loop");
getch();
}
continue:- This statement is used to transfer the control for the next
iteration (++,--) in loop statement.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=20;i++)
{
if(i==5||i==10)
continue;
printf("\n %d",i);
}
printf("\n End of loop");
getch();
}
exit():- This function is used to terminate from program.
syntax:- exit(int);
prototype:- #include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int i;
clrscr();
for(i=1;i<=20;i++)
{
printf("\n %d",i);
if(i==12)
exit(0);
}
printf("\n End of loop");
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
if(i!=6)
printf("\n %d",i);
printf("\n End of loop");
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
if(i!=6)
printf("\n %d",i);
printf("\n End of loop");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
if(i!=4)
continue;
printf("\n %d",i);
printf("\n End of loop");
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
if(i!=4)
continue;
printf("\n %d",i);
}
printf("\n End of loop");
getch();
}

nested loops:- placing a loop with in another loop

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
printf("\n Welcome");
printf("\n End of loop");
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
clrscr();
for(i=1;i<=3;i++)
for(j=1;j<=5;j++)
printf("\n %d\t%d",j,i);
printf("\n End of loop");
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
clrscr();
for(i=1;i<=5;i++)
{
printf("Hello\t");
for(j=1;j<=5;j++)
{
printf("Hai\t");
}
printf("Bye\n");
}
getch();
}
*
* *
* * *
* * * *
* * * * *
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(" *");
}
printf("\n");
}
getch();
}

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

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(" %d",i);
}
printf("\n");
}
getch();
}
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k=1;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(" %d",k);
k++;
}
printf("\n");
}
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,b;
clrscr();
for(i=1;i<=5;i++)
{
b=1;
for(j=1;j<=5;j++)
if(i+j<=5)
{
printf(" ");
}
else
{
printf("%2d",b);
b++;
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,b=1;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
if(i+j<=5)
{
printf(" ");
}
else
{
printf("%3d",b);
b++;
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,b;
clrscr();
for(i=1;i<=5;i++)
{
b=1;
for(j=1;j<=5;j++)
if(i>j)
{
printf(" ");
}
else
{
printf("%2d",b);
b++;
}
printf("\n");
}
getch();
}
n1 to n2 multiplication tabels
n1 to n2 prime nos
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,i,j;
clrscr();
printf("Enter n1,n2 values");
scanf("%d%d",&n1,&n2);
for(i=n1;i<=n2;i++)
{
for(j=1;j<=10;j++)
{
printf("\n %d * %d = %d",i,j,i*j);
}
printf("\nPress any key to continue..");
getch();
clrscr();
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,i,j,c=0;
clrscr();
printf("Enter n1,n2 values");
scanf("%d%d",&n1,&n2);
for(i=n1;i<=n2;i++)
{
c=0;
for(j=1;j<=i;j++)
if(i%j==0)
c++;
if(c==2)
printf("%d ",i);
}
getch();
}

while:- It is called entry control loop statement.


syntax:-
Initilization;
while(condition)
{
statements;
status;
}
Note:- The statements followd by while will be executed repeatedly
until the condition becomes false.

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while(i<=5)
{
printf("Welcome\n");
i++;
}
printf("End loop");
getch();
}
1 to 10 natural nos
1 to 10 odd nos
20 to 40 even nos
20 to 1 reverse nos
1 to n natural nos
n1 to n2 natural nos
multiplication table of given no
sum of 1 to n digits
factorial value of given no
power value (2 power 5 is 32)
even nos,odd nos upto n
prime no or not

How many digits


sum of digits
how many digits
reverse no
palindrome no
armstrong no
fibonacci series

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while(i<=10)
{
printf("%d\n",i);
i++;
}
printf("End loop");
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while(i<=10)
{
printf("%d\n",i);
i=i+2;
}
printf("End loop");
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i=20;
clrscr();
while(i<=40)
{
printf("%d\n",i);
i=i+2;
}
printf("End loop");
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter n1,n2 values");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
printf("%d ",n1);
n1++;
}
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n;
clrscr();
printf("Enter n value");
scanf("%d",&n);
while(i<=10)
{
printf("%d * %d = %d\n",n,i,n*i);
i++;
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,sum=0;
clrscr();
printf("Enter n value");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("\nsum of 1 to %d digits : %d",n,sum);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
long int n,i=1,fact=1;
clrscr();
printf("Enter n value");
scanf("%ld",&n);
while(i<=n)
{
fact=fact*i;
i++;
}
printf("\n%ld fact is : %ld",n,fact);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int b,p,i=1,r=1;
clrscr();
printf("Enter base,power values");
scanf("%d%d",&b,&p);
while(i<=p)
{
r=r*b;
i++;
}
printf("\n%d power %d is : %d",b,p,r);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1;
clrscr();
printf("Enter n value");
scanf("%d",&n);
printf("Even nos are");
while(i<=n)
{
if(i%2==0)
printf("\n%d",i);
i++;
}
printf("\nOdd nos are");
i=1;
while(i<=n)
{
if(i%2==1)
printf("\n%d",i);
i++;
}
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("Enter n value");
scanf("%d",&n);
printf("Even nos are");
i=2;
while(i<=n)
{
printf("\n%d",i);
i=i+2;
}
printf("\nOdd nos are");
i=1;
while(i<=n)
{
printf("\n%d",i);
i=i+2;
}
getch();
}

How many digits


10)789(78
780
9
10)78(7
70
8
10)7(0
7
#include<stdio.h>
#include<conio.h>
void main()
{
int no,c=0;
clrscr();
printf("Enter a no");
scanf("%d",&no);
while(no>0)
{
c++;
no=no/10;
}
printf("No of digits : %d",c);
getch();
}
sum of digits
#include<stdio.h>
#include<conio.h>
void main()
{
int no,r,sum=0;
clrscr();
printf("Enter a no");
scanf("%d",&no);
while(no>0)
{
r=no%10;
sum=sum+r;
no=no/10;
}
printf("sum of digits : %d",sum);
getch();
}
reverse no
#include<stdio.h>
#include<conio.h>
void main()
{
int no,r,sum=0;
clrscr();
printf("Enter a no");
scanf("%d",&no);
while(no>0)
{
r=no%10;
sum=(sum*10)+r;
no=no/10;
}
printf("Reverse value : %d",sum);
getch();
}
palindrome or not
#include<stdio.h>
#include<conio.h>
void main()
{
int no,r,sum=0,t;
clrscr();
printf("Enter a no");
scanf("%d",&no);
t=no;
while(no>0)
{
r=no%10;
sum=(sum*10)+r;
no=no/10;
}
if(sum==t)
printf("It is palindrome : %d",sum);
else
printf("It is not palindrome : %d",sum);
getch();
}
Armstrong or not (153,370,371)
1 cube+5 cube+3 cube
1+125+27=153
125
1+8+125=134

#include<stdio.h>
#include<conio.h>
void main()
{
int no,r,sum=0,t;
clrscr();
printf("Enter a no");
scanf("%d",&no);
t=no;
while(no>0)
{
r=no%10;
sum=sum+(r*r*r);
no=no/10;
}
if(sum==t)
printf("It is armstrong : %d",sum);
else
printf("It is not armstrong : %d",sum);
getch();
}
Fibonacci series ( 0 1 1 2 3 5 8 13 21 34 55 89 )
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,f1=0,f2=1,f3;
clrscr();
printf("Enter n value");
scanf("%d",&n);
printf("%d %d",f1,f2);
while(i<=n)
{
f3=f1+f2;
printf(" %d",f3);
f1=f2;
f2=f3;
i++;
}
getch();
}
do while:- It is called exit control loop statements.
syntax:-
do
{
statements;
}
while(condition);

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

#include<stdio.h>
#include<conio.h>
void main()
{
int empno;
char name[10],ch;
float sal;
clrscr();
do
{
printf("Enter emp no");
scanf("%d",&empno);
printf("Enter emp name");
scanf("%s",&name);
printf("Enter emp salary");
scanf("%f",&sal);
printf("%d\t%s\t%.2f",empno,name,sal);
printf("\nDo you want to continue(y/n)..");
fflush(stdin);
scanf("%c",&ch);
}
while(ch!='n');
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
char a[10];
clrscr();
a="Ramesh";
printf("A value : %s",a);
getch();
}
string functions:- Certain operations like assignment,comparision
concatenation(attach) can not be perfomed on strings using operators, for
that C provided the following string functions.
prototype: #include<string.h>

strcpy():- This function is used to copy one string to another string.(assign)


syntax:- strcpy(string1,string2);
string2 will be assigned to string1

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10];
clrscr();
strcpy(name,"Ramesh");
printf("Name is : %s",name);
getch();
}

strcat():- This function is used to attach one string to another string.


syntax:- strcat(string1,string2);
string2 will be added to string1

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20]="Raja";
clrscr();
strcat(name,"Ramesh");
printf("Name is : %s",name);
getch();
}

w a p for the following expresion


name=fname+lname
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20],fname[10],lname[10];
clrscr();
printf("Enter first name");
gets(fname);
printf("Enter last name");
gets(lname);
strcpy(name,fname);
strcat(name,lname);
//strcat(fname,lname);
//strcpy(name,fname);
printf("Name is : %s",name);
getch();
}
strrev():- This function is used to reverse a string. The reverse string
will be placed in the same string.
syntax:-strrev(string);
strupr():- It is used to convert a string in to upper case.
syntax:-strupr(string);
strlwr():- It is used to convert a string in to lower case.
syntax:-strlwr(string);

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
clrscr();
printf("Enter a name");
gets(name);
printf("Name is : %s",name);
strupr(name);
printf("\nName is : %s",name);
strlwr(name);
printf("\nName is : %s",name);
strrev(name);
printf("\nReverse string is : %s",name);
getch();
}

strlen():- This function is used to get the string length.


syntax:- int strlen(string);
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
int l;
clrscr();
printf("Enter a name");
gets(name);
l=strlen(name);
printf("String length is : %d",l);
getch();
}
strcmp():- It is used to compare two strings. After comparing two strings
this function returns an integer value depending upon ASCII codes.
syntax:- int strcmp(string1,string2);
Return values:-
0 -- both are same(rama,rama)
>0 -- str1 is > str2 (rama,arun)
<0 -- str1 is < str2 (arun,mohan)

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],b[10];
int c;
clrscr();
printf("Enter a name");
gets(a);
printf("Enter b name");
gets(b);
c=strcmp(a,b);
printf("C value is : %d\n",c);
if(c==0)
printf("Both are same");
else
printf("Both are different");
getch();
}
Palindrome or not
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10],temp[10];
int c;
clrscr();
printf("Enter a name");
gets(name);
strcpy(temp,name);
strrev(name);
c=strcmp(temp,name);
if(c==0)
printf("It is palindrome");
else
printf("it is not palindrome");
getch();
}

GOTO STATEMENT
================

THIS STATEMENT IS USED TO TRANSFER CONTROL TO REQUIRED STATEMENT.

# include <stdio.h>
# include <conio.h>
void main()
{
clrscr();
printf("One\n");
printf("Two\n");
goto xxx;
printf("Three\n");
printf("Four\n");
printf("Five\n");
xxx :
printf("Six");
getch();
}

ARRAYS
=======

AN ARRAY IS A GROUP OF ITEMS OF SIMILAR DATA TYPE. ITEMS IN THE ARRAY ARE CALLED
ELEMENTS. EACH ELEMENT HAVE AN UNIQUE INDEX / SUB SCRIPT. ARRAY INDEX ALWAYS STARTS
WITH ZERO. SO AN ARRAY ALWAYS CREATES WITH N-1 SIZE. TO PERFORM ANY OPERATION IN
ARRAYS WE MUST MENTION INDEX.

TYPES OF ARRAYS
================

1) SINGLE / ONE DIMENSIONAL ARRAYS

2) DOUBLE / TWO DIMENSIONAL ARRAYS

syntax:- datatype arrayname[size];


Array boundaries:- An array will have two boundaries.
1. lower boundary (0)
2. upper boundary (size-1)

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5];
clrscr();
a[0]=0;
a[1]=0;
a[2]=0;
a[3]=0;
a[4]=0;
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={0,0,0,0,0};
clrscr();
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={0,0,0,0,0},i;
clrscr();
for(i=0;i<=4;i++)
printf("a[%d]=%d\n",i,a[i]);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
for(i=0;i<=4;i++)
a[i]=200;

for(i=0;i<=4;i++)
printf("a[%d]=%d\n",i,a[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i;
clrscr();
for(i=0;i<=19;i++)
{
a[i]=15;
printf("a[%d]=%d\n",i,a[i]);
}
getch();
}
=======================================
SINGLE / ONE DIMENSIONAL ARRAYS
================================

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5];
clrscr();
a[0]=0;
a[1]=0;
a[2]=0;
a[3]=0;
a[4]=0;
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[5]={0,0,0,0,0};
clrscr();
for(i=0;i<=4;i++)
printf("a[%d]=%d\n",i,a[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[5];
clrscr();
for(i=0;i<=4;i++)
a[i]=996;

for(i=0;i<=4;i++)
printf("a[%d]=%d\n",i,a[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[5];
clrscr();
for(i=0;i<=4;i++)
{
a[i]=9;
printf("a[%d]=%d\n",i,a[i]);
}
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[6];
clrscr();
for(i=0;i<=5;i++)
{
a[i]=i;
printf("a[%d]=%d\n",i,a[i]);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[9];
clrscr();
for(i=0;i<=8;i++)
{
a[i]=i+1;
printf("a[%d]=%d\n",i,a[i]);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[5];
clrscr();
for(i=0;i<=4;i++)
{
//a[i]=(2*i)+2;
a[i]=(i+1)*2;
printf("a[%d]=%d\n",i,a[i]);
}
getch();
}
Providing data at run time
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf("Enter array elements");
for(i=0;i<=4;i++)
scanf("%d",&a[i]);

printf("Array elements are\n");


for(i=0;i<=4;i++)
printf("\n%d",a[i]);
getch();
}
w a p to input no of elements in to an array dispaly sum and avg of elements
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n,sum=0;
float avg;
clrscr();
printf("Enter how many elements(max size : 100)");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
avg=(float)sum/n;
printf("\nSum of elements : %d",sum);
printf("\nAvg of elements : %.2f",avg);
getch();
}
biggest element , smallest element and its positions

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n,big=0,spos=0,bpos=0,small;
float avg;
clrscr();
printf("Enter how many elements(max size : 100)");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
small=a[0];
for(i=0;i<n;i++)
{
if(a[i]>big)
{
big=a[i];
bpos=i;
}
if(a[i]<small)
{
small=a[i];
spos=i;
}
}
printf("Biggest element : %d",big);
printf("\nIts position is : %d",bpos+1);
printf("\nsmallest element : %d",small);
printf("\nIts position is : %d",spos+1);
getch();
}
w a p to input no of elements in to an array and search for an element
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[100],i,n,s;
clrscr();
printf("Enter how many elements(max size : 100)");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);

printf("Enter for search element");


scanf("%d",&s);

for(i=0;i<n;i++)
if(a[i]==s)
{
printf("\nIt is found");
getch();
exit(0);
}
printf("\nIt is not found");
getch();
}
Numbers Ascending order

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,temp;
clrscr();
printf("Enter array elements\n");
for(i=0;i<=4;i++)
scanf("%d",&a[i]);

for(i=0;i<=4;i++)
for(j=i+1;j<=4;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}

printf("Array elements are");


for(i=0;i<=4;i++)
printf("\n%d",a[i]);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
char a[5],temp;
clrscr();
printf("Enter array elements\n");
for(i=0;i<=4;i++)
{
fflush(stdin);
scanf("%c",&a[i]);
}
for(i=0;i<=4;i++)
for(j=i+1;j<=4;j++)
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}

printf("Descending order elements are");


for(i=0;i<=4;i++)
printf("\n%c",a[i]);
getch();
}
w a p to input a string and print the given string in vertical order
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
int i;
clrscr();
printf("Enter a name");
gets(name);
for(i=0;name[i]!='\0';i++)
printf("%c\n",name[i]);
getch();
}
w a p to find string length without using string functions
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
int i,c=0;
clrscr();
printf("Enter a name");
gets(name);
for(i=0;name[i]!='\0';i++)
{
c++;
}
printf("String length : %d",c);
getch();
}
string reverse without using string functions
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
int i,c=0;
clrscr();
printf("Enter a name");
gets(name);
for(i=0;name[i]!='\0';i++)
{
c++;
}
for(i=c-1;i>=0;i--)
printf("%c",name[i]);
getch();
}
DOUBLE / TWO DIMENSIONAL ARRAYS
=================================

GENERALLY NESTED LOOP IS USED TO MANIPULATING DOUBLE DIMENSIONAL ARRAYS.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3]={{0,0,0},{0,0,0},{0,0,0}};
clrscr();
printf("%d %d %d\n%d %d %d\n%d %d %d",a[0][0],a[0][1],a[0][2],
a[1][0],a[1][1],a[1][2],a[2][0],a[2][1],a[2][2]);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int j,i,a[3][3]={{0,0,0},{0,0,0},{0,0,0}};
clrscr();
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int j,i,a[3][3];
clrscr();
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
a[i][j]=24;

for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int j,i,a[3][3];
clrscr();
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
a[i][j]=22;
printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int j,i,a[3][3];
clrscr();
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
if(i==j)
a[i][j]=1;
else
a[i][j]=0;

for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int j,i,a[5][5],k=1;
clrscr();
for(i=0;i<=4;i++)
{
for(j=0;j<=4;j++)
{
a[i][j]=k;
printf("%d ",a[i][j]);
k++;
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int j,i,a[5][5],k=1;
clrscr();
for(i=0;i<=4;i++)
if(i%2==0)
for(j=0;j<=4;j++)
a[i][j]=k++;
else
for(j=4;j>=0;j--)
a[i][j]=k++;

for(i=0;i<=4;i++)
{
for(j=0;j<=4;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
}
Input data at runtime
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf("Enter 3 x 3 array elements");
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
scanf("%d",&a[i][j]);

printf("Array elements are\n");


for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Transpose elements are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",a[j][i]);
}
printf("\n");
}

printf("Mirror elements are\n");


for(i=0;i<=2;i++)
{
for(j=2;j>=0;j--)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
}
Addition of matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("Enter A array elements");
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
scanf("%d",&a[i][j]);

printf("Enter B array elements");


for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
scanf("%d",&b[i][j]);

for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
c[i][j]=a[i][j]+b[i][j];

printf("Addition elements are\n");


for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
}

MATRIX MULTIPLICATION
=======================

# include <stdio.h>
# include <conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
clrscr();
printf("Enter 2 * 2 Array 'A' Elements\n");
for(i = 0; i <= 2; i++)
for(j = 0; j <= 2; j++)
scanf("%d",&a[i][j]);

printf("Enter 2 * 2 Array 'B' Elements\n");


for(i = 0; i <= 2; i++)
for(j = 0; j <= 2; j++)
scanf("%d",&b[i][j]);

for(i = 0; i <= 2; i++)


{
for(j = 0; j <= 2; j++)
{
c[i][j] = 0;
for(k = 0; k <= 2; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
printf("Matrix Multiplication\n");
for(i = 0; i <= 1; i++)
{
for(j = 0; j <= 1; j++)
printf("%d\t",c[i][j]);

printf("\n");
}
getch();
}

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[3][20];
clrscr();
printf("Enter first name");
gets(name[0]);
printf("Enter last name");
gets(name[1]);
strcpy(name[2],name[0]);
strcat(name[2],name[1]);
printf("Your name is : %s",name[2]);
getch();
}
String sorting
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[5][20],temp[20];
int i,j;
clrscr();
printf("Enter five names");
for(i=0;i<=4;i++)
gets(name[i]);

for(i=0;i<=4;i++)
for(j=i+1;j<=4;j++)
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}

printf("Strings are\n");
for(i=0;i<=4;i++)
printf("%s\n",name[i]);
getch();
}
POINTERS
=========

A POINTER IS A VARIABLE WHICH STORES ADDRESS OF THE ANOTHER VARIABLE. IF WE KNOW


THE ADDRESS OF A VARIABLE WE CAN ACCESS THE VALUE. ADVANTAGE OF USING POINTERS IS
FAST PROCESSING. ADDRESS DATA TYPE IS ALWAYS UNSIGNED INTEGER (%u, 2 BYTES).

TYPES OF VARIABLES
==================

1) MEMORY VARIABLES : WHICH STORES DATA.


2) POINTER VARIABLES : WHICH STORES ADDRESS.
POINTER DATA TYPE : ALWAYS UNSIGNED INT.

UNSIGNED INT : IT STORES ONLY POSITIVE VALUES. IT OCCUPIES TWO BYTES AND IT'S SIGN
IS %u.
ADVANTAGES OF POINTERS
========================
1) FAST PROCESSING.
2) GETS UPTO DATE INFORMATION.
3) A FUNCTION CAN RETURN MORE THAN ONE VALUE.
4) WE CAN USE DYNAMIC MEMORY.
5) WE CAN STORE DATA PERMANENTLY.
SYNTAX:-
DATATYPE *POINTER;
EX:-
int *a;
Note:- Here 'a' can be treated as a pointer variable which stores the address of
another integer variable.
Note:- we can not assign one type of address in to another type of pointer.
Ex:- int *ptr;
int a;
float b;
ptr=&a; possible
ptr=&b; not possible

--> pointer referes address


--> *pointer referes value
FINDING ADDRESS OF A VARIABLE
=============================

# include <stdio.h>
# include <conio.h>
void main()
{
int a = 10;
clrscr();
printf("A Value : %d\n",a);
printf("A Address : %u\n",&a); // & : ADDRESS OPERATOR
getch();
}

# include <stdio.h>
# include <conio.h>
void main()
{
int a = 10;
int *p; //*p : POINTER VARIABLE
clrscr();
p = &a;
printf("A Address : %u\n",&a);
printf("A Address : %u\n",p);
printf("P Address : %u\n",&p);
printf("A Value : %d\n",a);
printf("A Value : %d\n",*p); // * : Value at Address Operator
getch();
}

# include <stdio.h>
# include <conio.h>
void main()
{
int a;
int *p;
clrscr();
p = &a;
a = 10;
printf("A Value : %d\n",*p);
*p = 20;
printf("A Value : %d\n",a);
getch();
}

# include <stdio.h>
# include <conio.h>
void main()
{
int a,b;
int *p;
clrscr();
a = 10;
b = 20;
p = &a;
*p = 30;
p = &b;
*p = 40;
printf("%d %d %d",a,b,*p);
getch();
}

# include <stdio.h>
# include <conio.h>
void main()
{
int a;
int *p;
clrscr();
p = &a;
printf("Enter A Value\n");
scanf("%d",p);
printf("A Value : %d\n",*p);
getch();
}

POINTER DATA TYPE IS ALWAYS UNSIGNED INTEGER (%u, 2 BYTES).

# include <stdio.h>
# include <conio.h>
void main()
{
int a,*p1;
float b,*p2;
char c,*p3;
clrscr();
p1 = &a;
p2 = &b;
p3 = &c;
printf("Integer Occupies %d Bytes\n",sizeof(a));
printf("Float Occupies %d Bytes\n",sizeof(b));
printf("Character Occupies %d Bytes\n",sizeof(c));
printf("Integer Pointer Occupies %d Bytes\n",sizeof(p1));
printf("Float Pointer Occupies %d Bytes\n",sizeof(p2));
printf("Character Pointer Occupies %d Bytes\n",sizeof(p3));
getch();
}

USING ARITHMETICAL OPERATORS ON POINTERS


==========================================

# include <stdio.h>
# include <conio.h>
void main()
{
int a,b,c;
int *pa,*pb,*pc;
clrscr();
a = 10;
b = 20;
pa = &a;
pb = &b;
pc = &c;
*pc = *pa + *pb;
printf("Sum : %d\n",c);
getch();
}
USING RELATIONAL OPERATORS ON POINTERS
========================================

# include <stdio.h>
# include <conio.h>
void main()
{
int a,b;
int *pa,*pb;
clrscr();
pa = &a;
pb = &b;
printf("Enter Two Numbers\n");
scanf("%d %d",pa,pb);
if(*pa > *pb)
{
printf("A is Big Number\n");
}
else
{
printf("B is Big Number\n");
}
getch();
}

UPTO DATE INFORMATION


=======================

# include <stdio.h>
# include <conio.h>
void main()
{
int a,*b;
clrscr();
a = 10;
b = &a;
printf("A Value : %d\n",a);
printf("B Value : %d\n",*b);
a = 20;
printf("A Value : %d\n",a);
printf("B Value : %d\n",*b);
*b = 30;
printf("A Value : %d\n",a);
printf("B Value : %d\n",*b);
getch();
}

POINTERS USING ARRAYS


======================

# include <stdio.h>
# include <conio.h>
void main()
{
int a[5] = {10,20,30,40,50};
int *p,i;
clrscr();
p = &a[0]; // p = &a; / p = a;
for(i = 1; i <= 5; i++,p++) // p++ : Traversing
{
printf("%d\t",*p);
}
getch();
}

POINTER ARRAYS
===============

# include <stdio.h>
# include <conio.h>
void main()
{
int a[5],*p[5],i;
clrscr();
for(i = 0; i <= 4; i++)
{
p[i] = &a[i];
}
printf("Enter Five Elements\n");
for(i = 0; i <= 4; i++)
{
scanf("%d",p[i]);
}
printf("Array Elements : ");
for(i = 0; i <= 4; i++)
{
printf("%d\t",*p[i]);
}
getch();
}

POINTERS USING STRINGS


======================
# include <stdio.h>
# include <conio.h>
void main()
{
char *str = "King";
clrscr();
printf("String : %s\n",str);
str = "Rama";
printf("String : %s\n",str);
printf("Enter String\n");
scanf("%s",str);
printf("String : %s\n",str);
getch();
}

# include <stdio.h>
# include <conio.h>
void main()
{
char str[10] = "Rama";
char *p;
clrscr();
for(p = str; *p != '\0'; p++)
{
printf("%c",*p);
}
getch();
}

POINTER TO POINTERS
====================

# include <stdio.h>
# include <conio.h>
void main()
{
int a,*p1,**p2,***p3;
clrscr();
a = 10;
p1 = &a;
p2 = &p1;
p3 = &p2;
printf("A Value : %d\n",a);
printf("A Value : %d\n",*p1);
printf("A Value : %d\n",**p2);
printf("A Value : %d\n",***p3);
getch();
}

VOID POINTERS
==============
THESE ARE SPECIAL POINTER WHICH HAS NO SPECIFIC DATA TYPE. SO THESE POINTER CAN
POINT ANY TYPE OF DATA. WHEN WE CAN ACCESS DATA THROUGH VOID POINTER, WE MUST TYPE
CAST THAT POINTER TO APPROPRIATE DATA TYPE.

# include <stdio.h>
# include <conio.h>
void main()
{
int a = 10;
float b = 12.34;
char c = 'K';
void *p;
clrscr();
p = &a;
printf("A Value : %d\n",*(int *)p);
p = &b;
printf("B Value : %.2f\n",*(float *)p);
p = &c;
printf("C Value : %c\n",*(char *)p);
getch();
}
MATHEMATICAL FUNCTIONS [MATH.H]
==================================

ALL FUNCTIONS IN MATH.H TAKES DOUBLE TYPE PARAMETERS.

SQRT( ) :- THIS FUNCTION IS USED TO FIND SQUARE ROOT OF A NUMBER.

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
double n,s;
clrscr();
printf("Enter Number\n");
scanf("%lf",&n);
s = sqrt(n);
printf("Square Root : %.2lf\n",s);
getch();
}

POW( ) :- THIS FUNCTION IS USED TO FIND POWER OF BASE.

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
double b,p,r;
clrscr();
printf("Enter Base, Power\n");
scanf("%lf %lf",&b,&p);
r = pow(b,p);
printf("Result : %.2lf\n",r);
getch();
}

POW10( ) :- THIS FUNCTION IS USED TO FIND POWER OF 10.

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
double p,r;
clrscr();
printf("Enter Power\n");
scanf("%lf",&p);
r = pow10(p);
printf("Result : %.2lf\n",r);
getch();
}

LOG( ) :- THIS FUNCTION IS USED TO FIND LOGARTHIMS.

LOG10( ) :- THIS FUNCTION IS USED TO FIND LOGARTHIMS.

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
double n,r;
clrscr();
printf("Enter Number\n");
scanf("%lf",&n);
r = log(n); // r = log10(n);
printf("Result : %.2lf\n",r);
getch();
}
EXP( ) :- THIS FUNCTION IS USED TO FIND EXPONENTIAL VALUES.

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
double n,r;
clrscr();
printf("Enter Number\n");
scanf("%lf",&n);
r = exp(n);
printf("Result : %.2lf\n",r);
getch();
}

ABS( ) :- THIS FUNCTION IS USED TO FIND ABSOLUTE VALUE OF A NUMBER. IT MEANS ALWAYS
POSITIVE VALUE. ABSOLUTE VALUE MEANS VALUE WITHOUT SIGN.

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
double n,r;
clrscr();
printf("Enter Number\n");
scanf("%lf",&n);
r = abs(n);
printf("Result : %.2lf\n",r);
getch();
}

CEIL( ) :- THIS FUNCTION IS USED TO ROUND TO NEXT INTEGER.

FLOOR( ) :- THIS FUNCTION IS USED TO ROUND TO PREVIOUS INTEGER.

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
double n,r;
clrscr();
printf("Enter Number\n");
scanf("%lf",&n);
r = ceil(n); // r = floor(n);
printf("Result : %.2lf\n",r);
getch();
}

FMOD( ) :- THIS FUNCTION IS USED TO FIND REMAINDER OF FLOAT VALUES.

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
double a,b,r;
clrscr();
a = 10.5;
b = 4.5;
r = fmod(a,b);
printf("Result : %.2lf\n",r);
getch();
}

SIN( ) :- THIS FUNCTION IS USED TO FIND SIN VALUE. ALL THE TRIGONOMETRY FUNCTIONS
ARE DESIGNED TO TAKE RADIANS NOT DEGREES. SO WE HAVE TO CONVERT THAT RADIANS INTO
DEGREES.

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
double d,r,res;
clrscr();
printf("Enter Degrees\n");
scanf("%lf",&r);
d = r * 3.14 / 180;
res = sin(d); // res = 1 / sin(d);
// res = cos(d); // res = 1 / cos(d);
// res = tan(d); // res = 1 / tan(d);
printf("Result : %.2lf\n",res);
getch();
}

MAX( ) :- :- THIS FUNCTION IS USED TO FIND BIG OF TWO NUMBERS.

MIN( ) :- :- THIS FUNCTION IS USED TO FIND SMALL OF TWO NUMBERS.

BOTH ABOVE TWO FUNCTIONS ARE AVAILABLE IN "STDLIB.H".

# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
void main()
{
int a,b,big,small;
clrscr();
printf("Enter Two Numbers\n");
scanf("%d %d",&a,&b);
big = max(a,b);
small = min(a,b);
printf("Big Number : %d\n",big);
printf("Small Number : %d\n",small);
getch();
}

function :- it contain set of statements.


program :- it contain set of functions
software :- it contain set of programs

Function:- A function is a part of program which can be used to perform a specific


task. There are two types of functions available.
1. predefined functions / Library functions / Built in functions:- ex:-
printf(),scanf(),getch() etc..
If we want to use any predefined function we must include that relavant header file
in our program.
2. User defined functions:- own definition prepare.
Advantages :-
code reusability:- once if we define a function we can call or use the function
as many no of times as we want.
maintainbility:- we can maintain a large program very easily we can split the
program in to functions.

syntax:- returntype functionname(datatype arg1,datatype arg2,datatype arg n)

Note:- Depending upon the returntype, arguments functions can be clasified in to


four types.

1. functions without arguments, without returntype


ex:- clrscr(),getch(),line(),addr() etc..

2. functions with arguments, without returntype


ex:- exit(0), line('*'),big(a,b,c),table(no) etc..

3. functions with arguments, with returntype


ex:- c=sum(a,b), r=pow(b,p), l=strlen(name) etc..

4. functions without arguments, with returntype


ex:- x=sum( ) etc..
Note:- The default return type for function is an integer at the time of function
definition

note:- At the time of function calling if there is no return type we must


specify the return type as void at the time of its definition

1. functions without arguments,without returntype

#include<stdio.h>
#include<conio.h>
//function definition
void show() //function header
{
//function body (optional)
printf("Welcome to Functions");
}
void main()
{
clrscr(); //predefined function calling
show(); //userdefined function calling
getch();
}

#include<stdio.h>
#include<conio.h>
void addr()
{
printf("\nInformatics Computers");
printf("\nKothapet");
printf("\nPhone: 227675");
printf("\nTenali-522201");
}
void main()
{
clrscr();
printf("My address is");
addr();//function calling
addr();
addr();
getch();
}

#include<stdio.h>
#include<conio.h>
void line()
{
int i;
for(i=1;i<=80;i++)
printf("-");
}
void main()
{
clrscr();
line();
line();
line();
printf("\t\t\t\tWELCOME\n");
line();
line();
line();
getch();
}
2. Functions with args without returntype

#include<stdio.h>
#include<conio.h>
void line(char ch,int no)
{
int i;
for(i=1;i<=no;i++)
printf("%c",ch);
}
void main()
{
clrscr();
line('*',400);
line('-',80);
printf("\t\t\t\tWELCOME\n");
line('@',40);
line('#',200);
getch();
}

/*
#include<stdio.h>
#include<conio.h>
//function definition
void line(char ch)
{
int i;
for(i=1;i<=80;i++)
printf("%c",ch);
}
void main()
{
clrscr();
line('*');
line('-');
printf("\t\t\t\tWELCOME\n");
line('#');
line('$');
getch();
}

=============================
Rama
===================
Siva
===========================================================
Anand
==============================================================================

#include<stdio.h>
#include<conio.h>
void line(int no)
{
int i;
for(i=1;i<=no;i++)
printf("=");
}
void main()
{
clrscr();
line(30);
printf("\nRama\n");
line(20);
printf("\nSiva\n");
line(60);
printf("\nKrishna\n");
line(80);
getch();
}

#include<stdio.h>
#include<conio.h>
void line(int no,char ch)
{
int i;
for(i=1;i<=no;i++)
printf("%c",ch);
}
void main()
{
clrscr();
line(30,'*');
printf("\nRama\n");
line(20,'#');
printf("\nSiva\n");
line(60,'%');
printf("\nKrishna\n");
line(80,'$');
getch();
}
#include<stdio.h>
#include<conio.h>
void table(int x)
{
int i;
for(i=1;i<=10;i++)
printf("\n %d * %d = %d",x,i,x*i);

}
void main()
{
int no;
clrscr();
printf("enter a no");
scanf("%d",&no);
table(no);
table(3);
getch();
}

#include<stdio.h>
#include<conio.h>
void big(int x,int y,int z)
{
if(x>y&&x>z)
printf("Big no is %d",x);
else if(y>x&&y>z)
printf("Big no is %d",y);
else
printf("Big no is %d",z);
}
void main()
{
int a,b,c;
clrscr();
printf("enter a,b,c values");
scanf("%d%d%d",&a,&b,&c);
big(a,b,c);
getch();
}
fucntions with args,with return type

#include<stdio.h>
#include<conio.h>
int big(int x,int y,int z)
{
if(x>y&&x>z)
return x;
else if(y>x&&y>z)
return y;
else
return z;
}
void main()
{
int a,b,c,d;
clrscr();
printf("enter a,b,c values");
scanf("%d%d%d",&a,&b,&c);
d=big(a,b,c);
printf("Big no is %d",d);
getch();
}
*/
call by value
/*
#include<stdio.h>
#include<conio.h>
int sum(int x,int y)
{ //x,y are called formal parameters
int z;
z=x+y;
return z;
}
void main()
{
int a,b,c;
clrscr();
printf("enter a,b values");
scanf("%d%d",&a,&b);
c=sum(a,b);//function calling
//a,b are called actual parameters
printf("sum is %d",c);
getch();
}

#include<stdio.h>
#include<conio.h>
sub(int x,int y)
{
return x-y;
}
void main()
{
int a,b;
clrscr();
printf("enter a,b values");
scanf("%d%d",&a,&b);
printf("subtraction is %d",sub(a,b));
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int mul(int x,int y); //function declaration
clrscr();
printf("enter a,b values");
scanf("%d%d",&a,&b);
c=mul(a,b);//function calling
//a,b are called actual parameters
printf("mul is %d",c);
getch();
}
int mul(int x,int y)
{
int z;
z=x*y;
return z;
}

function calling:-
There are two ways to call a function
1 call by value
2 call by address / reference

1. call by value:- In the call by value method the values of actual parameters
(main variables) are transferd to the formal parameters(function variables)
2. Any modification performed on the formal parameters will not effect the
values of actual parameters.
3. You can not return more than one value from the function to the calling
part

#include<stdio.h>
#include<conio.h>
void modify(int x)
{
x++;
}
void main()
{
int a=5;
clrscr();
modify(a);
printf("A value is %d",a);
getch();
}

#include<stdio.h>
#include<conio.h>
int modify(int x)
{
x++;
return x;
}
void main()
{
int a=5;
clrscr();
a=modify(a);
printf("A value is %d",a);
getch();
}

#include<stdio.h>
#include<conio.h>
void swap(int x,int y)
{
int z;
z=x;
x=y;
y=z;
printf("\nIn swap fun x is %d",x);
printf("\nIn swap fun y is %d",y);
}
void main()
{
int a=5,b=20,c;
clrscr();
printf("\nBefore swaping");
printf("\nA=%d",a);
printf("\nB=%d",b);
swap(a,b);
printf("\nAfter swaping");
printf("\nA=%d",a);
printf("\nB=%d",b);
getch();
}
r=fact(no);
r=power(b,p);
l=length(name);
r=prime(no);

#include<stdio.h>
#include<conio.h>
int fact(int x)
{
int i,f=1;
for(i=1;i<=x;i++)
f=f*i;
return f;
}
void main()
{
int i;
clrscr();
for(i=1;i<=7;i++)
printf("\n %d fact is %d",i,fact(i));
getch();
}

#include<stdio.h>
#include<conio.h>
int power(int x,int y)
{
int i,r=1;
for(i=1;i<=y;i++)
r=r*x;
return r;
}
void main()
{
int b,p,r;
clrscr();
printf("enter base,power values");
scanf("%d%d",&b,&p);
r=power(b,p);
printf("%d power %d is %d",b,p,r);
getch();
}

#include<stdio.h>
#include<conio.h>
int len(char x[])
{
int i,c=0;
for(i=0;x[i]!='\0';i++)
c++;
return c;
}
void main()
{
int l;
char name[20];
clrscr();
printf("enter a name");
gets(name);
l=len(name);
printf("String length is %d",l);
getch();
}
*/
name[20];
rama\0
krishna\0
sai\0
# include <stdio.h>
# include <conio.h>
void main()
{
int n,s;
int square(int x);
clrscr();
printf("Enter Number\n");
scanf("%d",&n);
s = square(n);
printf("Square : %d\n",s);
getch();
}
int square(int x)
{
int k;
k = x * x;
return k;
}

DESIGNING AREA FUNCTION FOR CIRCLE


===================================

# include <stdio.h>
# include <conio.h>
void main()
{
int rds;
float area;
float area_circle(int x);
clrscr();
printf("Enter Radius\n");
scanf("%d",&rds);
area = area_circle(rds);
printf("Area of Circle : %.2f\n",area);
getch();
}
float area_circle(int x)
{
float k;
k = 3.14 * x * x;
return k;
}

PASSING ARRAYS AS ARGUMENTS


=============================

# include <stdio.h>
# include <conio.h>
void main()
{
int a[5] = {10,20,30,40,50};
void show(int x[5],int y);
clrscr();
show(a,5);
getch();
}
void show(int x[5],int y) // void show(int x[ ],int y) // void show(int *x,int y)
{
int i;
for(i = 0; i <= 4; i++)
{
printf("%d\t",x[i]);
}
}

PASSING 2-D ARRAYS AS ARGUMENTS


================================

# include <stdio.h>
# include <conio.h>
void main()
{
int a[3][5] = {
{10,20,30,40,50},
{60,70,80,90,100},
{110,120,130,140,150}
};
void show(int x[3][5],int r,int c);
clrscr();
show(a,3,5);
getch();
}
void show(int x[3][5],int r,int c) // void show(int x[][5],int r,int c)
{
int i,j;
printf("3 * 5 Array Elements\n");
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
}

STRUCTURES
===========
TYPES OF DATA TYPES
===================
1) PRIMARY / STANDARD / PRIMITIVE / BUILT-IN DATA TYPES
Ex :- INT, FLOAT, CHAR, DOUBLE

2) SECONDARY / DERIVED DATA TYPES


Ex :- ARRAYS,POINTERS, STRINGS

3) USER-DEFINED / ABSTRACT DATA TYPES


Ex :- STRUCTURES

A STRUCTURE IS A SET OF MEMBERS OF DIFFERENT DATA TYPES. A STRUCTURE IS AN USER-


DEFINED / ABSTRACT DATA TYPE. IT MEANS WE CAN CREATE OUR OWN DATA TYPES AS PER OUR
REQUIREMENTS. A STRUCTURE CAN HAVE ANY NUMBER OF MEMBERS. VARIABLES WHICH ARE
CREATED FROM THE STRUCTURE ARE CALLED OBJECTS. DOT / PERIOD OPERATOR (.) IS USED TO
ACCESS MEMBERS OF OBJECTS.

Syntax:-
struct Identifier
{
datatype variable 1;
datatype variable 2;
datatype variable n;
};
Note:- Structure must be ends with semicolon (;)
ex:-
struct employee
{
int no;
char name[20];
float sal;
};
Structures vs Arrays:-
Similarties:-
1.Similar to an array all the members in structure will share a common name.
ex:- array :- a[0] a[1] a[2]
struct employee x;
structure:- x.no,x.name,x.sal
2. Similar to an array all the members are placed continously like a row
Differences:-
1.Unlike an array all the member can have different data types.
2.The structure members have names instead of index numbers.

Creating structure variable/Object:


Syntax:- struct structure_name variable_list;
ex:- struct employee a,b;

Refering the members in the structure:-


Syntax:- structurevariable.member or object.member
ex:- a.no,a.name,a.sal
b.no,b.name,b.sal etc..

'.' is called member reference operator

# include <stdio.h>
# include <conio.h>
struct sample
{
int a;
char b;
float c;
};
void main()
{
struct sample s; // s : object
clrscr();
s.a = 10;
s.b = 'K';
s.c = 6.3;
printf("%d\t%c\t%.1f\n",s.a,s.b,s.c);
getch();
}

ASSIGNING OBJECTS
==================

# include <stdio.h>
# include <conio.h>
# include <string.h>
struct student
{
int sno;
char sname[20];
int c;
int cpp;
int java;
int tot;
float avg;
};
void main()
{
struct student s;
clrscr();
s.sno = 101;
strcpy(s.sname,"Rama");
s.c = 11;
s.cpp = 11;
s.java = 12;
s.tot = s.c + s.cpp + s.java;
s.avg = s.tot / 3.0;
printf("Student Number : %d\n",s.sno);
printf("Student Name : %s\n",s.sname);
printf("Total Marks : %d\n",s.tot);
printf("Average Marks : %.2f\n",s.avg);
getch();
}
ONCE A STRUCTURE IS DEFINED, WE CAN CREATE ANY NUMBER OF OBJECTS FROM THAT
STRUCTURE.

# include <stdio.h>
# include <conio.h>
# include <string.h>
struct student
{
int sno;
char sname[20];
int c;
int cpp;
int java;
int tot;
float avg;
};
void main()
{
struct student s1,s2,s3;
clrscr();
s1.sno = 101;
strcpy(s1.sname,"Rama");
s1.c = 11;
s1.cpp = 11;
s1.java = 12;
s2.sno = 101;
strcpy(s2.sname,"Tata");
s2.c = 22;
s2.cpp = 22;
s2.java = 23;
s3.sno = 103;
strcpy(s3.sname,"Harsha");
s3.c = 1;
s3.cpp = 0;
s3.java = -1;
s1.tot = s1.c + s1.cpp + s1.java;
s1.avg = s1.tot / 3.0;
s2.tot = s2.c + s2.cpp + s2.java;
s2.avg = s2.tot / 3.0;
s3.tot = s3.c + s3.cpp + s3.java;
s3.avg = s3.tot / 3.0;
printf("First Student Details\n");
printf("Student Number : %d\n",s1.sno);
printf("Student Name : %s\n",s1.sname);
printf("Total Marks : %d\n",s1.tot);
printf("Average Marks : %.2f\n",s1.avg);
printf("Second Student Details\n");
printf("Student Number : %d\n",s2.sno);
printf("Student Name : %s\n",s2.sname);
printf("Total Marks : %d\n",s2.tot);
printf("Average Marks : %.2f\n",s2.avg);
printf("Third Student Details\n");
printf("Student Number : %d\n",s3.sno);
printf("Student Name : %s\n",s3.sname);
printf("Total Marks : %d\n",s3.tot);
printf("Average Marks : %.2f\n",s3.avg);
getch();
}
INITIALIZING OBJECTS
====================

# include <stdio.h>
# include <conio.h>
struct student
{
int sno;
char sname[20];
int c;
int cpp;
int java;
int tot;
float avg;
};
void main()
{
struct student s = {101,"Rama",11,11,12};
clrscr();
s.tot = s.c + s.cpp + s.java;
s.avg = s.tot / 3.0;
printf("Student Number : %d\n",s.sno);
printf("Student Name : %s\n",s.sname);
printf("Total Marks : %d\n",s.tot);
printf("Average Marks : %.2f\n",s.avg);
getch();
}

PROVIDING DATA INTO OBJECTS AT RUNTIME


=======================================

# include <stdio.h>
# include <conio.h>
struct student
{
int sno;
char sname[20];
int c;
int cpp;
int java;
int tot;
float avg;
};
void main()
{
struct student s;
clrscr();
printf("Enter Student Number, Name\n");
scanf("%d %s",&s.sno,&s.sname);
printf("Enter C, Cpp, Java Marks\n");
scanf("%d %d %d",&s.c,&s.cpp,&s.java);
s.tot = s.c + s.cpp + s.java;
s.avg = s.tot / 3.0;
printf("Student Number : %d\n",s.sno);
printf("Student Name : %s\n",s.sname);
printf("Total Marks : %d\n",s.tot);
printf("Average Marks : %.2f\n",s.avg);
getch();
}
NESTED STRUCTURES / NESTING OF STRUCTURES
==========================================

A STRUCTURE CONTAINS ANOTHER STRUCTURE OBJECT.

# include <stdio.h>
# include <conio.h>
struct date
{
int day;
int month;
int year;
};
struct student
{
int sno;
char sname[20];
struct date dob;
int c;
int cpp;
int java;
int tot;
float avg;
};
void main()
{
struct student s;
clrscr();
printf("Enter Student Number, Name\n");
scanf("%d %s",&s.sno,&s.sname);
printf("Enter Date of Birth [dd/mm/yyyy]\n");
scanf("%d/%d/%d",&s.dob.day,&s.dob.month,&s.dob.year);
printf("Enter C, Cpp, Java Marks\n");
scanf("%d %d %d",&s.c,&s.cpp,&s.java);
s.tot = s.c + s.cpp + s.java;
s.avg = s.tot / 3.0;
printf("Student Number : %d\n",s.sno);
printf("Student Name : %s\n",s.sname);
printf("Date of birth : %d/%d/%d\n",s.dob.day,s.dob.month,s.dob.year);
printf("Total Marks : %d\n",s.tot);
printf("Average Marks : %.2f\n",s.avg);
getch();
}

STRUCTUERS USING ARRAYS / OBJECT ARRAYS


========================================

# include <stdio.h>
# include <conio.h>
# include <dos.h>
struct student
{
int sno;
char sname[20];
int c;
int cpp;
int java;
int tot;
float avg;
};
void main()
{
struct student s[5];
int i;
clrscr();
for(i = 0; i <= 4; i++)
{
printf("Enter Student Number, Name\n");
scanf("%d %s",&s[i].sno,&s[i].sname);
printf("Enter C, Cpp, Java Marks\n");
scanf("%d %d %d",&s[i].c,&s[i].cpp,&s[i].java);
}
for(i = 0; i <= 4; i++)
{
s[i].tot = s[i].c + s[i].cpp + s[i].java;
s[i].avg = s[i].tot / 3.0;
}
for(i = 0; i <= 4; i++)
{
clrscr();
printf("Student Number : %d\n",s[i].sno);
printf("Student Name : %s\n",s[i].sname);
printf("Total Marks : %d\n",s[i].tot);
printf("Average Marks : %.2f\n",s[i].avg);
sleep(3);
}
}

STRUCTUERS USING ARRAY INITIALIZATION / OBJECT ARRAY INITIALIZATION


====================================================

# include <stdio.h>
# include <conio.h>
# include <dos.h>
struct student
{
int sno;
char sname[20];
int c;
int cpp;
int java;
int tot;
float avg;
};
void main()
{
struct student s[5] = {
{101,"Rama",11,11,12},
{102,"King",22,22,23},
{103,"Tata",33,33,34},
{104,"Ford",44,44,45},
{105,"Ward",55,55,56}
};
int i;
clrscr();
for(i = 0; i <= 4; i++)
{
s[i].tot = s[i].c + s[i].cpp + s[i].java;
s[i].avg = s[i].tot / 3.0;
}
for(i = 0; i <= 4; i++)
{
clrscr();
printf("Student Number : %d\n",s[i].sno);
printf("Student Name : %s\n",s[i].sname);
printf("Total Marks : %d\n",s[i].tot);
printf("Average Marks : %.2f\n",s[i].avg);
sleep(3);
}
}

STRUCTURES USING POINTERS


==========================

# include <stdio.h>
# include <conio.h>
struct student
{
int sno;
char sname[20];
int c;
int cpp;
int java;
int tot;
float avg;
};
void main()
{
struct student s,*p;
clrscr();
printf("Enter Student Number, Name\n");
scanf("%d %s",&s.sno,&s.sname);
printf("Enter C, Cpp, Java Marks\n");
scanf("%d %d %d",&s.c,&s.cpp,&s.java);
p = &s;
p->tot = p->c + p->cpp + p->java; // -> Member Selection Operator
(*p).avg = (*p).tot / 3.0; // Arrow Operator
printf("Student Number : %d\n",(*p).sno);
printf("Student Name : %s\n",(*p).sname);
printf("Total Marks : %d\n",p->tot);
printf("Average Marks : %.2f\n",p->avg);
getch();
}

FILES
=====

FILES ARE USED TO STORE DATA PERMANENTLY. TO PERFORM FILE OPERATIONS WE MUST FOLLOW
THE FOLLOWING STEPS.

1) CREATE FILE POINTER

2) OPEN FILE IN APPROPRIATE MODE

3) MAKE TRANSACTIONS (READ / WRITE / APPEND)


4) CLOSE FILE

TYPES OF FILES
==============

1) TEXT FILES

2) NUMBER FILES

3) RECORD FILES

4) BINARY FILES

TEXT FILES
==========

THESE FILES STORES TEXT DATA.

STORING DATA INTO TEXT FILE


===========================

PUTC( ) :- THIS FUNCTION IS USED TO STORE A CHARACTER IN A FILE.


Syntax:- putc(char,FILE *);

# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fp;
clrscr();
fp = fopen("Sample.txt","w"); // w : write mode
putc('K',fp);
fclose(fp);
printf("Data stored in Sample.txt\n");
getch();
}
ADDING DATA TO TEXT FILE
========================

# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fp;
clrscr();
fp = fopen("Sample.txt","a"); // a : append mode
putc('V',fp);
fclose(fp);
printf("Data added to Sample.txt\n");
getch();
}

READING DATA FROM TEXT FILE


============================

GETC( ) :- THIS FUNCTION IS USED TO READ A CHARACTER FROM FILE.


Syntax:- char getc(fp);
# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
printf("Data from Sample.txt\n");
fp = fopen("Sample.txt","r"); // r : read mode
ch = getc(fp);
printf("%c",ch);
ch = getc(fp);
printf("%c",ch);
fclose(fp);
getch();
}

STORING CONTINIOUS DATA INTO TEXT FILE


=======================================

# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp = fopen("Simple.txt","w");
printf("Enter Text, ends with 0\n");
while(1) // infinite loop
{
scanf("%c",&ch);
if(ch == '0')
break;
putc(ch,fp);
}
fclose(fp);
printf("Data stored in Simple.txt\n");
getch();
}

ADDING CONTINIOUS DATA TO TEXT FILE


====================================

SAME ABOVE PROGRAM

fp = fopen("Simple.txt","a");

READING CONTINIOUS DATA FROM TEXT FILE


========================================

# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
printf("Data from Simple.txt\n");
fp = fopen("Simple.txt","r");
while(1) // while((ch = getc(fp)) != EOF)
{
ch = getc(fp);
if(ch == EOF) // EOF : end of file
break;
printf("%c",ch);
}
fclose(fp);
getch();
}

NUMBER FILES
=============

THESE FILES STORES NUMBER DATA.

STORING DATA INTO NUMBER FILE


==============================

PUTW( ) :- THIS FUNCTION IS USED TO STORE A NUMBER IN A FILE.

# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fp;
int n;
clrscr();
fp = fopen("Nums.txt","w");
printf("Enter Numbers, ends with 0\n");
while(1)
{
scanf("%d",&n);
if(n == 0)
break;
putw(n,fp);
}
printf("Data stored in Nums.txt\n");
fclose(fp);
getch();
}

ADDING DATA TO NUMBER FILE


===========================

SAME ABOVE PROGRAM

fp = fopen("Nums.txt","a");

READING DATA FROM NUMBER FILE


===============================

GETW( ) :- THIS FUNCTION IS USED TO READ A NUMBER FROM FILE.

# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fp;
int n;
clrscr();
printf("Data from Nums.txt\n");
fp = fopen("Nums.txt","r");
while(1) // while((n = getw(fp)) != EOF)
{
n = getw(fp);
if(n == EOF)
break;
printf("%d\t",n);
}
fclose(fp);
getch();
}

RECORD FILES
=============

THESE FILES STORES DATA IN THE FORM OF RECORDS.

STORING DATA INTO RECORD FILE


==============================

FPRINTF( ) :- THIS FUNCTION IS USED TO STORE DATA IN FILE.

# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fp;
int sno;
char sname[10];
int p1,p2,p3,i;
clrscr();
fp = fopen("Student.dat","w");
for(i = 1; i <= 5; i++)
{
printf("Enter Student Number, Name\n");
scanf("%d %s",&sno,&sname);
printf("Enter 3 Paper Marks\n");
scanf("%d %d %d",&p1,&p2,&p3);
fprintf(fp,"%d\t%s\t%d\t%d\t%d\n",sno,sname,p1,p2,p3);
}
fclose(fp);
printf("Data stored in Student.dat\n");
getch();
}

ADDING DATA TO RECORD FILE


===========================

SAME ABOVE PROGRAM

fp = fopen("Student.dat","a");

READING DATA FROM RECORD FILE


===============================

FSCANF( ) :- THIS FUNCTION IS USED TO READ DATA FROM FILE.


# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fp;
int sno;
char sname[10];
int p1,p2,p3,tot;
float avg;
clrscr();
printf("Data from Student.dat\n");
fp = fopen("Student.dat","r");
while(1) // while(fscanf(fp,"%d %s %d %d %d",&sno,&sname,&p1,&p2,&p3) != EOF)
{
fscanf(fp,"%d %s %d %d %d",&sno,&sname,&p1,&p2,&p3);
if(feof(fp)) // feof() : File End of File
break;
tot = p1 + p2 + p3;
avg = tot / 3.0;
pritf("%d\t%s\t%d\t%d\t%d\t%d\t%.2f\n",sno,sname,p1,p2,p3,tot,avg);
}
fclose(fp);
getch();
}

BINARY FILES
============

THESE FILES STORES DATA IN ASCII FORM.

STORING DATA INTO BINARY FILE


=============================

FWRITE( ) :- THIS FUNCTION IS USED TO STORE DATA IN FILE.

# include <stdio.h>
# include <conio.h>
struct student
{
int sno;
char sname[10];
int p1,p2,p3;
};
void main()
{
FILE *fp;
struct student s;
int i;
clrscr();
fp = fopen("Stud.dat","wb");
for(i = 1; i <= 5; i++)
{
printf("Enter Student Number, Name\n");
scanf("%d %s",&s.sno,&s.sname);
printf("Enter 3 Paper Marks\n");
scanf("%d %d %d",&s.p1,&s.p2,&s.p3);
fwrite(&s,sizeof(s),1,fp);
}
fclose(fp);
printf("Data stored in Stud.dat\n");
getch();
}

ADDING DATA TO BINARY FILE


===========================

SAME ABOVE PROGRAM

fp = fopen("Stud.dat","ab");

READING DATA FROM BINARY FILE


===============================

FREAD( ) :- THIS FUNCTION IS USED TO READ DATA FROM FILE.

# include <stdio.h>
# include <conio.h>
struct student
{
int sno;
char sname[10];
int p1,p2,p3;
};
void main()
{
FILE *fp;
struct student s;
int tot;
float avg;
clrscr();
printf("Data from Stud.dat\n");
fp = fopen("Stud.dat","rb");
while(1) // while(fread(&s,sizeof(s),1,fp) != NULL)
{
fread(&s,sizeof(s),1,fp);
if(feof(fp))
break;
tot = s.p1 + s.p2 + s.p3;
avg = tot / 3.0;
printf("%d\t%s\t%d\t%d\t%d\t%d\t%.2f\n",s.sno,s.sname,s.p1,s.p2,s.p3,tot,avg);
}
fclose(fp);
getch();
}

You might also like