C Module 2notes
C Module 2notes
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
Rectangles
Parallelograms
Flow Lines
Loop
Subroutine
ALGORITHM
Definition
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
6) The most commonly used logical operators will be
„AND‟ Conjunction
„OR‟ Disjunction
„NOT‟ Negation
Step 1: Start
Step 3: Input A, B
Step 4: C=A+B
Step 5: Print C
Step 6: Stop
2) Construct the algorithm for interchanging (Swapping) the numeric values of two
variables.
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
3) Write an algorithm that compares two numbers and prints either the message identifying
the greater number or the message stating that both numbers are equal.
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
4) Write an algorithm to check whether a number given by the user is odd or even.
Step 1: Start
Step 3: Input N
Step 5: R=N-Q*2
Print “ N is Odd “
Step 8: Stop
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
6) Construct an algorithm for incrementing the value of a variable that starts with an initial
value of 1 and stops when the value becomes 5.
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
Star
t
Read A,B
C=A+B
Print C
Stop
2) Draw a Flow Chart to find the sum of the first 50 natural numbers.
Start
SUM=0
N=0
N=N+1
SUM=SUM+N
No
IS
N = 50?
Yes
Print SUM
Stop
3) Draw a Flow Chart to find the largest of three numbers A, B, C
Start
Read A,B, C
No No
Stop
STRUCTURE OF C PROGRAM
Every C program consists of one or more modules or building blocks
called functions. A function is a subroutine that may include one or more statements designed to
perform a specific task.
An Overview of a C program
Documentation Section
Link Section
Definition Section
{
Declaration part
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.
Link Section
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
function section is the logical end of the program. All statements in the declaration and
executable parts end with a semicolon (;).
Subprogram section
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
C Character-Set Table
Letters Digits
Upper Case A to Z 0 to 9
Lower Case a to z
Special Characters
. Period ^
Caret
; Semicolon * Asterisk
%
Percentage Sign
White Space
Blank space
Horizontal tab
Carriage return
New line
Form feed
C TOKENS
In a C program the smallest individual units are known as C tokens. C has six types
of tokens. C programs are written using these tokens and the syntax of the language.
C TOKENS
main $#
amount {}
KEYWORDS AND IDENTIFIERS
Every C word is classified as either a keyword or an identifier. All keywords
have fixed meanings and these meanings cannot be changed. Keywords serve as basic building
blocks for program statements. All keywords must be written in lowercase.
Default if struct
Do int 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
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
exponent is an integer number with an optional plus or minus sign.
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
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
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
consume all the bits for the magnitude of the number. The long and unsigned integers are
used to declare a longer range of values.
Floating Point Types :
Floating point number represents a real number with 6 digits precision.
Floating point numbers are denoted by the keyword float. When the accuracy of the
floating point number is insufficient, we can use the double to define the number. The
double data type number uses 64 bits giving a precision of 14 digits. To extend the
precision further we can use long double which consumes 80 bits of memory space.
Void Type :
The void type has no values. Using void data type, we can specify the type
of a function. The type of a function is said to be void when it does not return any values
to the calling function.
Character Type :
A single character can be defined as a defined as a character type of data.
Characters are usually stored in 8 bits of internal storage. The qualifier signed or
unsigned can be explicitly applied to char. While unsigned characters have values
between 0 and 255, signed characters have values from –128 to 127.
Size and Range of Data Types on 16 bit machine.
Every variable used in the program should be declared to the compiler. The declaration
does two things.
Character 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:
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
Example:
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. Logical operators.
4. Assignment operators.
5. Increment and decrement operators.
6. Conditional operators.
7. Bitwise operators.
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:-
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.
Operator Meaning
== 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
only if both operands are true, whereas the result of a logical or operation will be true if either
operand is true or if both operands are true. In other words, the result of a logical or operation
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 -.
Pre-increment/decrement operator: - If the operator precedes the operand (e.g., ++i/--i), then it is
called as pre-increment/decrement operator .when prefix is used in an expression, the variable is
incremented/decremented first and then the expression is evaluated using the new value of the
variable.
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.
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)
The bitwise AND operator compares the corresponding bits of its
operands and produces a 1 when both bits are 1, and 0 otherwise. Bitwise OR operator compares
the corresponding bits of its operands and produces a 0 when both bits are 0, and 1 otherwise.
Bitwise exclusive OR compares the corresponding bits of its operands and produces a 0 when
both bits are 1 or both bits are 0 and 1 otherwise. Bitwise left shift operator and bitwise right shift
operator both take a bit sequence as their left operand a positive integer quantity n as their right
operand. These operators perform left and right shifts of their left operand by the number of bit
positions given by the right operand, which must be positive. The Unary operator ~ yields the
one‟s complement of an integer; that is, it converts each 1-bit into a 0-bit and vice versa.
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 -> member selection operator
The comma operator can be used to link the related
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.
& Address
Sizeof Size of an object
* Multiplication Left to right 3
/ Divisio
n
% Modulu
s
+ Addition Left to right 4
- Subtraction
<< Left shift Left to right 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
+= -= &=
, Comma operator Left to right 15
EVALUATION OF EXPRESSIONS
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:
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.
Else, if one of the operands is long int, the other will be converted to long int and
the result will be long int.
Else, if one of the operands is unsigned int, the other will be converted to
unsigned int and the result will be unsigned int.
Single characters can be entered into the computer using the C library
function getchar. It returns a single character from a standard input device (typically a
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 ) ;
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 percentage sign (%) indicates that a conversion specification follows. The
conversion character indicates the type of data that is to be assigned to the variables associated
with the corresponding argument. The field width specifier is optional. Within the control string,
multiple character groups can be separated by whitespace characters.
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 );
………….
}
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.
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 */
}
if statement
Switch statement
Conditional operator statement
goto statement
if statement
simple if statement
if .... else statement
Nested if…else statement
Else if ladder
simple if statement
if ( expression ) statement;
or
if ( expression )
{ statement-block; }
statement-x;
Entry
True
if(expression)
Statement-block
Statement-x
Next statement
The if .... else 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;
True false
if(expression)
Statement-block 1 Statement-block 2
Statement-x
Nested if…else statement
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;
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)
Statement-n;
else
Default-statement;
Statement-x;
Switch statement
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;
Conditional operator statement
Example:
a = (10>6) ? 10 : 6 ;
goto statement
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
statement goto label; a loop will be formed and some statements will be executed repeatedly.
Such a jump is known as a backward jump. On the other hand, if the label: is placed after the
goto label; some statements will be skipped and the jump is known as a forward jump.
Example1:
goto skip_point;
printf ("This part was skipped.\n");
skip_point: /* skip_point is a label*/
printf("Hi there!\n");
Output:
Example2:
Test
false
Condition?
true
true
Test
false
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; /*incrementing*/
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);
On reaching the do statement, the program first evaluates the body of the
loop. At the end of the loop, the test-condition in the while statement is evaluated. The process
continues as long as the condition is true. When the condition becomes false, the loop will be
terminated and the control goes to the statement that appears immediately after the while
statement. Since the test-condition is evaluated at the bottom of the loop, the do…while construct
provides an exit-controlled loop and therefore the body of the loop is always executed at least
once.
Example
main()
{
int n,sum;
n=1; /*initialization*/
sum=0;
do
{
sum=sum+n;
n=n+1; /*incrementing*/
}
whie( n<=5) /* testing*/
printf(“sum =%d”, sum);
}
Output
sum =15
The for loop is another entry-controlled loop. The general form of the for loop is :
}
break AND continue
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;
Example
if(loop==10)
break;
printf("%i\n",loop);
output
for( )
if( )
continue;
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>
main()
{
int a,b,c;
a=10;
b=5;
c=a+b;
Output
The result is : 15
/* Programming example of the use various arithmetic operators */
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);
printf(“ a + b * c= %d \n “ , result);
Output
a - b =98
b * c= 50
a / c =4
a + b * c=150
a* b +c * d =300
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);
}
Output
6 + a/ 5 * b =16
a / b * b = 24
c / d * d =25.00000
-a = -25
#include<stdio.h>
main()
{
int a;
int b=4;
int c=8;
int d=2;
int e=4;
int f=2;
Output
The value of a is = 16
The value of a is = 14
The value of a is = 2
#include<stdio.h>
main()
{
float a;
int b;
a=8.59;
b=(int) a;
printf(“ The result is %d”,b);
Output
The result is 8
#include<stdio.h>
int main(void )
{
int x;
scanf(“%d”, &x);
printf(“You typed %d \n”, x);
return 0;
}
Input : 100
Output: You typed 100
9) Program
/* example involving both gets and puts */
#include<stdio.h>
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
The string typed is : 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 ”);
}
{
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.
Enter an integer : 0
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>
main ()
int marks;
else
printf ("Fail");
Output
Enter marks 85
Distinction
Enter marks 54
Second class
printf("%d is greater",a);
else
printf("%d is greater",c);
}
else
{
if(b>c)
printf("%d is greater",b);
else
printf("%d is greater",c);
}
Output
Enter three numbers:89 66 45
89 is greater
Enter three numbers:23 45 11
45 is greater
Enter three numbers:8 5 65
65 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;
printf("Enter an expression:");
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");
break;
}
}
#include<stdio.h>
void main()
{
int n,c;
long int f=1;
clrscr();
printf("\n Enter the number :");
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);
while(i!= -1)
sum= sum+ i;
scanf(“%d”,&i);
average= =sum/count;
Output
12 14 16 18 -1
The average is 15.00
/* program to display the numbers which are divisible by 5 within the range 1-50*/
#include<stdio.h>
main()
do
printf(“%d\t”, i);
i=i+5;
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
/* 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)
{
if( i==5)
break; /* when i=5 the break causes exit from the while loop*/
printf(“%d”,i);
i++;
Output
#include<stdio.h>
main()
int i=1,sum=0;;
while( i<=6)
if( i==3)
continue;
sum=sum+i;
i++;
}
printf(“%d\t”,sum)
Output