C Material
C Material
HARDWARE :- PHYSICAL PARTS OF THE COMPUTER. WHICH ARE SEE AND TOUCHABLE.
TYPES OF SOFTWARES
====================
1) System software (OPERATING SYSTEMS)
2) Application software
A) PACKAGES
B) LANGUAGES
DATA :- IT IS NOTHING BUT A KNOWN VALUE, WHICH IS FORMED WITH DIGITS, ALPHABETS AND
SPECIAL CHARACTERS.
6 + 3 : 9
Ex :- A = 10
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
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.
1) ASSEMBLER
2) INTERPRETER
3) COMPILER
ASSEMBLER :- IT IS USED TO TRANSLATE ASSEMBLY LANGUAGE INSTRUCTIONS INTO MACHINE
LANGUAGE.
================================================
NAME YEAR DEVELOPED BY
================================================
ALGOL-60 1960 INTERNATIONAL COMMITE
(ALGORTHEMIC
LANGUAGE)
'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.
//Variable: A quantity which may vary during the execution of a program is called
as variable.
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.
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.
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
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.
float: The numbers which are stored in the form of floating point representation is
called as float data type.
double: The numbers which are stored in the form of double pr-ecision floating
point representation is called as double data type.
Sub classification of Primary data types, memory size, format specifiers and their
accessibility range :
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";
ex:- a+b
a,b are operands
+ is operator
1. Airthmetic operators
+ --> addition
- --> subtraction
* --> mlutiplication
/ --> It returns quotient
% --> It returns reminder (modulos operator)
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
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
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
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.
/*
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.
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>
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( ).
{ } :- THESE ARE CALLED CURELY BRACES. BRACES INDICATES STARTING AND ENDING OF THE
MAIN( ).
The printf function moves the data from program to outside like monitor.
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
NOTE :- 'C' LANGUAGE PROGRAMS MUST BE WRITEN IN LOWER CASE ALPHABETS. EVERY
STATEMENT MUST BE ENDS WITH SEMICOLON EXCEPT VOID MAIN( )
2) WRITE PROGRAM
3) SAVE PROGRAM
FILE ====> SAVE (NAME.C) (F2)
4) COMPILE PROGRAM
COMPILE ====> COMPILE (ALT + F9)
6) VIEW OUTPUT
WINDOW ====> USER SCREEN (ALT + F5)
7) CLOSING FILE
WINDOW ====> CLOSE (ALT + F3)
ZOOM ====> F5
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
#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
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.
\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.
# 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
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
=======================================
#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();
}
OUTPUT
=======
Grade : A
Rama got A Grade
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.
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
#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();
}
#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();
}
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();
}
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
#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();
}
#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
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.
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;
# 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;
#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();
}
#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();
}
#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;
#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;
}
#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;
#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=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--)
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();
}
#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);
#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();
}
#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();
}
#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
#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();
}
#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>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10];
clrscr();
strcpy(name,"Ramesh");
printf("Name is : %s",name);
getch();
}
#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();
}
#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();
}
#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
================
# 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
================
#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]);
#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]);
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;
}
#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;
}
#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]);
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
c[i][j]=a[i][j]+b[i][j];
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("\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
=========
TYPES OF VARIABLES
==================
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
# 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();
}
# 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();
}
# 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();
}
# 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();
}
# 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();
}
# 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]
==================================
# 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();
}
# 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();
}
# 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();
}
# 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();
}
# 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();
}
# 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();
}
# 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();
}
#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;
}
# 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;
}
# 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]);
}
}
# 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
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.
# 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();
}
# 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
==========================================
# 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();
}
# 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);
}
}
# 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);
}
}
# 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.
TYPES OF FILES
==============
1) TEXT FILES
2) NUMBER FILES
3) RECORD FILES
4) BINARY FILES
TEXT FILES
==========
# 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();
}
# 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();
}
fp = fopen("Simple.txt","a");
# 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
=============
# 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();
}
fp = fopen("Nums.txt","a");
# 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
=============
# 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();
}
fp = fopen("Student.dat","a");
BINARY FILES
============
# 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();
}
fp = fopen("Stud.dat","ab");
# 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();
}