This document provides an overview of the C programming language. It discusses that C was developed in the 1970s as a system programming language and is still widely used today. The document outlines key features of C including data types, operators, expressions, input/output functions, and the basic structure of a C program with function definitions and variable declarations. It also describes basic program elements in C like variables, constants, and data types in detail.
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 -