SlideShare a Scribd company logo
1 of 76
S. MADHU MOHAN C - Language
C- language
β€œC’ seems to be a strange name for a programming language. This is one of the
most powerful and popular programming language. β€œC” is an off-spring of β€œBasic
Combined Programming Language (BCPL)” called β€œB” developed in 1960’s at
Cambridge University. β€œB” language was modified by β€œDENNIS M. RITCHIE” and was
implemented at β€œBell Labs” in 1970, for many years β€œC” language was used mainly in
academic environments. But the release of β€œC” compilers increased the popularity of the
β€œC” language.
Features :
1. β€œC” language is having rich set of built in functions and different types of
operators can be used to write program. This language will be used to develop
application software, and also the system software.
2. It is a structured programming language.
3. β€œC” is highly portable. It means that the β€œC” program are written on one computer
can be run on another computer with a little (or) no modifications.
4. β€œC” program are efficient and fast.
5. It is a case sensitive language.
β€œC” Character Set :
There are a few pre-defined characters can from a character set in β€œC” language.
The β€œC” character set should be consisting of
1. Alphabets (A – Z and a – z)
2. Digits (0-9)
3. Special Characters (+,-,*,/,<,>,,;,”,’, ( ), [ ], { },……etc)
4. White spaces (blank space, horizontal tab, new line character etc)
Key Words or Reserved Words :
A key word means already it is having some predefined meanings or fined
meanings, and the meanings cannot be changed. All the key words must be written in
lower case letters only. They are auto, break, case, char, const, continue, default, double,
else, enum, extern, float, go to, for, int, if, long, register, return, short, signed, size of,
static, struct, switch, typedef, union, unsigned, void, volatile, while,…… etc.
Identifiers :
An identifier is a name given to various program elements to identify an object.
Variables, functions, arrays, …. Etc.
Rules :
1. An identifier may consist of alphabets and digits, but the first character must be an
alphabet.
2. Both upper case and lower case letters are permitted, but commonly we can use
lower case letters only.
3. The β€œ_” (underscore) character is also permitted in identifiers. It is namely used to
link between two letters.
- 1 -
S. MADHU MOHAN C - Language
Constants :
A constant in β€œC” language refers to the fixed value that do not change during the
program execution. The β€˜C’ language supports several types of consonants. They are
Integer Constant :
An Integer constant refers to a sequence of digits. There are 3 types of integer
constants namely decimal, octal, and hexa decimal.
The decimal integers consists of a set of digits from 0-9 and preceded by an
optional β€˜+’ or β€˜- ’ sign.
Ex : 565, -123, +676
Note :
Spaces, commas, and special characters are not permitted in between the digits.
An octal integer consist of the combination of digits from the set 0-7 with a
leading β€˜0’.
Ex : 0123, 0456, 0321, etc.
A sequence of digits preceded by β€˜ox’ or β€˜OX’ is considered as hexa-decimal
integer. It may also include alphabets from A to F and digits from 0-9. the alphabets A –
F represents from 10 – 15.
Ex : ox67AB, oxAB123, ox56F
Real Constants :
The integer numbers are inadequate to represent quantities likes prices, distances,
heights, weights, etc. there quantities are represented by numbers containing fractional
part like 5.67, 70.62 etc., and such numbers are called real or floating point constants.
Ex : -2.16, 3.34m 80.318
A real no may also represents in an exponential format.
Ex : The value 215.65 can be represented in an exponential format is 2.1565E2.
- 2 -
Decimal
Constants
Octal
Constants
Hexa
Deciaml
Numeric Constants Character Constants
Integer
Constants
Real
Constants
Single
Character
Constants
String
Constants
Constant
s
S. MADHU MOHAN C - Language
Single Character Constants :
It contains a single character enclosed in a single quotation marks.
Ex : β€˜5’, β€˜x’, β€˜+’ etc.
String Constant :
A sequence of character can be enclosed in a double quotation marks are called
string constants.
Ex : β€œ123”, β€œABC”, β€œA123” etc.
Variables :
A variable is a data name that may be used to store a data value. The variable
value can change during the program exhibition. The value of a variable is a varied from
time to time.
Rules :
1. They must begin with an alphabet, some compilers may accept underscore(_) as the
first character also.
2. The maximum length of a variable is almost 31 characters, but the first 8 characters
are treated as a variable name by most of the compilers.
3. Upper case and lower case or significant that is the variable Total is not equal to total.
4. The variable should not be a reserved word.
5. Spaces are not allowed in between the variable.
Data Types :
β€œC” – language is having a rich set of data types. The storage representation and
the machine instructions to handle constants differ from system to system. The variety of
data type available to allow the programmer to select the appropriate type to the needs of
a program.
- 3 -
User defined
data type
Ex: Structures
Union
enum
Derived
type
Ex: Arrays
Functions
Pointers
Built in
Data types
Ex: Structures
Union
enum
Integral data
types
void Floating
data types
Int Char Float Double
C data types
S. MADHU MOHAN C - Language
The β€œC” compiler supports four fundamental (built in) data types namely interger
(int), character (char), floating point value and double precision (double).
Integer data type (int) :
Integer are whole number with a range of values supported by a particular
machine. Generally an integer occupies 2-4 bytes of storage and the word is size varies
from machine to machine. The range of an integer is -32,768 to 32,767.
There are three classes of integers namely short int, int and long int in both signed
and unsigned forms. The short int represents small integer values and requires half the
amount of storage as regular integer uses. If we declared as a long integer (int), we will
increase the range of an integer. If we done specify either signed or unsigned, by default
the computer will assume only the signed format.
Float data type :
The float numbers (real) are stored in 32 bits (4 bytes or 1 word) with 6 digits of
precessions. The floating point numbers are defined by the keyword β€œfloat”, If we specify
the data types as β€œdouble”, it uses 64 bits (8 bytes) and giving a precision of 14 digits. To
extend the precision for that, we may use long double also.
Character data type :
A single character can be defined as a character type data (char). The characters
are usually stored in 8 bits of internal storage. The signed characters have the values from
-128 to 127, and unsigned have the values from 0 to 255.
Size and Range of Data types :
Type Size (bits) Range
1. char or signed char 8 -128 to 127
2. unsigned char 8 0 to 255
3. int or signed int 16-32 -32,768 to32,767
4. unsigned int 16-32 0 to 65,535
5. short int or signed short int 8 -128 to 127
6. unsigned short int 8 0 to 255
7. long int or singed long int 32 -2,147,483,648
to
2,147,483,647
8. unsigned long int 32 0 to 4,294,967,295
9. float 32 3.4e-38 to 3.4e+38
10. double 64 1.7e-308 to 1.7e+308
11. long double 80 3.4e-4932 to 1.1e+4932
- 4 -
S. MADHU MOHAN C - Language
Operators :
β€œC” language is having rich set of operators. An operator is a symbol that tells the
computer to perform a mathematical or a logical work. The β€˜C’ operators can be
classified into no. of categories. They are
1. Arithmetic Operators :
β€˜C’ provides a few basic arithmetic operators. The operators β€˜+’, β€˜-’, β€˜*’, etc
are work in the same way as they do in the other languages also.
Operator Meaning
+ (plus) Addition
- (unary) Subtraction
* Multiplication
/ Division
% Modules or Remainder
2. Relational Operators :
The following are the relational operators used in β€˜C’. They are
Operator Meaning
< Less than
> Greater than
<= Less than equal to
>= Greater than equal to
= = Equal to
!= Not equal to
3. Logical Operators :
β€˜C’ has three types of logical operators. They are
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
4. Assignment Operators :
The assignment operator is used to assign a value to a variable or to assign
the result of an expression to a variable. The assignment operator in β€˜C’ language is
β€œ=”. They are
Variable = expression
- 5 -
S. MADHU MOHAN C - Language
Ex : x=5; y=a+b;
5. Increment or Decrement Operators :
β€˜C’ has two very useful operators, not generally found in other languages.
These are the increment(++) and decrement(--) operators. The operator β€˜++’ adds β€œ1”
to the operand. While β€˜- -’ subtracts β€œ1” from the operand. Both are called unary
operators.
Here ++m is equivalent to m=m+1
-- m is equivalent to m=m-1 or m=-1
++m is called pre incrementation, m++ is called post incrementation.
Similarly --m is called pre decrementation and m-- is called post decrementation.
Ex : m=5; y=++m
In the case the value of y and m would be 6.
If we rewrite the statement as
M=6; y=m++
In this case the value of y is 5 and m is 6.
6. Conditional Operator (Ternary) :
A ternary operator pair β€œ?” is available in β€˜C’ language to construct a
conditional expression. The syntax is
Variable=exp1 ? exp2 : exp 3;
Example : a=10, b=15
X=a>b ? a:b
In this example first it will evaluate the exp 1. If it is true the value of a (exp2) will
be assigned to x. If the condition is false, the value of b(exp3) will be assigned to x.
7. Bitwise Operators :
β€˜C’ has a distinction of supporting some operators called as bitwise
operators and are used for manipulation of data at bit level. These operators are used
for testing the bits or shifting them from right to left, left to right etc., They are
Operator Meaning
& Bitwise AND
| (pipe symbol) Bitwise OR
^ Bitwise x-OR (ap)
~ /S
Complement (tilde)
<< Left shift
>> Right shift
8. Special Operators :
β€˜C’ supports some special operators, such as comma(,) operaor, β€œsize=of”
operator, point operators (& and *), member selection operators (. and ).
- 6 -
S. MADHU MOHAN C - Language
Expression :
An expression is a combination of variables, constants and operators and form an
algebra type of equation is called an expression. β€˜C’ can handle any type of mathematical
expression.
The expression are evaluated by using an assignment statement like
Variable=expression;
Here the variable is a valid β€˜C’ variable name. After evaluating the expression the
result will be assigned to the variable on the left hand side.
Precedence of Operators :
An arithmetic expression without parenthesis [( )] will be evaluated from left to
right, using the rules of precedence of operators. There are two priority levels of
arithmetic operators. They are
High priority --------- *, /, %
Low priority --------- +, -
When ever parenthesis are used, the expression within the parenthesis are assumed
high priority. If an expression consist of two or more sets of parenthesis, the expression in
the left most is evaluated first and the right most is evaluated last.
Ex : a=9, b=12, c=3
x=a-b/3+c*2-1 y=a-b/(3+c)*(2-1)
=9-2/3+3*2-1 =9-12/(3+3)*(1)
=5+5 =9-12/6*1 = 9-12/6
=10 =9-2 = 7
Input and Output Statements :
There are a few pairs of input and output statement in β€˜C’ language. They are
getchar( ) and put char( ), scanf( ) and printf( ).
getchar( ) :
This is a one type of input statement in β€˜C’ language. By using of this statement,
we can read a single character at a time from the keyboard. (This can also be done by
using scanf( ) statement).
Syntax : variable name= getchar( )
Here the variable name is a valid β€˜C’ name that has been declared as a character
type when this statement is executed. The computer waits until a key is pressed and that
character is assigned to the variable name. They getchar( ) function is always on the right
hand side of the assignment statement and the value of the getchar( ) function is assigned
to the left hand side variable.
Example : char name;
- 7 -
S. MADHU MOHAN C - Language
Name= getchar( );
putchar( ) :
it is an output statement in β€˜C’ language. By using of this statement we can write
(display) the characters one at a time on to the screen.
Syntax : putchar(variable name)
Example : name= β€˜y’;
Putchar(name);
scanf( ) :
The input data can be entered into the computer from a standard input device
(keyboard) by using of a scanf statement. This function can be used to enter any
combination of integer and floating point values, single character and also strings.
Syntax : scanf(β€œcontrol string”, arg1,arg2,………argn);
Here the control string specifies field formats, in which the data is to be entered
and arg1, arg2,…….. arg n specifies the address of the variables where the data is stored.
The control string contains the field specification and also the conversion character
(%) with a data type specifier. The blank spaces, new line characters are ignored.
printf( ) :
The output data can be written from a computer on to a standard output device
(screen) using a function called β€œprinf( )”. This function can be used to display any
combination of integer values, floating point values, single character, strings etc.
Syntax : printf(β€œcontrol string”, arg1, arg2, ……….. arg n);
Here the control string specifies
a. the character that will be printed on the screen as they appear.
b. Format specifications.
c. Escape sequence characters.
Escape sequence characters (or) back slash characters :
β€˜C’ supports some special back slash characters, that are used in output statements.
Note that each of them represents one character although they consist of two characters.
Character Constant Meaning
n New line charcter
b Back space
a Alert bell
t Horizontal tab
v Vertical tab
’ Single quotation mark
” Double quotation mark
? Question mark
- 8 -
S. MADHU MOHAN C - Language
0 Null character
 Back slash
Declaration of a variable :
After designing the suitable variable names, we must declare them in a program.
A variable can be used to store a value of any data type.
Syntax ; data type var1, var2, …….var n;
Here the data type specifies the type of the data (int, float, char ……) and var1,
var2, …… are the variable names.
Example : int a,b;
float x,y;
char n;
The variable specifies mainly two things. They are
1. It tells the compiler what the variable name is.
2. It specifies what type of data the variable will hold.
Structure of a β€˜C’ program :
Every β€˜C’ program may consist of one or more functions and one of the function is
main( ). A β€˜C’ program contains one or more sections also.
Syntax : documentation section
Link section
Definition section
Global variable declaration section
Example : main( )
{
Declaration part;
Executable part;
}
Subprogram section
Here the documentation section consists set of comment lines giving the name of
the program. The programmers name and other details like time, date etc.
The link section provides instructions to the compiler to link the functions from
the library.
The definition section will be used to declare the constants. There are some
variables that are used in more than one function and such variables are called global
variables and declared in the global declaration section i.e., outside the main ( ) function.
Every β€˜C’ program must have a main ( ) function. This section contains two parts
i.e., the declaration part we and the executable part. In the declaration part we declare all
the variables that are used in the executable part. These two parts must appear between
the opening and closing brackets. The program execution begins at the opening bracket
- 9 -
S. MADHU MOHAN C - Language
and end at the closing bracket. The closing bracket indicates the end of the program. All
the statements in the declaration part and executable part will be terminated by a
semicolon (;). The subprogram section contains all the user defined functions that are
called in the main( ) function. The user defined functions are generally placed
immediately after the main function.
β€˜C’ Header files or Library Functions :
β€˜C’ language is having a no. of library functions that performs various files. The
β€˜C’ language header files contains all these functions. They are
<stdio.h> Standard input output functions.
<ctype.h> Character testing and conversion.
<math.h> Mathematical function.
<string.h> String manipulation function.
<time.h> Time manipulation function.
Some Character Codes used in Printf( ) and Scanf( ) Functions :
Code Meaning of purpose
%c Display or read a single character.
%d Display or read an integer value.
%f Display or read a floating point value.
%s Display or read a string.
%[..] Display or read a string of words.
PROGRAMS
1. Write a program to display your address.
Solution : #include <stdio.h>
main( )
{
printf(β€œn Name : S. MADHU MOHAN”);
printf(β€œn Street : College Road”);
printf(β€œn Town : NANDIKOTKUR”);
}
2. Write a program to read any 2 values and find the sum of 2 values.
Solution : #include <stdio.h>
main( )
{
Int a,b,c,
Clrscr();
printf(β€œn Enter any numbers :”);
- 10 -
S. MADHU MOHAN C - Language
scanf(β€œ%d”, &a);
printf(β€œn Enter Another Number :”);
scanf(β€œ%d”, &b);
c=a+b;
printf(β€œn Sum of two numbers :%d”,c);
getch();
}
3. Write a program to read principle, time, and rate. Calculate the simple interest
and display it.
Solution : #include<stdio.h>
main()
{
Float p,t,r,I;
clrscr();
printf(β€œn Enter P,T,R :”);
scanf(β€œ%f”,&p);
scanf(β€œ%f”,&p);
scanf(β€œ%f”,&p);
i=(p*t*r)/100
printf(β€œn The Interest is :%f”,i);
getch();
}
4. Write a program to read any 3 values and find the product.
Solution : #include<stdio.h>
main()
{
int a,b,c,d;
printf(β€œn Enter any 3 numbers :”);
scanf(β€œ%d”,&a);
scanf(β€œ%d”,&b);
scanf(β€œ%d”,&c);
d=a*b*c;
printf(β€œn The Product of 3 numbers :%d”, d);
getch();
}
5. Write a program to read the radius of circle and calculate area and circumference
Solution : #include<stdio.h>
main()
{
float r,a,c;
clrscr();
printf(β€œn Enter the radius of Circle :”);
scanf(β€œ%f”,&r);
a=3.14*r*d;
- 11 -
S. MADHU MOHAN C - Language
c=2*3.14*r
printf(β€œn Area :%f”,a); getch();
printf(β€œn Circumferene :%f”, c); }
6. write a program to read the city name and the temperature in Celsius ? Calculate
the temperature in Fahrenheit and display it.
Solution : #include<stdio.h>
main()
{
char name[10];
float C,F;
clrscr();
printf(β€œn Enter city name and temperature in celsius :”);
scanf(β€œ%s”, name);
scanf(β€œ%f”, &c);
f=32+(1.8*c);
printf(β€œTemperature in Fahrenheit :%f”, f);
getch();
}
7. Write a program to read a city name and temp in Fahrenheit. Calculate the
Celsius tem and display it.
Solution : #include<stdio.h>
main()
{
Char name[10];
Float c,f;
clrscr();
printf(β€œn Enter city name and Temperature in Fahrenheit :”);
scanf(β€œ%s”, name);
scanf(β€œ%f”, &f);
c=(f-32)/1.8;
printf(β€œn The Temperature in Celsius :%f”,c);
getch();
}
8. Write a program read rollno, name, 2 subject marks. Calculate total and average
and display it.
Solution : #include<stdio.h>
main()
{
int no;
char name[10];
float m1, m2, t,a;
clrscr();
printf(β€œn Enter name, rollno, marks in 2 subjects :”);
scanf(β€œ%s”, name);
- 12 -
S. MADHU MOHAN C - Language
scanf(β€œd”, &no);
scanf(β€œ%f”, &m1);
scanf(β€œ%f”, &m2);
t=m1+m2;
a=t/3;
printf(β€œn Total Marks :%f”, t);
printf(β€œn Average Marks :%f”, a);
getch();
}
9. Write a program to read empno, name, basic salary. Calculate DA(30%),
HRA(15%), and PF(10%). Compute gross and net salary and display it.
Solution : #include<stdio.h>
main()
{
Int eno,b;
char name[20];
float da,hra,pf,gs,ns;
clrscr();
printf(β€œn Enter eno,ename, basic salary :”);
scanf(β€œ%d,%d”, &eno, &b);
scanf(β€œ%s”, name);
da=b*30/100;
hra=b*15/100;
pr=b*10/100;
gs=basic+da+hra;
ns=gs-pf;
printf(β€œn DA :%f”,da);
printf(β€œn HRA :%f”, hra);
printf(β€œn PF :%f”, pf);
printf(β€œGross Salary :%f”, gs);
printf(β€œNet salary :%f”, ns);
getch();
}
Control Statements (or) Control Structures :
If condition :
It is a conditional control structure in β€˜C’ language. It is a powerful decision
making statement and it is used to control the flow of execution of statements. The β€œif”
structure is having 4 types of syntax. They are.
1. Simple If :
If (condition)
Statement -1;
Statement -2;
Statement –n;
- 13 -
S. MADHU MOHAN C - Language
Here first it will evaluate the condition if it is true statement will be executed and
execute the remaining statements in a sequential order (statement -2 onwards). If the
condition is false it will skip the statement -1 and execute the remaining statements in
sequential order.
If we want to execute more than one statement in a single if condition, we have to
enclose all the statements within the brackets. The format is
If (condition)
{
State -1;
State -2;
}
-----;
-----;
2. If .. Else structure :
if(condition)
{
True statement block;
}
Else
{
False statement block;
}
State –x;
State – y;
Here first it will check the condition, if it is true, the true statement block will be
executed and comes outside the loop and execute the remaining statements n a sequential
order. (state –x ) onwards.
If the condition is false, it will execute the false statement block and comes out
side the loop and execute the remaining statements (state – x) onwards one by one.
3. Nested If statement :
If(condition 1)
{
If(condition 2)
{
State – 1;
------;
------;
}
else else
{ {
State – 2; state – 3;
- 14 -
S. MADHU MOHAN C - Language
------; ------;
------; ------;
} }
}
Here first it will check the condition -1. If it is true, then it will evaluate the
condition -2. If it is also true, the statement 1 ….. will be executed and comes outside the
loop and execute the remaining statements one by one (statement – x onwards).
If the condition 1 is true and condition 2 is false it will execute the state -2 …. and
comes outside the loop and execute the remaining statements one by one. (state x…).
If the condition 1 is initially false, then it will execute the state -3 …. And comes
outside the loop and execute the remaining statements one by one. (state – x …..).
4. Else … If Ladder :
if(condition 1)
state – 1;
else
if (condition 2)
state – 2;
else
if (condition 3)
state – 3;
else
state – 4;
state – x;
state – y;
---------
---------
Here first it will evaluate the condition 1. If it is true, the state -1 will be executed
and comes outside the loop and execute the remaining statements, in sequential order
(state – x ) onwards.
If the condition 1 is false then it will evaluate the condition 2. If it is true the state
-2 will be executed and comes out side the loop and execute the remaining statements
that is (state – x) onwards.
If the condition 1 and condition 2 are true then it will be evaluate the condition 3.
If it is true statement 3 will be executed and comes out of the loop and execute the
remaining statements one by one.
If the condition 1, condition 2, condition 3 are false, automatically it will execute
the statement 4 and comes out side the loop and execute the remaining statements in
sequential order (state – x) onwards.
10. Write a program to read any two values and find biggest number.
Solution : #include<stdio.h>
- 15 -
S. MADHU MOHAN C - Language
main()
{
int a,b;
clrscr();
printf(β€œn Enter any two numbers :”);
scanf(β€œ%d %d”, &a, &b);
if(a>b)
printf(β€œn Biggest number :%d”,a);
else
printf(β€œn Biggest number :%d”,b);
getch();
}
11. Write a program to read any 3 values and find the biggest number.
Solution : #include<stdio.h>
main()
{
int a,b,c;
clrscr();
printf(β€œn Enter any three numbers :”);
scanf(β€œ%d %d %d”, &a, &b,&c);
if(a>b && a>c)
printf(β€œn Biggest number :%d”,a);
else
if(b>c)
printf(β€œn Biggest number :%d”,b);
else
printf(β€œn Biggest number :%d”,c);
getch();
}
12. Write a program to read a person name and his age. Find out whether he is
eligible to vote or not. Display how many years he has to wait.
Solution : #include<stdio.h>
main()
{
char name[10];
int a,x;
clrscr();
printf(β€œn Enter persons name :”);
scanf(β€œ%s”, name);
printf(β€œn Enter Persons age :”);
scanf(β€œ%d”,&a);
if(a>=18)
printf(β€œn Eligible to Vote);
else
{
x=18-a;
- 16 -
S. MADHU MOHAN C - Language
printf(β€œn Not Eligible and he has to wait %d years to vote”, x);
}
getch();
}
13. Write a program to read no, name, marks in 2 subjects. Find out total, average
and display the results. The result will be avg<35 fail, avg<50 third, avg<60 second,
avg<75 fist, avg>=75 distinction.
Solution : #include<stdio.h>
main()
{
char name[20];
int no,m1,m2,tot;
float avg;
clrscr();
printf(β€œn Enter no, name, marks in 2 subjects :”);
scanf(β€œ%d %s%d%d”, &no, name, &m1,&m2);
tot=m1+m2;
avg=tot/2;
printf(β€œn Roll number :%d”,rno);
printf(β€œn Name :%s”, name);
printf(β€œn Marks 1 :%d”, m1);
printf(β€œn Marks 2 :%d”,m2);
printf(β€œn Total Marks :%d”,tot);
printf(β€œn Average Marks :%f”, avg);
if(m1<35 || m2<35)
printf(β€œn Result :Fail”);
else
if (avg>=35 && avg<50)
printf(β€œn Result :Third class”);
else
if (avg>=50 && avg<60)
printf(β€œn Result :Second class”);
else
if (avg>=60 && avg<75)
printf(β€œn Result :First class”);
else
printf(β€œn Result : Distinction”);
getch();
}
14. Write a program to read meter no, customer name, present, previous readings.
Calculate the no. of units and display the bill amount. The rate will be
1. 1st
100 units --- 2/- per unit.
2. Next 100 units --- 3/- per unit.
3. above 200 units --- 4/- per unit.
4. the min bill is ---- 150/-
- 17 -
S. MADHU MOHAN C - Language
5. if the bill exceeds 450/-, 10% additional charge will be added to the bill
amount. .
Solution : #include<stdio.h>
main()
{
char name[20];
int mno,pre,prv, units;
float amount;
clrscr();
printf(β€œnEnter mno, name, present and previous readings :”);
scanf(β€œ%d%s%d%d”, &mno, name, &pre, &prv);
units=pre-prv;
if (units<=100)
amount=units*2;
else
if (units>100 && units<=200)
amount=200+(units-100)*3;
else
amount=500+(units-200)*4;
if (amount<1500
amount=150;
if (amount>450)
amount=amount+(amount*10/100);
printf(β€œn Meter Number :%d”, mno);
printf(β€œn Customer Name :%s”, name);
printf(β€œn Number of Units :%d”, units);
printf(β€œn Total Amount :%d”, amount);
getch();
}
While – statement :
β€œWhile” is a repeated conditional control structure in β€˜C’ language. The syntax is
While(condition)
{
Statement -1;
Statement -2;
Statement -3;
---------
---------
---------
Statement – n;
}
Statement – x;
Statement – y;
---------
---------
---------
- 18 -
S. MADHU MOHAN C - Language
Here the while condition first it will evaluate the condition. If it is true all the
statements will be executed within the brackets. After executing the statements once
again it will test the condition. If it sis true again, the same statements will be executed
repeatedly and so on. This process will be continued as long as the condition is true.
When ever the condition becomes false it comes outside the loop and execute the
remaining statements in a sequential order (state –x ) onwards.
15. Write a program to display the natural numbers. Up to 20.
Solution : #include<stdio.h>
main()
{
int i=1;
clrscr();
printf(β€œn The natural no’s are :”);
while(i<=20)
{
printf(β€œn %d”,i);
i=i+1;
}
getch();
}
16. Write a program to display all the even numbers up to 50.
Solution : #include<stdio.h>
main()
{
int n=2;
clrscr();
printf(β€œn The Even no’s are :”);
while(i<=50)
{
printf(β€œn %d”,n);
n=n+2;
}
getch();
}
17. Write a program to display all the odd numbers up to 50.
Solution : #include<stdio.h>
main()
{
int n=1;
clrscr();
printf(β€œn The Even no’s are :”);
while(i<=50)
{
printf(β€œn %d”,n);
n=n+2;
- 19 -
S. MADHU MOHAN C - Language
}
getch();
}
18. Write a program to find the sum and average of first 100 natural numbers.
Solution : #include<stdio.h>
main()
{
int n=1,sum=0;
float avg;
clrscr();
while(i<=100)
{
sum=sum+n;
n=n+1;
}
Aavg=sum/100;
printf(β€œn Sum of 100 Numbers :%d”,sum);
printf(β€œn Average of 100 Numbers :%f”, avg);
getch();
}
19. Write a program to read any 5 values and find the sum and average.
Solution : #include<stdio.h>
main()
{
int i=1, sum=0,n;
float avg;
clrscr();
printf(β€œn Enter any 5 values:”);
while (i<=5)
{
scanf(β€œ%d”,&n);
sum=sum+n;
i=i+1;
}
printf(β€œn Sum of Five numbers :%d”, sum);
avg=sum/5;
printf(β€œn Average of Five numbes :%f”, avg);
getch();
}
20. Write a program to read β€˜n’ values and find the sum and average.
Solution : #include<stdio.h.
main()
{
int n,i=1, sum=0,x;
- 20 -
S. MADHU MOHAN C - Language
float avg;
clrscr();
printf(β€œn Enter values:”);
scanf(β€œ%d”, &x);
printf(β€œn Enter the values one by one :”);
while(i<=x)
{
scanf(β€œ%d”,&n);
sum=sum+n;
i=i+1;
}
avg=sum/x;
printf(β€œn Sum :%d”, num);
printf(β€œ=n Average :%f”, avg);
getch();
}
21. Write a program to read a no, and print it in reverse order.
Solution : #include<stdio.h>
main()
{
int n,m,r=0;
clrscr();
printf(β€œn Enter a number :”);
scanf(β€œ%d”, &n);
while(n>0)
{
m=n%10;
r=(r*10)+m;
n=n/10;
}
printf(β€œn The Reverse of Number :%d”, r);
getch();
}
22. Write a program to read a number and find the sum of the individual digit of
that no.
Solution : #include<stdio.h>
main()
{
int n,m,r=0;
clrscr();
printf(β€œn Enter a number :”);
scanf(β€œ%d”,&n);
while(n.0)
{
m=n%10;
r=r+m;
- 21 -
S. MADHU MOHAN C - Language
n=n/10;
}
printf(β€œn The Sum of individual digit :”,r);
getch();
}
23. Write a program to read a number find out whether the given no is palindrome
or not.
Solution : #include<stdio.h>
main()
{
int n,m,r=0,x;
clrscr();
printf(β€œn Enter a number :”);
scanf(β€œ%d”,&n);
x=n;
while(n>0)
{
m=n%10;
r=(r*10)+m;
n=n/10;
}
if(x= =r)
printf(β€œn The given number is Palindrome”);
else
printf(β€œn The given number is not Palindrome”);
getch();
}
24. Write a program to read a number. Find out whether the given number is
Armstrong or not.
Solution : #include<stdio.h>
main()
{
int n,m,s=0,x;
clrscr();
printf(β€œn Enter a number :”);
scanf(β€œ%d”,&n);
x=n;
while(n>0)
{
m=n%10;
s=s+m*m*m;
n=n/10;
}
if(x= =s)
printf(β€œn The given number is Armstrong”);
else
- 22 -
S. MADHU MOHAN C - Language
printf(β€œn The given number is not Armstrong”);
getch();
}
25. Write a program to read a number. Find out whether the given number is
perfect or not.
Solution : #include<stdio.h>
main()
{
int n,m,s=0,p=1;
clrscr();
printf(β€œn Enter a number :”);
scanf(β€œ%d”,&n);
while(n>0)
{
m=n%10;
s=s+m;
p=p*m;
n=n/10;
}
if(s= =p)
printf(β€œn The given number is Perfect”);
else
printf(β€œn The given number is not Perfect”);
getch();
}
26. Write a program to evaluate ex
value up to 5 significant digits.
Solution : #include<stdio.h>
#include<math.h>
main()
{
int x,n;
float s,t;
clrscr();
printf(β€œn Enter the values of x :”);
scanf(β€œ%d”, &x);
t=1;
s=1;
n=0;
while(t>0.00001)
{
n=n+1;
t=(t*x)/n;
s=s+t;
}
printf(β€œn ex
value up to 5 significant digits :%f”,s);
getch();
- 23 -
S. MADHU MOHAN C - Language
}
27. Write a program to evaluate the sin x value up to 5 digits.
Solution : #include<stdio.h>
#include<math.h>
main()
{
float x,n,s,t;
clrscr();
printf(β€œn Enter the value of x:”);
scanf(β€œ%f”,&x);
x=(x*3.1416/180);
t=x;
s=x;
n=1;
while (fabs(t)>0.00001)
{
n=n+2;
t=(-t)*x*x/(n*(n-1));
s=s+t;
}
printf(β€œn Sin x value :%f”,s);
getch();
}
28. Write a program to evaluate cos x value up to 5 significant digits.
Solution : #include<stdio.h>
#include<math.h>
main()
{
float x,n,s,t;
clrscr();
printf(β€œn Enter the value of x:”);
scanf(β€œ%f”,&x);
x=(x*3.1416/180);
t=1;
s=1;
n=0;
while (fabs(t)>0.00001)
{
n=n+2;
t=(-t)*x*x/(n*(n-1));
s=s+t;
}
printf(β€œn Cos x value :%f”,s);
getch();
}
- 24 -
S. MADHU MOHAN C - Language
Do … while statement :
Do-while is a repeated conditional control structure in β€˜C’ language. The syntax is
do
{
Statement -1;
Statement -2;
----------
}
While (condition)
Here in the do-while structure first the statements will be executed and then test
the condition. If it is true the same statements will be executed. After executing the
statements. Once again it will test the condition and if it is true the same statements will
be executed repeatedly and so on. This process will be continued as long as the condition
is true. When ever the condition becomes false, it comes outside the loop and execute the
remaining statements in the sequential order. The main draw back in the do-while
structure is the statements will be executed at least once even though the condition is
initially false.
29. Write a program to display the natural numbers up to 20. using do while.
Solution : #include<stdio.h>
main()
{
int n=1;
clrscr();
printf(β€œn The Natural numbers are :”);
do
{
printf(β€œ%d”,n);
n=n+1;
}
while (n<=20)
getch();
}
30. Write a program to read a number, find the factorial value.
Solution : #include<stdio.h>
main()
{
int n,f=1;
clrscr();
printf(β€œn Enter n value :”);
scanf(β€œ%d”,&n);
do
{
f=f*n;
n=n-1;
}
- 25 -
S. MADHU MOHAN C - Language
while (n>0)
printf(β€œn Factorial value :”,f);
getch();
}
31. Write a program to display the Fibonacci series of numbers.
Solution : #include<stdio.h>
main()
{
int a,b,c;
clrscr();
printf(β€œn The Fibonacci numbers :”);
a=1;b=1;
do
{
a=b;
b=c;
c=a+b;
printf(β€œ%d”, c); printf(β€œ%d”,a);
} printf(β€œ%d”,b);
While (c<100) c=a+b;
getch(); printf(β€œ%d”,c);
}
32. Write a program to display the multiplication table of a given number 10.
Solution : #include,stdio.h>
main()
{
int n,m,r;
clrscr();
printf(β€œn The Multiplication table :”);
n=10;
m=1;
do
{
r=n*m;
printf(β€œn %d X %d = %d”,n,m,r);
m=m+1;
}
While (m<=10)
getch();
}
33. Write a program to display the multiplication table of given any number.
Solution : #include,stdio.h>
main()
{
int n,m,r;
- 26 -
S. MADHU MOHAN C - Language
clrscr();
printf(β€œn Enter any number :”);
scanf(β€œ%d”,&n);
m=1;
do
{
r=n*m;
printf(β€œn %d X %d = %d”,n,m,r);
m=m+1; getch();
} }
While (m<=10)
34. Write a program to read n numbers and find sum and average. (or)
Write a program to find the average of a list of n numbers.
Solution : #include<stdio.h>
main()
{
int n,i=1,sum=0,x;
float avg;
clrscr();
printf(β€œn Enter the values :”);
scanf(β€œ%dn”,&x);
printf(β€œn Enter the values one by one :”);
do
{
scanf(β€œ%dn”,&n);
sum=sum+n;
i=i+1;
}
while (i<=x)
avg=sum/x;
printf(β€œn Sum :%d”, sum);
printf(β€œn Average :%d”, avg);
getch();
}
35. Write a program to display the multiplication table in the below format.
Solution : #inlcude<stdio.h>
#define rowmax 10
#define col max 10
main()
{
int y,row,col;
clrscr();
printf(β€œn Multiplication tables are :”);
row=1;
do
{
Col=1;
do
- 27 -
S. MADHU MOHAN C - Language
{
y=row*col;
printf(β€œ%4d”,y);
col=col+1;
}
while (col<=clomax)
row=row+1;
printf(β€œn”);
}
While (row<=rowmax)
getch(); }
For …. Loop :
For – loop is an entry-controlled loop and it is a one of the most consized loop in
β€˜C’ language. The syntax is
For(initialization; test condition; incrementation or decrementation)
{
Statement – 1;
Statement – 2;
------------
------------
}
Example : for(i=1;i<=10;i++)
{
Statement block;
}
Here the initialization of the controlled variable(i) is done first by using the
assignment statement such as i=1,i>5 etc. Here the variable β€œi” is called a loop control
variable. The value of the control variable is tested using the test condition is true, the
statement block will be executed. After executing the statement block, the control is
transferred back to the β€œfor” statement and the value of β€œi” will be increment by β€œ1”. And
test the condition again. If it is true, the same statements will be executed repeatedly and
so on. This process will be continued as long as the condition is ture. Whenever the
condition becomes false, it comes outside the loop and execute the remaining statements
in a sequential order.
36. Write a program to display the natural numbers up to 20.
Solution : #include<stdio.h>
main()
{
int i;
clrscr();
printf(β€œn The Natural numbers :”);
for(i=1;i<=20;i++)
printf(β€œ%d”,i);
getch();
}
- 28 -
S. MADHU MOHAN C - Language
37. Write a program to display the numbers from 20 to 1.
Solution : #include<stdio.h>
main()
{
int i;
clrscr();
printf(β€œn The numbers are :”);
for(i=20;i>=1;i--)
printf(β€œ%d”,i);
getch(); }
38. Write a program to find the sum and average of first 100 numbers.
Solution : #include<stdio.h>
main()
{
int n,sum=0;
float avg;
clrscr();
for(n=1;n<=100;n++)
sum=sum+n;
avg=sum/100;
printf(β€œn Sum of 100 numbers :”,sum);
printf(β€œn Average of 100 numbers :”, avg);
getch();
}
39. Write a program to read a number and find the factorial value.
Solution : #inlcude<stdio.h>
main()
{
int n,f=1,x=1;
clrscr();
printf(β€œn Enter the Number :”);
scanf(β€œ%d”,&n);
for(;n>f;n--)
x=x*n;
printf(β€œn The Factorial Value :%d”,x);
getch();
}
40. Write a program to find the average of a list of n numbers.
Solution : #include<stdio.h>
main()
{
int n,I,sum=0,x;
float avg;
clrscr();
- 29 -
S. MADHU MOHAN C - Language
printf(β€œn Give the values :”);
scanf(β€œ%d”, &x);
printf(β€œn Enter the values one by one :”);
for(i=1;i<=x;i++)
{
scanf(β€œ%d”, &n);
sum=sum+n;
}
avg=sum/n;
printf(β€œn Sum of :%d”,sum);
printf(β€œn Average of :%f”,avg);
getch(); }
41. Write a program to display the out in the below format.
Solution : #include<stdio.h>
main()
{
int i,j,k;
clrscr();
printf(β€œn The output is :”); output :
for(i=1;i<=5;i++) 1 1 1 1 1
{ 1 1 1 1
for(k=1;k<i;k++) 1 1 1
printf(β€œ ”); 1 1
for(j=5;j>=i,j--) 1
printf(β€œ|”);
printf(β€œn”);
}
getch();
}
42. Write a program to display the out in the below format.
Solution : #include<stdio.h>
main()
{
int i,j,;
clrscr();
printf(β€œn The output is :”); output :
for(i=1;i<=5;i++) 1
{ 1 2
for(j=1;j<=i;j++) 1 2 3
printf(β€œ%4d”,i); 1 2 3 4
printf(β€œn”); 1 2 3 4 5
}
getch();
}
43. Write a program to display the out in the below format.
Solution : include<stdio.h>
- 30 -
S. MADHU MOHAN C - Language
main()
{
int i,j;
clrscr();
printf(β€œn The output is :”); output :
for(i=1;i<=5;i++) 1
{ 2 2
for(j=1;j<=i;j++) 3 3 3
printf(β€œ%4d”,i); 4 4 4 4
printf(β€œn”); 5 5 5 5 5
}
getch(); }
44. Write a program to display the out in the below format.
Solution : include<stdio.h>
main()
{
int i,j;
clrscr();
printf(β€œn The output is :”); output :
for(i=1;i<=5;i++) 1
{ 2 1 2
for(j=1;j<=5-i;j++) 3 2 1 2 3
printf(β€œ ”); 4 3 2 1 2 3 4
for(j=1;j>=1;j--) 5 4 3 2 1 2 3 4 5
printf(β€œ%4d”,j);
for(j=2;j<=I;j++)
printf(β€œ%4d”,j);
printf(β€œn”);
}
getch();
}
45. Write a program to display the out in the below format.
Solution : include<stdio.h>
main()
{
int i,j;
clrscr();
printf(β€œn The output is :”); output :
for(i=1;i<=5;i++) 1
{ 1 2 1
for(j=1;j<=5-i;j++) 1 2 3 2 1
printf(β€œ ”); 1 2 3 4 3 2 1
for(j=1;j<=1;j++) 1 2 3 4 5 4 3 2 1
printf(β€œ%4d”,j);
for(j=i=1;j>=I;j--)
printf(β€œ%4d”,j);
printf(β€œn”);
- 31 -
S. MADHU MOHAN C - Language
}
getch();
}
46. Write a program to evaluate the roots of a quadratic equation in all possible
conditions.
Solution : #include<stdio.h>
#include<math.h.
main()
{
int a,b,c;
float r1,r2,disc;
clrscr();
printf(β€œn Enter the quadratic equations a,b,c :”);
scanf(β€œ%d%d%d”,&a,&b,&c);
disc=(b*b)-4*a*c;
if(disc= = 0)
{
printf(β€œn Roots, r1 and r2 are real and equation :”);
r1=-b/(2*a);
r2=-b/2*a;
printf(β€œn The Roots are %f and %f”,r1,r2);
}
else
if(disc>0)
{
printf(β€œn The Roots are real and distinct :”);
r1=(-b+sqrt(disc))/ (2*a);
r2=(-b-sqrt(disc))/ (2*a);
printf(β€œn The Roots are %f and %f”,r1,r2);
}
else
{
printf(β€œn The Roots are imaginary”);
r1=-b/ (2*a);
r2=(sqrt(-disc))/ (2*a);
printf(β€œn The Roots are : %f amd %f”, r1,r2);
}
getch();
}
Switch statement :
If a program is having no. of alternatives, the program becomes difficult to read
and follows. Some times it may confuse even the person who designed it. The β€˜C’
language is having built in multi-way decision statement known as β€˜switch’ statement.
The syntax is.
switch(expression)
- 32 -
S. MADHU MOHAN C - Language
{
case value 1: block;
break;
case value 2: block;
break;
case value 3: block;
break;
-------
-------
default :default block;
break;
}
Here the switch statement test the value of a given expression or a variable against
a list of case values. If the match is found, the block of statements will be executed. Here
the expression is either an integer or character expression. The value 1, value 2,…. Are
constants also known as case labels.
Each of these case labels should be unique within a switch statement. Here the
block 1, block2, …… are the set of statements. There is no need to put the brackets
around the blocks. Note that the case labels are end with a colon (:). The break statement
to the end of the each block specifies, the end of a particular case and exit from the loop.
The default is an optional case. The value of the expression does not match with any case
values, the default block is expressed.
47. Write a program to read any two values and find the sum, product, difference
and division of that two numbers of user choice, using switch.
Solution : #include<stdio.h>
main()
{
int a,b,c,p,choice;
clrscr();
printf(β€œn Enter any two values :”);
scanf(β€œ%d%d”,&a,&b);
printf(β€œn Enter your choice : 1. Sum 2. Difference
3. Product 4. Division 5. Exit”);
scanf(β€œ%d’,&choice);
switch(choice)
{
case 1 : c=a+b;
printf(β€œn Sum :%d”,c);
break;
case 2 : c=a-b;
printf(β€œn Difference :%d”,c);
break;
case 3 : c=a*b;
printf(β€œn Product :%d”,c);
break;
- 33 -
S. MADHU MOHAN C - Language
case 4 : c=a/b;
printf(β€œn Division :%d”,c);
break;
case 51 : printf(β€œn The program is ended”);
break;
}
getch();
}
48. Write a program to read the radius of a circle and find the area and
circumference using switch case.
Solution : #include<stdio.h>
main()
{
int r,choice;
float a,c;
clrscr();
printf(β€œn Enter Radus :”);
scanf(β€œ%d”,&r);
printf(β€œn Enter choice … 1. Area 2. Circumference 3. Exit”):
scanf(β€œ%d”,&choice);
switch(choice)
{
case 1 : a=3.14*r*r;
printf(β€œn Area is :%f”,a);
break
case 2 : a=3.14*2*r;
printf(β€œn Circumference is :%f”,a);
break
case 3 : printf(β€œn The program is ended”);
}
getch()
}
49. Write a program to evaluate roots of a quadratic equation in all possible
condition by using switch statement.
Solution : #include<stdio.h>
#include<math.h.
main()
{
int a,b,c,choice;
float r1,r2,disc;
clrscr();
printf(β€œn Enter a,b,c values :”);
scanf(β€œ%d%d%d”,&a,&b,&c);
disc=(b*b)-(4*a*c);
if(disc= =0)
- 34 -
S. MADHU MOHAN C - Language
choice=1;
else
if(disc>0)
choice=2;
else
choice=3;
switch(choice)
{
case 1 : r1=-b/2*a;
r2=-b/2*a;
printf(β€œn The Roots are real and equal to”);
printf(β€œn The Roots are %f and %f”,r1,r2);
break;
case 2 : printf(β€œn The Roots are real and distinct”);
r1=-b+sqrt(disc)/2*a;
r2=-b-sqrt(disc)/2*a;
printf(β€œn The Roots are %f and %f”,r1,r2)l
break;
case 3 ; printf(β€œn The Roots are imaginary”);
r1=-b/2*a;
r2=sqrt(-disc)/2*a;
printf(β€œn The Roots are %f and %f”,r1,r2);
break;
}
getch();
}
50. Write a program to a college has 100 students, who came from 4 regions, i.e.
north(code1), south(code2), east(code3), and west(code4). Calculate the total no. of
students came from each region. The input is rno, name, and code.
Solution : #inlcude<stdio.h>
main()
{
int rno,code,n,s,w,e,total;
char name[20];
clrscr();
n=s=w=e=total=0;
while(total<100)
{
Printf(β€œn Enter rno, name and code :”);
scanf(β€œ%d%s%d”,&rno,name,&code);
switch (code)
{
case 1 : n=n+1;
break;
case 2 : s=s+1;
break;
case 3 : e=e+1;
- 35 -
S. MADHU MOHAN C - Language
break;
case 4 : w=w+1;
break;
}
total=total+1;
}
Printf(β€œn Number of students from North :%d”,n);
Printf(β€œn Number of students from South :%d”,s);
Printf(β€œn Number of students from East :%d”,e);
Printf(β€œn Number of students from West :%d”,w);
getch()
}
51. Write a program to calculate the depreciation of any item either a building or a
machinery, using any no. of the three methods, i.e., straight line method, double
declining method and sum of the years digit method.
Solution : #include<stdio.h>
main()
{
int n,year,choice;
float value, depre,t;
clrscr();
printf(β€œn Enter the original value of a component and the no. of years :”);
scanf(β€œ%f%d”,&value,&n);
printf(β€œn Enter your choice 1. SLM 2.. DDM 3. SOYDM 4. EXIT”);
scanf(β€œ%d”,&choice);
switch(choice)
{
case 1 : printf(β€œn Straight Line method :”);
depre=value/n;
for(year=1;year<=n;year++)
{
value=value-depre;
printf(β€œn End of the year :%d”, year);
printf(β€œn Depreciation :%f”, depre);
printf(β€œn Original Value :%f”, value);
}
Break
case 2 : printf(β€œn Double Declining method :”);
for(year=1;year<=n;year++)
{
depre=(2*value)/n;
value=value-depre;
printf(β€œn End of the year :%d”, year);
printf(β€œn Depreciation :%f”, depre);
printf(β€œn Original Value :%f”, value);
}
Break
- 36 -
S. MADHU MOHAN C - Language
case 3 : printf(β€œn Sum of the years digit method :”);
t=value;
for(year=1;year<=n;year++)
{
depre=(n-year+1)*t(n*(n-1))/2;
value=value-depre;
printf(β€œn End of the year :%d”, year);
printf(β€œn Depreciation :%f”, depre);
printf(β€œn Original Value :%f”, value);
}
Break
case 4 : printf(β€œn The program is ended”);
break;
}
getch();
}
ARRAYS
An β€œArray” is nothing but a collect of related elements of same data type stored in
consecutive storage locations and assigned to a single data name. if we want to access a
specific element from an array is that, we have to mention the subscript (location) is that,
we have to mention the subscript.
Single (one) Dimensional Array :
A list of items can be given with one variable name using one subscript. Such type
of variable are called one dimensional array. In other words we can say that the one
dimensional array may consist of different rows and single columns.
Declaration of an Array :
Like other variable the array must be declared before they are used . syntax is
Storage class data type array name[size];
Here the storage class is optional (auto, static…..) the data type indicates the type
of the data that the array will contain such as int, float, char etc., and the array name
indicates the name of the array and the size indicates the maximum no. of elements is
possible to store in the array.
Example : int a[10];
flaot x[5];
Initialization of an Array ;
They can be initialized with different values by using the below syntax. Syntax is
Storage class data type arrayname[size]={ele1, ele2, …….ele n};
Example : int a[3]={5,10,15}
flaot x[3]={1.2,1.3,1.8};
- 37 -
S. MADHU MOHAN C - Language
52. Write a program to store 5 values in an array and display the same values on the
screen.
Solution : #include<stdio.h>
main()
{
int a[5],i;
clrscr();
printf(β€œn Enter any five values :”);
for(i=0;i<5;i++)
scanf(β€œ%d”,&a[i]);
printf(β€œn The above values are :”);
for(i=0;i<5;i++)
printf(β€œ%d”,a[i]);
getch();
}
53. Write a program to store any 5 values in an array, and find the sum, average.
Solution : #include<stdio.h>
main()
{
int a[5],i,sum=0;
float avg;
clrscr();
printf(β€œn Enter any five values :”);
for(i=0;i<5;i++)
{
scanf(β€œ%d”,&a[i]);
sum=sum+a[i]);
}
avg=sum/5;
printf(β€œn Sum is :%d”,sum);
printf(β€œn Average is :%f”avg);
getch();
}
54. Write a program to store n values in an array and find the sum and average.
Solution : #include<stdio.h>
main()
{
int a[50],i,sum=0,n;
float avg;
clrscr();
printf(β€œn Enter the no. of values to be entered :”);
scanf(β€œ%d”, &n);
printf(β€œn Enter the values one by one :”);
for(i=0;i<n;i++)
- 38 -
S. MADHU MOHAN C - Language
{
scanf(β€œ%d”,&a[i]);
sum=sum+a[i]);
}
avg=sum/5;
printf(β€œn Sum is :%d”,sum);
printf(β€œn Average is :%f”avg);
getch();
}
55. Write a program to store 5 values in an array and find the biggest element and
display it.
Solution : #include<stdio.h>
main()
{
int a[5],i,big=0;
clrscr();
printf(β€œn Enter any five values :”);
for(i=0;i<5;i++)
scanf(β€œ%d”,&a[i]);
for(i=0;i<5;i++)
{
if(a[i]>big)
big=a[i];
}
printf(β€œn Biggest value :%d”, big);
getch();
}
56. Write a program t store 5 values in an array, and find the smallest element and
display it.
Solution : #include<stdio.h>
main()
{
int a[5],i,small;
clrscr();
printf(β€œn Enter any five values :”);
for(i=0;i<5;i++)
scanf(β€œ%d”,&a[i]);
for(i=0;i<5;i++)
{
if(a[i]>small)
small=a[i];
}
printf(β€œn Smallest value :%d”, small);
getch();
}
- 39 -
S. MADHU MOHAN C - Language
57. Write a program to 5 values in one array, another 5 values in the second array,
add the two arrays and store the result in third array and display it.
Solution : #include<stdio.h>
main()
{
int a[5],b[5],c[5];
clrscr();
printf(β€œn Enter any five values of first array :”);
for(i=0;i<5;i++)
scanf(β€œ%d”,&a[i]);
printf(β€œn Enter any five values of second array :”);
for(i=0;i<5;i++)
scanf(β€œ%d”,&b[i]);
for(i=0;i<5;i++)
c[i]=a[i]+b[i];
printf(β€œn The sum of :”);
for(i=0;i<5;i++) getch();
printf(β€œ%d”,c[i]); }
58. Write a program to store 5 values in one array, another 5 values in second array,
merge the 2 arrays and store it in the third array display it.
Solution : #include<stdio.h>
main()
{
int a[5],b[5],c[10],i;
clrscr();
printf(β€œn Enter any five values of first array :”);
for(i=0;i<5;i++)
scanf(β€œ%d”,&a[i]);
printf(β€œn Enter any five values of second array :”);
for(i=0;i<5;i++)
scanf(β€œ%d”,&b[i]);
for(i=0;i<5;i++)
{
c[i]=a[i]
c[i+5]=b[i];
}
printf(β€œn The values of third array :”);
for(i=0;i<10;i++)
printf(β€œ%d”,c[i]);
getch();
}
59. Write a program to read n values and sort it an ascending order.
Solution : #include<stdio.h>
main()
{
- 40 -
S. MADHU MOHAN C - Language
int a[20],t,i,j,n;
clrscr();
printf(β€œn Give the no. of values :”);
scanf(β€œ%d”,&n);
printf(β€œn Enter the values one by one :”);
for(i=0;i<n;i++)
scanf(β€œ%d”,&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf(β€œn The values in ascending order :”);
for(i=0;i<n;i++)
printf(β€œ%d”,a[i]);
getch();
}
60. Write a program to read n values and sort it in descending order.
Solution : #include<stdio.h>
main()
{
int a[20],t,i,j,n;
clrscr();
printf(β€œn Give the no. of values :”);
scanf(β€œ%d”,&n);
printf(β€œn Enter the values one by one :”);
for(i=0;i<n;i++)
scanf(β€œ%d”,&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf(β€œn The values in descending order :”);
- 41 -
S. MADHU MOHAN C - Language
for(i=0;i<n;i++)
printf(β€œ%d”,a[i]);
getch();
}
Double (two) Dimensional Array :
So far we have discussed the variables that can store a list of values. If we want to
store a table of values i.e., in the way of rows and columns, we can use the two
dimensional arrays.
The declaration of a two dimensional array is storage class. The syntax is
Data type array name[row size][column size]
Example : int a[3][3];
float x[3][4];
Here the storage class is optional. And the data type specifies the type of the data
and the array name indicates the name of the array and the row size indicates the
maximum number of rows in that array and the column size indicates the maximum
number of columns in that array.
61. Write a program to read a(3x3) matrix and display it in a matrix format.
Solution : #include<stdio.h>
main()
{
int a[3][3],i,j;
clrscr();
printf(β€œn Enter the values of 3x3 matrix :”);
for(i=0;i<3,i++)
{
for(j=0;j<3,j++)
scanf(β€œ%d”,a[i][j]);
}
Printf(β€œn The array in matrix from :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf(β€œ%4d”,a[i][j]);
printf(β€œn”);
}
getch();
}
62. Write a program to read two 3x3 matrices. And them and store the result in to
the third matrix and display it.
Solution : #include<stdio.h>
main()
{
- 42 -
S. MADHU MOHAN C - Language
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf(β€œn Enter elements of A :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf(β€œ%d”,&a[i][j]);
}
printf(β€œn Enter the elements of B :”);
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
scanf(β€œ%d”,&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
}
{
for(j=0;j<3;j++)
printf(β€œ%4d”,c[i][j]);
printf(β€œn”);
}
getch();
}
63. Write a program to read a 3x3 matrix, find the biggest element and display it.
Solution : #include<stdio.h>
main()
{
int a[3][3],big=0,i,j;
clrscr();
printf(β€œn Enter elements of A :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf(β€œ%d”,&a[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]>big)
big=a[i][j];
}
}
printf(β€œn The biggest value is :%d”,big);
- 43 -
S. MADHU MOHAN C - Language
getch();
}
64. Write a program to read a 3x3 matrix and find the smallest element in it.
Solution : #include<stdio.h>
main()
{
int a[3][3],small=0,i,j;
clrscr();
printf(β€œn Enter elements of A :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf(β€œ%d”,&a[i][j]);
}
small=a[0][0];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]>small)
small=a[i][j];
}
}
printf(β€œn The smallest value is :%d”,big);
getch();
}
65. Write a program to read a 3x4 matrix and display the transpose of that matrix.
Solution : #include<stdio.h>
main()
{
int a[3][4],i,j;
clrscr();
printf(β€œn Enter values of A :”);
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
scanf(β€œ%d”,&a[i][j]);
}
printf(β€œn The transpose of the matrix :”);
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
printf(β€œ%4d”,a[i][j]);
printf(β€œn”);
}
getch();
- 44 -
S. MADHU MOHAN C - Language
}
66. Write a program to read a 3x3 matrix and calculate the sum of the diagonal
elements and display it.
Solution : #include<stdio.h>
main()
{
int a[3][3],sum=0,i,j;
clrscr();
printf(β€œn Enter elements of A :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf(β€œ%d”,&a[i][j]);
}
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
if(i= =j)
sum=sum+a[i][j];
}
}
printf(β€œn The sum of diagonal elements :%d”,big);
getch();
}
67. Write a program to read a 3x3 matrix and find the biggest element and display
the positions where ever it is.
Solution : #include<stdio.h>
main()
{
int a[3][3],big=0,i,j;
clrscr();
printf(β€œn Enter the values of matrix :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf(β€œ%d”,&a[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]>big)
big=a[i][j];
}
- 45 -
S. MADHU MOHAN C - Language
}
printf(β€œn The biggest value is :%d”,big);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]= = big)
printf(β€œn Row :%d, Coulmn :%d”, i,j);
}
}
getch();
}
68. Write a program to read a 3x3 matrix and find the smallest element and display
the position where ever it is.
Solution : #include<stdio.h>
main()
{
int a[3][3],small,i,j;
clrscr();
printf(β€œn Enter the values of matrix :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf(β€œ%d”,&a[i][j]);
}
small=a[o][o];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]>small)
small=a[i][j];
}
}
printf(β€œn The smallest value is :%d”,big);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]= = small)
printf(β€œn Row :%d, Column :%d”,i,j);
}
}
getch();
}
- 46 -
S. MADHU MOHAN C - Language
69. Write a program to read a 3x3 matrix and display the position of a given element
if formed. If the element is not there in the array; just display the message β€œthe
given element is not in the array”.
Solution : #include<stdio.h>
main()
{
int a[3][3],p=0,i,j,x;
clrscr();
printf(β€œn Enter the values of matrix :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf(β€œ%d”,&a[i][j]);
}
printf(β€œn Enter a values to find the position :”);
scanf(β€œ%d”, &x);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]= =x)
{
printf(β€œn Row :%d, Column :%d”,i,j);
p=1;
}
}
}
if(p= =0)
printf(β€œn The given number is not in the array”);
getch();
}
70. Write a program to read two matrices and display them and store the result in
3rd
array matrix and display it.
Solution : #include<stdio.h>
main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
clrscr();
printf(β€œn Enter elements of matrix A :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf(β€œ%d”,&a[i][j]);
}
printf(β€œn Enter the elements of matrix B :”);
for(i=0;i<3;i++)
- 47 -
S. MADHU MOHAN C - Language
{
for(j=0;j<3;j++)
scanf(β€œ%d”,&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
}
}
printf(β€œn The product of two matrices :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf(β€œ%4d”,c[i][j]);
printf(β€œn”);
}
getch();
}
71. Write a program to read two different sizes of matrices and if the multiplication
is possible, multiply them and display the result. If not possible display the message
β€œmultiplication is not possible for the above 2 matrices”.
Solution : #include<stdio.h>
main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,p,q,m,n;
clrscr();
printf(β€œn Enter the rows and columns of first matrix :”);
scanf(β€œ%d%d”,&p,&q);
printf(β€œn Enter the rows and columns of second matrix :”);
scanf(β€œ%d%d”, &m,&n);
if(q= =m)
printf(β€œn Enter the value of pxq matrix :”);
{
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf(β€œ%d”,&a[i][j]);
}
printf(β€œn Enter a values of mxn matrix :”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf(β€œ%d”,&b[i][j]);
}
- 48 -
S. MADHU MOHAN C - Language
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]= c[i][j]+a[i][k]*b[k][j];
}
}
printf(β€œn The product of two matrices :”);
for(i=0;i<p;i++)
{
for(j=0;j<n;j++)
printf(β€œ%4d”,c[i][j]);
printf(β€œn”);
}
}
else
printf(β€œn The multiplication is not possible”);
getch();
}
STRING HANDLING FUNCTIONS
β€˜C’ library supports a large number of string handling functions that can be used to
carry out many of the string manipulations. The following are the most commonly used
string handling functions. They are
1. strcat()
2. strcmp()
3. strcpy()
4. strlen()
1. strcat( ) :
This function joins two strings together. The syntax is
strcat (sring1, string2)
Here the string1 and string2 are the character arrays. When the function strcat() is
executed. The string 2 is appended to string 1. It removes the null character at the end of
the string 1 and insert 2 from there itself. The string 2 will remain unchanged.
2. strcmp( ) :
This function compares two strings and identify the arguments and has a value β€˜0’.
If they are equal. If they are not equal, it will take the numeric difference between the
first non-matching characters in the string. The syntax is
strcmp(string1, string2)
Here the string 1 and string 2 are string variables.
- 49 -
S. MADHU MOHAN C - Language
3. strcpy( ) :
This function works almost like a string assignment operator. The syntax is
strcpy (string1, string2)
Here string 1 and string 2 are string variables or string constants. The value of
string 2 is assigned to string 1.
4. strlen( ) :
This function counts and returns the no. of characters in a string. The syntax is
n=strlen(string 1)
Here β€˜n’ is an integer variable which receives the value of the length of a string.
The counting ends as the first null character occurs.
72. Write a program to read an upper case character and convert into lower case
character.
Solution : #include<stdio.h>
#include<string.h>
main()
{
char a;
clrscr();
printf(β€œn Enter an upper case letter :”);
a=getchar(); getch();
putchar(to lower(a)); }
73. Write a program to read a lower case character and convert into upper case
character.
Solution : #include<stdio.h>
#include<string.h>
main()
{
char a;
clrscr();
printf(β€œn Enter an lower case letter :”);
a=getchar(a);
putchar(to upper(a));
getch();
}
74. Write a program to read β€˜n’ strings (names) and sort it in alphabetical order.
Solution : #include<stdio.h>
#include<string.h>
main()
{
char name[30][15],t[15];
int i,j,n;
- 50 -
S. MADHU MOHAN C - Language
clrscr();
printf(β€œn Enter the number of strings :”);
scanf(β€œ%d”,&n);
printf(β€œn Enter the strings one by one :”);
for(i=0;i<n;i++)
scanf(β€œ%s”,name[i])
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j]>0)
{
strcpy(t,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],t);
}
}
}
Printf(β€œn The names in Alphabetical Order :”);
for(i=0;i<n;i++)
printf(β€œ%s”,name[i]);
getch();
}
75. Write a program to read β€˜n’ strings and sort it in alphabetical reverse order.
Solution : #include<stdio.h>
#include<string.h>
main()
{
char name[30][15],t[15];
int i,j,n;
clrscr();
printf(β€œn Enter the number of strings :”);
scanf(β€œ%d”,&n);
printf(β€œn Enter the strings one by one :”);
for(i=0;i<n;i++)
scanf(β€œ%s”,name[i])
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j]<0)
{
strcpy(t,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],t);
}
}
}
Printf(β€œn The names in Alphabetical Reverse Order:”);
- 51 -
S. MADHU MOHAN C - Language
for(i=0;i<n;i++)
printf(β€œ%s”,name[i]);
getch();
}
76. Write a program to read a line of text in upper case and print it in lower case.
Solution : #include<stdio.h>
#include<string.h>
main()
{
char lind[80];
int i=0;
clrscr();
printf(β€œn Enter a text in upper case :”);
scanf(β€œ%[^n]”,line);
printf(β€œn The upper case line convert into lower case :”);
while(line[i]!= β€˜Ο†β€™)
{
printf(β€œ%c”, to lower (line[i]));
i++;
}
getch();
}
77. Write a program to read a line of text in lower case and print it in upper case.
Solution : #include<stdio.h>
#include<string.h>
main()
{
char lind[80];
int i=0;
clrscr();
printf(β€œn Enter a text in upper case :”);
scanf(β€œ%[^n]”,line);
printf(β€œn The lower case line convert into upper case :”);
while(line[i]!= β€˜Ο†β€™)
{
printf(β€œ%c”, to upper (line[i]));
i++;
}
getch();
}
78. Write a program to read a line of text and count the no. of vowels, no. of
consonants, digits, spaces and other characters in that line.
Solution : Solution : #include<stdio.h>
- 52 -
S. MADHU MOHAN C - Language
#include<string.h>
main()
{
char lind[80],c;
int i,vow,con,dig,spa,oth;
clrscr();
printf(β€œn Enter a line of text :”);
scanf(β€œ%[^n]”,line);
i=vow=con=dig=spa=oth=0;
while((c= to lower(line[i]))!= β€˜Ο†β€™)
{
if(c= = β€˜a’||c= = β€˜e’||c= = β€˜i’ ||c= = β€˜o’ || c= = β€˜u’)
vow++;
else
if(c>= β€˜a’ && c<= β€˜z’)
con++;
else
if(c>= β€˜0’ && c<=’9’)
dig++;
else
if(c= = β€˜ ’ || c= = β€˜t’)
spa++;
else
oth++
i++
}
printf(β€œn Total no. of Characters :%d”, i);
printf(β€œn Total no. of Vowels :%d”, vow);
printf(β€œn Total no. of Consonants :%d”, con);
printf(β€œn Total no. of Digits :%d”, dig);
printf(β€œn Total no. of Spaces :%d”, spa);
printf(β€œn Total no. of other Characters :%d”, oth);
getch();
}
FUNCITONS
In computer programming it is a good practice to break down a big module
(program) into small modules. Each module may be developed separately and compiled
separately. In this method it is easy to locate and debug (rectify) the errors. In the β€˜C’
language we can write the functions to avoid the same code (statements) again an again.
A function is a self-contained subprogram which performs some specific well-
defined task. A β€˜C’ program (has only one function) may consist of one or more
functions. If a program has only one function that must be the main() function. In β€˜C’
language here are two types of functions. They are
1. Library functions (or) Built – in functions.
2. User defined functions.
- 53 -
S. MADHU MOHAN C - Language
1. Library Functions :
β€˜C’ has a facility to provide some library functions to the programmer
(user) for doing some operations. For example β€˜C’ has a mathematical function which
is used for finding the square root of a number i.e. β€œsqrt”. Whenever it is required in
our program we can use the library functions.
2. User defined Functions :
If the user is going to define any type of functions for the sake of problem
solving and that type of functions are called user-defined functions.
Advantages :
1. It allows the division of a large program into small programs (modules).
2. Small programs are easy to understand and easy to debug the error.
3. Small programs are easy to understand and easy to debug the errors.
4. Small programs are generally self documented and highly readable.
Function declaration (or) Function Proto type :
One of the most important feature of β€˜C’ is the function declaration. The function
declaration tells the computer, the type of the data returned by the function, the no. of
arguments that the function expect to receive and the order of the arguments. The syntax
data type function name(arg1, arg2, ……..arg n)
Here the data type specifies the type of the data returned by the function and the
function name indicates the name of the function and arg 1, arg 2, ….. are the type of the
arguments (variables).
Function Definition :
A function is defined once in a program and can be called number of times. A
function can receive many values but returns a single value. The syntax is
data type specifier function name (arg 1, arg 2, ……. Arg n)
{
body of the function;
-------
-------
-------
Return (something)
}
Here the datayic specifier specifies the value that the function will return using
β€œreturn” statement. The function name indicates the name of the function and the function
name precedes a set of parenthesis. Within the parenthesis the argument can be specifies.
If the arguments are not there, we can specify the empty parenthesis only.
Return Statement :
- 54 -
S. MADHU MOHAN C - Language
We have two types of usages with a return statement. They are
1. It causes an immediate exit from a function.
2. It can be used to return a value. The syntax is
Return; or return (value); or return (expression);
Sometime a function does not return any value. In such cases its data type
becomes β€œvoid”. The syntax
void function name (………);
79. Write a program to display your address using functions.
Solution : #include<stdio.h>
main()
{
void display(); /* function declaration */
clrscr();
display (); /* function calling */
}
void display()
{
printf(β€œn Name : S.MADHU MOHAN”);
printf(β€œn Qualification : MBA., PGDSE & DHE”);
printf(β€œn Village : NANDIKOTKUR”);
}
80. Write a program to read any 2 values and find the biggest one using functions.
Solution : #include<stdio.h>
main()
{
int x,y;
int big();
clrscr();
printf(β€œn Enter any two numbers :”);
scanf(β€œ%d”, &x);
scanf(β€œ%d”, &y);
printf(β€œn The biggest value :%d”, big(x,y));
}
int big(int a, int b)
{
if(a>b)
return (a);
else
return (b);
}
81. Write a program to read 3 values and find the biggest one using functions.
Solution : #include<stdio.h>
- 55 -
S. MADHU MOHAN C - Language
main()
{
int x,y,z,p;
int big();
clrscr();
printf(β€œn Enter any three numbers :”);
scanf(β€œ%d”, &x);
scanf(β€œ%d”, &y);
scanf(β€œ%d”, &z);
p=big(x,y);
printf(β€œn The biggest value :%d”, big(p,z));
}
int big(int a, int b)
{
if(a>b)
return (a);
else
return (b);
}
82. Write a program to read a number and find the factorial value using functions.
Solution : #include<stdio.h>
main()
{
int n;
int fact();
clrscr();
printf(β€œn Enter n value :”);
scanf(β€œ%d”,&n);
printf(β€œn The Factorial value :%d”,fact(n));
}
int fact(int n)
{
int I,p=1;
if(n>1)
{
for(i=2;i<=n;i++)
p=p*i;
}
return(p);
}
83. Write a program to read the radius of a circle and calculate area and
circumference using functions.
Solution : #include<stdio.h>
main()
{
float r;
- 56 -
S. MADHU MOHAN C - Language
void ac(float r);
clrscr();
printf(β€œn Enter the Radius :”);
scanf(β€œ%f”,&r);
ac(r);
}
void ac(float r)
{
float area, cir;
area=3.14*r*r;
cir=2*3.14*r;
printf(β€œn Area :%f”, area);
printf(β€œn Circumference :%f”, cir);
}
84. Write a program to read principle, time, rate. And calculate interest, display it.
Solution : #include<stdio.h>
main()
{
float p,t,r,s;
float si(); or float si(float p, float t, float r);
clrscr();
printf(β€œn Enter principle, time and rate :”);
scanf(β€œ%f%f%f”,&p,&t,&r);
s=si9p,t,r);
printf(β€œn Simple Interest :%f”, s);
}
float si(float p, float t, float r) a=(p*t*r)/100;
{ return (a);
s=float a; }
85. Write a program to calculate the Binomial coeff value.
Solution : #include<stdio.h>
main()
{
int n,r,b,p;
int bino();
clrscr();
printf(β€œn Enter the values of n and r:”);
scanf(β€œ%d%d”,&n,&r);
p=n-r;
b=bino(n)/(bino cr)*bino(p));
printf(β€œn Binomial Coeff value :%d”, b);
}
int bino (int n)
{
int i,x=1;
if(n>1)
{
- 57 -
S. MADHU MOHAN C - Language
for(i=2;i<=n;i++)
x=x+i;
}
return (x);
}
86. Write a program to read any two values and swap them using functions.
Solution : #incldue<stdio.h>
main()
{
int a,b;
int swap();
clrscr();
printf(β€œEnter a and b values :”);
scanf(β€œ%d%d”,&a,&b);
swap (a,b);
{
int swap(int a, int b)
{
int t;
t=a;
a=b;
b=t;
printf(β€œn A value after swap :%d”, a);
printf(β€œn B value after swap :%d”, b);
}
87. Write a program to evaluate ex
using function.
Solution : #include<stdio.h>
main()
{
float s;
float expp(float s);
clrscr();
printf(β€œn Enter S value :”);
scanf(β€œ%f”, &s);
printf(β€œn The Expression Value :%f”,expp(s));
}
float expp(float x)
{
int n;
float t;
t=1;
s=1;
n=0;
while(t>0.00001)
{
n=n+1;
- 58 -
S. MADHU MOHAN C - Language
t=t*x/n;
s=s+t;
}
return s;
}
88. Write a program to evaluate the sin x value up to 4 significant digits using
function.
Solution : #inlcude<stdio.h>
main()
{
float s;
float sinn(float s);
clrscr();
printf(β€œn Enter S value :”);
scanf(β€œ%f”,&s);
printf(β€œn The sin x value :%f”,sinn(x));
}
float sinn(float x)
{
float s,t,n;
x=(x*3.14/180);
t=x;
s=x;
n=1;
while (abs(t)>0.00001)
{
n=n+2;
t=(-t)*x*x/(n*(n-1));
s=s+t; return s;
} }
89. Write a program to evaluate cos x value using functions.
Solution : #inlcude<stdio.h>
main()
{
float s;
float coss(float s);
clrscr();
printf(β€œn Enter S value :”);
scanf(β€œ%f”,&s);
printf(β€œn The cos x value :%f”,sinn(x));
}
float coss(float x)
{
float s,t,n;
x=x*3.14/180;
t=1;
s=1;
- 59 -
S. MADHU MOHAN C - Language
n=0;
while (abs(t)>0.00001)
{
n=n+2;
t=(-t)*x*x/(n*(n-1));
s=s+t;
}
return s;
}
Functions with Arrays :
90. Write a program to read n values and find the biggest one using functions.
Solution : #include<stdio.h>
main()
{
int a[100],n,i;
int big(int a[], int n);
clrscr();
printf(β€œn Enter no. of values :”);
scanf(β€œ%d”,&n);
printf(β€œn The biggest value :%d”, big(a,n));
}
int big(int a[], int n)
{
int b=0,i;
for(i=0;i<n;i++)
{
if(a[i]>b)
b=a[i];
}
return b; }
91. Write a program to read n values and find the smallest one using functions.
Solution : #include<stdio.h>
main()
{
int a[100],n,i;
int small(int a[], int n);
clrscr();
printf(β€œn Enter no. of values :”);
scanf(β€œ%d”,&n);
(printf(β€œn Enter values value :”);)
for(i=0;i<n;i++)
scanf(β€œ%d”, &a[i]);
printf(β€œn The smallest value :%d”, small(a,n));
}
int small(int a[], int n)
{
- 60 -
S. MADHU MOHAN C - Language
int s=a[0],i;
for(i=0;i<n;i++)
{
if(a[i]<s)
s=a[i];
}
return s;
}
92. Write a program to read n values and sort it in ascending order using functions.
Solution : #include<stdio.h>
main()
{
int a[100],i,n;
int sort(inta[], int n);
clrscr();
printf(β€œn Enter the no. of values :”);
scanf(β€œ%d”,&n);
printf(β€œn Enter values one by one :”);
for(i=0;i<n;i++)
scanf(β€œ%d”,&a[i]);
sort(a,n);
printf(β€œn The values in Ascending Order :”);
fir(i=0;i<n;i++)
printf(β€œ%d”,a[i]);
}
int sort (int a[], int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
Return (sort);
}
93. Write a program to read n values sort it in descending order using functions.
Solution : #include<stdio.h>
main()
{
- 61 -
S. MADHU MOHAN C - Language
int a[100],i,n;
int sort(inta[], int n);
clrscr();
printf(β€œn Enter the no. of values :”);
scanf(β€œ%d”,&n);
printf(β€œn Enter values one by one :”);
for(i=0;i<n;i++)
scanf(β€œ%d”,&a[i]);
sort(a,n);
printf(β€œn The values in Descending Order :”);
fir(i=0;i<n;i++)
printf(β€œ%d”,a[i]);
}
int sort (int a[], int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
Return (sort);
}
94. Write a program to read a names and sort it in alphabetical order using
functions.
Solution : #include<stdio.h>
#include<string.h>
main()
{
char name[30][10];
int i,n;
char sort (char name[][10], int n);
clrscr();
printf(β€œn Enter the no. of names :”);
scanf(β€œ%d”,&n);
printf(β€œn Enter names one by one :”);
for(i=0;i<n;i++)
scanf(β€œ%s”,name[i]);
sort(name.n);
printf(β€œn The sorted array names :”);
- 62 -
S. MADHU MOHAN C - Language
fir(i=0;i<n;i++)
printf(β€œ%s”,name[i]);
}
char sort (char name[][10], int n)
{
int i,j;
char t[10];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy (t,name[i]);
strcpy(name[i], name[j]);
strcpy(name[j],t);
}
}
}
Return (sort);
}
95. Write a program to read n strings and sort it in alphabetical reverse order.
Solution : #include<stdio.h>
#include<string.h>
main()
{
char name[30][10];
int i,n;
char sort (char name[][10], int n);
clrscr();
printf(β€œn Enter the no. of names :”);
scanf(β€œ%d”,&n);
printf(β€œn Enter names one by one :”);
for(i=0;i<n;i++)
scanf(β€œ%s”,name[i]);
sort(name.n);
printf(β€œn The sorted array name :”);
fir(i=0;i<n;i++)
printf(β€œ%s”,name[i]);
}
char sort (char name[][10], int n)
{
int i,j;
char t[10];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
- 63 -
S. MADHU MOHAN C - Language
if(strcmp(name[i],name[j])<0)
{
strcpy (t,name[i]);
strcpy(name[i], name[j]);
strcpy(name[j],t);
}
}
}
Return (sort);
}
96. Write a program to read n values and find the variance and standard deviation.
Solution : #include<stdio.h>
#inlcude<math.h>
main()
{
int n,i,a[50];
float sd(int a[],int n);
clrscr();
printf(β€œn Enter the no. of values :”);
scanf(β€œ%d”,&n);
printf(β€œn Enter the values one by one :”);
for(i=0;i<n;i++)
scanf(β€œ%d”,&aa[i]);
printf(β€œn The standard deviation :%f”,sd(a,n));
}
float sd(int a[], int n)
{
float vari, avg, dev, sumsq=0;
int i,0;
float mean(int a[], int n);
avg=mean(a,n);
for(i=0;i<n;i++)
{
dev=avg-a[i]
sumsq=sumsq+dev*dev;
}
vari=(sumsq)/n;
return (vari);
}
float mean(int a[], int n)
{
int i, sum=0;
for(i=0;i<n;i++)
sum=sum+a[i];
return (sum n);
}
RECCURSION :
- 64 -
S. MADHU MOHAN C - Language
Recursion is a technique where a function calls by it self until a specific condition
has been met.
97. Write a program to read a number and find the factorial value using recursion.
Solution : #include<stdio.h>
main()
{
int n;
int fact(int n);
clrscr();
printf(β€œn Enter n value :”);
scanf(β€œ%d”, &n);
printf(β€œn The Factorial value :%d”, fact(n));
}
int fact(int n)
{
if(n<=1)
return (1);
else
return(n*fact(n-1));
}
98. Write a program to evaluate pronominal coeff value using recursion.
Solution : #include<stdio.h>
main()
{
int n,r,b,p;
int bino();
clrscr();
printf(β€œn Enter the values of n and r :”);
scanf(β€œ%d%d”, &n,&r);
p=n-r;
b=bino(n)/bino(r)*bino(p);
printf(β€œn The binomial coeff value :%d”, b);
}
int bino(int n)
{
if(n<=1)
return (1);
else
return(n*bino(n-1));
}
99. Write a program to display the Fibonacci series of numbers on basis of user’s
choice using recursion.
Solution : #include<stdio.h>
main()
- 65 -
S. MADHU MOHAN C - Language
{
int n,f1,f2;
int fibo(int n, int f1, int f2);
clrscr();
printf(β€œn Enter the no. of Fibonacci to display :”);
scanf(β€œ%d”, &n);
fibo(n,o,1);
}
int fibo(int n, int f1, int f2)
{
int t;
if(n>=1)
{
printf(β€œ%d”,f1);
t=f2;
f2=f1+f2
f1=t;
fibo(n-1,f1,f2)
}
}
100. Write a program to evaluate the well known game the towers of Hanoi.
Solution : #include<stdio.h>
main()
{
int n,
char sour= β€˜l’, ini= β€˜c’, dest= β€˜r’;
void Hanoi();
clrscr();
printf(β€œn Enter the no. of dies :”);
scanf(β€œ%d”,&n);
hanoi(n,sour,ini,dest);
}
void main(int n, char sour, char ini, char dest)
{
if(n>0)
{
hanoi(n-1, ini, dest, sour);
printf(β€œn Moves of the disk from %c to %d”, sour, dest);
hanoi(n-1, ini, sour,dest);
}
Return;
}
Types of user defined functions :
There are three types of user defined functions in β€˜C’. They are
1. Functions with no arguments and no return values.
2. Functions with arguments and no return values.
- 66 -
S. MADHU MOHAN C - Language
3. Functions with arguments and with return values.
1. Functions with no arguments and no return values ;
When we are calling a function, if we are not sending any kind of arguments to the
called function and also the called function is not returning any arguments to the calling
function.
101. Write a program to display address.
Solution : #include<stdio.h>
main()
{
void display();
clrscr();
display();
}
void display()
{
printf(β€œn Name : S. MADHU MOHAN”);
printf(β€œn Address : NANDIKOTKUR”);
}
2. Functions with arguments and no return values :
When we are calling a function, at the time of calling we are sending some
arguments to the called function. After the execution of the called function, it won’t
return any values to the calling function.
102. Write a program to read radium and find area, circumference.
Solution : #include<stdio.h>
main()
{
float r;
void ac();
clrscr();
printf(β€œn Enter Radius :”);
scanf(β€œ%f”,&r);
ac(r);
}
void ac(float r)
{
float are, cir;
area=3.1416*r*r;
cir=2*3.1416*r
printf(β€œn Area :%f”, area);
printf(β€œn Circumference :%f”,cir);
}
4. Functions with arguments and with return values :
- 67 -
S. MADHU MOHAN C - Language
When we are calling a function and at the time of calling we are sending some
arguments to the calling function. After the execution of the calling function, it returns a
value to the calling function. In this method the data communication is in both ways.
103. Write a program to read any two values, find the biggest value.
Solution : #include<stdio.h.
main()
{
int x.,y;
int big;
clrscr();
printf(β€œn Enter any two values :”);
scanf(β€œ%d%d”, &x,&y);
printf(β€œn The biggest value :%d”, big(x,y));
}
int big(int a, int b)
{
if(a>b)
return (a);
else
return (b);
}
STORAGE CLASSES
They are four types of storage classes in β€˜C’ language. They are
1. Automatic Storage Class (auto)
2. Static Storage Class (static)
3. External Storage Class (extern)
4. Register Storage Class (register)
Automatic Storage Class (auto) :
The variable which we have define by using β€œauto” storage class, these variables
are stored in the computers memory. The default value of an auto variable is
unpredictable. Its scope is local to the block in which they are defined. The life the auto
variable is within the block till the control remains. If we wont specify any storage class,
by default the computer will assume as auto only.
Example : #include<stdio.h>
main()
{
void increment();
clrscr();
increment();
- 68 -
S. MADHU MOHAN C - Language
increment();
increment();
}
void increment();
{
auto int i=1;
printf(β€œ%d”,i);
i=i+1;
}
2. Static Storage Class (static) :
The variable which we have defined by using static storage class, it is stored in the
computer memory. The default value of a static variable is assumed to be zero. The scope
is local to the block in which the variable is defined. The life of a static variable is global,
i.e. the values of a variable persist between different functions calls.
Example : #include<stdio.h>
main()
{
void increment();
clrscr();
increment();
increment();
increment();
}
void increment();
{
auto int i=1;
printf(β€œ%d”,i);
i=i+1;
}
The difference between auto and static is that, the static variable do not disappear
when the function is no longer active and their value persists. If the control comes back to
the same function again the static variables have the values, they had last time around.
In case of an auto variable, each time the function is called, the value will be
reinitialized to the variable. If you call the function for 10 times, the same value will be
initialized time.
3. External Storage Class (extern) :
The variable which we define by using external storage class, that variables are
stored in computer memory. The default value of an external variable is zero and scope is
global. The life of the external variable is as long as the program is executing their values
will be retained the program.
Syntax : #include<stdio.h> #incldue<stdio.h>
main() main()
- 69 -
S. MADHU MOHAN C - Language
{ {
state -1; (or) extern int I;
state -2; state -1;
------- -------
------- -------
} }
4. Register Storage Class (register) :
The variable which we define by using register storage class, the values of the
variables will be stored in cpu register. The default value of the variable is unpredictable.
It’s scope is local to the block in which the variables are defined. Its life is till the control
remains within the block.
A value is stored in the cpu registers can always be accessed faster than the one
which is stored in the computers memory. However the computer is consisting of limited
no. of cpu registers. So we cannot declare each and every variable as register data type.
Syntax : #include<stdio.h>
main()
{
register int i,j,k;
state -1;
state -2;
----------
----------
}
POINTERS
A pointer is a variable that points the address of the another variable.
Example : int i=3;
This declaration tells to the β€˜C’ compiler to
1. Reverse the space in memory to store the value.
2. Associate the name i with this memory location.
3. Store the value 3 at this location.
We may represent the location in the memory by the following memory map.
i = Associate variable
[3] = Actual value
Declaring a pointer variable :
To declare and refer a pointer type variable, β€˜C’ provides two special unary
operators β€œ&” and β€œ*”.
A pointer variable can be declared in the same way as the other variable, but an
β€œ*” (asterisk) symbol should proceed the variable name. the syntax is
- 70 -
S. MADHU MOHAN C - Language
Syntax : data type * variable;
Example : int *a; flaot *x,*y; etc
Here is a pointer variable pointing to a integer type address.
The address Operators (&) :
The address of any variable can be got proceeds the β€œ&” with a variable name.
Example : int *a;
int x,n;
a=&n;
Here β€˜n’ is a variable and &n is the address in which the value of β€˜n’ stored.
Pointer Multiplications :
The no. of bytes occupied by a particular data type is called the scale factor. For
that we can use β€œsize of” operator in β€˜C’ language.
Example : If β€˜n’ is an integer type variable. The no. of bytes occupied by β€˜n’ can be
done using size of (n); The length of variable data types are
1. char ….. 1 byte
2. int ….. 2 bytes
3. float ….. 4 bytes
4. long int ….. 4 bytes
5. double ….. 8 bytes etc.
We can use the arithmetic operators like β€˜+’ and β€˜-’, relational operators β€˜<’, β€˜>’,
β€˜= =’, β€˜!=’ and unary operators like β€˜++’, β€˜- -’ in the pointers.
Example : p1+p2, p1-p2,p1= = p2,p1!=p2,p1<p2 are all valid expressions.
P1*p2, p1/p2 are invalid expressions.
Consider a pointer expression.
P1=p1+1; where p1 is an integer pointer with the initial address 6400. After the
execution of the statement (p1=p1+1), the value of p1 will not be 6401, it will be 6402.
Because the scale factor of an integer is 2 bytes.
104 . Write a program to read a variable from the keyboard, display the address.
Solution : #include<stdio.h>
main()
{
int a;
clrscr();
printf(β€œn Enter a number :”);
scanf(β€œ%d”.&a);
printf(β€œn The address of the a is :%d”, &a);
getch();
}
- 71 -
S. MADHU MOHAN C - Language
105. Write a program to accept a number and store the address into another
variable and display it.
Solution : #include<stdio.h>
main()
{
int a;
int *p;
clrscr();
printf(β€œn Enter a number :”);
scanf(β€œ%d”,&a);
printf(β€œn The address of the a is :%u”, &p);
printf(β€œn The original value :%d”,*p);
printf(β€œn A value :%d”, *(&a));
getch();
}
106. Write a program to read any 2 values and swap them using pointers.
Solution : #include<stdio.h>
main()
{
int a,b;
void swap();
clrscr();
printf(β€œn Enter a,b values :”);
scanf(β€œ%d%d”,&a,&b);
swap(&a,&b);
printf(β€œn After swapping in main program :%d”, a);
printf(β€œn After swapping in main program :%d”, b);
}
void swap(int *x, int *y)
{
int t; t=*x; *x=*y; *y=t;
printf(β€œn In the function a :%d”, *x);
printf(β€œn In the function b”%d”, *y);
}
107. Write a program to read any values and find the biggest one using pointers.
Solution : #include<stdio.h>
main()
{
int a,b;
int big();
clrscr();
printf(β€œn Enter a,b values :”);
scanf(β€œ%d%d”,&a,&b);
printf(β€œn The biggest value is :%d”, big(&a,&b));
}
- 72 -
S. MADHU MOHAN C - Language
int big(int *x, int *y)
{
if(*x>*y)
return (*x);
else
return (*y);
}
108. Write a program to read any 3 values and find the biggest one using functions.
Solution : #include<stdio.h>
main()
{
int a,b,c;
int big();
clrscr();
printf(β€œn Enter a,b,c values :”);
scanf(β€œ%d%d%d”, &a,&b,&c);
printf(β€œn The biggest values :%d”, big(&a,&b,&c));
}
int big(int *x, int *y, int *z)
{
if(*x>*y && *x>*z)
return (*x);
else
if(*y>*z);
return(*y);
else
return (*z);
}
109. Write a program to read any 3 values and find the smallest one using functions.
Solution : #include<stdio.h>
main()
{
int a,b,c;
int small();
clrscr();
printf(β€œn Enter a,b,c values :”);
scanf(β€œ%d%d%d”, &a,&b,&c);
printf(β€œn The smallest values :%d”, small(&a,&b,&c));
}
int small(int *x, int *y, int *z)
{
if(*x<*y && *x<*z)
return (*x);
else
if(*y<*z);
return(*y);
- 73 -
S. MADHU MOHAN C - Language
else
return (*z);
}
110. Write a program to read a no as find the factorial value.
Solution : #include<stdio.h>
main()
{
int a;
int fact();
clrscr();
printf(β€œn Enter n value :”);
scanf(β€œ%d”,&a);
printf(β€œn The Factorial value :%d”, fact(&a));
}
int fact (int *n)
{
int I,f=1;
for(i=2;i<=*n;i++)
f=f*i;
return (f);
}
111. Write a program to read a line of text and count the no. of vowels, consonants,
digits, spaces and other characters using pointers.
Solution : #include<stdio.h>
#include<string.h>
#inlcude<ctype.h>
main()
{
char line[80];
int i,vow,con,dig,spa,oth;
void line (char line[], int *pv, int *pc, int *pd, int *ps, int *po);
clrscr();
printf(β€œn Enter a line of text :”);
scanf(β€œ%[^n]”,line);
i=vow=con=dig=spa=oth=0;
sline(line, &vow, &con, &dig, &spa, &oth);
printf(β€œn Total no. of Vowels :%d”, vow);
printf(β€œn Total no. of Consonants :%d”, con);
printf(β€œn Total no. of Digits :%d”, dig);
printf(β€œn Total no. of Spaces :%d”, spa);
printf(β€œn Total no. of other Characters :%d”, oth);
}
void line (char line[], int *pv, int *pc, int *pd, int *ps, int *po);
{
int i=0;
char c;
- 74 -
S. MADHU MOHAN C - Language
while((c= to lower(line[i]))!= β€˜Ο†β€™)
{
if(c= = β€˜a’||c= = β€˜e’||c= = β€˜i’ ||c= = β€˜o’ || c= = β€˜u’)
++*pv;
else
if(c>= β€˜a’ && c<= β€˜z’)
++ *pc;
else
if(c>= β€˜0’ && c<=’9’)
++ *pd;
else
if(c= = β€˜ ’ || c= =’t’)
++ *ps;
else
++ *po
i++;
}
}
112. Write a program to display the character in.
Solution : #include<stdio.h>
main()
{
char a;
clrscr();
for(i=0;i<=15;i++)
printf(β€œn %c”,2);
getch();
}
113. Write a program to read a string (or) line, and count total no. of characters and
words, display it.
Solution : #include<stdio.h>
#include<string.h>
main()
{
char line[70];
int words=o,c;
clrscr();
printf(β€œn Enter any text :”);
flushall();
gets(line);
for(c=0;line[c]!= β€˜0’;c++)
{
if(line[c]= = β€˜ ’)
words=words+1;
}
printf(β€œn No. of Characters in the text :%d”, c);
- 75 -
S. MADHU MOHAN C - Language
printf(β€œn No. of Words in the text :%d”, words+1);
getch();
}
114. Write a program to read two words and display one word using functions.
Solution : #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[]= β€œMADHU”;
char str2[]= β€œMOHAN”
clrscr();
strcat(str1, str2); (or) strcpy(str1, str2);
puts(str1);
getch();
}

S.MADHU MOHAN
MBA.,PGDSE & DHE.
H. No : 16-38F/1A;
NANDIKOTKUR.
Phone No : 09440289220
09032900170.
- 76 -

More Related Content

What's hot

C programming tutorial
C programming tutorialC programming tutorial
C programming tutorialMohit Saini
Β 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingABHISHEK fulwadhwa
Β 
Strings IN C
Strings IN CStrings IN C
Strings IN Cyndaravind
Β 
Learn C
Learn CLearn C
Learn Ckantila
Β 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)indrasir
Β 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerVineet Kumar Saini
Β 
Programming in c
Programming in cProgramming in c
Programming in cankitjain851
Β 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C ProgrammingAnil Pokhrel
Β 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Languagesatvirsandhu9
Β 
pdf c programming language.pdf
pdf c programming language.pdfpdf c programming language.pdf
pdf c programming language.pdfVineethReddy560178
Β 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C ProgrammingQazi Shahzad Ali
Β 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
Β 

What's hot (20)

C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
Β 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
Β 
C++ programming
C++ programmingC++ programming
C++ programming
Β 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Β 
Strings IN C
Strings IN CStrings IN C
Strings IN C
Β 
Function in C
Function in CFunction in C
Function in C
Β 
Learn C
Learn CLearn C
Learn C
Β 
C notes
C notesC notes
C notes
Β 
Strings and pointers
Strings and pointersStrings and pointers
Strings and pointers
Β 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
Β 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
Β 
C LANGUAGE NOTES
C LANGUAGE NOTESC LANGUAGE NOTES
C LANGUAGE NOTES
Β 
Programming in c
Programming in cProgramming in c
Programming in c
Β 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
Β 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
Β 
pdf c programming language.pdf
pdf c programming language.pdfpdf c programming language.pdf
pdf c programming language.pdf
Β 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
Β 
C functions
C functionsC functions
C functions
Β 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
Β 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
Β 

Similar to C Language Fundamentals

Interview Questions For C Language
Interview Questions For C Language Interview Questions For C Language
Interview Questions For C Language Rowank2
Β 
Interview Questions For C Language .pptx
Interview Questions For C Language .pptxInterview Questions For C Language .pptx
Interview Questions For C Language .pptxRowank2
Β 
C presentation book
C presentation bookC presentation book
C presentation bookkrunal1210
Β 
C material
C materialC material
C materialtarique472
Β 
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...Rowank2
Β 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centrejatin batra
Β 
Msc prev completed
Msc prev completedMsc prev completed
Msc prev completedmshoaib15
Β 
Msc prev updated
Msc prev updatedMsc prev updated
Msc prev updatedmshoaib15
Β 
venkatesh.pptx
venkatesh.pptxvenkatesh.pptx
venkatesh.pptxKishoreRedla
Β 
Basic of the C language
Basic of the C languageBasic of the C language
Basic of the C languageSachin Verma
Β 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.pptAshwini Rao
Β 
Aniket tore
Aniket toreAniket tore
Aniket toreanikettore1
Β 
C programming language
C programming languageC programming language
C programming languageAbin Rimal
Β 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computerShankar Gangaju
Β 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorialMuthuselvam RS
Β 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxLikhil181
Β 

Similar to C Language Fundamentals (20)

Interview Questions For C Language
Interview Questions For C Language Interview Questions For C Language
Interview Questions For C Language
Β 
Interview Questions For C Language .pptx
Interview Questions For C Language .pptxInterview Questions For C Language .pptx
Interview Questions For C Language .pptx
Β 
C presentation book
C presentation bookC presentation book
C presentation book
Β 
C material
C materialC material
C material
Β 
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
Β 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centre
Β 
Msc prev completed
Msc prev completedMsc prev completed
Msc prev completed
Β 
Msc prev updated
Msc prev updatedMsc prev updated
Msc prev updated
Β 
venkatesh.pptx
venkatesh.pptxvenkatesh.pptx
venkatesh.pptx
Β 
Basic of the C language
Basic of the C languageBasic of the C language
Basic of the C language
Β 
Basics of c
Basics of cBasics of c
Basics of c
Β 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
Β 
Aniket tore
Aniket toreAniket tore
Aniket tore
Β 
C programming language
C programming languageC programming language
C programming language
Β 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computer
Β 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
Β 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptx
Β 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
Β 
Unit - 1.ppt
Unit - 1.pptUnit - 1.ppt
Unit - 1.ppt
Β 
fundamentals of c
fundamentals of cfundamentals of c
fundamentals of c
Β 

Recently uploaded

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
Β 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
Β 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
Β 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
Β 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
Β 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
Β 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Dr. Mazin Mohamed alkathiri
Β 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
Β 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
Β 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
Β 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
Β 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
Β 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
Β 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
Β 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
Β 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
Β 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
Β 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
Β 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
Β 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
Β 
CΓ³digo Creativo y Arte de Software | Unidad 1
CΓ³digo Creativo y Arte de Software | Unidad 1CΓ³digo Creativo y Arte de Software | Unidad 1
CΓ³digo Creativo y Arte de Software | Unidad 1
Β 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
Β 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Β 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
Β 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
Β 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
Β 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
Β 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Β 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Β 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
Β 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
Β 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Β 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
Β 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Β 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Β 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
Β 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
Β 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
Β 

C Language Fundamentals

  • 1. S. MADHU MOHAN C - Language C- language β€œC’ seems to be a strange name for a programming language. This is one of the most powerful and popular programming language. β€œC” is an off-spring of β€œBasic Combined Programming Language (BCPL)” called β€œB” developed in 1960’s at Cambridge University. β€œB” language was modified by β€œDENNIS M. RITCHIE” and was implemented at β€œBell Labs” in 1970, for many years β€œC” language was used mainly in academic environments. But the release of β€œC” compilers increased the popularity of the β€œC” language. Features : 1. β€œC” language is having rich set of built in functions and different types of operators can be used to write program. This language will be used to develop application software, and also the system software. 2. It is a structured programming language. 3. β€œC” is highly portable. It means that the β€œC” program are written on one computer can be run on another computer with a little (or) no modifications. 4. β€œC” program are efficient and fast. 5. It is a case sensitive language. β€œC” Character Set : There are a few pre-defined characters can from a character set in β€œC” language. The β€œC” character set should be consisting of 1. Alphabets (A – Z and a – z) 2. Digits (0-9) 3. Special Characters (+,-,*,/,<,>,,;,”,’, ( ), [ ], { },……etc) 4. White spaces (blank space, horizontal tab, new line character etc) Key Words or Reserved Words : A key word means already it is having some predefined meanings or fined meanings, and the meanings cannot be changed. All the key words must be written in lower case letters only. They are auto, break, case, char, const, continue, default, double, else, enum, extern, float, go to, for, int, if, long, register, return, short, signed, size of, static, struct, switch, typedef, union, unsigned, void, volatile, while,…… etc. Identifiers : An identifier is a name given to various program elements to identify an object. Variables, functions, arrays, …. Etc. Rules : 1. An identifier may consist of alphabets and digits, but the first character must be an alphabet. 2. Both upper case and lower case letters are permitted, but commonly we can use lower case letters only. 3. The β€œ_” (underscore) character is also permitted in identifiers. It is namely used to link between two letters. - 1 -
  • 2. S. MADHU MOHAN C - Language Constants : A constant in β€œC” language refers to the fixed value that do not change during the program execution. The β€˜C’ language supports several types of consonants. They are Integer Constant : An Integer constant refers to a sequence of digits. There are 3 types of integer constants namely decimal, octal, and hexa decimal. The decimal integers consists of a set of digits from 0-9 and preceded by an optional β€˜+’ or β€˜- ’ sign. Ex : 565, -123, +676 Note : Spaces, commas, and special characters are not permitted in between the digits. An octal integer consist of the combination of digits from the set 0-7 with a leading β€˜0’. Ex : 0123, 0456, 0321, etc. A sequence of digits preceded by β€˜ox’ or β€˜OX’ is considered as hexa-decimal integer. It may also include alphabets from A to F and digits from 0-9. the alphabets A – F represents from 10 – 15. Ex : ox67AB, oxAB123, ox56F Real Constants : The integer numbers are inadequate to represent quantities likes prices, distances, heights, weights, etc. there quantities are represented by numbers containing fractional part like 5.67, 70.62 etc., and such numbers are called real or floating point constants. Ex : -2.16, 3.34m 80.318 A real no may also represents in an exponential format. Ex : The value 215.65 can be represented in an exponential format is 2.1565E2. - 2 - Decimal Constants Octal Constants Hexa Deciaml Numeric Constants Character Constants Integer Constants Real Constants Single Character Constants String Constants Constant s
  • 3. S. MADHU MOHAN C - Language Single Character Constants : It contains a single character enclosed in a single quotation marks. Ex : β€˜5’, β€˜x’, β€˜+’ etc. String Constant : A sequence of character can be enclosed in a double quotation marks are called string constants. Ex : β€œ123”, β€œABC”, β€œA123” etc. Variables : A variable is a data name that may be used to store a data value. The variable value can change during the program exhibition. The value of a variable is a varied from time to time. Rules : 1. They must begin with an alphabet, some compilers may accept underscore(_) as the first character also. 2. The maximum length of a variable is almost 31 characters, but the first 8 characters are treated as a variable name by most of the compilers. 3. Upper case and lower case or significant that is the variable Total is not equal to total. 4. The variable should not be a reserved word. 5. Spaces are not allowed in between the variable. Data Types : β€œC” – language is having a rich set of data types. The storage representation and the machine instructions to handle constants differ from system to system. The variety of data type available to allow the programmer to select the appropriate type to the needs of a program. - 3 - User defined data type Ex: Structures Union enum Derived type Ex: Arrays Functions Pointers Built in Data types Ex: Structures Union enum Integral data types void Floating data types Int Char Float Double C data types
  • 4. S. MADHU MOHAN C - Language The β€œC” compiler supports four fundamental (built in) data types namely interger (int), character (char), floating point value and double precision (double). Integer data type (int) : Integer are whole number with a range of values supported by a particular machine. Generally an integer occupies 2-4 bytes of storage and the word is size varies from machine to machine. The range of an integer is -32,768 to 32,767. There are three classes of integers namely short int, int and long int in both signed and unsigned forms. The short int represents small integer values and requires half the amount of storage as regular integer uses. If we declared as a long integer (int), we will increase the range of an integer. If we done specify either signed or unsigned, by default the computer will assume only the signed format. Float data type : The float numbers (real) are stored in 32 bits (4 bytes or 1 word) with 6 digits of precessions. The floating point numbers are defined by the keyword β€œfloat”, If we specify the data types as β€œdouble”, it uses 64 bits (8 bytes) and giving a precision of 14 digits. To extend the precision for that, we may use long double also. Character data type : A single character can be defined as a character type data (char). The characters are usually stored in 8 bits of internal storage. The signed characters have the values from -128 to 127, and unsigned have the values from 0 to 255. Size and Range of Data types : Type Size (bits) Range 1. char or signed char 8 -128 to 127 2. unsigned char 8 0 to 255 3. int or signed int 16-32 -32,768 to32,767 4. unsigned int 16-32 0 to 65,535 5. short int or signed short int 8 -128 to 127 6. unsigned short int 8 0 to 255 7. long int or singed long int 32 -2,147,483,648 to 2,147,483,647 8. unsigned long int 32 0 to 4,294,967,295 9. float 32 3.4e-38 to 3.4e+38 10. double 64 1.7e-308 to 1.7e+308 11. long double 80 3.4e-4932 to 1.1e+4932 - 4 -
  • 5. S. MADHU MOHAN C - Language Operators : β€œC” language is having rich set of operators. An operator is a symbol that tells the computer to perform a mathematical or a logical work. The β€˜C’ operators can be classified into no. of categories. They are 1. Arithmetic Operators : β€˜C’ provides a few basic arithmetic operators. The operators β€˜+’, β€˜-’, β€˜*’, etc are work in the same way as they do in the other languages also. Operator Meaning + (plus) Addition - (unary) Subtraction * Multiplication / Division % Modules or Remainder 2. Relational Operators : The following are the relational operators used in β€˜C’. They are Operator Meaning < Less than > Greater than <= Less than equal to >= Greater than equal to = = Equal to != Not equal to 3. Logical Operators : β€˜C’ has three types of logical operators. They are Operator Meaning && Logical AND || Logical OR ! Logical NOT 4. Assignment Operators : The assignment operator is used to assign a value to a variable or to assign the result of an expression to a variable. The assignment operator in β€˜C’ language is β€œ=”. They are Variable = expression - 5 -
  • 6. S. MADHU MOHAN C - Language Ex : x=5; y=a+b; 5. Increment or Decrement Operators : β€˜C’ has two very useful operators, not generally found in other languages. These are the increment(++) and decrement(--) operators. The operator β€˜++’ adds β€œ1” to the operand. While β€˜- -’ subtracts β€œ1” from the operand. Both are called unary operators. Here ++m is equivalent to m=m+1 -- m is equivalent to m=m-1 or m=-1 ++m is called pre incrementation, m++ is called post incrementation. Similarly --m is called pre decrementation and m-- is called post decrementation. Ex : m=5; y=++m In the case the value of y and m would be 6. If we rewrite the statement as M=6; y=m++ In this case the value of y is 5 and m is 6. 6. Conditional Operator (Ternary) : A ternary operator pair β€œ?” is available in β€˜C’ language to construct a conditional expression. The syntax is Variable=exp1 ? exp2 : exp 3; Example : a=10, b=15 X=a>b ? a:b In this example first it will evaluate the exp 1. If it is true the value of a (exp2) will be assigned to x. If the condition is false, the value of b(exp3) will be assigned to x. 7. Bitwise Operators : β€˜C’ has a distinction of supporting some operators called as bitwise operators and are used for manipulation of data at bit level. These operators are used for testing the bits or shifting them from right to left, left to right etc., They are Operator Meaning & Bitwise AND | (pipe symbol) Bitwise OR ^ Bitwise x-OR (ap) ~ /S Complement (tilde) << Left shift >> Right shift 8. Special Operators : β€˜C’ supports some special operators, such as comma(,) operaor, β€œsize=of” operator, point operators (& and *), member selection operators (. and ). - 6 -
  • 7. S. MADHU MOHAN C - Language Expression : An expression is a combination of variables, constants and operators and form an algebra type of equation is called an expression. β€˜C’ can handle any type of mathematical expression. The expression are evaluated by using an assignment statement like Variable=expression; Here the variable is a valid β€˜C’ variable name. After evaluating the expression the result will be assigned to the variable on the left hand side. Precedence of Operators : An arithmetic expression without parenthesis [( )] will be evaluated from left to right, using the rules of precedence of operators. There are two priority levels of arithmetic operators. They are High priority --------- *, /, % Low priority --------- +, - When ever parenthesis are used, the expression within the parenthesis are assumed high priority. If an expression consist of two or more sets of parenthesis, the expression in the left most is evaluated first and the right most is evaluated last. Ex : a=9, b=12, c=3 x=a-b/3+c*2-1 y=a-b/(3+c)*(2-1) =9-2/3+3*2-1 =9-12/(3+3)*(1) =5+5 =9-12/6*1 = 9-12/6 =10 =9-2 = 7 Input and Output Statements : There are a few pairs of input and output statement in β€˜C’ language. They are getchar( ) and put char( ), scanf( ) and printf( ). getchar( ) : This is a one type of input statement in β€˜C’ language. By using of this statement, we can read a single character at a time from the keyboard. (This can also be done by using scanf( ) statement). Syntax : variable name= getchar( ) Here the variable name is a valid β€˜C’ name that has been declared as a character type when this statement is executed. The computer waits until a key is pressed and that character is assigned to the variable name. They getchar( ) function is always on the right hand side of the assignment statement and the value of the getchar( ) function is assigned to the left hand side variable. Example : char name; - 7 -
  • 8. S. MADHU MOHAN C - Language Name= getchar( ); putchar( ) : it is an output statement in β€˜C’ language. By using of this statement we can write (display) the characters one at a time on to the screen. Syntax : putchar(variable name) Example : name= β€˜y’; Putchar(name); scanf( ) : The input data can be entered into the computer from a standard input device (keyboard) by using of a scanf statement. This function can be used to enter any combination of integer and floating point values, single character and also strings. Syntax : scanf(β€œcontrol string”, arg1,arg2,………argn); Here the control string specifies field formats, in which the data is to be entered and arg1, arg2,…….. arg n specifies the address of the variables where the data is stored. The control string contains the field specification and also the conversion character (%) with a data type specifier. The blank spaces, new line characters are ignored. printf( ) : The output data can be written from a computer on to a standard output device (screen) using a function called β€œprinf( )”. This function can be used to display any combination of integer values, floating point values, single character, strings etc. Syntax : printf(β€œcontrol string”, arg1, arg2, ……….. arg n); Here the control string specifies a. the character that will be printed on the screen as they appear. b. Format specifications. c. Escape sequence characters. Escape sequence characters (or) back slash characters : β€˜C’ supports some special back slash characters, that are used in output statements. Note that each of them represents one character although they consist of two characters. Character Constant Meaning n New line charcter b Back space a Alert bell t Horizontal tab v Vertical tab ’ Single quotation mark ” Double quotation mark ? Question mark - 8 -
  • 9. S. MADHU MOHAN C - Language 0 Null character Back slash Declaration of a variable : After designing the suitable variable names, we must declare them in a program. A variable can be used to store a value of any data type. Syntax ; data type var1, var2, …….var n; Here the data type specifies the type of the data (int, float, char ……) and var1, var2, …… are the variable names. Example : int a,b; float x,y; char n; The variable specifies mainly two things. They are 1. It tells the compiler what the variable name is. 2. It specifies what type of data the variable will hold. Structure of a β€˜C’ program : Every β€˜C’ program may consist of one or more functions and one of the function is main( ). A β€˜C’ program contains one or more sections also. Syntax : documentation section Link section Definition section Global variable declaration section Example : main( ) { Declaration part; Executable part; } Subprogram section Here the documentation section consists set of comment lines giving the name of the program. The programmers name and other details like time, date etc. The link section provides instructions to the compiler to link the functions from the library. The definition section will be used to declare the constants. There are some variables that are used in more than one function and such variables are called global variables and declared in the global declaration section i.e., outside the main ( ) function. Every β€˜C’ program must have a main ( ) function. This section contains two parts i.e., the declaration part we and the executable part. In the declaration part we declare all the variables that are used in the executable part. These two parts must appear between the opening and closing brackets. The program execution begins at the opening bracket - 9 -
  • 10. S. MADHU MOHAN C - Language and end at the closing bracket. The closing bracket indicates the end of the program. All the statements in the declaration part and executable part will be terminated by a semicolon (;). The subprogram section contains all the user defined functions that are called in the main( ) function. The user defined functions are generally placed immediately after the main function. β€˜C’ Header files or Library Functions : β€˜C’ language is having a no. of library functions that performs various files. The β€˜C’ language header files contains all these functions. They are <stdio.h> Standard input output functions. <ctype.h> Character testing and conversion. <math.h> Mathematical function. <string.h> String manipulation function. <time.h> Time manipulation function. Some Character Codes used in Printf( ) and Scanf( ) Functions : Code Meaning of purpose %c Display or read a single character. %d Display or read an integer value. %f Display or read a floating point value. %s Display or read a string. %[..] Display or read a string of words. PROGRAMS 1. Write a program to display your address. Solution : #include <stdio.h> main( ) { printf(β€œn Name : S. MADHU MOHAN”); printf(β€œn Street : College Road”); printf(β€œn Town : NANDIKOTKUR”); } 2. Write a program to read any 2 values and find the sum of 2 values. Solution : #include <stdio.h> main( ) { Int a,b,c, Clrscr(); printf(β€œn Enter any numbers :”); - 10 -
  • 11. S. MADHU MOHAN C - Language scanf(β€œ%d”, &a); printf(β€œn Enter Another Number :”); scanf(β€œ%d”, &b); c=a+b; printf(β€œn Sum of two numbers :%d”,c); getch(); } 3. Write a program to read principle, time, and rate. Calculate the simple interest and display it. Solution : #include<stdio.h> main() { Float p,t,r,I; clrscr(); printf(β€œn Enter P,T,R :”); scanf(β€œ%f”,&p); scanf(β€œ%f”,&p); scanf(β€œ%f”,&p); i=(p*t*r)/100 printf(β€œn The Interest is :%f”,i); getch(); } 4. Write a program to read any 3 values and find the product. Solution : #include<stdio.h> main() { int a,b,c,d; printf(β€œn Enter any 3 numbers :”); scanf(β€œ%d”,&a); scanf(β€œ%d”,&b); scanf(β€œ%d”,&c); d=a*b*c; printf(β€œn The Product of 3 numbers :%d”, d); getch(); } 5. Write a program to read the radius of circle and calculate area and circumference Solution : #include<stdio.h> main() { float r,a,c; clrscr(); printf(β€œn Enter the radius of Circle :”); scanf(β€œ%f”,&r); a=3.14*r*d; - 11 -
  • 12. S. MADHU MOHAN C - Language c=2*3.14*r printf(β€œn Area :%f”,a); getch(); printf(β€œn Circumferene :%f”, c); } 6. write a program to read the city name and the temperature in Celsius ? Calculate the temperature in Fahrenheit and display it. Solution : #include<stdio.h> main() { char name[10]; float C,F; clrscr(); printf(β€œn Enter city name and temperature in celsius :”); scanf(β€œ%s”, name); scanf(β€œ%f”, &c); f=32+(1.8*c); printf(β€œTemperature in Fahrenheit :%f”, f); getch(); } 7. Write a program to read a city name and temp in Fahrenheit. Calculate the Celsius tem and display it. Solution : #include<stdio.h> main() { Char name[10]; Float c,f; clrscr(); printf(β€œn Enter city name and Temperature in Fahrenheit :”); scanf(β€œ%s”, name); scanf(β€œ%f”, &f); c=(f-32)/1.8; printf(β€œn The Temperature in Celsius :%f”,c); getch(); } 8. Write a program read rollno, name, 2 subject marks. Calculate total and average and display it. Solution : #include<stdio.h> main() { int no; char name[10]; float m1, m2, t,a; clrscr(); printf(β€œn Enter name, rollno, marks in 2 subjects :”); scanf(β€œ%s”, name); - 12 -
  • 13. S. MADHU MOHAN C - Language scanf(β€œd”, &no); scanf(β€œ%f”, &m1); scanf(β€œ%f”, &m2); t=m1+m2; a=t/3; printf(β€œn Total Marks :%f”, t); printf(β€œn Average Marks :%f”, a); getch(); } 9. Write a program to read empno, name, basic salary. Calculate DA(30%), HRA(15%), and PF(10%). Compute gross and net salary and display it. Solution : #include<stdio.h> main() { Int eno,b; char name[20]; float da,hra,pf,gs,ns; clrscr(); printf(β€œn Enter eno,ename, basic salary :”); scanf(β€œ%d,%d”, &eno, &b); scanf(β€œ%s”, name); da=b*30/100; hra=b*15/100; pr=b*10/100; gs=basic+da+hra; ns=gs-pf; printf(β€œn DA :%f”,da); printf(β€œn HRA :%f”, hra); printf(β€œn PF :%f”, pf); printf(β€œGross Salary :%f”, gs); printf(β€œNet salary :%f”, ns); getch(); } Control Statements (or) Control Structures : If condition : It is a conditional control structure in β€˜C’ language. It is a powerful decision making statement and it is used to control the flow of execution of statements. The β€œif” structure is having 4 types of syntax. They are. 1. Simple If : If (condition) Statement -1; Statement -2; Statement –n; - 13 -
  • 14. S. MADHU MOHAN C - Language Here first it will evaluate the condition if it is true statement will be executed and execute the remaining statements in a sequential order (statement -2 onwards). If the condition is false it will skip the statement -1 and execute the remaining statements in sequential order. If we want to execute more than one statement in a single if condition, we have to enclose all the statements within the brackets. The format is If (condition) { State -1; State -2; } -----; -----; 2. If .. Else structure : if(condition) { True statement block; } Else { False statement block; } State –x; State – y; Here first it will check the condition, if it is true, the true statement block will be executed and comes outside the loop and execute the remaining statements n a sequential order. (state –x ) onwards. If the condition is false, it will execute the false statement block and comes out side the loop and execute the remaining statements (state – x) onwards one by one. 3. Nested If statement : If(condition 1) { If(condition 2) { State – 1; ------; ------; } else else { { State – 2; state – 3; - 14 -
  • 15. S. MADHU MOHAN C - Language ------; ------; ------; ------; } } } Here first it will check the condition -1. If it is true, then it will evaluate the condition -2. If it is also true, the statement 1 ….. will be executed and comes outside the loop and execute the remaining statements one by one (statement – x onwards). If the condition 1 is true and condition 2 is false it will execute the state -2 …. and comes outside the loop and execute the remaining statements one by one. (state x…). If the condition 1 is initially false, then it will execute the state -3 …. And comes outside the loop and execute the remaining statements one by one. (state – x …..). 4. Else … If Ladder : if(condition 1) state – 1; else if (condition 2) state – 2; else if (condition 3) state – 3; else state – 4; state – x; state – y; --------- --------- Here first it will evaluate the condition 1. If it is true, the state -1 will be executed and comes outside the loop and execute the remaining statements, in sequential order (state – x ) onwards. If the condition 1 is false then it will evaluate the condition 2. If it is true the state -2 will be executed and comes out side the loop and execute the remaining statements that is (state – x) onwards. If the condition 1 and condition 2 are true then it will be evaluate the condition 3. If it is true statement 3 will be executed and comes out of the loop and execute the remaining statements one by one. If the condition 1, condition 2, condition 3 are false, automatically it will execute the statement 4 and comes out side the loop and execute the remaining statements in sequential order (state – x) onwards. 10. Write a program to read any two values and find biggest number. Solution : #include<stdio.h> - 15 -
  • 16. S. MADHU MOHAN C - Language main() { int a,b; clrscr(); printf(β€œn Enter any two numbers :”); scanf(β€œ%d %d”, &a, &b); if(a>b) printf(β€œn Biggest number :%d”,a); else printf(β€œn Biggest number :%d”,b); getch(); } 11. Write a program to read any 3 values and find the biggest number. Solution : #include<stdio.h> main() { int a,b,c; clrscr(); printf(β€œn Enter any three numbers :”); scanf(β€œ%d %d %d”, &a, &b,&c); if(a>b && a>c) printf(β€œn Biggest number :%d”,a); else if(b>c) printf(β€œn Biggest number :%d”,b); else printf(β€œn Biggest number :%d”,c); getch(); } 12. Write a program to read a person name and his age. Find out whether he is eligible to vote or not. Display how many years he has to wait. Solution : #include<stdio.h> main() { char name[10]; int a,x; clrscr(); printf(β€œn Enter persons name :”); scanf(β€œ%s”, name); printf(β€œn Enter Persons age :”); scanf(β€œ%d”,&a); if(a>=18) printf(β€œn Eligible to Vote); else { x=18-a; - 16 -
  • 17. S. MADHU MOHAN C - Language printf(β€œn Not Eligible and he has to wait %d years to vote”, x); } getch(); } 13. Write a program to read no, name, marks in 2 subjects. Find out total, average and display the results. The result will be avg<35 fail, avg<50 third, avg<60 second, avg<75 fist, avg>=75 distinction. Solution : #include<stdio.h> main() { char name[20]; int no,m1,m2,tot; float avg; clrscr(); printf(β€œn Enter no, name, marks in 2 subjects :”); scanf(β€œ%d %s%d%d”, &no, name, &m1,&m2); tot=m1+m2; avg=tot/2; printf(β€œn Roll number :%d”,rno); printf(β€œn Name :%s”, name); printf(β€œn Marks 1 :%d”, m1); printf(β€œn Marks 2 :%d”,m2); printf(β€œn Total Marks :%d”,tot); printf(β€œn Average Marks :%f”, avg); if(m1<35 || m2<35) printf(β€œn Result :Fail”); else if (avg>=35 && avg<50) printf(β€œn Result :Third class”); else if (avg>=50 && avg<60) printf(β€œn Result :Second class”); else if (avg>=60 && avg<75) printf(β€œn Result :First class”); else printf(β€œn Result : Distinction”); getch(); } 14. Write a program to read meter no, customer name, present, previous readings. Calculate the no. of units and display the bill amount. The rate will be 1. 1st 100 units --- 2/- per unit. 2. Next 100 units --- 3/- per unit. 3. above 200 units --- 4/- per unit. 4. the min bill is ---- 150/- - 17 -
  • 18. S. MADHU MOHAN C - Language 5. if the bill exceeds 450/-, 10% additional charge will be added to the bill amount. . Solution : #include<stdio.h> main() { char name[20]; int mno,pre,prv, units; float amount; clrscr(); printf(β€œnEnter mno, name, present and previous readings :”); scanf(β€œ%d%s%d%d”, &mno, name, &pre, &prv); units=pre-prv; if (units<=100) amount=units*2; else if (units>100 && units<=200) amount=200+(units-100)*3; else amount=500+(units-200)*4; if (amount<1500 amount=150; if (amount>450) amount=amount+(amount*10/100); printf(β€œn Meter Number :%d”, mno); printf(β€œn Customer Name :%s”, name); printf(β€œn Number of Units :%d”, units); printf(β€œn Total Amount :%d”, amount); getch(); } While – statement : β€œWhile” is a repeated conditional control structure in β€˜C’ language. The syntax is While(condition) { Statement -1; Statement -2; Statement -3; --------- --------- --------- Statement – n; } Statement – x; Statement – y; --------- --------- --------- - 18 -
  • 19. S. MADHU MOHAN C - Language Here the while condition first it will evaluate the condition. If it is true all the statements will be executed within the brackets. After executing the statements once again it will test the condition. If it sis true again, the same statements will be executed repeatedly and so on. This process will be continued as long as the condition is true. When ever the condition becomes false it comes outside the loop and execute the remaining statements in a sequential order (state –x ) onwards. 15. Write a program to display the natural numbers. Up to 20. Solution : #include<stdio.h> main() { int i=1; clrscr(); printf(β€œn The natural no’s are :”); while(i<=20) { printf(β€œn %d”,i); i=i+1; } getch(); } 16. Write a program to display all the even numbers up to 50. Solution : #include<stdio.h> main() { int n=2; clrscr(); printf(β€œn The Even no’s are :”); while(i<=50) { printf(β€œn %d”,n); n=n+2; } getch(); } 17. Write a program to display all the odd numbers up to 50. Solution : #include<stdio.h> main() { int n=1; clrscr(); printf(β€œn The Even no’s are :”); while(i<=50) { printf(β€œn %d”,n); n=n+2; - 19 -
  • 20. S. MADHU MOHAN C - Language } getch(); } 18. Write a program to find the sum and average of first 100 natural numbers. Solution : #include<stdio.h> main() { int n=1,sum=0; float avg; clrscr(); while(i<=100) { sum=sum+n; n=n+1; } Aavg=sum/100; printf(β€œn Sum of 100 Numbers :%d”,sum); printf(β€œn Average of 100 Numbers :%f”, avg); getch(); } 19. Write a program to read any 5 values and find the sum and average. Solution : #include<stdio.h> main() { int i=1, sum=0,n; float avg; clrscr(); printf(β€œn Enter any 5 values:”); while (i<=5) { scanf(β€œ%d”,&n); sum=sum+n; i=i+1; } printf(β€œn Sum of Five numbers :%d”, sum); avg=sum/5; printf(β€œn Average of Five numbes :%f”, avg); getch(); } 20. Write a program to read β€˜n’ values and find the sum and average. Solution : #include<stdio.h. main() { int n,i=1, sum=0,x; - 20 -
  • 21. S. MADHU MOHAN C - Language float avg; clrscr(); printf(β€œn Enter values:”); scanf(β€œ%d”, &x); printf(β€œn Enter the values one by one :”); while(i<=x) { scanf(β€œ%d”,&n); sum=sum+n; i=i+1; } avg=sum/x; printf(β€œn Sum :%d”, num); printf(β€œ=n Average :%f”, avg); getch(); } 21. Write a program to read a no, and print it in reverse order. Solution : #include<stdio.h> main() { int n,m,r=0; clrscr(); printf(β€œn Enter a number :”); scanf(β€œ%d”, &n); while(n>0) { m=n%10; r=(r*10)+m; n=n/10; } printf(β€œn The Reverse of Number :%d”, r); getch(); } 22. Write a program to read a number and find the sum of the individual digit of that no. Solution : #include<stdio.h> main() { int n,m,r=0; clrscr(); printf(β€œn Enter a number :”); scanf(β€œ%d”,&n); while(n.0) { m=n%10; r=r+m; - 21 -
  • 22. S. MADHU MOHAN C - Language n=n/10; } printf(β€œn The Sum of individual digit :”,r); getch(); } 23. Write a program to read a number find out whether the given no is palindrome or not. Solution : #include<stdio.h> main() { int n,m,r=0,x; clrscr(); printf(β€œn Enter a number :”); scanf(β€œ%d”,&n); x=n; while(n>0) { m=n%10; r=(r*10)+m; n=n/10; } if(x= =r) printf(β€œn The given number is Palindrome”); else printf(β€œn The given number is not Palindrome”); getch(); } 24. Write a program to read a number. Find out whether the given number is Armstrong or not. Solution : #include<stdio.h> main() { int n,m,s=0,x; clrscr(); printf(β€œn Enter a number :”); scanf(β€œ%d”,&n); x=n; while(n>0) { m=n%10; s=s+m*m*m; n=n/10; } if(x= =s) printf(β€œn The given number is Armstrong”); else - 22 -
  • 23. S. MADHU MOHAN C - Language printf(β€œn The given number is not Armstrong”); getch(); } 25. Write a program to read a number. Find out whether the given number is perfect or not. Solution : #include<stdio.h> main() { int n,m,s=0,p=1; clrscr(); printf(β€œn Enter a number :”); scanf(β€œ%d”,&n); while(n>0) { m=n%10; s=s+m; p=p*m; n=n/10; } if(s= =p) printf(β€œn The given number is Perfect”); else printf(β€œn The given number is not Perfect”); getch(); } 26. Write a program to evaluate ex value up to 5 significant digits. Solution : #include<stdio.h> #include<math.h> main() { int x,n; float s,t; clrscr(); printf(β€œn Enter the values of x :”); scanf(β€œ%d”, &x); t=1; s=1; n=0; while(t>0.00001) { n=n+1; t=(t*x)/n; s=s+t; } printf(β€œn ex value up to 5 significant digits :%f”,s); getch(); - 23 -
  • 24. S. MADHU MOHAN C - Language } 27. Write a program to evaluate the sin x value up to 5 digits. Solution : #include<stdio.h> #include<math.h> main() { float x,n,s,t; clrscr(); printf(β€œn Enter the value of x:”); scanf(β€œ%f”,&x); x=(x*3.1416/180); t=x; s=x; n=1; while (fabs(t)>0.00001) { n=n+2; t=(-t)*x*x/(n*(n-1)); s=s+t; } printf(β€œn Sin x value :%f”,s); getch(); } 28. Write a program to evaluate cos x value up to 5 significant digits. Solution : #include<stdio.h> #include<math.h> main() { float x,n,s,t; clrscr(); printf(β€œn Enter the value of x:”); scanf(β€œ%f”,&x); x=(x*3.1416/180); t=1; s=1; n=0; while (fabs(t)>0.00001) { n=n+2; t=(-t)*x*x/(n*(n-1)); s=s+t; } printf(β€œn Cos x value :%f”,s); getch(); } - 24 -
  • 25. S. MADHU MOHAN C - Language Do … while statement : Do-while is a repeated conditional control structure in β€˜C’ language. The syntax is do { Statement -1; Statement -2; ---------- } While (condition) Here in the do-while structure first the statements will be executed and then test the condition. If it is true the same statements will be executed. After executing the statements. Once again it will test the condition and if it is true the same statements will be executed repeatedly and so on. This process will be continued as long as the condition is true. When ever the condition becomes false, it comes outside the loop and execute the remaining statements in the sequential order. The main draw back in the do-while structure is the statements will be executed at least once even though the condition is initially false. 29. Write a program to display the natural numbers up to 20. using do while. Solution : #include<stdio.h> main() { int n=1; clrscr(); printf(β€œn The Natural numbers are :”); do { printf(β€œ%d”,n); n=n+1; } while (n<=20) getch(); } 30. Write a program to read a number, find the factorial value. Solution : #include<stdio.h> main() { int n,f=1; clrscr(); printf(β€œn Enter n value :”); scanf(β€œ%d”,&n); do { f=f*n; n=n-1; } - 25 -
  • 26. S. MADHU MOHAN C - Language while (n>0) printf(β€œn Factorial value :”,f); getch(); } 31. Write a program to display the Fibonacci series of numbers. Solution : #include<stdio.h> main() { int a,b,c; clrscr(); printf(β€œn The Fibonacci numbers :”); a=1;b=1; do { a=b; b=c; c=a+b; printf(β€œ%d”, c); printf(β€œ%d”,a); } printf(β€œ%d”,b); While (c<100) c=a+b; getch(); printf(β€œ%d”,c); } 32. Write a program to display the multiplication table of a given number 10. Solution : #include,stdio.h> main() { int n,m,r; clrscr(); printf(β€œn The Multiplication table :”); n=10; m=1; do { r=n*m; printf(β€œn %d X %d = %d”,n,m,r); m=m+1; } While (m<=10) getch(); } 33. Write a program to display the multiplication table of given any number. Solution : #include,stdio.h> main() { int n,m,r; - 26 -
  • 27. S. MADHU MOHAN C - Language clrscr(); printf(β€œn Enter any number :”); scanf(β€œ%d”,&n); m=1; do { r=n*m; printf(β€œn %d X %d = %d”,n,m,r); m=m+1; getch(); } } While (m<=10) 34. Write a program to read n numbers and find sum and average. (or) Write a program to find the average of a list of n numbers. Solution : #include<stdio.h> main() { int n,i=1,sum=0,x; float avg; clrscr(); printf(β€œn Enter the values :”); scanf(β€œ%dn”,&x); printf(β€œn Enter the values one by one :”); do { scanf(β€œ%dn”,&n); sum=sum+n; i=i+1; } while (i<=x) avg=sum/x; printf(β€œn Sum :%d”, sum); printf(β€œn Average :%d”, avg); getch(); } 35. Write a program to display the multiplication table in the below format. Solution : #inlcude<stdio.h> #define rowmax 10 #define col max 10 main() { int y,row,col; clrscr(); printf(β€œn Multiplication tables are :”); row=1; do { Col=1; do - 27 -
  • 28. S. MADHU MOHAN C - Language { y=row*col; printf(β€œ%4d”,y); col=col+1; } while (col<=clomax) row=row+1; printf(β€œn”); } While (row<=rowmax) getch(); } For …. Loop : For – loop is an entry-controlled loop and it is a one of the most consized loop in β€˜C’ language. The syntax is For(initialization; test condition; incrementation or decrementation) { Statement – 1; Statement – 2; ------------ ------------ } Example : for(i=1;i<=10;i++) { Statement block; } Here the initialization of the controlled variable(i) is done first by using the assignment statement such as i=1,i>5 etc. Here the variable β€œi” is called a loop control variable. The value of the control variable is tested using the test condition is true, the statement block will be executed. After executing the statement block, the control is transferred back to the β€œfor” statement and the value of β€œi” will be increment by β€œ1”. And test the condition again. If it is true, the same statements will be executed repeatedly and so on. This process will be continued as long as the condition is ture. Whenever the condition becomes false, it comes outside the loop and execute the remaining statements in a sequential order. 36. Write a program to display the natural numbers up to 20. Solution : #include<stdio.h> main() { int i; clrscr(); printf(β€œn The Natural numbers :”); for(i=1;i<=20;i++) printf(β€œ%d”,i); getch(); } - 28 -
  • 29. S. MADHU MOHAN C - Language 37. Write a program to display the numbers from 20 to 1. Solution : #include<stdio.h> main() { int i; clrscr(); printf(β€œn The numbers are :”); for(i=20;i>=1;i--) printf(β€œ%d”,i); getch(); } 38. Write a program to find the sum and average of first 100 numbers. Solution : #include<stdio.h> main() { int n,sum=0; float avg; clrscr(); for(n=1;n<=100;n++) sum=sum+n; avg=sum/100; printf(β€œn Sum of 100 numbers :”,sum); printf(β€œn Average of 100 numbers :”, avg); getch(); } 39. Write a program to read a number and find the factorial value. Solution : #inlcude<stdio.h> main() { int n,f=1,x=1; clrscr(); printf(β€œn Enter the Number :”); scanf(β€œ%d”,&n); for(;n>f;n--) x=x*n; printf(β€œn The Factorial Value :%d”,x); getch(); } 40. Write a program to find the average of a list of n numbers. Solution : #include<stdio.h> main() { int n,I,sum=0,x; float avg; clrscr(); - 29 -
  • 30. S. MADHU MOHAN C - Language printf(β€œn Give the values :”); scanf(β€œ%d”, &x); printf(β€œn Enter the values one by one :”); for(i=1;i<=x;i++) { scanf(β€œ%d”, &n); sum=sum+n; } avg=sum/n; printf(β€œn Sum of :%d”,sum); printf(β€œn Average of :%f”,avg); getch(); } 41. Write a program to display the out in the below format. Solution : #include<stdio.h> main() { int i,j,k; clrscr(); printf(β€œn The output is :”); output : for(i=1;i<=5;i++) 1 1 1 1 1 { 1 1 1 1 for(k=1;k<i;k++) 1 1 1 printf(β€œ ”); 1 1 for(j=5;j>=i,j--) 1 printf(β€œ|”); printf(β€œn”); } getch(); } 42. Write a program to display the out in the below format. Solution : #include<stdio.h> main() { int i,j,; clrscr(); printf(β€œn The output is :”); output : for(i=1;i<=5;i++) 1 { 1 2 for(j=1;j<=i;j++) 1 2 3 printf(β€œ%4d”,i); 1 2 3 4 printf(β€œn”); 1 2 3 4 5 } getch(); } 43. Write a program to display the out in the below format. Solution : include<stdio.h> - 30 -
  • 31. S. MADHU MOHAN C - Language main() { int i,j; clrscr(); printf(β€œn The output is :”); output : for(i=1;i<=5;i++) 1 { 2 2 for(j=1;j<=i;j++) 3 3 3 printf(β€œ%4d”,i); 4 4 4 4 printf(β€œn”); 5 5 5 5 5 } getch(); } 44. Write a program to display the out in the below format. Solution : include<stdio.h> main() { int i,j; clrscr(); printf(β€œn The output is :”); output : for(i=1;i<=5;i++) 1 { 2 1 2 for(j=1;j<=5-i;j++) 3 2 1 2 3 printf(β€œ ”); 4 3 2 1 2 3 4 for(j=1;j>=1;j--) 5 4 3 2 1 2 3 4 5 printf(β€œ%4d”,j); for(j=2;j<=I;j++) printf(β€œ%4d”,j); printf(β€œn”); } getch(); } 45. Write a program to display the out in the below format. Solution : include<stdio.h> main() { int i,j; clrscr(); printf(β€œn The output is :”); output : for(i=1;i<=5;i++) 1 { 1 2 1 for(j=1;j<=5-i;j++) 1 2 3 2 1 printf(β€œ ”); 1 2 3 4 3 2 1 for(j=1;j<=1;j++) 1 2 3 4 5 4 3 2 1 printf(β€œ%4d”,j); for(j=i=1;j>=I;j--) printf(β€œ%4d”,j); printf(β€œn”); - 31 -
  • 32. S. MADHU MOHAN C - Language } getch(); } 46. Write a program to evaluate the roots of a quadratic equation in all possible conditions. Solution : #include<stdio.h> #include<math.h. main() { int a,b,c; float r1,r2,disc; clrscr(); printf(β€œn Enter the quadratic equations a,b,c :”); scanf(β€œ%d%d%d”,&a,&b,&c); disc=(b*b)-4*a*c; if(disc= = 0) { printf(β€œn Roots, r1 and r2 are real and equation :”); r1=-b/(2*a); r2=-b/2*a; printf(β€œn The Roots are %f and %f”,r1,r2); } else if(disc>0) { printf(β€œn The Roots are real and distinct :”); r1=(-b+sqrt(disc))/ (2*a); r2=(-b-sqrt(disc))/ (2*a); printf(β€œn The Roots are %f and %f”,r1,r2); } else { printf(β€œn The Roots are imaginary”); r1=-b/ (2*a); r2=(sqrt(-disc))/ (2*a); printf(β€œn The Roots are : %f amd %f”, r1,r2); } getch(); } Switch statement : If a program is having no. of alternatives, the program becomes difficult to read and follows. Some times it may confuse even the person who designed it. The β€˜C’ language is having built in multi-way decision statement known as β€˜switch’ statement. The syntax is. switch(expression) - 32 -
  • 33. S. MADHU MOHAN C - Language { case value 1: block; break; case value 2: block; break; case value 3: block; break; ------- ------- default :default block; break; } Here the switch statement test the value of a given expression or a variable against a list of case values. If the match is found, the block of statements will be executed. Here the expression is either an integer or character expression. The value 1, value 2,…. Are constants also known as case labels. Each of these case labels should be unique within a switch statement. Here the block 1, block2, …… are the set of statements. There is no need to put the brackets around the blocks. Note that the case labels are end with a colon (:). The break statement to the end of the each block specifies, the end of a particular case and exit from the loop. The default is an optional case. The value of the expression does not match with any case values, the default block is expressed. 47. Write a program to read any two values and find the sum, product, difference and division of that two numbers of user choice, using switch. Solution : #include<stdio.h> main() { int a,b,c,p,choice; clrscr(); printf(β€œn Enter any two values :”); scanf(β€œ%d%d”,&a,&b); printf(β€œn Enter your choice : 1. Sum 2. Difference 3. Product 4. Division 5. Exit”); scanf(β€œ%d’,&choice); switch(choice) { case 1 : c=a+b; printf(β€œn Sum :%d”,c); break; case 2 : c=a-b; printf(β€œn Difference :%d”,c); break; case 3 : c=a*b; printf(β€œn Product :%d”,c); break; - 33 -
  • 34. S. MADHU MOHAN C - Language case 4 : c=a/b; printf(β€œn Division :%d”,c); break; case 51 : printf(β€œn The program is ended”); break; } getch(); } 48. Write a program to read the radius of a circle and find the area and circumference using switch case. Solution : #include<stdio.h> main() { int r,choice; float a,c; clrscr(); printf(β€œn Enter Radus :”); scanf(β€œ%d”,&r); printf(β€œn Enter choice … 1. Area 2. Circumference 3. Exit”): scanf(β€œ%d”,&choice); switch(choice) { case 1 : a=3.14*r*r; printf(β€œn Area is :%f”,a); break case 2 : a=3.14*2*r; printf(β€œn Circumference is :%f”,a); break case 3 : printf(β€œn The program is ended”); } getch() } 49. Write a program to evaluate roots of a quadratic equation in all possible condition by using switch statement. Solution : #include<stdio.h> #include<math.h. main() { int a,b,c,choice; float r1,r2,disc; clrscr(); printf(β€œn Enter a,b,c values :”); scanf(β€œ%d%d%d”,&a,&b,&c); disc=(b*b)-(4*a*c); if(disc= =0) - 34 -
  • 35. S. MADHU MOHAN C - Language choice=1; else if(disc>0) choice=2; else choice=3; switch(choice) { case 1 : r1=-b/2*a; r2=-b/2*a; printf(β€œn The Roots are real and equal to”); printf(β€œn The Roots are %f and %f”,r1,r2); break; case 2 : printf(β€œn The Roots are real and distinct”); r1=-b+sqrt(disc)/2*a; r2=-b-sqrt(disc)/2*a; printf(β€œn The Roots are %f and %f”,r1,r2)l break; case 3 ; printf(β€œn The Roots are imaginary”); r1=-b/2*a; r2=sqrt(-disc)/2*a; printf(β€œn The Roots are %f and %f”,r1,r2); break; } getch(); } 50. Write a program to a college has 100 students, who came from 4 regions, i.e. north(code1), south(code2), east(code3), and west(code4). Calculate the total no. of students came from each region. The input is rno, name, and code. Solution : #inlcude<stdio.h> main() { int rno,code,n,s,w,e,total; char name[20]; clrscr(); n=s=w=e=total=0; while(total<100) { Printf(β€œn Enter rno, name and code :”); scanf(β€œ%d%s%d”,&rno,name,&code); switch (code) { case 1 : n=n+1; break; case 2 : s=s+1; break; case 3 : e=e+1; - 35 -
  • 36. S. MADHU MOHAN C - Language break; case 4 : w=w+1; break; } total=total+1; } Printf(β€œn Number of students from North :%d”,n); Printf(β€œn Number of students from South :%d”,s); Printf(β€œn Number of students from East :%d”,e); Printf(β€œn Number of students from West :%d”,w); getch() } 51. Write a program to calculate the depreciation of any item either a building or a machinery, using any no. of the three methods, i.e., straight line method, double declining method and sum of the years digit method. Solution : #include<stdio.h> main() { int n,year,choice; float value, depre,t; clrscr(); printf(β€œn Enter the original value of a component and the no. of years :”); scanf(β€œ%f%d”,&value,&n); printf(β€œn Enter your choice 1. SLM 2.. DDM 3. SOYDM 4. EXIT”); scanf(β€œ%d”,&choice); switch(choice) { case 1 : printf(β€œn Straight Line method :”); depre=value/n; for(year=1;year<=n;year++) { value=value-depre; printf(β€œn End of the year :%d”, year); printf(β€œn Depreciation :%f”, depre); printf(β€œn Original Value :%f”, value); } Break case 2 : printf(β€œn Double Declining method :”); for(year=1;year<=n;year++) { depre=(2*value)/n; value=value-depre; printf(β€œn End of the year :%d”, year); printf(β€œn Depreciation :%f”, depre); printf(β€œn Original Value :%f”, value); } Break - 36 -
  • 37. S. MADHU MOHAN C - Language case 3 : printf(β€œn Sum of the years digit method :”); t=value; for(year=1;year<=n;year++) { depre=(n-year+1)*t(n*(n-1))/2; value=value-depre; printf(β€œn End of the year :%d”, year); printf(β€œn Depreciation :%f”, depre); printf(β€œn Original Value :%f”, value); } Break case 4 : printf(β€œn The program is ended”); break; } getch(); } ARRAYS An β€œArray” is nothing but a collect of related elements of same data type stored in consecutive storage locations and assigned to a single data name. if we want to access a specific element from an array is that, we have to mention the subscript (location) is that, we have to mention the subscript. Single (one) Dimensional Array : A list of items can be given with one variable name using one subscript. Such type of variable are called one dimensional array. In other words we can say that the one dimensional array may consist of different rows and single columns. Declaration of an Array : Like other variable the array must be declared before they are used . syntax is Storage class data type array name[size]; Here the storage class is optional (auto, static…..) the data type indicates the type of the data that the array will contain such as int, float, char etc., and the array name indicates the name of the array and the size indicates the maximum no. of elements is possible to store in the array. Example : int a[10]; flaot x[5]; Initialization of an Array ; They can be initialized with different values by using the below syntax. Syntax is Storage class data type arrayname[size]={ele1, ele2, …….ele n}; Example : int a[3]={5,10,15} flaot x[3]={1.2,1.3,1.8}; - 37 -
  • 38. S. MADHU MOHAN C - Language 52. Write a program to store 5 values in an array and display the same values on the screen. Solution : #include<stdio.h> main() { int a[5],i; clrscr(); printf(β€œn Enter any five values :”); for(i=0;i<5;i++) scanf(β€œ%d”,&a[i]); printf(β€œn The above values are :”); for(i=0;i<5;i++) printf(β€œ%d”,a[i]); getch(); } 53. Write a program to store any 5 values in an array, and find the sum, average. Solution : #include<stdio.h> main() { int a[5],i,sum=0; float avg; clrscr(); printf(β€œn Enter any five values :”); for(i=0;i<5;i++) { scanf(β€œ%d”,&a[i]); sum=sum+a[i]); } avg=sum/5; printf(β€œn Sum is :%d”,sum); printf(β€œn Average is :%f”avg); getch(); } 54. Write a program to store n values in an array and find the sum and average. Solution : #include<stdio.h> main() { int a[50],i,sum=0,n; float avg; clrscr(); printf(β€œn Enter the no. of values to be entered :”); scanf(β€œ%d”, &n); printf(β€œn Enter the values one by one :”); for(i=0;i<n;i++) - 38 -
  • 39. S. MADHU MOHAN C - Language { scanf(β€œ%d”,&a[i]); sum=sum+a[i]); } avg=sum/5; printf(β€œn Sum is :%d”,sum); printf(β€œn Average is :%f”avg); getch(); } 55. Write a program to store 5 values in an array and find the biggest element and display it. Solution : #include<stdio.h> main() { int a[5],i,big=0; clrscr(); printf(β€œn Enter any five values :”); for(i=0;i<5;i++) scanf(β€œ%d”,&a[i]); for(i=0;i<5;i++) { if(a[i]>big) big=a[i]; } printf(β€œn Biggest value :%d”, big); getch(); } 56. Write a program t store 5 values in an array, and find the smallest element and display it. Solution : #include<stdio.h> main() { int a[5],i,small; clrscr(); printf(β€œn Enter any five values :”); for(i=0;i<5;i++) scanf(β€œ%d”,&a[i]); for(i=0;i<5;i++) { if(a[i]>small) small=a[i]; } printf(β€œn Smallest value :%d”, small); getch(); } - 39 -
  • 40. S. MADHU MOHAN C - Language 57. Write a program to 5 values in one array, another 5 values in the second array, add the two arrays and store the result in third array and display it. Solution : #include<stdio.h> main() { int a[5],b[5],c[5]; clrscr(); printf(β€œn Enter any five values of first array :”); for(i=0;i<5;i++) scanf(β€œ%d”,&a[i]); printf(β€œn Enter any five values of second array :”); for(i=0;i<5;i++) scanf(β€œ%d”,&b[i]); for(i=0;i<5;i++) c[i]=a[i]+b[i]; printf(β€œn The sum of :”); for(i=0;i<5;i++) getch(); printf(β€œ%d”,c[i]); } 58. Write a program to store 5 values in one array, another 5 values in second array, merge the 2 arrays and store it in the third array display it. Solution : #include<stdio.h> main() { int a[5],b[5],c[10],i; clrscr(); printf(β€œn Enter any five values of first array :”); for(i=0;i<5;i++) scanf(β€œ%d”,&a[i]); printf(β€œn Enter any five values of second array :”); for(i=0;i<5;i++) scanf(β€œ%d”,&b[i]); for(i=0;i<5;i++) { c[i]=a[i] c[i+5]=b[i]; } printf(β€œn The values of third array :”); for(i=0;i<10;i++) printf(β€œ%d”,c[i]); getch(); } 59. Write a program to read n values and sort it an ascending order. Solution : #include<stdio.h> main() { - 40 -
  • 41. S. MADHU MOHAN C - Language int a[20],t,i,j,n; clrscr(); printf(β€œn Give the no. of values :”); scanf(β€œ%d”,&n); printf(β€œn Enter the values one by one :”); for(i=0;i<n;i++) scanf(β€œ%d”,&a[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf(β€œn The values in ascending order :”); for(i=0;i<n;i++) printf(β€œ%d”,a[i]); getch(); } 60. Write a program to read n values and sort it in descending order. Solution : #include<stdio.h> main() { int a[20],t,i,j,n; clrscr(); printf(β€œn Give the no. of values :”); scanf(β€œ%d”,&n); printf(β€œn Enter the values one by one :”); for(i=0;i<n;i++) scanf(β€œ%d”,&a[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]<a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf(β€œn The values in descending order :”); - 41 -
  • 42. S. MADHU MOHAN C - Language for(i=0;i<n;i++) printf(β€œ%d”,a[i]); getch(); } Double (two) Dimensional Array : So far we have discussed the variables that can store a list of values. If we want to store a table of values i.e., in the way of rows and columns, we can use the two dimensional arrays. The declaration of a two dimensional array is storage class. The syntax is Data type array name[row size][column size] Example : int a[3][3]; float x[3][4]; Here the storage class is optional. And the data type specifies the type of the data and the array name indicates the name of the array and the row size indicates the maximum number of rows in that array and the column size indicates the maximum number of columns in that array. 61. Write a program to read a(3x3) matrix and display it in a matrix format. Solution : #include<stdio.h> main() { int a[3][3],i,j; clrscr(); printf(β€œn Enter the values of 3x3 matrix :”); for(i=0;i<3,i++) { for(j=0;j<3,j++) scanf(β€œ%d”,a[i][j]); } Printf(β€œn The array in matrix from :”); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(β€œ%4d”,a[i][j]); printf(β€œn”); } getch(); } 62. Write a program to read two 3x3 matrices. And them and store the result in to the third matrix and display it. Solution : #include<stdio.h> main() { - 42 -
  • 43. S. MADHU MOHAN C - Language int a[3][3],b[3][3],c[3][3],i,j; clrscr(); printf(β€œn Enter elements of A :”); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf(β€œ%d”,&a[i][j]); } printf(β€œn Enter the elements of B :”); for(i=0;i<5;i++) { for(j=0;j<3;j++) scanf(β€œ%d”,&b[i][j]); } for(i=0;i<3;i++) { for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; } { for(j=0;j<3;j++) printf(β€œ%4d”,c[i][j]); printf(β€œn”); } getch(); } 63. Write a program to read a 3x3 matrix, find the biggest element and display it. Solution : #include<stdio.h> main() { int a[3][3],big=0,i,j; clrscr(); printf(β€œn Enter elements of A :”); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf(β€œ%d”,&a[i][j]); } for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(a[i][j]>big) big=a[i][j]; } } printf(β€œn The biggest value is :%d”,big); - 43 -
  • 44. S. MADHU MOHAN C - Language getch(); } 64. Write a program to read a 3x3 matrix and find the smallest element in it. Solution : #include<stdio.h> main() { int a[3][3],small=0,i,j; clrscr(); printf(β€œn Enter elements of A :”); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf(β€œ%d”,&a[i][j]); } small=a[0][0]; for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(a[i][j]>small) small=a[i][j]; } } printf(β€œn The smallest value is :%d”,big); getch(); } 65. Write a program to read a 3x4 matrix and display the transpose of that matrix. Solution : #include<stdio.h> main() { int a[3][4],i,j; clrscr(); printf(β€œn Enter values of A :”); for(i=0;i<3;i++) { for(j=0;j<4;j++) scanf(β€œ%d”,&a[i][j]); } printf(β€œn The transpose of the matrix :”); for(i=0;i<4;i++) { for(j=0;j<3;j++) printf(β€œ%4d”,a[i][j]); printf(β€œn”); } getch(); - 44 -
  • 45. S. MADHU MOHAN C - Language } 66. Write a program to read a 3x3 matrix and calculate the sum of the diagonal elements and display it. Solution : #include<stdio.h> main() { int a[3][3],sum=0,i,j; clrscr(); printf(β€œn Enter elements of A :”); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf(β€œ%d”,&a[i][j]); } for(i=0;i<5;i++) { for(j=0;j<3;j++) { if(i= =j) sum=sum+a[i][j]; } } printf(β€œn The sum of diagonal elements :%d”,big); getch(); } 67. Write a program to read a 3x3 matrix and find the biggest element and display the positions where ever it is. Solution : #include<stdio.h> main() { int a[3][3],big=0,i,j; clrscr(); printf(β€œn Enter the values of matrix :”); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf(β€œ%d”,&a[i][j]); } for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(a[i][j]>big) big=a[i][j]; } - 45 -
  • 46. S. MADHU MOHAN C - Language } printf(β€œn The biggest value is :%d”,big); for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(a[i][j]= = big) printf(β€œn Row :%d, Coulmn :%d”, i,j); } } getch(); } 68. Write a program to read a 3x3 matrix and find the smallest element and display the position where ever it is. Solution : #include<stdio.h> main() { int a[3][3],small,i,j; clrscr(); printf(β€œn Enter the values of matrix :”); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf(β€œ%d”,&a[i][j]); } small=a[o][o]; for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(a[i][j]>small) small=a[i][j]; } } printf(β€œn The smallest value is :%d”,big); for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(a[i][j]= = small) printf(β€œn Row :%d, Column :%d”,i,j); } } getch(); } - 46 -
  • 47. S. MADHU MOHAN C - Language 69. Write a program to read a 3x3 matrix and display the position of a given element if formed. If the element is not there in the array; just display the message β€œthe given element is not in the array”. Solution : #include<stdio.h> main() { int a[3][3],p=0,i,j,x; clrscr(); printf(β€œn Enter the values of matrix :”); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf(β€œ%d”,&a[i][j]); } printf(β€œn Enter a values to find the position :”); scanf(β€œ%d”, &x); for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(a[i][j]= =x) { printf(β€œn Row :%d, Column :%d”,i,j); p=1; } } } if(p= =0) printf(β€œn The given number is not in the array”); getch(); } 70. Write a program to read two matrices and display them and store the result in 3rd array matrix and display it. Solution : #include<stdio.h> main() { int a[3][3],b[3][3],c[3][3],i,j,k; clrscr(); printf(β€œn Enter elements of matrix A :”); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf(β€œ%d”,&a[i][j]); } printf(β€œn Enter the elements of matrix B :”); for(i=0;i<3;i++) - 47 -
  • 48. S. MADHU MOHAN C - Language { for(j=0;j<3;j++) scanf(β€œ%d”,&b[i][j]); } for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) } } printf(β€œn The product of two matrices :”); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(β€œ%4d”,c[i][j]); printf(β€œn”); } getch(); } 71. Write a program to read two different sizes of matrices and if the multiplication is possible, multiply them and display the result. If not possible display the message β€œmultiplication is not possible for the above 2 matrices”. Solution : #include<stdio.h> main() { int a[5][5],b[5][5],c[5][5],i,j,k,p,q,m,n; clrscr(); printf(β€œn Enter the rows and columns of first matrix :”); scanf(β€œ%d%d”,&p,&q); printf(β€œn Enter the rows and columns of second matrix :”); scanf(β€œ%d%d”, &m,&n); if(q= =m) printf(β€œn Enter the value of pxq matrix :”); { for(i=0;i<p;i++) { for(j=0;j<q;j++) scanf(β€œ%d”,&a[i][j]); } printf(β€œn Enter a values of mxn matrix :”); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf(β€œ%d”,&b[i][j]); } - 48 -
  • 49. S. MADHU MOHAN C - Language for(i=0;i<p;i++) { for(j=0;j<q;j++) { c[i][j]=0; for(k=0;k<m;k++) c[i][j]= c[i][j]+a[i][k]*b[k][j]; } } printf(β€œn The product of two matrices :”); for(i=0;i<p;i++) { for(j=0;j<n;j++) printf(β€œ%4d”,c[i][j]); printf(β€œn”); } } else printf(β€œn The multiplication is not possible”); getch(); } STRING HANDLING FUNCTIONS β€˜C’ library supports a large number of string handling functions that can be used to carry out many of the string manipulations. The following are the most commonly used string handling functions. They are 1. strcat() 2. strcmp() 3. strcpy() 4. strlen() 1. strcat( ) : This function joins two strings together. The syntax is strcat (sring1, string2) Here the string1 and string2 are the character arrays. When the function strcat() is executed. The string 2 is appended to string 1. It removes the null character at the end of the string 1 and insert 2 from there itself. The string 2 will remain unchanged. 2. strcmp( ) : This function compares two strings and identify the arguments and has a value β€˜0’. If they are equal. If they are not equal, it will take the numeric difference between the first non-matching characters in the string. The syntax is strcmp(string1, string2) Here the string 1 and string 2 are string variables. - 49 -
  • 50. S. MADHU MOHAN C - Language 3. strcpy( ) : This function works almost like a string assignment operator. The syntax is strcpy (string1, string2) Here string 1 and string 2 are string variables or string constants. The value of string 2 is assigned to string 1. 4. strlen( ) : This function counts and returns the no. of characters in a string. The syntax is n=strlen(string 1) Here β€˜n’ is an integer variable which receives the value of the length of a string. The counting ends as the first null character occurs. 72. Write a program to read an upper case character and convert into lower case character. Solution : #include<stdio.h> #include<string.h> main() { char a; clrscr(); printf(β€œn Enter an upper case letter :”); a=getchar(); getch(); putchar(to lower(a)); } 73. Write a program to read a lower case character and convert into upper case character. Solution : #include<stdio.h> #include<string.h> main() { char a; clrscr(); printf(β€œn Enter an lower case letter :”); a=getchar(a); putchar(to upper(a)); getch(); } 74. Write a program to read β€˜n’ strings (names) and sort it in alphabetical order. Solution : #include<stdio.h> #include<string.h> main() { char name[30][15],t[15]; int i,j,n; - 50 -
  • 51. S. MADHU MOHAN C - Language clrscr(); printf(β€œn Enter the number of strings :”); scanf(β€œ%d”,&n); printf(β€œn Enter the strings one by one :”); for(i=0;i<n;i++) scanf(β€œ%s”,name[i]) { for(j=i+1;j<n;j++) { if(strcmp(name[i],name[j]>0) { strcpy(t,name[i]); strcpy(name[i],name[j]); strcpy(name[j],t); } } } Printf(β€œn The names in Alphabetical Order :”); for(i=0;i<n;i++) printf(β€œ%s”,name[i]); getch(); } 75. Write a program to read β€˜n’ strings and sort it in alphabetical reverse order. Solution : #include<stdio.h> #include<string.h> main() { char name[30][15],t[15]; int i,j,n; clrscr(); printf(β€œn Enter the number of strings :”); scanf(β€œ%d”,&n); printf(β€œn Enter the strings one by one :”); for(i=0;i<n;i++) scanf(β€œ%s”,name[i]) for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(strcmp(name[i],name[j]<0) { strcpy(t,name[i]); strcpy(name[i],name[j]); strcpy(name[j],t); } } } Printf(β€œn The names in Alphabetical Reverse Order:”); - 51 -
  • 52. S. MADHU MOHAN C - Language for(i=0;i<n;i++) printf(β€œ%s”,name[i]); getch(); } 76. Write a program to read a line of text in upper case and print it in lower case. Solution : #include<stdio.h> #include<string.h> main() { char lind[80]; int i=0; clrscr(); printf(β€œn Enter a text in upper case :”); scanf(β€œ%[^n]”,line); printf(β€œn The upper case line convert into lower case :”); while(line[i]!= β€˜Ο†β€™) { printf(β€œ%c”, to lower (line[i])); i++; } getch(); } 77. Write a program to read a line of text in lower case and print it in upper case. Solution : #include<stdio.h> #include<string.h> main() { char lind[80]; int i=0; clrscr(); printf(β€œn Enter a text in upper case :”); scanf(β€œ%[^n]”,line); printf(β€œn The lower case line convert into upper case :”); while(line[i]!= β€˜Ο†β€™) { printf(β€œ%c”, to upper (line[i])); i++; } getch(); } 78. Write a program to read a line of text and count the no. of vowels, no. of consonants, digits, spaces and other characters in that line. Solution : Solution : #include<stdio.h> - 52 -
  • 53. S. MADHU MOHAN C - Language #include<string.h> main() { char lind[80],c; int i,vow,con,dig,spa,oth; clrscr(); printf(β€œn Enter a line of text :”); scanf(β€œ%[^n]”,line); i=vow=con=dig=spa=oth=0; while((c= to lower(line[i]))!= β€˜Ο†β€™) { if(c= = β€˜a’||c= = β€˜e’||c= = β€˜i’ ||c= = β€˜o’ || c= = β€˜u’) vow++; else if(c>= β€˜a’ && c<= β€˜z’) con++; else if(c>= β€˜0’ && c<=’9’) dig++; else if(c= = β€˜ ’ || c= = β€˜t’) spa++; else oth++ i++ } printf(β€œn Total no. of Characters :%d”, i); printf(β€œn Total no. of Vowels :%d”, vow); printf(β€œn Total no. of Consonants :%d”, con); printf(β€œn Total no. of Digits :%d”, dig); printf(β€œn Total no. of Spaces :%d”, spa); printf(β€œn Total no. of other Characters :%d”, oth); getch(); } FUNCITONS In computer programming it is a good practice to break down a big module (program) into small modules. Each module may be developed separately and compiled separately. In this method it is easy to locate and debug (rectify) the errors. In the β€˜C’ language we can write the functions to avoid the same code (statements) again an again. A function is a self-contained subprogram which performs some specific well- defined task. A β€˜C’ program (has only one function) may consist of one or more functions. If a program has only one function that must be the main() function. In β€˜C’ language here are two types of functions. They are 1. Library functions (or) Built – in functions. 2. User defined functions. - 53 -
  • 54. S. MADHU MOHAN C - Language 1. Library Functions : β€˜C’ has a facility to provide some library functions to the programmer (user) for doing some operations. For example β€˜C’ has a mathematical function which is used for finding the square root of a number i.e. β€œsqrt”. Whenever it is required in our program we can use the library functions. 2. User defined Functions : If the user is going to define any type of functions for the sake of problem solving and that type of functions are called user-defined functions. Advantages : 1. It allows the division of a large program into small programs (modules). 2. Small programs are easy to understand and easy to debug the error. 3. Small programs are easy to understand and easy to debug the errors. 4. Small programs are generally self documented and highly readable. Function declaration (or) Function Proto type : One of the most important feature of β€˜C’ is the function declaration. The function declaration tells the computer, the type of the data returned by the function, the no. of arguments that the function expect to receive and the order of the arguments. The syntax data type function name(arg1, arg2, ……..arg n) Here the data type specifies the type of the data returned by the function and the function name indicates the name of the function and arg 1, arg 2, ….. are the type of the arguments (variables). Function Definition : A function is defined once in a program and can be called number of times. A function can receive many values but returns a single value. The syntax is data type specifier function name (arg 1, arg 2, ……. Arg n) { body of the function; ------- ------- ------- Return (something) } Here the datayic specifier specifies the value that the function will return using β€œreturn” statement. The function name indicates the name of the function and the function name precedes a set of parenthesis. Within the parenthesis the argument can be specifies. If the arguments are not there, we can specify the empty parenthesis only. Return Statement : - 54 -
  • 55. S. MADHU MOHAN C - Language We have two types of usages with a return statement. They are 1. It causes an immediate exit from a function. 2. It can be used to return a value. The syntax is Return; or return (value); or return (expression); Sometime a function does not return any value. In such cases its data type becomes β€œvoid”. The syntax void function name (………); 79. Write a program to display your address using functions. Solution : #include<stdio.h> main() { void display(); /* function declaration */ clrscr(); display (); /* function calling */ } void display() { printf(β€œn Name : S.MADHU MOHAN”); printf(β€œn Qualification : MBA., PGDSE & DHE”); printf(β€œn Village : NANDIKOTKUR”); } 80. Write a program to read any 2 values and find the biggest one using functions. Solution : #include<stdio.h> main() { int x,y; int big(); clrscr(); printf(β€œn Enter any two numbers :”); scanf(β€œ%d”, &x); scanf(β€œ%d”, &y); printf(β€œn The biggest value :%d”, big(x,y)); } int big(int a, int b) { if(a>b) return (a); else return (b); } 81. Write a program to read 3 values and find the biggest one using functions. Solution : #include<stdio.h> - 55 -
  • 56. S. MADHU MOHAN C - Language main() { int x,y,z,p; int big(); clrscr(); printf(β€œn Enter any three numbers :”); scanf(β€œ%d”, &x); scanf(β€œ%d”, &y); scanf(β€œ%d”, &z); p=big(x,y); printf(β€œn The biggest value :%d”, big(p,z)); } int big(int a, int b) { if(a>b) return (a); else return (b); } 82. Write a program to read a number and find the factorial value using functions. Solution : #include<stdio.h> main() { int n; int fact(); clrscr(); printf(β€œn Enter n value :”); scanf(β€œ%d”,&n); printf(β€œn The Factorial value :%d”,fact(n)); } int fact(int n) { int I,p=1; if(n>1) { for(i=2;i<=n;i++) p=p*i; } return(p); } 83. Write a program to read the radius of a circle and calculate area and circumference using functions. Solution : #include<stdio.h> main() { float r; - 56 -
  • 57. S. MADHU MOHAN C - Language void ac(float r); clrscr(); printf(β€œn Enter the Radius :”); scanf(β€œ%f”,&r); ac(r); } void ac(float r) { float area, cir; area=3.14*r*r; cir=2*3.14*r; printf(β€œn Area :%f”, area); printf(β€œn Circumference :%f”, cir); } 84. Write a program to read principle, time, rate. And calculate interest, display it. Solution : #include<stdio.h> main() { float p,t,r,s; float si(); or float si(float p, float t, float r); clrscr(); printf(β€œn Enter principle, time and rate :”); scanf(β€œ%f%f%f”,&p,&t,&r); s=si9p,t,r); printf(β€œn Simple Interest :%f”, s); } float si(float p, float t, float r) a=(p*t*r)/100; { return (a); s=float a; } 85. Write a program to calculate the Binomial coeff value. Solution : #include<stdio.h> main() { int n,r,b,p; int bino(); clrscr(); printf(β€œn Enter the values of n and r:”); scanf(β€œ%d%d”,&n,&r); p=n-r; b=bino(n)/(bino cr)*bino(p)); printf(β€œn Binomial Coeff value :%d”, b); } int bino (int n) { int i,x=1; if(n>1) { - 57 -
  • 58. S. MADHU MOHAN C - Language for(i=2;i<=n;i++) x=x+i; } return (x); } 86. Write a program to read any two values and swap them using functions. Solution : #incldue<stdio.h> main() { int a,b; int swap(); clrscr(); printf(β€œEnter a and b values :”); scanf(β€œ%d%d”,&a,&b); swap (a,b); { int swap(int a, int b) { int t; t=a; a=b; b=t; printf(β€œn A value after swap :%d”, a); printf(β€œn B value after swap :%d”, b); } 87. Write a program to evaluate ex using function. Solution : #include<stdio.h> main() { float s; float expp(float s); clrscr(); printf(β€œn Enter S value :”); scanf(β€œ%f”, &s); printf(β€œn The Expression Value :%f”,expp(s)); } float expp(float x) { int n; float t; t=1; s=1; n=0; while(t>0.00001) { n=n+1; - 58 -
  • 59. S. MADHU MOHAN C - Language t=t*x/n; s=s+t; } return s; } 88. Write a program to evaluate the sin x value up to 4 significant digits using function. Solution : #inlcude<stdio.h> main() { float s; float sinn(float s); clrscr(); printf(β€œn Enter S value :”); scanf(β€œ%f”,&s); printf(β€œn The sin x value :%f”,sinn(x)); } float sinn(float x) { float s,t,n; x=(x*3.14/180); t=x; s=x; n=1; while (abs(t)>0.00001) { n=n+2; t=(-t)*x*x/(n*(n-1)); s=s+t; return s; } } 89. Write a program to evaluate cos x value using functions. Solution : #inlcude<stdio.h> main() { float s; float coss(float s); clrscr(); printf(β€œn Enter S value :”); scanf(β€œ%f”,&s); printf(β€œn The cos x value :%f”,sinn(x)); } float coss(float x) { float s,t,n; x=x*3.14/180; t=1; s=1; - 59 -
  • 60. S. MADHU MOHAN C - Language n=0; while (abs(t)>0.00001) { n=n+2; t=(-t)*x*x/(n*(n-1)); s=s+t; } return s; } Functions with Arrays : 90. Write a program to read n values and find the biggest one using functions. Solution : #include<stdio.h> main() { int a[100],n,i; int big(int a[], int n); clrscr(); printf(β€œn Enter no. of values :”); scanf(β€œ%d”,&n); printf(β€œn The biggest value :%d”, big(a,n)); } int big(int a[], int n) { int b=0,i; for(i=0;i<n;i++) { if(a[i]>b) b=a[i]; } return b; } 91. Write a program to read n values and find the smallest one using functions. Solution : #include<stdio.h> main() { int a[100],n,i; int small(int a[], int n); clrscr(); printf(β€œn Enter no. of values :”); scanf(β€œ%d”,&n); (printf(β€œn Enter values value :”);) for(i=0;i<n;i++) scanf(β€œ%d”, &a[i]); printf(β€œn The smallest value :%d”, small(a,n)); } int small(int a[], int n) { - 60 -
  • 61. S. MADHU MOHAN C - Language int s=a[0],i; for(i=0;i<n;i++) { if(a[i]<s) s=a[i]; } return s; } 92. Write a program to read n values and sort it in ascending order using functions. Solution : #include<stdio.h> main() { int a[100],i,n; int sort(inta[], int n); clrscr(); printf(β€œn Enter the no. of values :”); scanf(β€œ%d”,&n); printf(β€œn Enter values one by one :”); for(i=0;i<n;i++) scanf(β€œ%d”,&a[i]); sort(a,n); printf(β€œn The values in Ascending Order :”); fir(i=0;i<n;i++) printf(β€œ%d”,a[i]); } int sort (int a[], int n) { int i,j,t; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } Return (sort); } 93. Write a program to read n values sort it in descending order using functions. Solution : #include<stdio.h> main() { - 61 -
  • 62. S. MADHU MOHAN C - Language int a[100],i,n; int sort(inta[], int n); clrscr(); printf(β€œn Enter the no. of values :”); scanf(β€œ%d”,&n); printf(β€œn Enter values one by one :”); for(i=0;i<n;i++) scanf(β€œ%d”,&a[i]); sort(a,n); printf(β€œn The values in Descending Order :”); fir(i=0;i<n;i++) printf(β€œ%d”,a[i]); } int sort (int a[], int n) { int i,j,t; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]<a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } Return (sort); } 94. Write a program to read a names and sort it in alphabetical order using functions. Solution : #include<stdio.h> #include<string.h> main() { char name[30][10]; int i,n; char sort (char name[][10], int n); clrscr(); printf(β€œn Enter the no. of names :”); scanf(β€œ%d”,&n); printf(β€œn Enter names one by one :”); for(i=0;i<n;i++) scanf(β€œ%s”,name[i]); sort(name.n); printf(β€œn The sorted array names :”); - 62 -
  • 63. S. MADHU MOHAN C - Language fir(i=0;i<n;i++) printf(β€œ%s”,name[i]); } char sort (char name[][10], int n) { int i,j; char t[10]; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(strcmp(name[i],name[j])>0) { strcpy (t,name[i]); strcpy(name[i], name[j]); strcpy(name[j],t); } } } Return (sort); } 95. Write a program to read n strings and sort it in alphabetical reverse order. Solution : #include<stdio.h> #include<string.h> main() { char name[30][10]; int i,n; char sort (char name[][10], int n); clrscr(); printf(β€œn Enter the no. of names :”); scanf(β€œ%d”,&n); printf(β€œn Enter names one by one :”); for(i=0;i<n;i++) scanf(β€œ%s”,name[i]); sort(name.n); printf(β€œn The sorted array name :”); fir(i=0;i<n;i++) printf(β€œ%s”,name[i]); } char sort (char name[][10], int n) { int i,j; char t[10]; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { - 63 -
  • 64. S. MADHU MOHAN C - Language if(strcmp(name[i],name[j])<0) { strcpy (t,name[i]); strcpy(name[i], name[j]); strcpy(name[j],t); } } } Return (sort); } 96. Write a program to read n values and find the variance and standard deviation. Solution : #include<stdio.h> #inlcude<math.h> main() { int n,i,a[50]; float sd(int a[],int n); clrscr(); printf(β€œn Enter the no. of values :”); scanf(β€œ%d”,&n); printf(β€œn Enter the values one by one :”); for(i=0;i<n;i++) scanf(β€œ%d”,&aa[i]); printf(β€œn The standard deviation :%f”,sd(a,n)); } float sd(int a[], int n) { float vari, avg, dev, sumsq=0; int i,0; float mean(int a[], int n); avg=mean(a,n); for(i=0;i<n;i++) { dev=avg-a[i] sumsq=sumsq+dev*dev; } vari=(sumsq)/n; return (vari); } float mean(int a[], int n) { int i, sum=0; for(i=0;i<n;i++) sum=sum+a[i]; return (sum n); } RECCURSION : - 64 -
  • 65. S. MADHU MOHAN C - Language Recursion is a technique where a function calls by it self until a specific condition has been met. 97. Write a program to read a number and find the factorial value using recursion. Solution : #include<stdio.h> main() { int n; int fact(int n); clrscr(); printf(β€œn Enter n value :”); scanf(β€œ%d”, &n); printf(β€œn The Factorial value :%d”, fact(n)); } int fact(int n) { if(n<=1) return (1); else return(n*fact(n-1)); } 98. Write a program to evaluate pronominal coeff value using recursion. Solution : #include<stdio.h> main() { int n,r,b,p; int bino(); clrscr(); printf(β€œn Enter the values of n and r :”); scanf(β€œ%d%d”, &n,&r); p=n-r; b=bino(n)/bino(r)*bino(p); printf(β€œn The binomial coeff value :%d”, b); } int bino(int n) { if(n<=1) return (1); else return(n*bino(n-1)); } 99. Write a program to display the Fibonacci series of numbers on basis of user’s choice using recursion. Solution : #include<stdio.h> main() - 65 -
  • 66. S. MADHU MOHAN C - Language { int n,f1,f2; int fibo(int n, int f1, int f2); clrscr(); printf(β€œn Enter the no. of Fibonacci to display :”); scanf(β€œ%d”, &n); fibo(n,o,1); } int fibo(int n, int f1, int f2) { int t; if(n>=1) { printf(β€œ%d”,f1); t=f2; f2=f1+f2 f1=t; fibo(n-1,f1,f2) } } 100. Write a program to evaluate the well known game the towers of Hanoi. Solution : #include<stdio.h> main() { int n, char sour= β€˜l’, ini= β€˜c’, dest= β€˜r’; void Hanoi(); clrscr(); printf(β€œn Enter the no. of dies :”); scanf(β€œ%d”,&n); hanoi(n,sour,ini,dest); } void main(int n, char sour, char ini, char dest) { if(n>0) { hanoi(n-1, ini, dest, sour); printf(β€œn Moves of the disk from %c to %d”, sour, dest); hanoi(n-1, ini, sour,dest); } Return; } Types of user defined functions : There are three types of user defined functions in β€˜C’. They are 1. Functions with no arguments and no return values. 2. Functions with arguments and no return values. - 66 -
  • 67. S. MADHU MOHAN C - Language 3. Functions with arguments and with return values. 1. Functions with no arguments and no return values ; When we are calling a function, if we are not sending any kind of arguments to the called function and also the called function is not returning any arguments to the calling function. 101. Write a program to display address. Solution : #include<stdio.h> main() { void display(); clrscr(); display(); } void display() { printf(β€œn Name : S. MADHU MOHAN”); printf(β€œn Address : NANDIKOTKUR”); } 2. Functions with arguments and no return values : When we are calling a function, at the time of calling we are sending some arguments to the called function. After the execution of the called function, it won’t return any values to the calling function. 102. Write a program to read radium and find area, circumference. Solution : #include<stdio.h> main() { float r; void ac(); clrscr(); printf(β€œn Enter Radius :”); scanf(β€œ%f”,&r); ac(r); } void ac(float r) { float are, cir; area=3.1416*r*r; cir=2*3.1416*r printf(β€œn Area :%f”, area); printf(β€œn Circumference :%f”,cir); } 4. Functions with arguments and with return values : - 67 -
  • 68. S. MADHU MOHAN C - Language When we are calling a function and at the time of calling we are sending some arguments to the calling function. After the execution of the calling function, it returns a value to the calling function. In this method the data communication is in both ways. 103. Write a program to read any two values, find the biggest value. Solution : #include<stdio.h. main() { int x.,y; int big; clrscr(); printf(β€œn Enter any two values :”); scanf(β€œ%d%d”, &x,&y); printf(β€œn The biggest value :%d”, big(x,y)); } int big(int a, int b) { if(a>b) return (a); else return (b); } STORAGE CLASSES They are four types of storage classes in β€˜C’ language. They are 1. Automatic Storage Class (auto) 2. Static Storage Class (static) 3. External Storage Class (extern) 4. Register Storage Class (register) Automatic Storage Class (auto) : The variable which we have define by using β€œauto” storage class, these variables are stored in the computers memory. The default value of an auto variable is unpredictable. Its scope is local to the block in which they are defined. The life the auto variable is within the block till the control remains. If we wont specify any storage class, by default the computer will assume as auto only. Example : #include<stdio.h> main() { void increment(); clrscr(); increment(); - 68 -
  • 69. S. MADHU MOHAN C - Language increment(); increment(); } void increment(); { auto int i=1; printf(β€œ%d”,i); i=i+1; } 2. Static Storage Class (static) : The variable which we have defined by using static storage class, it is stored in the computer memory. The default value of a static variable is assumed to be zero. The scope is local to the block in which the variable is defined. The life of a static variable is global, i.e. the values of a variable persist between different functions calls. Example : #include<stdio.h> main() { void increment(); clrscr(); increment(); increment(); increment(); } void increment(); { auto int i=1; printf(β€œ%d”,i); i=i+1; } The difference between auto and static is that, the static variable do not disappear when the function is no longer active and their value persists. If the control comes back to the same function again the static variables have the values, they had last time around. In case of an auto variable, each time the function is called, the value will be reinitialized to the variable. If you call the function for 10 times, the same value will be initialized time. 3. External Storage Class (extern) : The variable which we define by using external storage class, that variables are stored in computer memory. The default value of an external variable is zero and scope is global. The life of the external variable is as long as the program is executing their values will be retained the program. Syntax : #include<stdio.h> #incldue<stdio.h> main() main() - 69 -
  • 70. S. MADHU MOHAN C - Language { { state -1; (or) extern int I; state -2; state -1; ------- ------- ------- ------- } } 4. Register Storage Class (register) : The variable which we define by using register storage class, the values of the variables will be stored in cpu register. The default value of the variable is unpredictable. It’s scope is local to the block in which the variables are defined. Its life is till the control remains within the block. A value is stored in the cpu registers can always be accessed faster than the one which is stored in the computers memory. However the computer is consisting of limited no. of cpu registers. So we cannot declare each and every variable as register data type. Syntax : #include<stdio.h> main() { register int i,j,k; state -1; state -2; ---------- ---------- } POINTERS A pointer is a variable that points the address of the another variable. Example : int i=3; This declaration tells to the β€˜C’ compiler to 1. Reverse the space in memory to store the value. 2. Associate the name i with this memory location. 3. Store the value 3 at this location. We may represent the location in the memory by the following memory map. i = Associate variable [3] = Actual value Declaring a pointer variable : To declare and refer a pointer type variable, β€˜C’ provides two special unary operators β€œ&” and β€œ*”. A pointer variable can be declared in the same way as the other variable, but an β€œ*” (asterisk) symbol should proceed the variable name. the syntax is - 70 -
  • 71. S. MADHU MOHAN C - Language Syntax : data type * variable; Example : int *a; flaot *x,*y; etc Here is a pointer variable pointing to a integer type address. The address Operators (&) : The address of any variable can be got proceeds the β€œ&” with a variable name. Example : int *a; int x,n; a=&n; Here β€˜n’ is a variable and &n is the address in which the value of β€˜n’ stored. Pointer Multiplications : The no. of bytes occupied by a particular data type is called the scale factor. For that we can use β€œsize of” operator in β€˜C’ language. Example : If β€˜n’ is an integer type variable. The no. of bytes occupied by β€˜n’ can be done using size of (n); The length of variable data types are 1. char ….. 1 byte 2. int ….. 2 bytes 3. float ….. 4 bytes 4. long int ….. 4 bytes 5. double ….. 8 bytes etc. We can use the arithmetic operators like β€˜+’ and β€˜-’, relational operators β€˜<’, β€˜>’, β€˜= =’, β€˜!=’ and unary operators like β€˜++’, β€˜- -’ in the pointers. Example : p1+p2, p1-p2,p1= = p2,p1!=p2,p1<p2 are all valid expressions. P1*p2, p1/p2 are invalid expressions. Consider a pointer expression. P1=p1+1; where p1 is an integer pointer with the initial address 6400. After the execution of the statement (p1=p1+1), the value of p1 will not be 6401, it will be 6402. Because the scale factor of an integer is 2 bytes. 104 . Write a program to read a variable from the keyboard, display the address. Solution : #include<stdio.h> main() { int a; clrscr(); printf(β€œn Enter a number :”); scanf(β€œ%d”.&a); printf(β€œn The address of the a is :%d”, &a); getch(); } - 71 -
  • 72. S. MADHU MOHAN C - Language 105. Write a program to accept a number and store the address into another variable and display it. Solution : #include<stdio.h> main() { int a; int *p; clrscr(); printf(β€œn Enter a number :”); scanf(β€œ%d”,&a); printf(β€œn The address of the a is :%u”, &p); printf(β€œn The original value :%d”,*p); printf(β€œn A value :%d”, *(&a)); getch(); } 106. Write a program to read any 2 values and swap them using pointers. Solution : #include<stdio.h> main() { int a,b; void swap(); clrscr(); printf(β€œn Enter a,b values :”); scanf(β€œ%d%d”,&a,&b); swap(&a,&b); printf(β€œn After swapping in main program :%d”, a); printf(β€œn After swapping in main program :%d”, b); } void swap(int *x, int *y) { int t; t=*x; *x=*y; *y=t; printf(β€œn In the function a :%d”, *x); printf(β€œn In the function b”%d”, *y); } 107. Write a program to read any values and find the biggest one using pointers. Solution : #include<stdio.h> main() { int a,b; int big(); clrscr(); printf(β€œn Enter a,b values :”); scanf(β€œ%d%d”,&a,&b); printf(β€œn The biggest value is :%d”, big(&a,&b)); } - 72 -
  • 73. S. MADHU MOHAN C - Language int big(int *x, int *y) { if(*x>*y) return (*x); else return (*y); } 108. Write a program to read any 3 values and find the biggest one using functions. Solution : #include<stdio.h> main() { int a,b,c; int big(); clrscr(); printf(β€œn Enter a,b,c values :”); scanf(β€œ%d%d%d”, &a,&b,&c); printf(β€œn The biggest values :%d”, big(&a,&b,&c)); } int big(int *x, int *y, int *z) { if(*x>*y && *x>*z) return (*x); else if(*y>*z); return(*y); else return (*z); } 109. Write a program to read any 3 values and find the smallest one using functions. Solution : #include<stdio.h> main() { int a,b,c; int small(); clrscr(); printf(β€œn Enter a,b,c values :”); scanf(β€œ%d%d%d”, &a,&b,&c); printf(β€œn The smallest values :%d”, small(&a,&b,&c)); } int small(int *x, int *y, int *z) { if(*x<*y && *x<*z) return (*x); else if(*y<*z); return(*y); - 73 -
  • 74. S. MADHU MOHAN C - Language else return (*z); } 110. Write a program to read a no as find the factorial value. Solution : #include<stdio.h> main() { int a; int fact(); clrscr(); printf(β€œn Enter n value :”); scanf(β€œ%d”,&a); printf(β€œn The Factorial value :%d”, fact(&a)); } int fact (int *n) { int I,f=1; for(i=2;i<=*n;i++) f=f*i; return (f); } 111. Write a program to read a line of text and count the no. of vowels, consonants, digits, spaces and other characters using pointers. Solution : #include<stdio.h> #include<string.h> #inlcude<ctype.h> main() { char line[80]; int i,vow,con,dig,spa,oth; void line (char line[], int *pv, int *pc, int *pd, int *ps, int *po); clrscr(); printf(β€œn Enter a line of text :”); scanf(β€œ%[^n]”,line); i=vow=con=dig=spa=oth=0; sline(line, &vow, &con, &dig, &spa, &oth); printf(β€œn Total no. of Vowels :%d”, vow); printf(β€œn Total no. of Consonants :%d”, con); printf(β€œn Total no. of Digits :%d”, dig); printf(β€œn Total no. of Spaces :%d”, spa); printf(β€œn Total no. of other Characters :%d”, oth); } void line (char line[], int *pv, int *pc, int *pd, int *ps, int *po); { int i=0; char c; - 74 -
  • 75. S. MADHU MOHAN C - Language while((c= to lower(line[i]))!= β€˜Ο†β€™) { if(c= = β€˜a’||c= = β€˜e’||c= = β€˜i’ ||c= = β€˜o’ || c= = β€˜u’) ++*pv; else if(c>= β€˜a’ && c<= β€˜z’) ++ *pc; else if(c>= β€˜0’ && c<=’9’) ++ *pd; else if(c= = β€˜ ’ || c= =’t’) ++ *ps; else ++ *po i++; } } 112. Write a program to display the character in. Solution : #include<stdio.h> main() { char a; clrscr(); for(i=0;i<=15;i++) printf(β€œn %c”,2); getch(); } 113. Write a program to read a string (or) line, and count total no. of characters and words, display it. Solution : #include<stdio.h> #include<string.h> main() { char line[70]; int words=o,c; clrscr(); printf(β€œn Enter any text :”); flushall(); gets(line); for(c=0;line[c]!= β€˜0’;c++) { if(line[c]= = β€˜ ’) words=words+1; } printf(β€œn No. of Characters in the text :%d”, c); - 75 -
  • 76. S. MADHU MOHAN C - Language printf(β€œn No. of Words in the text :%d”, words+1); getch(); } 114. Write a program to read two words and display one word using functions. Solution : #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str1[]= β€œMADHU”; char str2[]= β€œMOHAN” clrscr(); strcat(str1, str2); (or) strcpy(str1, str2); puts(str1); getch(); }  S.MADHU MOHAN MBA.,PGDSE & DHE. H. No : 16-38F/1A; NANDIKOTKUR. Phone No : 09440289220 09032900170. - 76 -