QUALITY COMPUTER EDUCATION Language The media of communication
between the user and the system is known as Language. It is
classified into three types. They are. a) Machine language: This
language is also known as binary language or first generation
language or low level language. It consists of binary digits or
bits (0-pulse off,1-pulse on). b) Assembly language: It is also
known as middle level language or second generation language. Its
process is faster than high level language and easy to understand
when compared to machine language. Ex: c, c++ c) High level
language: This is the user language. It consists of codes which is
very easy to understand as the code is similar to English language.
Ex: BASIC, COBOL, FORTRAN, JAVA, C# Operating system: It is an
interface between user and the computer. It is used to perform the
required operations in the system. It is classified into two types.
They are 1) single user operating system: it supports only one user
at a time. These are micro computers Ex: DOS 2) Multi user
operating system: it supports more than one user at a time. These
are mini, main-frame and super computers. Ex: Windows NT, UNIX,
XENIX etc., Booting: The process of loading an operating system
into the computer internal memory is known as Booting. It is
classified into two types. They are 1) Cold booting: when the power
is switched on then the process of loading operating system into
the computer memory is known as cold booting 2) warm booting: when
there is a problem with the computer then the process of rebooting
is known as warm booting. Warm booting is done using warm keys or
control keys (alt+del+ctrl) or Reset button in CPU. History of
c-language: COBOL :COmmon Business Oriented Language : Business
BASIC : Beginners All purpose Symbolic Instruction Code : Beginners
FORTRAN: FORmula TRANslation : Scientific and Engineering purpose
Dbase : Data base -doALGOL: AlGOrithmic Language : Some
requirements. CPL : Combined Programming Language : More
requirement, difficult. BCPL : Basic Combined Programming Language:
More requirement, easy B : New concepts (different from BCPL).
1
QUALITY COMPUTER EDUCATION C : BCPL + B + some more features(
data types)
Advantages or features of c-language 1) C-language is designed
in assembly language because its compilation will be faster and
also easy to understand 2) C-language is used for scientific
purpose, engineering purpose, business purpose and general purpose.
3) C-language is used to design and application software and system
software. Ex: Unix 4) C-language supports the storage classes such
as static, external, register and automatic 5) In C-language a
large program is divided into number of functions so that the size
of the program can be reduced as well as program becomes simple. 6)
C-language is very easy to understand and use. 7) C-language
supports pre-defined functions, user-defined functions and
recursive functions. 8) It is flexible. The same program can be
designed in required format with different logics. 9) Data
structures can be build in c-language. 10) Memory can be allocated
at run-time using Dynamic Memory Locations. 11) The user can create
Derived data types and User-defined data type using the Primary
data types. 12) The arguments can be passed from the command prompt
using command line arguments and can perform required operations.
C-language: It is a middle level language and is known as
structured programming language or function oriented programming
language. It is designed and developed by Dennis Ritchie at AT
& T (American Telegraph and Telephone) bell laboratory in USA
in the year 1972. It could satisfy all the user requirements.
Hardware: The set of physical components of the computer is known
as hardware Software : The set of programs (language, package,
operating system, files, folders etc) is known as software.
Firmware: The set of standard routine tasks is known as Firmware
Lineware: The Computer Personnel is known as Lineware Program: The
set of instruction is known as program Instruction: The set of
commands to perform a particular task is known as instruction. A=5
B=6 instruction program
2
QUALITY COMPUTER EDUCATION C=A+B DISPLAY C Source code: The
code, which is written by the user is known as source code Object
code : The system understandable code is called object code. The
source code is converted into ASCII code or EBCDIC code for the
system identification based on the operating system. ASCII :
American Standard Coded for Information Interchange. It uses 8 bits
to represent a character. It belongs to Microsoft user ASCII Ex:
A=65, a=97, 0 =48 , BCD: Binary coded decimal. It uses 6 bits to
represent a character (IBM=International Business Machine) EBCDIC:
Extended Binary Coded Decimal Interchange Code. It uses 8 bits to
represent a character. Compiler and interpreter: These are used to
convert source code into object code. Compiler converts the entire
program at a time where as interpreter converts the program in line
wise. 0,1 8 bits 1024 bytes 1024 KB 1024 MB 1024 GB = 1bit = 1 byte
= 1 KB ( Kilo Byte) = 1MB (Mega Byte) = 1GB (Giga Bytes) = 1TB
(Tera Byte)
Variable: The value, which changes during the execution of a
program, is known as variable. It is used to store the contents in
a memory cell. a 65536 a : 65536 : 6 : 6 variable address of cell
value in the cell
Rules for writing a variable name
3
QUALITY COMPUTER EDUCATION 1) Every variable name should be
started with an alphabet. 2) The length of the variable should not
exceed more than 8 characters. Some compilers may support upto 40
characters. 3) Spaces should not be provided in a variable name. 4)
Reserved words should not be used as a variable name. 5) Except
underscore, other symbols should not be used as a variable name. 6)
A variable can store the value of particular data type only. Ex:
s_no, s1, snumber, a Constants: The value, which does not change
during the execution of the program is known as constant.
Numericals: the numbers should not contain quotations. These values
could be used for calculations. Ex: 56, 77.788 Characters: The
single character should be enclosed within single quotation. A
character may be numbers, special symbols, space, alphabets Ex: 7,
A, u, $, Strings: the set of characters is known as a string and
should be enclosed within double quotation. Ex: ravi, 123, it is
c-language Datatypes it is used to define the type of the data used
for a variable so that the contents can be stored in a variable
based on the data type. 1) Primary data types: it is also known as
fundamental data types or basic data types. These data types are
generally of 4 types. a) integer: it is used to accept numeric
values, which are used for calculations.It occupies 2 bytes and it
is represented with "int". It is again classified into 3 types.They
are int, short int, long int. b) Float: it is used to accept
numeric values as well as decimal values. it occupies 4 bytes.it is
classified into 3 types. Thay are float, double, long double. c)
Character: It is used to accept a single character into a variable.
It occupies 1 byte. It is represented with "char". The collection
of characters is referred as string and it is defined with arrays
ex: char s[4]="ravi" d) Void: it occupies 0 bytes and it will not
return any value.
Type4
formatted Length
Range
QUALITY COMPUTER EDUCATION
stringchar unsigned char enum short int int long int unsigned
short int unsigned int unsigned long int float double long double
%c %c -%hd %d %ld %hu %u %lu %f %lf %Lf 1 byte 1 byte 2 bytes 1
bytes 2 bytes 4 bytes 2 bytes 2 bytes 4 bytes 4 bytes 8 bytes 10
bytes -128 to 127 0 to 255 -32,768 to 32,767 -128 to +127 -32,768
to +32,767 -2,147,483,648 to 2,147,483,647 0 to 255 0 to 65,535 0
to 4,294,967,295 3.4 * e-38 to 3.4 * e+38 1.7 * e- 308 to 1.7
*e+308 3.4*e-4932 to 1.1*e+4932
2) User-defined data types: By using the all the primary data
types, user-defined data types will be created, i.e., using more
than one primary data types a new data type is created. ex: struct,
class, enum (enumerated data types) struct student { int sno, c,
cpp, java; char sname[500; float avg, total; }stud; 3) Derived data
types: By using a single primary data type a new data type is
created, it is known as Derived data types. ex: arrays, functions,
pointers clrscr(): This statement is used to clear the screen at
run-time. Rules for writing C Program 1) The header files should be
declared at the starting of the program in each line. ex: # include
"stdio.h" (input and output streams) #include If header file is
included within quotations then the file will be searched or path
of the file can be specified which may be located in any directory
but if the header file is included within then the file will be
searched only in the include directory. 2) Every program should
contain "main" statement with return-type void. By default it takes
"int" as the return-type. It is the main function where the
compiler starts executing the program. 3) The block of statements
should be enclosed within '{' and '}' 4) Every statement should be
terminated with semicolon (;) 5) The variable should be declared at
the starting of the statement. 6) C is a case sensitive so the
program should be written in lower case letters except some
5
QUALITY COMPUTER EDUCATION keywords, class name etc., Shortcuts
f2- save the program file alt + f9 : Compile the program ctrl+f9:
Run the program alt +f5 : To display the result alt + f : File menu
alt + red character in menu bar : to display the options of the
corresponding menu. alt + x : Exit for c++ editor (turbo editor)
Formatted input and output statement 1) scanf(): it is used to
accept the contents into the variable until it founds enter
(return) key or space symbol. Syntax: scanf(formatted
string,&var1,&var2.); Example: scanf(%d,&a);
scanf(%d%d%d,&a,&b,&c); scanf(%c,&ch); printf(): it
is used to display the string or to display the contents in the
output screen Syntax: printf(string /formatted string,var1,var2..);
Example: printf(it is c-language); printf(the value is: %d,a);
2)
Escape characters \n - new line \t - tab space \a - alert (
bell) \f - form feed \b - backspace \\ - slash symbol \ - single
quote symbol \ - double quotation \r - carriage return \0 - NULL
character Operators c-language is rich in operators because it
support many number of operators. If true it returns is 1 otherwise
value is 0 for any operator. ex: c=a+b+10 =, + operator c,a,b,10
operands (variables, constants)
6
QUALITY COMPUTER EDUCATION 1) Assignment operator: it is used to
assign or store a value into the variable. The operator is =
Syntax: variable=expression; Ex: c=6 (c 6) c=(a+b)-(d+e)/f
Arithmetic operator: it is used to perform the arithmetical
operations. These operators are also known as basic operators. The
operators are +, -, *, \, %(modulus). Ex: c=5+7 (c=12) C=5%2 c=1
(remainder) Relational operators or Comparison operators: These
operators are used to provide relation between the variables. The
operators are >,= ,b)?a:b;
7
QUALITY COMPUTER EDUCATION 7) Casting Operators: It is used to
change the data type of the variable and the content is stored in
the other variable. Ex: k=(int)f; (where k is int and f is float)
K=(char) ch; (where ch is a character containing integer value)
Comma Operator: It is used to separate the contents or variable. It
is indicated with , Ex: int a,b,c; Unary operators: a) Address
operator: it is represented with &. It is used to specify the
address of a variable. b) Increment operator: it is indicated with
++. It is used to increase the value of the variable. It is
classified into two types. They are Pre increment: it is used to
increase the value of the variable of the variable in the present
statement. Here the operator is preceded by the variable Post
increment: it is used to increase the value of the variable of the
variable in the next statement. Here the variable is preceded by
the operator. c) Decrement operator: it is indicated with - -. It
is used to decrease the value of the variable. It is classified
into two types. They are Pre decrement: it is used to decrease the
value of the variable of the variable in the present statement.
Here the operator is preceded by the variable Post decrement: it is
used to decrease the value of the variable of the variable in the
next statement. Here the variable is preceded by the operator. d)
Pointer operator: it is indicated with * . It is used to specify
the value in a pointer variable. Ex: int *p; e) typedef operator :
it is used to specify the alternative name for a data type so that
this altenative name can be used instead of original data type name
Ex: typedef int integer ; Integer a,b,c ; f) sizeof operator : It
is used to specify the number of bytes occupied by a variable. Ex:
printf( the number of byte of integer var is :%d ,sizeof(int))
;
8)
9)
8
QUALITY COMPUTER EDUCATION Other operators: in addition to these
operators c++ supports many number of operators which are used for
the special purpose such as - >, .( ) , [] , ,, { } etc.,
Program to display information on the output screen #include void
main() { clrscr(); printf("welcome to c world"); } WAP to display
your address #include #include main() { clrscr(); printf("\n Name :
GRK"); printf("\n D.No. : 19-168"); printf("\n Street : GPT");
printf("\n City : VSP"); getch(); } Program for demo on \n (new
line) #include #include void main() { clrscr(); printf("\nhello");
printf("\nhai"); printf("\nthis is ravi"); getch(); } Program for
demo on \t (tab space) #include #include void main() { clrscr();
printf("\nWelcome \t to \t c-language"); } Program to find sum of
two numbers #include 10)
9
QUALITY COMPUTER EDUCATION #include void main() { int a,b,c;
clrscr(); printf("enter two numbers"); scanf("%d%d",&a,&b);
c=a+b; printf("sum of two nos:%d",c); getch(); } WAP to accept
address and display the address #include #include main() { char
name[20],st[20],city[20],dno[20]; clrscr(); printf(" Enter Name
:"); scanf("%s",name); printf(" Enter D.No. :"); scanf("%s",dno);
printf(" Enter Street :"); scanf("%s",st); printf(" Enter City :");
scanf("%s",city); printf("\n Name : %s",name); printf("\n Door
no:%s",dno); printf("\n street :%s",st); printf("\n city
:%s",city); getch(); } Write a program to read a character from
keyboard and print its ASCII value. #include #include void main() {
char ch; clrscr(); printf("enter a character");
scanf("%c",&ch); printf("The Equivalent ASCII value for the
given characters is :%d",ch); getch(); }
10
QUALITY COMPUTER EDUCATION
Write a program to read length and breadth or a rectangle and
find its area and perimeter. #include #include void main() { int
l,b,area,peri; clrscr(); printf("enter length of rectangle:");
scanf("%d",&l); printf("enter breadth of rectangle:");
scanf("%d",&b); area=l*b; peri= printf("The area of the
rectangle:%d",area); printf("The perimeter of the
rectangle:%d",perm); getch(); } WAP to convert the given number is
ASCII character #include #include void main() { int n; clrscr();
printf("Enter a value:"); scanf("%d",&n); /* char ch; ch=n;*/
printf("\n The ASCII charcter is:%c",n); getch(); } Write a program
to read radius of a circle and find area and circumference.
#include #include void main() { int r,area,cir; clrscr();
printf("enter radius of the circle:"); scanf("%d",&r);
area=(3.14*r*r); cir=(2*3.14*r); printf("The area of the
circle:%d",area); printf("The circumference of the circle:%d",cir);
getch();
11
QUALITY COMPUTER EDUCATION } Program to find addition,
subtraction, multiplication, division and modulus of two numbers
#include #include void main() { int a,b,c,d,e,g; float f; clrscr();
printf("enter two numbers"); scanf("%d%d",&a,&b); c=a+b;
d=a-b; e=a*b; f=a/b; g=(float)a%b; printf("\n sum of two
numbers:%d",c); printf("\n sub of two numbers:%d",d); printf("\n
multiplication of two numbers:%d",e); printf("\n division of two
numbers:%d",f); printf("\n modulus of two numbers:%d",g); getch();
} Program to convert centigrade to Fahrenheit # include # include
void main() { int c,f; clrscr(); printf("enter the value of
centigrade"); scanf("%d",&c); f=(9*c/5)+32; printf(" the value
in Fahrenheit is %d",f); getch(); } program to swap two numbers
using intermediate variable #include #include void main() { int
a,b,c;
12
QUALITY COMPUTER EDUCATION clrscr(); printf(Enter two number);
scanf(%d%d,&a,&b); c=a; a=b; b=c; printf(After swapping the
values are :%d %d,a,b); getch(); } program to swap two numbers
without using intermediate variable #include #include void main() {
int a,b; clrscr(); printf(Enter two number);
scanf(%d%d,&a,&b); a=a+b; b=a-b; a=a-b; printf(After
swapping the values are :%d %d,a,b); getch(); } program to find the
biggest of 2 numbers using conditional operator #include #include
void main() { int a,b,max; clrscr(); printf("enter first value");
scanf("%d",&a); printf("enter second value");
scanf("%d",&b); max=(a>b)?a:b; printf("max value :%d",max);
getch(); } program to find the biggest of 3 numbers using
conditional operator #include #include void main()
13
QUALITY COMPUTER EDUCATION { int a,b,c,x,y; clrscr();
printf("enter three values"); scanf("%d%d%d",&a,&b,&c);
x=(a>b)?a:b; y=(x>c)?x:c; printf("max value:%d",y); getch();
} WAP to find the smallest of 5 numbers using ternary operator (or)
conditional operator #include #include void main() { int
a,b,c,d,e,x,y,z,p; clrscr(); printf("Enter five values");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e); x=(ac) {
printf(" the biggest number is:%d",b); } else { printf(" the
biggest number is:%d",c); } getch(); } program to find whether the
given number is positive, negative or zero #include #include void
main() { int n; clrscr(); printf ("enter one number");
scanf("%d",&n); if (n>0) { printf("the no is
positive:%d",n); } else if (n=21) printf("He is Eligible for
voting"); else prinf("He is Not eligible for voting"); } else
if(gender=='f' || gender=='F') { if(age>=18) printf("She is
Eligible for voting"); else prinf("She is Not eligible for
voting"); } else { printf("Wrong input"); } getch(); } the above
program using switch-case
21
QUALITY COMPUTER EDUCATION /* switch(n%2) { case 0: printf("It
is even"); case 1: printf("It is odd");
WAP to find Quadratic Roots #include #include #include void
main() { int a,b,c,d; float x1,x2; clrscr(); printf("Enter values
for a,b,c"); scanf("%d%d%d",&a,&b,&c); d=(b*b)-(4*a*c);
if (d==0) { printf("\n Roots are real & equal");
x1=x2=-b/(2*a); printf("\n The two roots are:%f\t%f" } else if
(d>0) { printf("\n Roots are real & unequal");
x1=(-b+sqrt(d) )/(2*a); x2=(-b-sqrt(d) )/(2*a); printf("\n The two
roots are:%f\t%f",x1,x2); } else { printf("\n Roots are
imaginary"); x1=-b/(2*a); x2=sqrt(d)/(2*a); printf("\n First root:
%f+i(%f)",x1,x2); printf("\n Second root: %f-i(%f)",x1,x2); }
getch(); }
,x1,x2);
Program to accept employ number, employ name, basic salary and
calculate ta,da, hra, pf, it, net salary, gross salary
(if-statement) #include #include void main()
22
QUALITY COMPUTER EDUCATION { char ename[10]; int eno; float
ta,da,hra,pf,it,nsal,gsal,basic; clrscr(); printf("Enter employ
number"); scanf("%d",&eno); printf("Enter Employ Name");
scanf("%s",ename); printf("Enter Basic salary");
scanf("%f",&basic); if (basic5000 && basic10000
&& basic20000) { ta=(15.8*basic)/100; da=(16.8*basic)/100;
hra=(19.8*basic)/100; pf=(19.5*basic)/100; it=(18.99*basic)/100; }
gsal=basic+ta+da+hra; nsal=gsal-it-pf; clrscr();
23
QUALITY COMPUTER EDUCATION printf("\n printf("\n printf("\n
printf("\n printf("\n printf("\n printf("\n printf("\n printf("\n
printf("\n getch(); } The employ number:%d",eno); The employ
name:%s",ename); The Basic Salary:%0.2f",basic); The Travelling
allowance:%0.2f",ta); The Dearness allowance:%0.2f",da); The
Provident fund:%0.2f",pf); The Income Tax:%0.2f",it); The House
Rent Alowance:%0.2f",hra); The Gross Salary:%0.2f",gsal); The Net
Salary:%0.2f",nsal);
write a program to accept 3 sides and check whether it is right
angled triangle, obtuse angled triangle, acute angled triangle,
scalene triangle, equivaleteral triangle or isosceles triangle
#include #include void main() { int a,b,c,max,x,y; clrscr();
printf("Enter values for three sides"):
scanf("%d%d%d",&a,&b,&c); if (a>b && a>c)
{ max=a; x=b; y=c; } else if (b>a && b>c) { max=b;
x=a; y=c; } else { max=c; x=a; y=b; } if (max(x*x)+(y*y) )
printf("\n It is Obtuse angled triangle"); else printf("\n It is
Acute angled triangle"); if(a==b==c) printf("\n It is Equilateral
triangle"); else if(a==b || b==c || c==a) printf("\n It is
Isosceles triangle"); else printf("\n It is Scalene triangle"); }
else { printf(It is not a triangle); } getch(); } Program to
display the equivalent day for the given value(1-7) (switch-case)
#include #include void main() { int n; clrscr(); printf("Enter a
number(1-7)"); scanf("%d",&n); switch(n) { case 1: printf("\n
Sunday");break; case 2: printf("\n Monday");break; case 3:
printf("\n Tuesday");break; case 4: printf("\n Wednesday");break;
case 5: printf("\n Thursday");break; case 6: printf("\n
Friday");break; case 7: printf("\n Saturday");break; default:
printf("Wrong input"); } getch(); } #include #include void main() {
int n;
25
QUALITY COMPUTER EDUCATION clrscr(); printf("Enter a
number(1-7)"); scanf("%d",&n); switch(n) { case 1: printf("\n
Sunday"); case 2: printf("\n Monday"); case 3: printf("\n
Tuesday"); case 4: printf("\n Wednesday"); case 5: printf("\n
Thursday"); case 6: printf("\n Friday"); case 7: printf("\n
Saturday");break; default: printf("Wrong input"); } getch(); } WAP
to check whether the given character is vowel or consonant #include
#include void main() { char ch; clrscr(); printf("Enter a
character"); scanf("%c",&ch); switch(ch) { case 'a': case 'A':
case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case
'u': case 'U': printf("It is vowel");break; default: printf("It is
consonant");break; } getch(); } Program to display the day for the
given date in august 2002 (switch-case) #include #include void
main() { int date; clrscr();
26
QUALITY COMPUTER EDUCATION printf("Enter date in August
2002(1-31)"); scanf("%d",&date); if (date>=1 &&
dateinfo) cout left); cout no) bnode->left =
build(bnode->left,tno); else bnode->right =
build(bnode->right,tno); return (bnode); }
void printtreeinorder(struct node *ptr) { if(ptr != NULL) {
95
QUALITY COMPUTER EDUCATION printtreeinorder(ptr->left);
printf("\t%d",ptr->no); printtreeinorder(ptr->right); } }
void printtreepreorder(struct node *ptr) { if(ptr != NULL) {
printf("\t%d",ptr->no); printtreepreorder(ptr->left);
printtreepreorder(ptr->right); } }
void printtreepostorder(struct node *ptr) { if(ptr != NULL) {
printtreepostorder(ptr->left);
printtreepostorder(ptr->right); printf("\t%d",ptr->no); } }
program for binary tree #include #include #include struct node {
int no; struct node *left; struct node *right; }; void
printtree1(struct node *); void printtree2(struct node *); void
printtree3(struct node *);
96
QUALITY COMPUTER EDUCATION
struct node *build(struct node *,int); void main() { struct node
*root,*build(); char ch; int tno=0; clrscr(); root=NULL;
printf("\nEnter any number: "); scanf("%d",&tno);
fflush(stdin); while(tno != 0) { root = build(root,tno);
printf("\nEnter any number (0-stop)"); scanf("%d",&tno);
fflush(stdin); } printtree1(root); printf("\n"); printtree2(root);
printf("\n"); printtree3(root); getch(); } struct node
*build(struct node *bnode , int tno) { if(bnode == NULL) { bnode
=(struct node *) malloc(sizeof(struct node)); bnode->no = tno;
bnode->left = bnode->right = NULL; } else if(tno <
bnode->no) bnode->left = build(bnode->left,tno); else
bnode->right = build(bnode->right,tno); return (bnode); }
//inorder
97
QUALITY COMPUTER EDUCATION
void printtree1(struct node *ptr) { if(ptr != NULL) {
printtree1(ptr->left); printf("%d\t",ptr->no);
printtree1(ptr->right); } } //preorder void printtree2(struct
node *ptr) { if(ptr != NULL) { printf("%d\t",ptr->no);
printtree2(ptr->left); printtree2(ptr->right); } }
//postorder void printtree3(struct node *ptr) { if(ptr != NULL) {
printtree3(ptr->left); printtree3(ptr->right);
printf("%d\t",ptr->no); } }
98