C Language Notes PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 99

GNANAJYOTHI DEGREE COLLEGE

NARASANNAPETA-SRIKAKULAM

B.Sc I YEAR II SEMESTER

C – Programming Language

Unit-I

Chapter – 1 Introduction to Algorithms and Programming Languages

Chapter – 2 Introduction to C

Unit – II

Chapter 3. Decision Control and Looping Statements

Chapter 4. Functions

Unit – III

Chapter 5. Arrays

Chapter 6. Strings

Unit – IV

Chapter 7. Pointers Chapter 8. Structure, Union and Enumerated Data Types

Unit – V

Chapter9. Files

Text Book: 1. E Balaguruswamy: Computing Fundamentals & C Programming – Tata MCGraw


– Hill, Second Reprint 2008, ISBN 978-0-07-066909-3. 2. “Computer Fundamentals and
Programming in C “ by REEMA THAREJA from OXFORD UNIVERSITY PRESS 3. C
Programming by Denni’s Riche.

Reference Book: 1. Ashok N Kamthane: Programming with ANSI and Turbo C, Pearson Edition
Publ, 2002 2. Henry Mullish & Huubert L. Cooper: The Sprit of C, Jaico Pub. House, 1996

Page 1

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Chapter-1
Introduction To Alagorithams And Programming Lanaguages
_________________________________________________________
Alagoritham: the typical menaing of an alagorotham is a formally defined procedure for
performing some calculation. If a procedure is formally defined, then it must be implemented
using some formal languages are known as programming languages. The alagoritham gives the
logoc of the program, that is step by step description of how to arrive at a solution.
In general terms, an alagoritham provides a blue print to writing a program to solve a
particular problem. It is considered to be an effective procedure for solving a problem in finate
number of steps.
Key features of algorithms:
An alagoritham has a finite number of steps and some steps may involve decision-making
and repetition. Boardly speaking, an alagoritham exhibits three key features, namely, sequence,
decision, and repetition
Sequence: sequence menas that each step of the alagoritham is in the specified order.

Q: an alagoritham to add two numbers

Step:1 input the firsty number as A


Step:2 input the firsty number as B
Step:3 set C= A + B
Step:4 print C
Step:4 print C

Decision: Decision statements are used when the outcome of the process depends on some
condition.
Q: an alagoritham to test the equality of two numbers

Step:1 input the firsty number as A


Step:2 input the firsty number as B
Step:3 if A = B
Then print “ equal”
else
Print Not Equal “
Step:4 end

Repetition : repetition, which involves executing one or more steps for a number of times, can
be implemented using constructs such as the while, do-while, for loop. These loops execute one
or more steps until some condition is true.
Q: an alagoritham to print the first 10 natural numbers.

Step:1 [ initilization ] set I=0, N= 10.


Step:2 repeat step while I<=N
Step:3 print “ I”
Step:4 end

Page 2

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Flow Charts
Flow Chart: a flow chart is a graphical symbolic representation of a process. It is basically used
to design and document virtually complex processors to help the viewers to visualize the logic of
the process. So that they can gain a better understanding of the process when designing a flow
chart , each step in the process is depicted by a diffrenet symbol and is associated with a short
decription

Start and end symbols: start and end symbols are also known as the terminal symbols and are
represented as a circules, ovals, or rounded rectangles .terminal symbols are always the first and
the last symbols in a flow chart.
Arrows: arrows depict the flow of control of the program. They illustrate the exact sequence in
which the instructions are executed.
Generic processing step: Generic processing step is also called as an activity is represented
using a rectangle .
Input and output symbols: Input and output symbols are represented using a parallelogram
and are used to get inputs from the users or display the results to them.
A conditional or decision symbol: A conditional or decision symbol is represendted using a
diamond. It is basically used to depict a Yes/No question.

Advantages:
 They are very good communication tools to explain the logic of a system to all
concerned.they are help to analyze the problem in a more effective manner.
 They are also used for program documentation .they are even more helpful in the case of
complex programs.
 They can be used to debug programs that have error(s). they help the programmers to
easily detect ,locate ,and remove mistakes in the program in a systematic manner.

Page 3

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Limitations:
 Drawing flowcharts is a laborious and a time-consuming activity. Just imagine the effort
required to draw a flw chart of a program having 50,000 statemetns in it!
 Many times , the flow chart of a complex program becomes complex asn clumsy.
 At a times, a little bit of alteration in the solution may require complete re-drawing of the
flow chart.

Type of Programming languages


(Or)
Generation of programming languages
We now know that programming languages are the primary tools for creating software.
To day, hundreds of programming languages are exists in the market, some more used than
others, and each claiming to be the best. However, back in the 1940s, when computers were
being developed, there was just one language – the machine language.

The concept of generations of programming languages (also known as levels) is closely


connected to the advances in technology that brought about the different generations of
computers.
The four generations of programming languages are

1. machine language,
2. assembly language,
3. high level language ( also known as third generation or 3GL)
4. very high level language ( also known as fourth generation or 4GL)

First generation: machine language


Programming of the first stored-program computer systems was performed in machine
language. This is the lowest level of programming language and also the only language that
computers understand. All the commands and data values are expressed using ‘1’s and ‘0’s .
Second generation: assembly language
The second-generation programming language include the assembley language. They are
symbolic programming languages. They are symbolic programming languages that use
syambolic notation to represent machine language instructions . these languages are closely
connected to the machine language and the internal architecture of the computer system on
which they are used.
Programmers written in assembly language require a translator, often known as an
assemblers to convert them into a machine language. This is the because the computer
understood only the language of 1’s and 0’s.
Third Generation: High Level Language
the third generations was introduced to make programming language more friendly.
Programmers are written in an English-like manner,making them more convenient to use and
giving the programmers more time to address a client problems. while some high level languages
were specifically designed to serve a specific purpose. Other languages were very flexible and
were considered to be general purpose languages. Most of the programmers preferred to use
general purpose high level languages such as BASIC,FORTRAN,PASCAL,COBOL,C++ or java.
Again translator is needed to translate the instructions written in high level languages into
computer executable machine language. Such translators are commonly known as interpreters

Page 4

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

and complers. Each high level language has many compilers, and there is one for each type of
computer
Fourth Generation: Very High Level Language
With each generation , programming languages have become easier to use and more
like natural languages. A typical example of a 4GL is the query language that allows a user to
request information from a database with precisely worded English like sentence. A query
language is used a databse from the user. For example, when working with structured query
language , the programmers jest need to remember a few rules of syntax and logic, but it is
much easier to learn than COBOL or C

Language processor

Language processor is special software it is used to convert the source code(high level)
into computer executable object code(machine language). Which is also known as interpreters
and compiler
Interpreters : interpreter is a special software which is used to convert soure code into object

code step by step process.

Compiler: compiler is special software which is used to covert source code into object code at a

time process. In this procees data will be saved to further use

Page 5

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Introduction to C

the programming languge C was deceloped by in the early 1970s by Dennis


Ritchie at Bell Laboratories to be used by the UNIX operating system. It was named ‘C’
because many of its features were derived from an earlier language called ‘B’

C is one of the most programming languages. It is being used on several different


software platforms. It is good idea to learn C because it has been around for a long time which
means there is a lot of information available on it. Quiet a few other programming languages
such as C++ and java are also based on C which means that one will be able to learn them more
easily in the future.

Like many other modern languages, C is derived from ALGOL (the first language
to use a block structure). Although ALGOL was not accepted widely in the united states but it
was widely used in Europe. ALGOL’s introduction in the 1960’s led the way for the development
of structured programming concepts.

Before C, several other programming languages were developed. For example , in


1967 Martin Richards developed a language BCPL( basic combined progarrmming language )
BCPL was basically a type-less(no data type concepts) language which facilitated the user with
direct access of memory. This is made it usefull for system programming. Then in 1970, ken
thomposn developed a language called B, B was used to develop the first version of UNIX. C
was developed by Dennis Ritche in 1972 that took concepts from ALGOL,BCPL, and B in
addition to the concepts of these languages, C also supports the concept of data types. Since
UNIX operating system the concept of data types. Since UNIX operating system was also
developed at Bell Laboratories along with the C language, C and UNIX are strongly associated
with each other.

Page 6

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Structure of a C program: a ‘C’ program is composed of preprocessor


commands, a global declarations section, and one or more functions
the preprocessor directives contain special instructions that include how to
prepare the program for compilation one of the most important and commonly used
preprocessor command is include which tells the compiler that to execute the program, some
more information is needed from the specified header file
a C program contains one or more functions, where a function is defined
as group of C statements that are executed together. The statements in a C program are written
in a logical sequence to perform a specific task. The main() function is most important function
and is a part of every C program. The execution of a C program begins at this function.

All functions are divided into two parts- the declaration section and the statement
section. The declaration section precedes the statements and is used to describe the data that will
be used in the function. Note that data declared within a function are known as local declaration
as that data will be visible only within that function. Stated in other terms, the life time of the
data will be only till the function ends. The statement section in a function contain the code that
manipulates the data to perform a specified task.

Page 7

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Files
used in
a C

program
Every C pogram has four kinds of files associated with it

S OHE F o x eib lu e ea j rec s u i t n a ab l e


d c c e e t r Cfi l e
Page 8

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Source code file: The source code file contain the source code of the program. The extension of
any C source code file is ‘.C’ this file contains C source code that defines the main function and
may be other functions. The main() is the starting point of execution when you successfully
compile and run the program. A C program in general may include even other source code files

Header files: when working with large projects, it is often desirable to separate out certain sub
routines from the main() of the program. There also may be a case that the same subroutine has
to be used in different programs. In the latter case

Standared header files in all our programs that we had been writing till now, we were using
many functions that were not written by us. For example, strcmp() function which compares
two strings. We do not know the details of how these functions work. Such functions that are
provided by all C compilers are included in standard header files

Object files: object files are generated by the compiler as a result of processing the source code
file. They contain compact binary code of the function definitions. Linker uses this object file to
produce an executable file (.exe file) by combining the object files together. Object files have a
‘.obj’ extension name

Executable file: The binary executable file is generated by the linker. The linker links the
various object files to produce a binary file that can be directly executed. On windows operating
system the excutable files have ‘.exe’ extension.

COMPILING AND EXECUTING ‘C’ PROGRAMS


C is compiled language. So once the C program is written you must run it through a C compiler
that can create an executable file to be run by the computer. While the C program is human-
readable, the executable file on the other hand, is a machine-readable file available in an
executable form
Page 9

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

every programming language has its own compiler


the compiler translate the source code into an object code. The object code contains the
manchine instructions for the CPU and calls to the operating system applications programming
interface(API).

Page 10

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Comment lines: many a time the meaning or the purpose of the program code is not clear to
the reader. Therefore. It is a good programming practice to place some comments in the code to
help the reader understand the code clearly
“ Comments are physically presented logically absented ”

C supports two types of Comments


 // is used to comment single statement. This is known as line comment.a line comment can be
placed anywhere on the line and it does now require to specifically ended as the end of the line.
 /* is used to comment multiple statements. It is state with /* end with */ and all statements
that lie within these characters are commented. This type of comment is known as block
statement.
C character set: Each and every Programming language have it’s own character set it is used to
define tokens.

Page 11

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Key words: like every computer language, C has a set of resevered words often known as
keywords that can’t be used as an identifier. All key words are basically a seqauence of
characters that have a fixed meaning. By convention, keywords must be written in lower case
(small) letters. In C language has a 32 keywords.

Identifiers: identifiers, as the name suggests helps us to identify data and other objects in the
program. Identifiers are basically the names given to the program elements such as variables,arrays, and
functions. They may consist of an alphabet,digit,or an underscore _

Rules for forming indentifier names:

 First letter must be an alaphabet


 White spaces are not allowed
 Uppercase letters are not allowed
 Key words not allowed

Variables: variables is nothing but identifiers. A variable is defined as a meaningful name


given to the data storage location in computer memory. When using a variable we actually refer
to an address of the memory where the data is stored. C language supports two kinds of
variables.
 Local variables
 Global variables

Local variable: local variables are variables which are declared in its body. These life time and
scope is valid in it’s block only.

Global variable: global variables are variables which are declared in out side body. These life
time and scope is valid in through out programm.

Page 12

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Badic Data Types In C: Data types specify how we enter data into our programs and
what type of data we enter. C language has some predefined set of data types to handle
various kinds of data that we use in our program. These datatypes have different
storage capacities.

C language supports 2 different type of data types,

Primary data types

These are fundamental data types in C namely integer(int), floating(float),


charater(char) and void.

Derived data types

Derived data types are like arrays, functions, stuctures and pointers. These are
dicussed in detail later.

Page 13

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Integer type

Integers are used to store whole numbers.

Size and range of Integer type on 16-bit machine


Type Size(bytes) Range

int or signed int 2 -32,768 to 32767

unsigned int 2 0 to 65535

short int or signed short int 1 -128 to 127

long int or signed long int 4 -2,147,483,648 to 2,147,483,647

unsigned long int 4 0 to 4,294,967,295

Floating type

Floating types are used to store real numbers.

Size and range of Integer type on 16-bit machine

Type Size(bytes) Range

Float 4 3.4E-38 to 3.4E+38

double 8 1.7E-308 to 1.7E+308

long double 10 3.4E-4932 to 1.1E+4932

Character type

Page 14

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Character types are used to store characters value.

Size and range of Integer type on 16-bit machine

Type Size(bytes) Range

char or signed char 1 -128 to 127

unsigned char 1 0 to 255

void type

void type means no value. This is usually used to specify the type of functions.

Constants: constants are identifiers whose value does not change. Variables can change their

value at any time but constants can never change their value. Constants are used to define fixed

Page 15

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

values. A constant is an explicit data value specified by the programmer. The compiler knows the

value of the constant at the compile time.

Operators in C: operator is defined as a symbol that specefiers the mathematical, logical, or


relational operation to be performed. C language supports many operations to be used in
expressions. These operators can be categorized into the following major groups

1. Arithmetic operators

2. Relation operators

3. Logical operators

4. Bitwise operators

5. Assignment operators

6. Conditional operators

7. Special operators
Page 16

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Operator: operator is a special symbol which can perform an opearation on it’s oprands

Operand: operand is identifiers which is part of the operation.

Arithmetic operators

C supports all the basic arithmetic operators. The following table shows all the basic
arithmetic operators.

Operator Description

+ adds two operands


-
subtract second operands from first
* multiply two operand

/ divide numerator by denumerator


% remainder of division

++ Increment operator increases integer value by one

-- Decrement operator decreases integer value by one

Relation operators
The following table shows all relation operators supported by C.

Operator Description
== Check if two operand are equal

!= Check if two operand are not equal.

> Check if operand on the left is greater than operand on the right

< Check operand on the left is smaller than right operand

>= check left operand is greater than or equal to right operand

<= Check if operand on left is smaller than or equal to right operand

Logical operators
C language supports following 3 logical operators. Suppose a=1 and b=0,
Page 17

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Operator Description Example

&& Logical AND (a && b) is false

|| Logical OR (a || b) is true

! Logical NOT (!a) is false

Bitwise operators

Bitwise operators perform manipulations of data at bit level. These operators also
perform shifting of bitsfrom right to left. Bitwise operators are not applied
to float or double.

Operator Description

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

<< left shift

>> right shift

The bitwise shift operators shifts the bit value. The left operand specifies the value to be

shifted and the right operand specifies the number of positions that the bits in the value

are to be shifted. Both operands have the same precedence.

Example :

a = 0001000

b= 2
Page 18

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

a << b = 0100000

a >> b = 0000010

Assignment Operators

Assignment operators supported by C language are as follows.

Operator Description Example

= assigns values from right side operands to left a=b


side operand

+= adds right operand to the left operand and assign a+=b is same as a=a+b
the result to left

-= subtracts right operand from the left operand and a-=b is same as a=a-b
assign the result to left operand

*= mutiply left operand with the right operand and a*=b is same as a=a*b
assign the result to left operand

/= divides left operand with the right operand and a/=b is same as a=a/b
assign the result to left operand

%= calculate modulus using two operands and a%=b is same as a=a


assign the result to left operand %b

Conditional operator

It is also known as ternary operator and used to evaluate conditional expression.

epr1 ? expr2 : expr3

If epr1 Condition is true ? Then value expr2 : Otherwise value expr3


Page 19

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Special operator

Operator Description Example

sizeof Returns the size of an variable sizeof(x) return size of the

variable x

& Returns the address of an &x ; return address of the variable x

variable

* Pointer to a variable *x ; will be pointer to a variable x

Page 20

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

INSTRUCTIONS
Each line of a C program is a instruction.it is named like that why because instruct the compiler
to do some task. In C every instruction end with semi column (;) c instructions also called
statements.in C Instructions can be categorized into 4 types.

 Type declaration instruction


 Arithmetic instructions
 Input and out put instructions
 Control instructions

Type declaration instructions: type declaration instruction is an instruction which is used to


declare the variables and arryas,strings,pointers…

Syntax: datatype variablename;


Ex: int w;
float u;
char s;
Arithmetic and logical instruction: arithmetic and logical instruction is an instruction which is
used to calculate the arithmetic and logical operations. In C the complete task will be depends on
these instructions.

Ex: c=a+b;
a & b is operands
+ is a operator
Input and output instructions: In C language the input and out put instructions are used to
perform each and every work make easily and define good programming.

Input instruction: input instruction is an instruction which is used to given the input to the
program.

Syntax: scanf(“typespecifier”,&variablename);

Ex: scanf(“%d”,&a);
scanf(“%f”,&b);
scanf(“%c”,&c);

Output instruction: output instruction is an instruction which is used to given the output to the
programer.

Syntax: printf(“typespecifier”,variablename);

Ex: printf(“%d”,variablename);
printf(“ the result is %d “,c);
printf(“ welome to c language ” );

Page 21

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

How to open C-Language:

 Click on start menu


 Select RUN option from Start menu
 Type COMMAND or CMD in Run dialog box
 Click on ok button
 Now type CD\ command in command line
 Type TC command in command line

Sample C-Programs

Q : Write a c-program to print your address ?


#include<stdio.h>
#include<conio.h>
main()
{
printf(“\n name: …………………..”);
printf(“\n fname: ………………….”);
printf(“\n street:……………………”);
printf(“\n village:………………….”);
printf(“\n state:…………………….”);
}

Q : Write a c-program to initialize the values into a variables?


#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
a=4;
b=6;
printf(“\n A= %d”,a);
printf(“\n B= %d”,b);
getch();
}

Page 22

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Q : Write a c-program to initialize the values into a variables using input instruction?
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
printf(“\n enter b value ”);
scanf(“%d”,&b);
printf(“\n a= %d”,a);
printf(“\n b= %d”,b);
getch();
}

Q : Write a c-program to print sum of two numbers?


#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
a=5;
b=7;
c=a+b;
printf(“\n the sum is %d “,c);
getch();
}

Q : Write a c-program to print sum of two numbers using input instruction?


#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“\n enter a value ‘);
scanf(“%d”,&a);
printf(“\n enter b value ‘);
scanf(“%d”,&b);
c=a+b;
printf(“\n the sum is %d”,c);
getch();
}

Q : Write a C-program to print subtraction of two numbers ?


Page 23

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“\n enter a value ‘);
scanf(“%d”,&a);
printf(“\n enter b value ‘);
scanf(“%d”,&b);
c=a-b;
printf(“\n the subtraction is %d”,c);
getch();
}
Q : Write a c-program to print Division of two numbers ?
#include<stdio.h>
#include<conio.h>
main()
{
float a,b,c;
clrscr();
printf(“\n enter a value ‘);
scanf(“%f”,&a);
printf(“\n enter b value ‘);
scanf(“%f”,&b);
c=a/b;
printf(“\n the division is %f”,c);
getch();
}
Q : Write a c-program to print Reminder of two numbers ?
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“\n enter a value ‘);
scanf(“%d”,&a);
printf(“\n enter b value ‘);
scanf(“%d”,&b);
c=a%b;
printf(“\n the reminder is %d”,c);
getch();
}

Page 24

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Type Conversion

Type conversion is the on of the popular concept in c-language. It is the technique


of converting one date type into another date type. Type conversion is done in two ways.

1.Implicit type conversion


2.Explicit type conversion

Implicit type conversion :- implicit type conversion is a type conversion which works
automatically

Eg:

main()
{
int a;
clrscr();
a=91;
printf(“\n a= %d”,a);
a=35.62;
printf(“\n a= %d”,a);
a=‘b’;
printf(“\n a= %d”,a);
getch();
}

Explicit type conversion :- explicit type conversion is a type conversion which done by the user.

Eg:

main()
{
int a,b;
flat c;
clrscr();
printf(“\n enter a value ‘);
scanf(“%d”,&a);
printf(“\n enter b value ‘);
scanf(“%d”,&b);
c=(float)(a/b);
printf(“\n the division is %f”,c);
getch();
}

Q : Write a c-program to find the simple interest ?


#include<stdio.h>
#include<conio.h>
Page 25

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

main()
{
int p;
float t,r,i;
clrscr();
printf(“ \n enter principle amount value ”);
scanf(“ %d ”,&p);
printf(“ \n enter time value ”);
scanf(“ %f ”,&t);
printf(“\n enter rate value ”);
scanf(“ %f ”,&r);
i=(p*t*r)/100;
printf(“\n the interest is %f ”,c);
getch();
}

Q : Write a c-program to find electricity bill ?


#include<stdio.h>
#include<conio.h>
main()
{
int pre,prv,nu,b;
clrscr();
printf(“\n enter the present reading ”);
scanf(%d”,&pre);
printf(“\n enter the previous reading ”);
scanf(%d”,&prv);
nu=pre-prv;
printf(“\n the units are %d”,nu);
b=nu*2;
printf(“\n the bill is %d ”,b);
getch();
}
Q : Write a c-program to print a Student Mark List ?
#include<stdio.h>
#include<conio.h>
main()
{
int t,e,h,m,ps,ss,tot;
flaot avg;
clrscr();
printf(“\n enter telugu marks ”);
scanf(“%d”,&t);
printf(“\n enter english marks ”);
scanf(“%d”,&e);
printf(“\n enter maths marks ”);
scanf(“%d”,&m);
printf(“\n enter physical science marks ”);
Page 26

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

scanf(“%d”,&ps);
printf(“\n enter social studies marks ”);
scanf(“%d”,&ss);
tot=t+e+h+m+ps+ss;
printf(“\n the total is %d ”,tot);
avg=tot/6;
printf(“\n the average is %f ”,avg);
getch();
}

Q : Write a c-program to print a character ?


#include<stdio.h>
#include<conio.h>
main()
{
char p;
clrscr();
printf(“\n enter a character ”);
scanf(“%c”,&p);
printf(“\n the character is %c”,p);
getch();
}

Q : Write a c-program to convert lower case letter into UPPER CASE letter ?
#include<stdio.h>
#include<conio.h>
main()
{
char g;
clrscr();
printf(“\n enter a character (lower case) ”);
scanf(“%c”,&g);
g=g-32;
printf(“\n the character is %c ”,g);
getch();
}

Q : Write a c-program to convert UPPER CASE letter into lower case letter ?
#include<stdio.h>
#include<conio.h>
main()
{
char g;
clrscr();
printf(“\n enter a character (upper case) ”);
scanf(“%c”,&g);
g=g+32;
printf(“\n the character is %c”,g);
Page 27

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

getch();
}

Q : Write a c-program of swapping of two numbers?


#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
printf(“\n enter b value ”);
scanf(“%d”,&a);
printf(“\n before swaping a= %d b=%d ”,a,b);
c=a;
a=b;
b=c;
printf(“\n after swaping a= %d b=%d ”,a,b);
getch();
}

Page 28

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

CONTROL INSTRUCTIONS

Control instruction is an instruction which instruct the complier. In C control


Instructions are two types those are

1. decision making and branching


2. decision making and looping

Decision Making And Branching : in this control will takes some decisions according to that
decision it take some branch. It is available in C in the following categories.

 Simple IF
 IF-Else
 Nesting
 IF-Else-IF
 Switch Case

Simple If : If the condition is satisfied then the statement part will get executed otherwise it Not.

Syntax: if(condition)
{
statement part;
}

Eg: if(a>0)
{
printf(“\n positive ”);
}

Program:

Q : Write a c-program to find given number is positive or Not ?


#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a>0)
{
printf(“\n positive ”);
}
getch();
}
Q : Write a c-program to find given number is Negative or Not ?
#include<stdio.h>
Page 29

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

#include<conio.h>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a<0)
{
printf(“\n negative ”);
}
getch();
}

Q : Write a c-program to find given number is Even or Not ?


#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a%2= =0)
{
printf(“\n even ”);
}
getch();
}

Q : Write a c-program to find given number is odd or Not ?


#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a%2!=0)
{
printf(“\n odd ”);
}
getch();
}
If - Else: If the condition is satisfied then the statement part-1 will get executed otherwise
statement part -2 will get executed.

Page 30

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Syntax: if(condition)
{
statement part-1;
}
else
{
statement part-2;
}

Eg: if(a>0)
{
printf(“\n positive ”);
}
else
{
printf(“\n negative ”);
}
Program:
Q : Write a c-program to find given number is positive or Negative ?
#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a>0)
{
printf(“\n positive ”);
}
else
{
printf(“\n negative ”);
}
getch();
}
Q : Write a c-program to find given number is Even or Odd ?
#Include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a%2= =0)
{
Page 31

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

printf(“\n even ”);


}
else
{
pritnf(“\n odd ”);
}
getch();
}
Nesting
The technique of placing the one block into another block is known as Nesting.

Nested IF : Placing the one if block into another if block is called as Nested IF.
--------- If the condition-1 and condition-2 is satisfied then the statement part will get
Executed.

Syntax: if(condition-1)
{
if(Condition-2)
{
Statement part-2;
}
}

Eg: if(a>0)
{
if(a%2= =0)
{
Printf(“\n positive Even”);
}
}
Program:
Q : Write a c-program to find given number is Positive Even or Not ?
#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a>0)
{
if(a%2= =0)
{
printf(“\n positive even”);
}
}
getch();
Page 32

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

}
Q : Write a c-program to find given number is Negative Even or Not ?
#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a<0)
{
if(a%2= =0)
{
printf(“\n negative even”);
}
}
getch();
}
Q : Write a c-program to find given number is positive odd or Not ?
#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a>0)
{
if(a%2= =1)
{
printf(“\n positive odd ”);
}
}
getch();
}
Q : Write a c-program to find given number is Negative odd or Not ?
#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a<0)
{
Page 33

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

if(a%2= =1)
{
printf(“\n negative odd ”);
}
}
getch();
}
Nested IF-Else: Placing the one if-else block into another if-else block is called as Nested IF-
else.
Syntax: if(condition-1)
{
if(condition-2)
{
statement part-1;
}
else
{
statement part-2;
}
}
else
{
if(condition-3)
{
statement part-1;
}
else
{
statement part-2;
}
}

Eg: if(a>0)
{
if(a%2= =0)
{
printf(“\n positive even”);
}
else
{
printf(“\n positive odd”);
}
}
else
{
if(a%2= =0)
{
printf(“\n negative even”);
Page 34

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

}
else
{
printf(“\n negative odd”);
}
}

Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
if(a>0)
{
if(a%2= =0)
{
printf(“\n positive even”);
}
else
{
printf(“\n positive odd”);
}
}
else
{
if(a%2= =0)
{
printf(“\n negative even”);
}
else
{
printf(“\n negative odd”);
}
}
getch();
}

IF –Else- If :
Syntax :
if(condition-1)
{
statement part;
}
else if(condition-2)
Page 35

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

{
statement part;
}
else if(condition-3)
{
statement part;
}
else
{
statement part;
}
Program :
Q : Write a c-program to find given Student mark list with Grade ?
#include<stdio.h>
#include<conio.h>
main()
{
int t,e,h,m,ps,ss,tot;
flaot avg;
clrscr();
printf(“\n enter telugu marks ”);
scanf(“%d”,&t);
printf(“\n enter english marks ”);
scanf(“%d”,&e);
printf(“\n enter maths marks ”);
scanf(“%d”,&m);
printf(“\n enter physical science marks ”);
scanf(“%d”,&ps);
printf(“\n enter social studies marks ”);
scanf(“%d”,&ss);
tot=t+e+h+m+ps+ss;
printf(“\n the total is %d ”,tot);
avg=tot/6;
printf(“\n the average is %f ”,avg);
if((t>=35)&&(e>=35)&&(h>=35)&&(m>=35)&&(ps>=35)&&(ss=35&&))
{
printf(“\n the result is pass “);
if(avg>=80)
{
printf(“\n grade a ”);
}
else if(avg>=70)
{
printf(“\n grade b”);
}
else if(avg>=55)
{
printf(“\n grade c”);
Page 36

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

}
else if(avg>=40)
{
printf(“\n grade d”);
}
else
{
printf(“\n grade e ”);
}
}
else
{
printf(“\n no grade e ”);
}
getch();
}

Switch Case :
Syntax:
switch(variable name)
{
case 1 : statement part 1;
break;
case 2 : statement part 2;
break;
case 3 : statement part 3;
break;
case 4 : statement part 4;
break;
default:
statement part n;
}
Eg:
Switch(a)
{
case 1 : printf(“\n HAI ”);
Break;
case 2 : printf(“\n hello ”);
break;
case 3 : printf(“\n bye ”);
break;
default:
printf(“\n welcome ”);
}
Program :

#include<stdio.h>
#include<conio.h>
Page 37

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

main()
{
int a;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
switch(a)
{
case 1 : printf(“\n hai ”);
break;
case 2 : printf(“\n hello ”);
break;
case 3 : printf(“\n bye ”);
break;
default:
printf(“\n welcome ”);
}
getch();
}
Program :
#include<stdio.h>
#include<conio.h>
main()
{
char a;
clrscr();
printf(“\n enter a character ”);
scanf(“%s”,&a);
switch(a)
{
case ‘a’: printf(“\n hai ”);
break;
case ‘s’: printf(“\n hello ”);
break;
case ‘d’: printf(“\n bye ”);
break;
default:
printf(“\n welcome ”);
}
getch();
}
Program :

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
Page 38

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

char ch;
clrscr();
printf(“\n ener a value ‘);
scanf(“%d”,&a);
printf(“\n ener b value ‘);
scanf(“%d”,&b);
printf(“\n ener your choice ‘);
scanf(“%s”,&ch);
switch(ch)
{
case ‘+’: c=a+b;
break;
case ‘-’: c=a-b;
break;
case ‘*’: c=a*b;
break;
case ‘/’: c=a/b;
break;
case ‘%’: c=a%b;
break;
default: printf(“\n invlaid choice ”);
}
printf(“\n the result is %d ”,c);
getch();
}

GOTO Statement : jump statement is a statement which is used to jump the control from one
place to another place of a program with out observing any condition
C supports a special jump statement named “goto” goto can be used into 2 ways
those are
1. forward goto
2. backward goto

forward goto :
Syntax: statement part -1;
statement part -2;
statement part -3;
goto label;
statement part -4;
statement part -5;
statement part -6;
label;
statement part -7;
statement part -8;
statement part -9;

Program :
#include<stdio.h>
Page 39

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

#include<conio.h>
main()
{
Clrscr();
printf(“\n HAI ”);
printf(“\n HELLO ”);
printf(“\n BYE ”);
printf(“\n NARASANNAPETA ”);
printf(“\n SRIKAKULAM ”);
printf(“\n VIZAG ”);
goto ramu;
printf(“\n JAMMU ”);
printf(“\n POLAKI ”);
printf(“\n NPETA ”);
printf(“\n SARAVAKOTA ”);
ramu:
printf(“\n CHENNAI ”);
printf(“\n BANGLOORE ”);
printf(“\n MUMBAI ”);
printf(“\n DELHI ”);
printf(“\n RAJASTAN ”);
getch();
}
Backward goto :
Syntax: statement part -1;
statement part -2;
statement part -3;
label:
statement part -4;
statement part -5;
statement part -6;
goto label;
statement part -7;
statement part -8;
statement part -9;

Program :
#include<stdio.h>
#include<conio.h>
main()
{
Clrscr();
printf(“\n HAI ”);
printf(“\n HELLO ”);
printf(“\n BYE ”);
printf(“\n NARASANNAPETA ”);
printf(“\n SRIKAKULAM ”);
printf(“\n VIZAG ”);
Page 40

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

ramu;
printf(“\n JAMMU ”);
printf(“\n POLAKI ”);
printf(“\n NPETA ”);
printf(“\n SARAVAKOTA ”);
goto ramu:
printf(“\n CHENNAI ”);
printf(“\n BANGLOORE ”);
printf(“\n MUMBAI ”);
printf(“\n DELHI ”);
printf(“\n RAJASTAN ”);
getch();
}

Page 41

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

LOOPING
Decision Making And Looping : As Long as the condition is satisfied the statement part will
get executed this technique is known as “Looping” in C there are three statements to support the
looping those are

1. while
2. do-while
3. for

While Loop: while loop is a loop it is an entry control loop why because it checks the condition
at checks the condition is first and allows the control into the block next.

Syntax:
initialization;
while(condition)
{
statement part ;
increment/decrement ;
}
Eg:
i=1;
while(i<=10)
{
printf(“\n gnanajyothi ”);
i=i+1;
}
Program :
Q : Write a c program to print N below natural numbers
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
i=1;
while(i<=n)
{
printf(“\n %d ”,i);
i++;
}
getch();
}

Page 42

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Q : Write a c program to print N below Even numbers


#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
i=2;
while(i<=n)
{
printf(“\n %d ”,i);
i=i+2;
}
getch();
}

Q : Write a c program to print N below Odd numbers


#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
i=1;
while(i<=n)
{
printf(“\n %d ”,i);
i=i+2;
}
getch();
}

Do-While Loop: Do-while loop is a loop it is an exit control loop. If the condition is satisfied or
not once statement part will be executed after that it checks the condition.

Syntax:
initialization;
do
{
Statement part ;
Increment/decrement ;
} while(condition);

Page 43

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Eg:
i=1;
do
{
printf(“\n gnanajyothi ”);
i=i+1;
} while(i<=10);
Program :
Q : Write a c program to print N below natural numbers
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
i=1;
do
{
printf(“\n %d “,i);
i++;
} while(i<=n);
getch();
}
Q : Write a c program to print N below Even numbers
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
i=2;
do
{
printf(“\n %d “,i);
i=i+2;
} while(i<=n);
getch();
}

Q : Write a c program to print N below Odd numbers


#include<stdio.h>
#include<conio.h>
main()
{
Page 44

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
i=1;
do
{
printf(“\n %d “,i);
i=i+2;
}while(i<=n);
getch();
}
For Loop : It is an also entry control loop. As long as the condition is satisfied the statement part
will get executed.

Syntax:
for (initialization; condition; increment/decrement)
{
Statement part ;
}
Eg:
for(i=0i<=10;i++)
{
Printf(“\n Gnanajyothi ”);
}
Program :
Q : Write a c program to print N below natural numbers
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
{
printf(“\n %d “,i);
}
getch();
}
Q : Write a c program to print N below Even numbers
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
Page 45

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

printf(“\n enter n value”);


scanf(“%d”,&n);
for(i=2;i<=n;i=i+2)
{
printf(“\n %d “,i);
}
getch();
}
Q : Write a c program to print N below Odd numbers
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i=i+2)
{
printf(“\n %d “,i);
}
getch();
}

Q : Write a c program to find factorial of given number .


#include<stdio.h>
#include<conio.h>
main()
{
int f =1,n,i;
clarscr();
printf(“\n enter n value ”);
scanf(%d”,&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf(“\n the factorial is %d ”,f);
getch();
}

Q : Write a c program to print the sum of N natural numbers


#include<stdio.h>
#include<conio.h>
main()
{
int n,i,s=0;
Page 46

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

clrscr();
printf(“\n enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i=i+2)
{
s=s+i;
printf(“\n the sum of %d below natural numbers is %d”,n,s);
}
getch();
}

Q : Write a c program to find MN value


#include<stdio.h>
#include<conio.h>
main()
{
int m,n,a,i;
clrscr();
printf(“\n enter the m value ”);
scanf(“%d”,&m);
printf(“\n enter the n value ”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
{
a=a*m;
}
printf(“\n the %d power %d is %d ”,m,n,a);
getch();
}

Q : Write a c program to find given number is prime or composite


#include<stdio.h>
#include<conio.h>
main()
{
int n,=0,i;
clrscr();
printf(“\n enter n value ”);
scanf(“%d”,&n);
if(n>1)
{
for(i=1;i<=n;i++)
{
if(n%i= =0)
c=c+1;
}
if(c= = 2)
{
Page 47

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

printf(“\n given number is prime ”);


}
else
{
printf(“\n given number is comosit ”);
}
}
else
{
printf(“\n given number is not a prime and composit ”);
getch();
}

Q : Write a c program to find reverse number of given number.


#include<stdio.h>
#include<conio.h>
main()
{
int n,r,s=0;
clrscr();
printf(“\n enter n value ”);
scanf(“%d”,&n);
while(n!=0)
{
r=n%10;
s=(s*10)+r;
n=n/10;
}
printf(“\n the reverse number of given number is %d ”,s);
getch();
}

Q : Write a c program to find given number is palindrome or not.


#include<stdio.h>
#include<conio.h>
main()
{
int n,r,s=0,k;
clrscr();
printf(“\n enter n value ”);
scanf(“%d”,&n);
k=
while(n!=0)
{
r=n%10;
s=(s*10)+r;
n=n/10;
}
Page 48

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

if(k= =s)
{
printf(“\n given number is palindrome ”);
}
else
{
printf(“\n given number is not palindrome ”);
}
getch();
}
Q : Write a c program to find given number is Armstrong or not.
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,s=0,k;
clrscr();
printf(“\n enter n value ”);
scanf(“%d”,&n);
k=
while(n!=0)
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
if(k= =s)
{
printf(“\n given number is armstrong ”);
}
else
{
printf(“\n given number is not armstrong ”);
}
getch();
}
Q : Write a c program to a number and then calculate the sum of its digits.
#include<stdio.h>
#include<conio.h>
main()
{
int n,t,s=0;
clrscr();
printf(“\n Enter a Number: ”);
scanf(“%d”,&n);
while(n!=0)
{
t=n%10;
Page 49

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

s=s+t;
s=n/10;
}
printf(“\n the sum of the digit = %d ”,s);
getch();
}

Page 50

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Nested For Loop


One for loop placing into another for loop this concept is known as nested for loop.

Q : Write a c program to print a Square diagram using Star’s


#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf(‘\n enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=2;j<=n;j++)
{
printf(“* ”);
}
printf(“\n”);
}
getch();
}
Output: enter n value : 5

*****
*****
*****
*****
*****

Q : Write a c program to print a Square diagram using I value


#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf(‘\n enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(“%d ”,i);
}
printf(“\n”);
}
getch();
Page 51

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Output: enter n value : 5

11111
22222
33333
44444
55555

Q : Write a c program to print a Square diagram using J value


#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf(‘\n enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(“%d ”,j);
}
printf(“\n”);
}
getch();
}

Output: enter n value : 5

12345
12345
12345
12345
12345

Q : Write a c program to print a pyramid diagram using star’s


#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf(‘\n enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
Page 52

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

{
for(j=1;j<=i;j++)
{
printf(“ * ”);
}
printf(“\n”);
}
getch();
}

Output: enter n value : 5

*
**
***
****
*****

Q : Write a c program to print a pyramid diagram using I value

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf(‘\n enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(“ * ”);
}
printf(“\n”);
}
getch();
}

Output: enter n value : 5

1
22
333
4444
Page 53

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

55555

Q : Write a c program to print a pyramid diagram using J value

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j;
clrscr();
printf(‘\n enter n value”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(“ * ”);
}
printf(“\n”);
}
getch();
}

Output: enter n value : 5

1
12
123
1234
12345

Page 54

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

ARRAYS
Array is collection of related data type items to share a common name. simply an array is
also a variable.

In array the memory is allocated to the data items in a sequential manner i.e linear
fashion.

The main objective of this concept is to reduce the accessing time


In array all the data items have the same name uniquely identify them we use subscript.
In C arrays may broadly categorized into three categories these are
1. one dimensional array
2. two dimensional array
3. three dimensional array

One Dimensional Array

An array in which elements are identified with the help of single subscript is called one
dimensional array or single subscripted variable.

Syntax.
datatype variable name[size];
Ex:
int a[5];
float b[5];
char c[5];
Initialization:

Syntax:

datatype variablename[ ]={value};


Ex:
int a[5]={1,2,3,4,5};
float b[3]={2.3,5.6,8.5};
Program-1:

#include<stdio.h>
#include<conio.h>
main()
{
int a[4]={11,88,99,44};
clrscr();
printf(“\n a[0]=%d”,a[0]);
printf(“\n a[1]=%d”,a[1]);
printf(“\n a[2]=%d”,a[2]);
printf(“\n a[3]=%d”,a[3]);
getch();
}
Page 55

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Program-2:

#include<stdio.h>
#include<conio.h>
main()
{
int a[ ]={11,88,99,44};
clrscr();
printf(“\n a[0]=%d”,a[0]);
printf(“\n a[1]=%d”,a[1]);
printf(“\n a[2]=%d”,a[2]);
printf(“\n a[3]=%d”,a[3]);
getch();
}

Program-3:

#include<stdio.h>
#include<conio.h>
main()
{
int a[4];
clrscr();
a[0]=45;
a[1]=86;
a[2]=26;
a[3]=63;
printf(“\n a[0]=%d”,a[0]);
printf(“\n a[1]=%d”,a[1]);
printf(“\n a[2]=%d”,a[2]);
printf(“\n a[3]=%d”,a[3]);
getch();
}

Initialization array using input instruction.

Program-1

#include<stdio.h>
#include<conio.h>
main()
{
int a[4];
clrscr();
printf(“\n enter a[0] ”);
scanf(“%d”,&a[0]);
printf(“\n enter a[1] ”);
Page 56

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

scanf(“%d”,&a[1]);
printf(“\n enter a[2] ”);
scanf(“%d”,&a[2]);
printf(“\n enter a[3] ”);
scanf(“%d”,&a[3]);
printf(“\n the values are”);
printf(“\n a[0]= %d ”,a[0]);
printf(“\n a[1]= %d ”,a[1]);
printf(“\n a[2]= %d ”,a[2]);
printf(“\n a[3]= %d ”,a[3]);
getch();
}

Initialization array using loops.

Program-1

#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i;
clrscr();
for(i=0;i<10;i++)
{
printf(“\n enter a[%d] ”,i);
scanf(“%d”,&a[i]);
}
printf(“\n the values are ”);
for(i=0;i<10;i++)
{
printf(“\n enter a[%d]= %d ”,i,a[i]);
}
getch();
}

Variable length of an array.


Program-1
#include<stdio.h>
#include<conio.h>
main()
{
int a[100],i,n;
clrscr();
printf(‘\n enter the size of array ”);
scanf(“%d”,&n);
printf(‘\n enter the elements of array ”);
for(i=0;i<n;i++)
Page 57

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

{
printf(“\n enter a[%d] ”,i);
scanf(“%d”,&a[i]);
}
printf(“\n the values are ”);
for(i=0;i<n;i++)
{
printf(“\n enter a[%d]= %d ”,i,a[i]);
}
getch();
}
Write a C Program to find the sum of elements of an array.
#Include<stdio.h>
#include<conio.h>
main()
{
int a[100],n,i,s=0;
clrscr();
printf(‘\n enter the size of array ”);
scanf(“%d”,&n);
printf(‘\n enter the elements of array ”);
for(i=0;i<n;i++)
{
printf(“\n enter a[%d] ”,i);
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
s=s+a[i];
}
printf(“\n the values are ”);
for(i=0;i<n;i++)
{
printf(“\n enter a[%d]= %d ”,i,a[i]);
}

printf(“\n the sum is %d ”,s);


getch();
}

Page 58

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

TWO DEMENTIONAL ARRAYS

Two dimensional array is an array in which elements are identified with the help of
double subscript so it is also called as double subscripted variable or two dimensional array

Syntax.
datatype variable name[row size][coloumn size];
Ex:
int a[5][3];
float b[5][5];
char c[5][4];
Initialization:
Syntax:

datatype variablename[ ][ ]={{value},{values}};


Ex:
int a[2][3]={{1,2},{3,4,5}};
float b[2][2]={{2.3,5.6,8.5,4.6};
Program-1:

#include<stdio.h>
#include<conio.h>
main()
{
int a[2][2]={{11,88},{99,44}};
clrscr();
printf(“\n a[0][0]=%d”,a[0][0]);
printf(“\n a[0][1]=%d”,a[0][1]);
printf(“\n a[1][0]=%d”,a[1][0]);
printf(“\n a[1][1]=%d”,a[1][1]);
getch();
}

Program-2:
#Include<stdio.h>
#include<conio.h>
main()
{
int a[ ]={11,88,99,44};
clrscr();
printf(“\n a[0][0]=%d”,a[0][0]);
printf(“\n a[0][1]=%d”,a[0][1]);
printf(“\n a[1][0]=%d”,a[1][0]);
printf(“\n a[1][1]=%d”,a[1][1]);
getch();
}
Program-3:
Page 59

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

#include<stdio.h>
#include<conio.h>
main()
{
int a[2][2];
clrscr();
a[0][0]=45;
a[0][1]=86;
a[1][0]=26;
a[1][1]=63;
printf(“\n a[0][0]=%d”,a[0][0]);
printf(“\n a[0][1]=%d”,a[0][1]);
printf(“\n a[1][0]=%d”,a[1][0]);
printf(“\n a[1][1]=%d”,a[1][1]);
getch();
}

Initialization array using input instruction.

Program-1

#include<stdio.h>
#include<conio.h>
main()
{
int a[2][2];
clrscr();
printf(“\n enter a[0][0] ”);
scanf(“%d”,&a[0][0]);
printf(“\n enter a[0][1] ”);
scanf(“%d”,&a[0][1]);
printf(“\n enter a[1][0] ”);
scanf(“%d”,&a[1][0]);
printf(“\n enter a[1][1] ”);
scanf(“%d”,&a[1][1]);
printf(“\n the values are”);
printf(“\n a[0][0]=%d”,a[0][0]);
printf(“\n a[0][1]=%d”,a[0][1]);
printf(“\n a[1][0]=%d”,a[1][0]);
printf(“\n a[1][1]=%d”,a[1][1]);
getch();
}

Initialization array using loops.

Page 60

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Program-1

#include<stdio.h>
#include<conio.h>
main()
{
int a[2][2],i,j;
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<2j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“\n the values are ”);
for(i=0;i<2;i++)
{
for(j=0;j<2j++)
{
printf(“\n enter a[%d][%d] = %d ”,i,j,a[i]j]);
}
}
getch();
}

Variable length of an array.

Program-1
#include<stdio.h>
#include<conio.h>
main()
{
int a[100][100],i,j,r,c;
clrscr();
printf(‘\n enter the row size of array ”);
scanf(“%d”,&r);
printf(‘\n enter the column size of array ”);
scanf(“%d”,&c);
printf(‘\n enter the elements of array ”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
Page 61

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

printf(“\n the values are ”);


for(i=0;i<2;i++)
{
for(j=0;j<2j++)
{
printf(“\n enter a[%d][%d] = %d ”,i,j,a[i]j]);
}
}
getch();
}

Q. Write a C Program to find the sum of elements of two dimensional array.


#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10],r,c,i, j,s=0;
clrscr();
printf(‘\n enter the row size of array ”);
scanf(“%d”,&r);
printf(‘\n enter the column size of array ”);
scanf(“%d”,&c);
printf(‘\n enter the elements of array ”);
for(i=0;i<r;i++)
{
for(j=0;j<cj++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<cj++)
{
s=s+a[i][j];
}
}
printf(“\n the values are ”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“\n enter a[%d][%d] = %d ”,i,j,a[i]j]);
}
}
Page 62

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

printf(“\n the sum of elements in array is %d ”,s);


getch();
}

Q : Write a c program to print a Matrix using loops.

#include<stdio.h>
#include<conio.h>
main()
{
int a[100][100],i,j,r,c;
clrscr();
printf(‘\n enter the row size of matrix ”);
scanf(“%d”,&r);
printf(‘\n enter the column size of matrix ”);
scanf(“%d”,&c);
printf(‘\n enter the elements of matrix ”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“\n the matrix is \n ”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“%d ”,a[i]j]);
}
printf(“\n”);
}
getch();
}

Q : Write a c program to print Trace of the given Matrix.

#include<stdio.h>
#include<conio.h>
main()
{
int a[100][100],i,j,r,c;
clrscr();
printf(‘\n enter the row size of matrix ”);
scanf(“%d”,&r);
Page 63

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

printf(‘\n enter the column size of matrix ”);


scanf(“%d”,&c);
printf(‘\n enter the elements of matrix ”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“\n the matrix is \n ”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“%d ”,a[i]j]);
}
printf(“\n”);
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i= = j)
s=s+a[i][j];
}
}
printf(“\n the trace of the matrix is %d ”,s);
getch();
}

Q : Write a c program to print Transpose of given Matrix.

#include<stdio.h>
#include<conio.h>
main()
{
int a[100][100],i,j,r,c;
clrscr();
printf(‘\n enter the row size of matrix ”);
scanf(“%d”,&r);
printf(‘\n enter the column size of matrix ”);
scanf(“%d”,&c);
printf(‘\n enter the elements of matrix ”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
Page 64

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“\n the matrix is \n ”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“%d ”,a[i]j]);
}
printf(“\n”);
}
printf(“\n the transpose of the matrix is \n”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“%d ”,a[j]i]);
}
}
getch();
}

Q : Write a c program to print two matrixes.

#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10], b[10][10],i,j,r1,c1,r2,c2;
clrscr();
printf(‘\n enter the row size of first matrix ”);
scanf(“%d”,&r1);
printf(‘\n enter the column size of first matrix ”);
scanf(“%d”,&c1);
printf(‘\n enter the row size of second matrix ”);
scanf(“%d”,&r2);
printf(‘\n enter the column size of second matrix ”);
scanf(“%d”,&c2);
printf(‘\n enter the elements of first matrix ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
Page 65

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

}
}
printf(‘\n enter the elements of second matrix ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“\n enter b[%d][%d] ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf(“\n the first matrix is \n ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“%d ”,a[i]j]);
}
printf(“\n”);
}
printf(“\n the second matrix is \n ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“%d ”,b[i]j]);
}
printf(“\n”);
}
getch();
}

Q : Write a c program to print sum of two matrixes.

#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10], b[10][10],c[10][10]i,j,r1,c1,r2,c2;
clrscr();
printf(‘\n enter the row size of first matrix ”);
scanf(“%d”,&r1);
printf(‘\n enter the column size of first matrix ”);
scanf(“%d”,&c1);
printf(‘\n enter the row size of second matrix ”);
scanf(“%d”,&r2);
printf(‘\n enter the column size of second matrix ”);
scanf(“%d”,&c2);
Page 66

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

printf(‘\n enter the elements of first matrix ”);


for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(‘\n enter the elements of second matrix ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“\n enter b[%d][%d] ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf(“\n the first matrix is \n ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“%d ”,a[i]j]);
}
printf(“\n”);
}
printf(“\n the second matrix is \n ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“%d ”,b[i]j]);
}
printf(“\n”);
}
if((r1= =r2)&&(c1= =c2))
{
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
printf(“\n the sum of two matrix is \n ”);
for(i=0;i<r1;i++)
Page 67

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

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

Q : Write a c program to print subtraction of two matrixes.

#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10], b[10][10],c[10][10]i,j,r1,c1,r2,c2;
clrscr();
printf(‘\n enter the row size of first matrix ”);
scanf(“%d”,&r1);
printf(‘\n enter the column size of first matrix ”);
scanf(“%d”,&c1);
printf(‘\n enter the row size of second matrix ”);
scanf(“%d”,&r2);
printf(‘\n enter the column size of second matrix ”);
scanf(“%d”,&c2);
printf(‘\n enter the elements of first matrix ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(‘\n enter the elements of second matrix ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“\n enter b[%d][%d] ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf(“\n the first matrix is \n ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
Page 68

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

{
printf(“%d ”,a[i]j]);
}
printf(“\n”);
}
printf(“\n the second matrix is \n ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“%d ”,b[i]j]);
}
printf(“\n”);
}
if((r1= =r2)&&(c1= =c2))
{
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
}

printf(“\n the subtraction of two matrix is \n ”);


for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“%d ”,c[i]j]);
}
printf(“\n”);
}
getch();
}

Q : Write a c program to print multiplication of two matrixes.

#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10], b[10][10],c[10][10]i,j,k,r1,c1,r2,c2;
clrscr();
printf(‘\n enter the row size of first matrix ”);
scanf(“%d”,&r1);
printf(‘\n enter the column size of first matrix ”);
Page 69

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

scanf(“%d”,&c1);
printf(‘\n enter the row size of second matrix ”);
scanf(“%d”,&r2);
printf(‘\n enter the column size of second matrix ”);
scanf(“%d”,&c2);
printf(‘\n enter the elements of first matrix ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“\n enter a[%d][%d] ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(‘\n enter the elements of second matrix ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“\n enter b[%d][%d] ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf(“\n the first matrix is \n ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“%d ”,a[i]j]);
}
printf(“\n”);
}
printf(“\n the second matrix is \n ”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“%d ”,b[i]j]);
}
printf(“\n”);
}
if(c1= =r2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
Page 70

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

for(k=0;k<c1;k++)
{
c[i][j]= c[i][j]+a[i][k]b[k][j];
}
}
}
printf(“\n the multiplication of two matrix is \n ”);
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf(“%d ”,c[i]j]);
}
printf(“\n”);
}
}
else
{
printf(“\n the multiplication of two matrix is not possible ”);
}
getch();
}

Page 71

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

STRINGS

A string is nothing but a character array it is also called as string variable. In C every
string ends with ‘\0’ character (null terminator). Simply every string is null terminated. In C the
strings are checked.

A sequence of characters enclosed in double quotes is called string constant.

E.g. “rama” ,“1024” ,“r4#a”

Declaration of strings.

Syntax. char variablename[size];

Eg. char a[10];


char f[10];

Initialization of strings .

Syntax. char variablename[size]={characters};

E.g. 1. char a[5]={‘r’,‘a’,‘m’,‘a’,‘\0’};


2. char s[ ]={‘m’,‘a’,‘l’,‘l’,‘e’,‘s’,‘h’,‘\0’};
3. char e[5]=“ramu”;

Program-1
#include<stdio.h>
#include<conio.h>
main()
{
{
char a[6]={‘s’,‘u’,‘r’,‘y’,‘a’,‘\0’};
char b[ ]={‘r’,‘a’,‘m’,‘u’,‘\0’};
char c[6]=“malli”;
char d[4];
d[0]= ‘s’;
d[1]= ‘a’;
d[2]= ‘i’;
d[3]= ‘\0’;
printf(“\n A= %s ”,a);
printf(“\n B= %s ”,b);
printf(“\n C= %s ”,c);
printf(“\n D= %s ”,d);
getch();
}

Page 72

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Program-2
#include<stdio.h>
#include<conio.h>
main()
{
char a[10];
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
printf(“\n the string is %s ”,a);
getch();
}

STRING MANIPULATION (OR) STRING SIMULATION

The process of manipulating string is called string simulations. The users have a right to
manipulate the strings with the help of some predefined functions in standard library. Those are

 Finding the length of string


 Copying one string into another string
 Concatenating the two strings
 Finding the reverse of string
 Comparing the two strings

Finding the length of string: we can find the length of a string using a predefined function
called “strlen()” this function takes one argument nothing but sting argument and it returns an
integer value which is length of a given string.

Syntax. l=strlen(“string”);
Eg. x=strlen(“sai”);

#include<stdio.h>
#include<conio.h>
main()
{
char a[10];
int x;
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
x=strlen(a)
printf(“\n the length of string is %d ”,x);
getch();
}

Copying one string into another string: copying one string into another string we can using
with the help of predefined function “strcpy()” which is in string.h header file.

Page 73

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Syntax. strcpy(“string1”, “string2”);


Eg. strcpy(b,a);

The above function takes two arguments both are string arguments. The first one is must be a
string variable it copies the second string into the first string.

#include<stdio.h>
#include<conio.h>
main()
{
char a[10],b[10];
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
strcmp(b,a);
printf(“\n the copying string is %s ”,b);
getch();
}

Concatenating the two strings: we can concatenate two strings with the help of a predefined
function “strcat()” which is in string.h header file.

Syntax. strcat(“string1”, “string2”);


Eg. strcpy(a,b);

The above function takes two arguments both are strings in that first argument must be a variable
it concatenates the given two strings and place the result in first string .

#include<stdio.h>
#include<conio.h>
main()
{
char a[10],b[10];
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
printf(“\n enter b string ” );
scanf(“%s”,&b);
strcat(a,b);
printf(“\n the concatenated string is %s ”,a);
getch();
}

Finding the reverse of string : we can reverse a string using a predefined function “strrev()”
which is in header file string.h

Syntax. strrev(“string”);
Page 74

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Eg. strrev(a);

The above two strings takes one argument nothing but string variable. It reverse the given sting
and replace into it.

#include<stdio.h>
#include<conio.h>
main()
{
char a[10];
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
strrev(a);
printf(“\n the reverse string is %s ”,a);
getch();
}

Comparing two strings: We can comparing two strings with the help of a predefined function
“strcmp()” which is in string.h header file

Syntax. strcmp(“string1”,“strcmp”);
Eg. x=strcmp(a,b);

The above function takes two arguments both are strings and returns integer value.
 If it is return zero then the given string are equal.
 If it is return a positive value then first string is > second string
 If it is return a negative value then first string is < second string

#include<stdio.h>
#include<conio.h>
main()
{
char a[10],b[10];
int x;
clrscr();
printf(“\n enter a string ” );
scanf(“%s”,&a);
printf(“\n enter b string ” );
scanf(“%s”,&b);
x=strcmp(a,b);
if(x>0)
{
printf(“\n %s is greater than %s ”,a,b);
}
else if(x<0)
{
printf(“\n %s is less than %s ”,a,b);
Page 75

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

}
else
{
printf(“\n %s and %s are equal ”,a,b);
}
getch();
}
Q. Write a c-program to convert the given string into upper case letter.

#include<stdio.h>
#include<conio.h>
main()
{
char a[10];
clrscr();
printf(“\n enter a string(lower case) ”);
scanf(“%s”,&a);
a=a-32;
printf(“\n the converted string is %s ”,a);
getch();
}

Q. Write a c-program to convert the given string into lower case letter.

#include<stdio.h>
#include<conio.h>
main()
{
char a[10];
clrscr();
printf(“\n enter a string(upper case) ”);
scanf(“%s”,&a);
a=a+32;
printf(“\n the converted string is %s ”,a);
getch();
}

Page 76

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

FUNCTIONS
Function is a self contained block of a code which is used to perform a manageable task.
Every C program is a collection of functions each and every C program at least contains one
function it is nothing but main( )
With the help of functions we can reduce the execution time of a program and
also we can increase the efficiency

“Functions ate building blocks of program”

General syntax for a function :-

return type function name(arguments)


{
local variables ;
Statements;
Statements;
Statements;
Statements;
return type;
}

Components of a function:

 Function declaration
 Function body
 Function definition
 Function calling
 Arguments(formal and actual)
 Return statement

Function calling statement: A statement which is used to call or invoke another function is
called function calling statement.
Eg. clrscr();

Caller function: A function which is calls another function is called “ caller function ”

Calee function: A function which is called by another function is called “ calle function ”

Types of Function:

Page 77

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

In C functions are divided into two types those are

1. predefined functions
2. user defined functions

Predefined Functions: A function whose meaning is already defined is called predefined


functions. Predefined functions are known by compiler.
Predefined functions are available in standard library those are available in header files.
Predefined functions are also called as library functions

Header files
|
Standard files
|
Predefined functions

Eg. getch()
clrscr()
isalnum()
isalpha()

User defined functions: A function whose meaning is defined by user is called user defined
functions.

Eg. raju()
anil()
sum()

Page 78

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Example Program of function components:

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“\n enter a value “); actual arguments
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b); caller function
c=sum(a,b); function calling statement
printf(“\n The sum is %d ”,c);
getch(); formal arguments function definition
} calee function

int sum(int x,int y) local variables


{
int z;
z=x+y; function definition
return z; function declaration
}

Return statement

Page 79

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Program – 1:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b);
c=sum(a,b);
printf(“\n The sum is %d ”,c);
getch();
}
int sum(int x,int y)
{
int z;
z=x+y;
return z;
}

Program – 2:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b);
c=sub(a,b);
printf(“\n The subtraction is %d ”,c);
getch();
}
int sub(int x,int y)
{
int z;
z=x-y;
return z;
}

Program – 3:
#include<stdio.h>
#include<conio.h>
main()
Page 80

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

{
int a,b,c;
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b);
c=mul(a,b);
printf(“\n The multification is %d ”,c);
getch();
}
int mul(int x,int y)
{
int z;
z=x*y;
return z;
}

Program – 4:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b);
c=div(a,b);
printf(“\n The reminder is %d ”,c);
getch();
}
int div(int x,int y)
{
int z;
z=x%y;
return z;
}

Page 81

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Categories of a function:

In c functions are bodily divided into four categories those are .

 function with return type with arguments


 function no return type with arguments
 function with return type no arguments
 function no return type no arguments

Function With Return Type With Arguments:

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b);
c=sum(a,b);
printf(“\n The sum is %d ”,c);
getch();
}
int sum(int x,int y)
{
int z;
z=x+y;
return z;
}

Function No Return Type With Arguments.

#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf(“\n enter a value “);
scanf(“%d”,&a);
printf(“\n enter b value “);
scanf(“%d”,&b);
sum(a,b);
printf(“\n The sum is %d ”,c);
Page 82

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

getch();
}
int sum(int x,int y)
{
int z;
z=x+y;
printf(“\n the sum is %d ”,z);
}

Function With Return Type No Arguments.

#include<stdio.h>
#include<conio.h>
main()
{
int c;
clrscr();
c=sum( );
printf(“\n the sum is %d ”,c);
getch();
}
int sum(int x,int y)
{
int z;
printf(“\n enter x value “);
scanf(“%d”,&x);
printf(“\n enter y value “);
scanf(“%d”,&y);
z=x+y;
return z;
}

Function No Return Type No Arguments.

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
sum( );
getch();
}
int sum(int x,int y)
{
int z;
printf(“\n enter x value “);
scanf(“%d”,&x);
printf(“\n enter y value “);
Page 83

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

scanf(“%d”,&y);
z=x+y;
printf(“\n the sum is %d ”,z);
}

Q :Write a c program to find factorial of given number using user defined functions .

#include<stdio.h>
#include<conio.h>
main()
{
int f ,n;
clarscr();
printf(“\n Enter N value ”);
scanf(%d”,&n);
f=fact(n);
printf(“\n the factorial is %d ”,f);
getch();
}
int fact (int x)
{
int z,i;
for(i=1;i<=x;i++)
{
z=z*i;
}
return z;
}
RECURSION

The technique of calling one function into in its body is known as “ recursion ” in
other words a function calls itself known as “ recursion ”
In below example factorial function is called in its body so it is the best example
for recursion.

#include<stdio.h>
#include<conio.h>
main()
{
int f ,n;
clarscr();
printf(“\n Enter N value ”);
scanf(%d”,&n);
f=fact(n);
printf(“\n the factorial is %d ”,f);
getch();
}
int fact (int x)
Page 84

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

{
if(x= =1)
Return 0;
else
Return x=fact(x-1);
}

Page 85

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

STORAGE CLASSES

Variables in C not only a data type but also a storage class storage class provides
information about variables, location and visibility ….. the storage class decides the portion of
the program with in the which the variables are recognized.

A variable storage class tells us


 when it stores
 what is the initial value of the variable
 what is the scope of the variable that means in which block value of the
variable is available
 what is the life time of the variable that means how long the variable is
exist

There are four types of storage classes are available in C

 automatic storage class


 register storage class
 static storage class
 external storage class

Automatic storage class: the feature of a variable defined in automatic storage class as.

Keyword: auto
Storage: main memory
Default value: An unpredictable value(garbage value)
Scope: local to the block in which it is defined.
Life time: Till the control remains with in the block in which the variable is defined.

Program :

#include<stdio.h>
#include<conio.h>
main()
{
auto int a;
clrscr();
printf(“\n A=%d ”,a);
getch();
}

Register storage class: the feature of a variable defined is register storage class as

Keyword: register
Storage: CPU registers
Default value: An unpredictable value (garbage value)
Scope: local to the block in which it is defined.

Page 86

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Life time: Till the control remains with in the block in which the variable is defined.

A variable stores in a CPU registers can always be accessed faster than the one i.e
stores main memory a variable which is used at many places in a program
Program :

#include<stdio.h>
#include<conio.h>
main()
{
register int a;
clrscr();
for(a=0;a<=100;a++)
printf(“\n A=%d ”,a);
getch();
}

Static storage class: feature of a variable defined is register storage class as

Keyword: static
Storage: main memory
Default value: zero
Scope: local to the block in which it is defined.
Life time: value of the variable presents between different function calls

Program :

#include<stdio.h>
#include<conio.h>
main()
{
static int a;
clrscr();
printf(“\n A=%d ”,a);
getch();
}

External Storage class: feature of a variable defined is register storage class as

Keyword: extern
Storage: main memory
Default value: zero
Scope: global
Life time: As long as the program execution doesn’t comes to end.

Program -1: Program -2:


Page 87

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

int a; int a=30;


#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
main() main()
{ {
clrscr(); extern int a;
printf(“\n A=%d ”,a); clrscr();
getch(); printf(“\n A=%d ”,a);
} getch();
}
STRUCTURES AND UNIONS

A structure is a collection of dissimilar elements it is a constrictive data structure.


It is a method of packing data of different types. A structure is a convenient for handling a group
of logically related data items.

It is used to declare a user defined data type. It is feature of C language to design


a powerful good program

Structure syntax:

struct structure name


{
data type 1 member 1;
data type 2 member 2;
data type 3 member 3;
.
.
.
data type n member n;
};
Ex:

struct student
{
int sno;
int sage;
char sname[10];
};
Declaring a structure variable:

struct structure name


{
Data type 1 member 1;
Data type 2 member 2;
Data type 3 member 3;
.
.
Page 88

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

.
Data type n member n;
} var-1, var-2, var-3;
(Or)
struct structure name var-1,var-2,var-3;

Ex:
struct student
{
int sno;
char sname[10];
}x,y,z;
(Or)
struct student x,y,z;
Initializing of structure:

Method -1:

Program:

#include<stdio.h>
#include<conio.h>
struct Test
{
int a;
flaot b;
char c;
}x={9,45.6,‘m’};
main()
{
clrscr();
printf(“\n %d ”,x.a);
printf(“\n %f ”,x.b);
printf(“\n %c ”,x.c);
getch();
}

Method -2:

Program:

#include<stdio.h>
#include<conio.h>
struct Test
{
int a;
flaot b;
char c;
Page 89

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

};
main()
{
struct Test x={9,45.6,‘m’}
clrscr();
printf(“\n %d ”,x.a);
printf(“\n %f ”,x.b);
printf(“\n %c ”,x.c);
getch();
}

Method -3:

Program:

#include<stdio.h>
#include<conio.h>
struct Test
{
int a;
flaot b;
char c;
}x;
main()
{
clrscr();
x.a=10;
x.b=45.6;
x.c= ‘s’;
printf(“\n %d ”,x.a);
printf(“\n %f ”,x.b);
printf(“\n %c ”,x.c);
getch();
}

Method -4:

Program:

#include<stdio.h>
#include<conio.h>
struct Test
{
int a;
flaot b;
char c;
};
main()
Page 90

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

{
struct Test x;
clrscr();
x.a=10;
x.b=45.6;
x.c= ‘s’;
printf(“\n %d ”,x.a);
printf(“\n %f ”,x.b);
printf(“\n %c ”,x.c);
getch();
}

Method -5:

Program:

#include<stdio.h>
#include<conio.h>
struct Test
{
int a;
flaot b;
char c;
};
main()
{
struct Test x;
clrscr();
printf(“\n enter x.a value”);
scanf(“%d”,&x.a);
printf(“\n enter x.b value”);
scanf(“%f”,&x.b);
printf(“\n enter x.c value”);
scanf(“%c”,&x.c);
printf(“\n %d ”,x.a);
printf(“\n %f ”,x.b);
printf(“\n %c ”,x.c);
getch();
}

Array with in a Structure:


The technique of placing an array as member of a structure is called “Array with
in a Structure ” this can be illustrated by the below example.

Program:
Page 91

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

#include<stdio.h>
#include<conio.h>
struct Student
{
int sno;
flaot sage;
char sname[10];
};
main()
{
struct Student x;
clrscr();
printf(“\n enter student number”);
scanf(“%d”,&x.sno);
printf(“\n enter student age”);
scanf(“%f”,&x.sage);
printf(“\n enter student name”);
scanf(“%s”,&x.sname);
printf(“\n the student number is %d ”,x.sno);
printf(“\n the student ahe is %f ”,x.sage);
printf(“\n the student name is %s ”,x.sname);
getch();
}

Page 92

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Nested Structure:
The technique of placing one structure variable as a member of anther structure is
called “nested structure” or “structure with in structure”

Program:

#include<stdio.h>
#include<conio.h>
struct Student
{
int sno;
flaot sage;
char sname[10];
struct Marks
{
int m;
int p;
int c;
}y;
}x;
main()
{
clrscr();
printf(“\n enter student number”);
scanf(“%d”,&x.sno);
printf(“\n enter student age”);
scanf(“%f”,&x.sage);
printf(“\n enter student name”);
scanf(“%s”,&x.sname);
printf(“\n enter Maths marks”);
scanf(“%d”,&x.y.m);
printf(“\n enter Physics marks”);
scanf(“%d”,&x.y.p);
printf(“\n enter Computers marks”);
scanf(“%d”,&x.y.c);
printf(“\n the student number is %d ”,x.sno);
printf(“\n the student ahe is %f ”,x.sage);
printf(“\n the student name is %s ”,x.sname);
printf(“\n the Maths marks is %d ”,x.y.m);
printf(“\n the Physics marks is %d ”,x.y.p);
printf(“\n the Computers marks is %d ”,x.y.c);
getch();
}

Page 93

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

POINTERS

Pointer is a dynamic feature of “C” language it is a powerful tool to write a good


program there is a no of reasons to using a pointers .

 a pointer enables us to access a variable i.e. defined outside the function


 pointer reduces the length of and compellability of the program
 pointers are increases the execution speed of the program

Under Standing the Pointers:

variable Variable value address

a----------> reference a 10 173


10--------> value
173-------> address
p 173 111

Pointer is a variable which is used to store the address of a memory location i.e. a pointer is a
variable which can holds the address of another variable

 a pointer variable always pointes to only one variable


 a variable have so many pointers
 a pointer of one type is always points to variable of same type

Declaring pointers:

Syntax: datatype *pointername;

Ex 1. int *p;

In above example p is a name of the pointer variable and it is always points to any
other integer variable

Ex 2. float *p;

In above example p is a name of the pointer variable and it is always points to any
other float variable

Initializing pointer variable:

Syntax: pointer name =&variablename;

Ex. p=&a;

Page 94

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Accessing a variable through its pointers:

#include<stdio.h>
#include<conio.h>
main()
{
int a,*p;
clrscr();
p=&a;
printf(“\n A is stored at %d ”,p);
getch();
}

Q. Write a c program for addition of two numbers using pointers:

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,*p,*q*,r;
clrscr();
p=&a;
p=&b;
p=&c;
printf(“\n enter a value ”);
scanf(“%d”,p);
printf(“\n enter b value ”);
scanf(“%d”,q);
*r=*p+*q;
printf(“\n the result is %d ”,c);
getch();
}

Pointer Vs Functions

The communication between caller function and calee function is possible through
passing values using arguments and return statements.
This can be achieved in two ways those are.
 Call by value
 Call by address

Call By Value: In this is technique the value of the actual arguments are passed to formal
arguments. If any changes occur to the formal arguments doesn’t effects the actual arguments.
Why because here only the value of the actual arguments are passed from caller function to calle
function.

Page 95

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

Program:

#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
printf(“\n enter b value ”);
scanf(“%d”,&b);
printf(“\n before swapping a= %d b=%d ”,a,b);
swap(a,b);
printf(“\n after swapping a= %d b=%d ”,a,b);
getch();
}
swap(int x, int y)
{
int z;
z=x;
x=y;
y=z;
}

Call By Adress: In this is technique the value of the actual arguments are passed to formal
arguments. If any changes occur to the formal arguments will effects the actual arguments. Why
because here the address of the actual arguments are passed from caller function to calle
function.
Program:

#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf(“\n enter a value ”);
scanf(“%d”,&a);
printf(“\n enter b value ”);
scanf(“%d”,&b);
printf(“\n before swapping a= %d b=%d ”,a,b);
swap(&a,&b);
printf(“\n after swapping a= %d b=%d ”,a,b);
getch();
}
swap(int *x, int *y)
{
Page 96

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

int *z;
*z=*x;
*x=*y;
*y=*z;
}

Page 97

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

FILE MANAGEMENT IN C

Many real life problems involving large volumes of data and in each situation the console
oriented programming post the major problems
1. it becomes time consuming to handle large volumes of data
2. the entire data is lost when either the program is terminated or computer is turned off

To avoid these problems the concept of file management is used

File: A file is place on the disk where a group of related data items are stored

File management: we can perform the following operators on file using C language

1. Naming a file
2. Opening a file
3. IO operations on file
4. Closing a file

Naming a file: to name a file there some specific rules those are depend upon the OS the rules
for naming a file in DOS

A file name has two parameters

Primary name
Secondary name

1. Primary name should not exceed 8 characters extension name should not exceed 3 characters
and they are separated by dot(.)
2.white spaces are not allowed
3.special characters are not allowed

Opening a File: To open a new file or open an existing file we have fopen( ) function in stdio.h
header file

Syntax: FILE *file pointer name;

filepointername =fopen(“file name”,“ mode”);

Ex. FILE *f;

f = fopen(“rama.txt”,“r”);

Opening a File: a file must be closed as soon as all operations on it have been completed it
prevents the accidental misuse of the file to close any file we use fclose( ) function which is in
stdio.h header file

Syntax: FILE *file pointer name;


Page 98

Programming in “ C ”
GNANAJYOTHI DEGREE COLLEGE
NARASANNAPETA-SRIKAKULAM

filepointername =fclose(“file name”,“ mode”);

Ex. FILE *p;

p = fclose(“rama.txt”,“r”);

C-pre processor: the c-preprocessor is a collection of special statements called directories that
are executed at the beginning of the compilation process #define ,#if , #else ,#line these are all
the pre-processor directive the preprocessor directives are usually appear at the beginning of a
program.

Program:

#define ramu 10
main()
{
int i,s=0;
clrscr();
for(i=0;i<=ramu;i++)
{
s=s+i;
}
printf(“\n the sum is %d ”,s);
getch();
}

Page 99

Programming in “ C ”

You might also like