Professional Documents
Culture Documents
CP M2 Ktunotes - in
CP M2 Ktunotes - in
CP M2 Ktunotes - in
BASIC ELEMENTS OF C
FLOW CHARTS
A Flow Chart depicts pictorially the sequence in which instructions are
carried out in an algorithm. Flow charts are used not only as aids in developing algorithms but
also to document algorithms.
For easy visual recognition a standard convention is used in drawing flow charts.
Symbol Use
E S . I N
KTU NOTComputational steps or processing function of a
program
Rectangles
Parallelograms
Flow Lines
Loop
Subroutine
E S . I N
ALGORITHM
Definition KTU NOT
An algorithm is a precise specification of a finite sequence of instructions
to be carried out in order to solve a given problem. Each instruction tells what task is to be
performed.
Properties of Algorithm
1) Each algorithm will be logically enclosed by two statements START and STOP.
2) To accept data from user, the INPUT or READ statements are to be used.
3) To display any user message or the content in a variable, PRINT statement will be
used. Note that the message will be enclosed within quotes.
4) The arithmetic operators that will be used in the expressions are
„=‟ Assignment (the left-hand side of ‟=‟ should always be a single variable)
„+‟ Addition „-‟ Subtraction
„*‟ Multiplication „/‟ Division
5) In propositions, the commonly used relational operators will include
„>‟ Greater than „<=‟ Less than or equal to
„<‟ Less than „=‟ Equality
„>=‟Greater than or equal to „!=‟ Non-equality
„AND‟ Conjunction
E S . I N
6) The most commonly used logical operators will be
„OR‟ Disjunction
KTU
„NOT‟ Negation NOT
DEVELOPMENT OF ALGORITHMS FOR SIMPLE PROBLEMS
1) Write the algorithm for finding the sum of any two numbers.
Solution) Let the two numbers be A and B and let their sum be equal to C. Then, the desired
algorithm is given as follows:
Step 1: Start
Step 3: Input A, B
Step 4: C=A+B
Step 5: Print C
Step 6: Stop
Solution) Let the two variables be A and B. Consider C to be a third variable that is used to store
the value of one of the variables during the process of interchanging the values. The desired
algorithm is given as follows:
Step 1: Start
Step 3: Input A, B
Step 4: C=A
Step 5: A=B
Step 6: B=C
Step 7: Print A, B
Step 8: Stop
S . I N
3) Write an algorithm that compares two numbers and prints either the message identifying
E
NOT
the greater number or the message stating that both numbers are equal.
KTU
Solution) Here, two variables, A and B, are assumed to represent the two numbers that are being
compared. The desired algorithm is given as follows:
Step 1: Start
Step 3: Input A, B
Step 7: Stop
Step 1: Start
Step 3: Input N
Step 5: R=N-Q*2
Print “ N is Odd “
Step 8: Stop
E S . I N
NOT
5) Print the largest number among three numbers
KTU
Solution) Let the three numbers be represented by A, B, and C. The desired algorithm is
Step 1: Start
Step 2: Print “Enter the three numbers”
Step 3: Input A, B, C
Step 4: If A > B Then
If A > C Then
Print A
Else
Print C
Else If B > C Then
Print B
Else
Print C
Step 5: Stop
Solution) This problem illustrates the use of iteration or loop construct. Let the variable be
represented by C. The algorithm for the problem is:-
Step 1: Start
Step 2: C = 1
Step 3: Print C
Step 4: C = C+1
Step 6: Stop
E S . I N
KTU NOT
Start
Read A,B
C=A+B
Print C
Stop
Start
SUM=0
N=0
N=N+1
SUM=SUM+N
E S . I N
KTU
No
NOTIS
N = 50?
Yes
Print SUM
Stop
Start
Read A,B, C
No No
E S . I N
KTU NOT
Print B Print C Print A
Stop
An Overview of a C program
Documentation Section
Link Section
Definition Section
{
E S . I N
NOT
Declaration part
KTU
Executable part
Subprogram section
Function 1
Function 2
- (User-defined functions)
-
Function n
Documentation Section
The documentation section consists of a set of comment lines giving the name of
the program, the author and other details, which the programmer would like to use later.
The link section provides instructions to the compiler to link functions from the system
library.
Definition Section
There are some variables that are used in more than one function. Such variables are
called global variables and are declared in the global declaration section that is outside of all the
functions. This section also declares all the user-defined functions.
Every C program must have one main() function section. The program will always
begin by executing the main() function, which may access other functions. This section
contains two parts, declaration part and executable part. The declaration part declares all the
variables used in the executable part. There is at least one statement in the executable part. These
two parts must appear between the opening and the closing brace. The closing brace of the main
S . I N
function section is the logical end of the program. All statements in the declaration and
E
NOT
executable parts end with a semicolon (;).
Subprogram section
KTU
The subprogram section contains all the user-defined functions that are called in the
main function. User-defined functions are generally placed immediately after the main function,
although they may appear in any order.
Character Set
The characters in C are grouped into the following categories:
1. Letters
2. Digits
3. Special characters
4. White space
Letters Digits
Upper Case A to Z 0 to 9
Lower Case a to z
Special Characters
. Period ^
Caret
; Semicolon
E S
*
. I N
Asterisk
?
Colon
+
Minus Sign
Plus Sign
%
Percentage Sign
White Space
Blank space
Horizontal tab
Carriage return
New line
Form feed
.C TOKENS
E S . I N
NOT
In a C program the smallest individual units are known as C tokens. C has six types
KTU
of tokens. C programs are written using these tokens and the syntax of the language.
C TOKENS
main $#
amount {}
.default .if
E S . I N
.struct .
.do
T U
.int
N OT .switch .
Identifiers refer to the names of variables, functions and arrays. These are user-defined names
and consist of a sequence of letters and digits, with a letter as a first character. Both uppercase
and lowercase letters are permitted, although lowercase letters are commonly used. The
underscore character is also permitted in identifiers.
CONSTANTS
Real
E S . I N String
OT
Integer Single Character
Constants
Integer Constants
An integer constant refers to a sequence of digits. There are three types of integers,
namely decimal integer, octal integer and hexadecimal integer.
An octal integer constant consists of any combination of digits from the set 0 through 7
with a leading 0. Examples are 037 0 0435
These numbers are shown in decimal notation, having a whole number followed by a decimal
point and the fractional part.
A real number may also be expressed in exponential (or scientific) notation. For example
the value 215.65 may be written as 2.1565e2 in exponential notation. e2 means multiply by 102 .
The general form is:
mantissa e exponent
The mantissa is either a real number expressed in decimal notation or an integer. The
S . I N
exponent is an integer number with an optional plus or minus sign.
E
Single Character Constant
KTU NOT
A Single Character constant represent a single character which is enclosed in a pair
of single quote marks. Example for character constants are
All character constants have an equivalent integer value which is called ASCII Value.
String Constant
A string constant is a set of characters enclosed in double quotation marks. The characters in a
string constant sequence may be a alphabet, number, special characters and blank space.
Example of string constants are
E S . I N
KTU NOT
VARIABLES
A variable is a data name that may be used to store a data value. Unlike constants that
remain unchanged during the execution of a program, a variable may take different values at
different times during execution.
1. They must always begin with a letter, although some systems permit underscore as
DATA TYPES
There are many data types in C language.
The size and range of each data type is given in the table below
Integral Type
Integer Character
char
signed unsigned type
signed char
int unsigned int
E S . I N
KTU NOT
Floating point Type
void
float double Long double
Integer Types :
Integers are whole numbers with a machine dependent range of values. A
good programming language as to support the programmer by giving a control on a range
of numbers and storage space. C has 3 classes of integer storage namely short int, int and
long int. All of these data types have signed and unsigned forms. A short int requires half
the space than normal integer values. Unsigned numbers are always positive and
TYPE
.
SIZE (Bits) Range
E S I N
char or signed char
8
NOT -128 to 127
0 to 255
Every variable used in the program should be declared to the compiler. The declaration
does two things.
Character
E S . I
char
N
Unsigned Character
signed char
Signed Short Integer signed short int (or) short int (or) short
Signed Long Integer signed long int (or) long int (or) long
In C language a user can define an identifier that represents an existing data type. The
user defined datatype identifier can later be used to declare variables. The general syntax
is
here type represents existing data type and „identifier‟ refers to the „row‟ name given to
the data type.
Example:
Here salary symbolizes int and average symbolizes float. They can be later used to
declare variables as follows:
E S . I N
NOT
salary dept1, dept2;
KTU
average section1, section2;
Therefore dept1 and dept2 are indirectly declared as integer datatype and section1 and
section2 are indirectly float data type.
The second type of user defined datatype is enumerated data type which is defined
as follows.
The enumerated variables V1, V2, ….. Vn can have only one of the values
value1, value2 ….. value n
An operator is a symbol that tells the computer to perform certain mathematical or logical
manipulations. Operators are used in programs to manipulate data and variables. C operators can
be classified into a number of categories. They include:
1. Arithmetic operators
2. Relational operators.
3.
4.
Logical operators.
Assignment operators.
E S . I N
5.
6.
7.
Conditional operators.
Bitwise operators.
NOT
Increment and decrement operators.
KTU
8. Special operators.
EXPRESSIONS
ARITHMETIC OPERATORS
Operators Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
When both the operands in a single arithmetic expression are integers, the
expression is called an integer expression, and the operation is called integer arithmetic. An
arithmetic operation involving only real operands is called real arithmetic. When one of the
operands is real and the other is integer, the expression is called a mixed -mode arithmetic
expression.
UNARY OPERATORS: -
Operators that act upon a single operand to produce a new value, such
operators are known as unary operators. Unary operators usually precede their single operands.
The most common unary operation is unary minus, where a numerical constant, variable or
expression is preceded by a minus sign.
The unary minus operation is distinctly different from the arithmetic operator
which denotes subtraction (-). The subtraction operator requires two separate operands.
RELATIONAL OPERATORS:-
E S . I N
KTU NOT
We often compare two quantities and depending on their relation, take certain
decisions. The value of a relational expression is either one or zero. It is one if the specified
relation is true and zero if the relation is false.
Operator Meaning
Closely associated with the relational operators are the following two equality
operators.
== Equal to
!= Not equal to
The equality operators fall into a separate precedence group, beneath the
relational operators. These operators also have a left-to-right associativity.
LOGICAL OPERATORS
Operator Meaning
|| Logical OR
! Logical NOT
The logical operators && and || are used when we want to test
more than one condition and make decisions. The result of a logical and operation will be true
S . I N
only if both operands are true, whereas the result of a logical or operation will be true if either
E
NOT
operand is true or if both operands are true. In other words, the result of a logical or operation
KTU
will be false only if both operands are false. The logical not (!) operator that negates the value of
a logical expression; i.e., it causes an expression that is originally true to become false and vice
versa.
ASSIGNMENT OPERATORS
identifier = expression
The Increment (++) and decrement (--) operators are unary operators and they
require variable as their operands. The increment operator causes its operand to be increased by
1, whereas the decrement operator causes its operand to be decreased by 1. The operand used
with each of these operators must be a single variable. The precedence and associativity of ++
and – operators are the same as those of unary + and unary -.
Post-increment/decrement operator: - If the operator follows the operand (e.g., i++/i--), then it is
known as post-increment/decrement operator. When postfix is used with a variable in an
expression, the expression is evaluated first using the original value of the variable and then the
variable is incremented/decremented by one.
CONDITIONAL OPERATORS
Simple conditional operations can be carried out with the conditional operator (? : ).
An expression that makes use of the conditional operator is called a conditional expression. Such
an expression can be written in place of the more traditional if-else statement.
E S . I N
evaluated first. If expression 1 is true (i.e., if its value is nonzero), then expression 2 is evaluated
and this becomes the value of the conditional expression. However, if expression 1 is false (i.e.,
expression.
KTU NOT
if its value is zero), then expression 3 is evaluated and this becomes the value of the conditional
BITWISE OPERATORS
C provides six operators for bit manipulation; these may only be applied to
integral operands, that is, char, short, int and long whether signed or unsigned. These operators
may not be applied to float or double.
Operator Meaning
| bitwise OR
^ bitwise exclusive OR
~ one‟s complement(unary)
SPECIAL OPERATORS
The special operators include the comma operator, sizeof operator, pointer
operators (& and *) and member selection operators (. and -> ).
Operator Meaning
, comma operator
sizeof sizeof operator
& and * pointer operator
. and ->
E . I N
member selection operator
S
NOT
The comma operator can be used to link the related
KTU
expressions together. The sizeof operator is normally used to determine the lengths of arrays and
structures when their sizes are not known to the programmer. It is also used to allocate memory
space dynamically to variables during execution of a program. Pointer operators & and * are
used for pointer operations and the member selection operators are used for selecting the
member of a structure.
*
S
Multiplication
E . I N Left to right 3
/
%
Division
N
Modulus
OT
+
-
<<
K TU
Addition
Subtraction
Left shift
Left to right
Left to right
4
5
>> Right shift
< Less than Left to right 6
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equality Left to right 7
!= Inequality
& Bitwise AND Left to right 8
^ Bitwise XOR Left to right 9
| Bitwise OR Left to right 10
&& Logical AND Left to right 11
|| Logical OR Left to right 12
?: Conditional expression Right to left 13
= 14
*= /= %= Assignment operators Right to left
+= -= &=
variable = expression ;
Variable is any valid C variable name. When the statement is encountered, the
expression is evaluated first and the result then replaces the previous value of the variable on the
left hand side.
Example:
E S . I N
NOT
X *= -2 * ( Y + Z ) / 3 where X=2, Y=3 and Z=6
KTU
The arithmetic operations precede the assignment operation.
Therefore the expression (Y +Z) will be evaluated first, resulting in 7.
Then the value of this expression will be multiplied by -2, yielding -18.
This product will then be divided by 3, resulting in -6.
This value is then multiplied by the original value of X (i.e., 2) to yield the final
result of -12.
X = a- b/3+c*2-1 and a=9, b=12, c=3.
X= 9-4+3*2-1
X= 9-4+6-1
X = 5+6-1
X = 11-1
X = 10
TYPE CONVERSIONS
If one of the operands is long double, the other will be converted to long double
and the result will be long double.
Else, if one of the operands is double, the other will be converted to double and
the result will be double.
Else, if one of the operands is float, the other will be converted to float and the
result will be float.
Else if, one of the operands is unsigned long int, the other will be converted to
unsigned long int and the result will be unsigned long int.
Else, if one of the operands is long int and the other is unsigned int, then
If unsigned int can be converted to long int, the unsigned int operand will be
converted as such and the result will be long int.
Else, both operands will be converted to unsigned long int and the result will be
unsigned long int.
S . I N
Else, if one of the operands is long int, the other will be converted to long int and
E
NOT
the result will be long int.
KTU
Else, if one of the operands is unsigned int, the other will be converted to
unsigned int and the result will be unsigned int.
. I N
Single characters can be entered into the computer using the C library
E S
NOT
function getchar. It returns a single character from a standard input device (typically a
KTU
keyboard). The function does not require any arguments.
Example:
char c;
------------
c = getchar ( );
Single characters can be displayed using the C library function putchar. The
putchar function transmits a character to a standard output device. The character being
transmitted will normally be represented as a character-type variable. It must be expressed as an
argument to the function, enclosed in parentheses, following the word putchar.
where the character variable refers to some previously declared character variable.
Example:
char c;
------------
putchar (c ) ;
. I N
statement causes the current value of c to be transmitted to the standard output device
E S
NOT
where it will be displayed.
KTU
The putchar function can be used with loops to output a string.
The control string specifies the field format in which the data is to be
entered and the arguments arg 1, arg 2, ….. ,arg n specify the address of locations where
the data is stored. Control string and arguments are separated by commas.
Control string (also known as format string) contains field specifications, which direct
the interpretation of input data. It may include:
Field (or format) specifications, consisting of the percentage sign %, a conversion
character (or type specifier), and an optional number, specifying the field width.
Whitespace characters (Blanks, tabs, or newlines).
The arguments are written as variables or arrays, whose types match the
corresponding character groups in the control string. Each variable name must be preceded by an
ampersand (&). However, array names should not begin with an ampersand.
Example
main()
{
char a[20];
int b;
float c;
………
scanf (“ %s %d %f “,a, &b, &c );
………….
E S . I N
}
KTU NOT
Within the scanf function, the control string is “%s %d %f “. It contains three
character groups. The first character group %s represents a string.
Numerical variables b and c are preceded by ampersands within the scanf function.
The printf function can be used to output any combination of numerical values,
single characters and strings. The printf function moves data from the computer‟s memory to the
standard output device, whereas the scanf function enters data from the standard input device and
stores it in the computer‟s memory.
S . I N
The second character group %d represents a decimal integer value.
E
NOT
The third character group %f represents a floating-point value.
KTU
The arguments are not preceded by ampersands within the printf function.
More frequently used conversion characters for input/output are listed below.
Commonly Used Conversion Characters For Data Input/output
Conversion
character Meaning
c dataitem is a single character
d dataitem is a decimal integer
e dataitem is a floating-point value
f dataitem is a floating-point value
g dataitem is a floating-point value
h dataitem is a short integer
i dataitem is a decimal, hexadecimal or octal integer
o dataitem is a octal integer
s dataitem is a string followed by a whitespace character
(the null character \0 will automatically be added at the end)
The gets and puts functions facilitates the transfer of strings between the computer and the
standard input/output devices. Each of these functions accepts a single argument. The argument
must be a string, (e.g., a character array). The string may include whitespace characters.
In the case of gets, the string will be entered from the keyboard, and will terminate with
a newline character (i.e., the string will end when the user presses the enter key). The gets and
puts functions offer simple alternatives to the use of scanf and printf for reading and displaying
strings.
Example
main()
{ char a[40];
gets(a); /* reads a line of text into the computer */
puts(a); /* writes the text back out in its original form */
}
DECISION MAKING AND BRANCHING
E S . I N
NOT
C language possesses some decision making capabilities by supporting the following
KTU
statements:
if statement
Switch statement
Conditional operator statement
goto statement
if statement
simple if statement
if.....else statement
Nested if…else statement
Else if ladder
if ( expression ) statement;
or
if ( expression )
{ statement-block; }
statement-x;
Entry
E S . I N
KTU NOT True
if(expression)
Statement-block
Statement-x
Next statement
The if…else statement is an extension of the simple if statement. The general form is:
if (expression )
{ Statement-block 1;
else
{ Statement-block 2;
Statement-x;
Entry
E S . I N Flow chart of if…else control
KTU NOT
True false
if(expression)
Statement-block 1 Statement-block 2
Statement-x
When a series of decisions are involved, we may have to use more than one
if..else statement in nested form. The general form is:
If ( expression 1)
If ( expression 2 ) { statement-1 ;}
else { statement-2;}
else
{ If ( expression { statement-3 ;}
Else { statement-4;}
Statement-x;
E S . I N
NOT
One complete if-else statement will be executed if expression-1 is true,
KTU
and another complete if-else statement will be executed if expression-1 is false.
else if ladder
The else if ladder is used when multipath decisions are involved. A multipath
decision is a chain of ifs in which the statement associated with each else is an if.
If (expression-1)
Statement-1;
else if ( expression-2)
Statement-2;
else if ( expression-3)
Statement-3;
else if (expression-n)
else
Default-statement;
Statement-x;
Switch statement
E S . I N
KTU { NOT
switch ( expression )
case value-1:
statement-1;
break;
case value-2:
statement-2;
break;
………
………
case value-n:
statement-n;
break;
default :
default statement;
break;
}
statement-x;
The break statement at the end of each statement signals the end of a
particular case and causes an exit from the switch statement, transferring the control to the
statement-x following the switch. The default is an optional case. When present, it will be
executed if the value of the expression does not match with any of the case values. If not present,
no action takes place if all matches fail and the control goes to the statement-x.
Entry
Switch
E S . I N
NOT
(expression)
KTU Expression=value-1
Statement-1
Expression=value-2
Statement-2
No match (default)
Default statement
Statement-x
Example:
a = (10>6) ? 10 : 6 ;
goto statement
E S . I N
NOT
C supports the goto statement to branch unconditionally from one point to
KTU
another in the program. The goto requires a label in order to identify the place where the branch
is to made. A label is any valid variable name, and must be followed by a colon. The label is
placed immediately before the statement where the control is to be transferred. The general
forms of goto and label statements are:
The „label:‟ can be anywhere in the program either before or after the „goto label;‟
statement. goto breaks the normal sequential execution of the program. If the label: is before the
Example1:
goto skip_point;
printf ("This part was skipped.\n");
skip_point: /* skip_point is a label*/
printf("Hi there!\n");
Output:
Example2:
E S . I N
NOT
break; /* exit from the goto*/
KTU
goto skip_point;
Output:
The text "This part was repeated" and "Hi there!" are printed .
Test false
Condition?
true
true
Test
E S . I N false
KTU NOT
entry-controlled loops exit-controlled loops
The C language provides three constructs for performing loop operations. They are:
While(condition)
Example
main()
{ int n,sum;
n=1; /*initialization*/
sum=0;
{ sum=sum+n;
n=n+1;
E S .
/*incrementing*/ I N
} KTU
} printf(“sum =%d”, sum);
NOT
Output
sum =15
THE do STATEMENT
On some occasions it might be necessary to execute the body of the loop before the
test is performed. Such situations can be handled with the help of the do statement.
do
} while(condition);
Example
main()
{
int n,sum;
n=1; /*initialization*/
sum=0;
do
{
sum=sum+n;
n=n+1; /*incrementing*/
}
whie( n<=5)
printf(“sum =%d”, sum);
/* testing*/
E S . I N
Output
}
KTU NOT
sum =15
The for loop is another entry-controlled loop. The general form of the for loop is :
KTU NOT
for(j=1 ; j!=5 ; j++)
{
------------- inner loop outer loop
---------------
}
---------------
---------------
}
------------
--------------
When a break statement is encountered inside a loop, the loop is immediately exited
and the program continues with the statement immediately following the loop. When the loops
are nested, the break would only exit from the loop containing it. That is, the break will exit only
a single loop.
for(--------)
----------
-----------
if(error)
break;
-------
--------
E S . I N
}
KTU
-----------
NOT
Example
if(loop==10)
break;
printf("%i\n",loop);
output
for(--------)
----------
-----------
if(---------)
continue;
-------
--------
E S . I N
}
KTU
-----------
NOT
Examples:
for (loop=0;loop<100;loop++)
if (loop==50)
continue;
printf ("%i\n",loop);
#include <stdio.h>
main()
{
printf(" Hello \n");
Output
Hello
#include <stdio.h>
E S . I N
NOT
main()
KTU
{
int a,b,c;
a=10;
b=5;
c=a+b;
Output
The result is : 15
3) Program
#include <stdio.h>
main()
{
int a=100;
int b=2;
int c=25;
int d=4;
int result;
result= a-b; /* Subtraction */
printf(“a- b= %d \n “ , result);
S . I N
printf(“ b*c= %d \n “ , result);
E
KTU
result = a/c;
NOT
printf(“ a/c= %d \n “ , result);
/* division */
printf(“ a + b * c= %d \n “ , result);
Output
a - b =98
b * c= 50
a / c =4
a + b * c=150
4) Program
#include<stdio.h>
main()
{
int a=25;
int b=2;
int result;
float c=25.0;
float d=2.0;
printf(“6 + a / 5 * b = %d \n”, 6+a/5*b);
printf(“a / b * b = %d \n”, a/b* b);
printf(“c / d * d = %f \n”, c/d*d);
printf(“- a = %d \n”, -a);
}
E S . I N
NOT
Output
KTU
6 + a/ 5 * b =16
a / b * b = 24
c / d * d =25.00000
-a = -25
#include<stdio.h>
main()
{
int a;
Output
The value of a is = 16
E S . I N
NOT
The value of a is = 14
KTU
The value of a is = 2
#include<stdio.h>
main()
Output
The result is 8
S . I N
7) Program for inputting a number from the keyboard and display it
E
#include<stdio.h>
int main(void ) KTU NOT
{
int x;
scanf(“%d”, &x);
printf(“You typed %d \n”, x);
return 0;
}
Input : 100
Output: You typed 100
9) Program
E S . I N
#include<stdio.h>
KTU NOT
/* example involving both gets and puts */
main()
{
char str[150];
printf("Type a string (Less than 150 characters) : " );
gets(str);
printf("The string typed is :");
puts(str);
return 0;
}
Output
Type a string (Less than 150 characters) : test
Branching
10) /* Program to illustrate the if construct */
#include<stdio.h>
void main()
{
int i;
printf( “ Enter a number: ”);
scanf(“%d”, &i);
if (i%5 = =0)
printf(“Number entered is divisible by 5. \n ”);
}
E S . I N
Input:
KTU
Enter a number: 60
NOT
Output: Number entered is divisible by 5.
{
int input;
printf( “Enter an integer :”);
scanf(“%d”, &input);
if ( input )
printf(“It is non-zero. \n ” );
else
printf(“It is zero. \n” );
}
Output
Enter an integer : 58
It is non-zero.
E S . I N
NOT
Enter an integer : 0
KTU
It is zero.
12) /* Example program using If else ladder to grade the student according to the following
rules.
Marks Grade
70 to 100 DISTINCTION
60 to 69 IST CLASS
50 to 59 IIND CLASS
40 to 49 PASS CLASS
0 to 39 FAIL
# include <stdio.h>
int marks;
E S
printf ("\n Pass class") ;
. I N
else
KTU
printf ("Fail");
NOT
}
Output
Enter marks 85
Distinction
Enter marks 54
Second class
printf("%d is greater",a);
else
printf("%d is greater",c);
}
else
{
E S . I N
if(b>c)
KTU NOT
printf("%d is greater",b);
else
printf("%d is greater",c);
}
Output
Enter three numbers:89 66 45
89 is greater
14) /* program to carry out the arithmetic operations addition, subtraction, multiplication and
division between two variables */
#include<stdio.h>
void main()
{
float value1,value2;
char op;
E S . I N
NOT
printf("Enter an expression:");
KTU
scanf("%f %c %f",&value1,&op,&value2);
switch(op)
{
case '+':printf("%f \n",value1+value2);
break;
case '-':printf("%f \n",value1-value2);
break;
case '*':printf("%f \n",value1*value2);
break;
case '/': if(value2==0)
printf("Division by zero. \n");
else
printf("%f \n",value1/value2);
break;
default : printf("Unknown operator\n");
#include<stdio.h>
void main()
{
int n,c;
long int f=1;
clrscr();
E S . I N
NOT
printf("\n Enter the number :");
KTU
scanf("%d",&n);
if(n<0)
goto end;
for(c=1; c<=n; c++)
f*=c;
printf("\n Factorial is %ld",f);
end:
getch();
}
Output
LOOPING
#include<stdio.h>
main()
int i, count=0;
scanf(%d”,&i);
E S . I N
NOT
while(i!= -1)
KTU
{
sum= sum+ i;
scanf(“%d”,&i);
average= =sum/count;
Output
12 14 16 18 -1
/* program to display the numbers which are divisible by 5 within the range 1-50*/
#include<stdio.h>
main()
do
printf(“%d\t”, i);
count++;
E S . I N
/* increment the count value */
}
i=i+5;
KTU NOT
while(count<=50);
Output
5 10 15 20 25 30 35 40 45 50
main()
int i, even;
for( i=1;i<=10;i++)
even= i%2;
if(even==0)
printf (“%d\t”,i);
Output
E S . I N
KTU
Even numbers are: 2 4 6 8 10
NOT
19) /*Program for illustrating break construct */
/* program to display the numbers 1-10(when the number is 5 then exit from the loop) */
#include<stdio.h>
main()
int i=1;
while( i<=10)
break; /* when i=5 the break causes exit from the while loop*/
printf(“%d”,i);
i++;
Output
E S . I N
#include<stdio.h>
int i=1,sum=0;;
while( i<=6)
if( i==3)
continue;
sum=sum+i;
i++;
Output
E S . I N
KTU NOT