2. Steps in learning C
Character
Set
Programs
Instructions
Tokens
2
N.Rajkumar
3. C Character Set
C Character Set
Execution
Character Set
Source
Character Set
Special
Characters
Digits
Alphabets Escape
Sequence
White
Spaces
3
N.Rajkumar
4. C Character Set (Cont)
• Source Character Set
– It is used to construct the statements in the
program.
• Executable Character Set
– These characters are employed at the time of
execution i.e. they have effects only when the
program is being executed.
4
N.Rajkumar
5. Source Character Set
Letters a to z ,A to Z
Digits 0 to 9
Special Characters ! @ # $ % ^ & * ( ) _ - +
= | { } [ ] etc,.
White Spaces Blank Space ,Horizontal
tab, New line, Vertical
tab etc,.
5
N.Rajkumar
6. Special characters
• Comma ,
• Period or dot .
• Semicolon ;
• Colon :
• Apostrophe ‘
• Quotation mark “
• Exclamation mark !
• Vertical bar |
• Back Slash
• Tilde ~
• Underscore -
• Dollar $
• Question mark ? 6
N.Rajkumar
7. • Ampersand &
• Caret ^
• Asterisk *
• Minus -
• Addition +
• Lesser than <
• Greater than >
• Parenthesis ()
• Bracket []
• Braces {}
• Percentage %
• Hash #
• Equal to =
• At the rate @ 7
N.Rajkumar
9. C Tokens
• The smallest element in the C language is
the token.
• It may be a single character or a sequence
of characters.
9
N.Rajkumar
10. C Tokens (Cont)
C Tokens
Identifiers
Eg:main,
avg
Keywords
Eg: int,
for
operators
Eg: +
-
Strings
Eg: “ab”
spI
symbol
Eg: #
$ %
Constants
Eg:17,
15.5
10
N.Rajkumar
11. Executing a C Program
Creating the Program
Compilation
Linking
Execution
11
N.Rajkumar
12. Executing a C Program (Cont)
• Enter the program in a C editor.
• Save the program (File Save) or F2.
Use the extension .c for saving the file.
Eg: sample.c
• Compile the program(Compile Compile)
or Alt+F9.
• Run the program(Run Run) or Ctrl+F9.
12
N.Rajkumar
13. Structure of C program
DOCUMENTATION SECTION
PREPROCESSOR SECTION
DEFINITION SECTION
GLOBAL DECLARATION SECTION
main()
{
Declaration part;
Executable Part;
}
sub program section
{
Body of the subprogram;
}
13
N.Rajkumar
14. • Documentation Section
– It contains the comment lines.
• Preprocessor Section
– It is used to link library files.
• Global Declaration Section
– The Global declaration section comes at the
beginning of the program and they are visible
to all parts of the program.
• Declaration Section
– It describes the data to be used within the
function.
• Executable Part
– It contains the valid statements. 14
N.Rajkumar
15. C Programs
C program may have many functions.
One and only one of the functions MUST BE
named main.
main is the starting point for the program.
main and other functions in a program are
divided into two sections, declaration
section and statement section.
15
N.Rajkumar
16. Preprocessor Directives
• Special instructions to the preprocessor
that tells how to prepare the program for
compilation
• E.g: include : tells the processor to
include information from selected libraries
known as header files e.g. <stdio.h>
16
N.Rajkumar
17. Comments (Program documentation)
The compiler simply ignores comments
when it translates the program into
executable code.
To identify a comments, C uses opening
/* and closing */ comment tokens.
17
N.Rajkumar
18. Comments (Cont)
Comments can appear anywhere in a
program.
Comments are also found wherever it is
necessary to explain a point about a code.
Comments cannot be nested in C i.e. you
cannot have comments inside comments.
18
N.Rajkumar
19. C program
/* Example program in C*/ Comments
# include <stdio.h>Preprocessor Section
#include<conio.h>
Global Declaration
void main ()
{ Local declaration
printf (“Hello World! n”); Statements
getch();
}
Output :
Hello World 19
N.Rajkumar
21. Identifiers
• Identifiers are names given to various
program elements such as variables,
functions and arrays etc,.
• Eg: #define N 10
#define a 15
Here N and a are user defined identifiers.
21
N.Rajkumar
22. Rules for naming identifier
• First character must be alphabetic or
underscore.
• Must consist only of alphabetic characters,
digits, or underscores.
• Only the first 31 characters of an identifier are
significant and are recognized by the compiler.
• Cannot use a keywords or reserved word (e.g.
main, include, printf & scanf etc.).
• No space are allowed between the identifiers
etc,.
• C is case sensitive, e.g. My_name my_name.
22
N.Rajkumar
23. Examples of Valid and Invalid Names
Valid Names Invalid Names
a a1 $sum /* $ is illegal */
student_name stdntNm 2names /* Starts with 2 */
_aSystemName _anthrSysNm stdnt Nmbr /* no spaces */
TRUE FALSE int /* reserved word */
23
N.Rajkumar
24. Variables
• Variable is an identifier that is used to
represent some specified type of
information.
• Eg: x=3
• Here x is variable.
24
N.Rajkumar
25. Keywords
• It is a reserved words.
• Cannot be used for anything else.
• Examples:
– int
– while
– for etc,.
25
N.Rajkumar
26. Keywords
Auto register Continue
Double typedef For
Int Char signed
Struct extern void
Break return Default
Else union Goto
Long Const sizeof
Switch Float do
Case short If
Enum unsigned
Static While
26
N.Rajkumar
27. Constants
• It is an entity whose value does not
changes during the execution.
• Eg: x=3
• Here 3 is a constant.
27
N.Rajkumar
30. Numeric constants
Integer constants
• It is formed using a sequence of digits.
Decimal - 0 to 9 .
Octal - 0 to 7.
Hexa - 0 to 9 ,A to F
Eg: 10,75 etc.
30
N.Rajkumar
31. Rules for defining Integer Constant
• It must have atleast one digit.
• Decimal point are not allowed.
• No blank space or commas are allowed.
• It can be either positive or negative. Etc,.
31
N.Rajkumar
32. Numeric constants
Real constants
• It is formed using a sequence of digits but
it contain decimal point.
• length, height, price distance measured in
real number
Eg: 2.5, 5.11, etc.
32
N.Rajkumar
33. Character constants
Single character constant
– A character constant is a single character
they also represented with single digit or a
single special symbol which is enclosed in
single quotes.
– Eg: ‘a’, ‘8’,’_’etc.
33
N.Rajkumar
34. Character constants
String constants
• String constant are sequence of characters
enclosed with in double quote.
• Eg: “Hello” ,”444”,”a” etc,.
34
N.Rajkumar
35. Operators
• An operator is a symbol that specifies an
operation to be performed on the
operands.
• Eg: a + b
+ is an operator.
a,b are operands.
35
N.Rajkumar
36. Data Types
A Data type is the type of data that are
going to access within the program.
36
N.Rajkumar
37. DATA TYPES
DATA TYPES
PRIMARY DERIVED
USER
DERIVED
CHARACTER INTEGER REAL
Array, Function,
Pointer
Structure, union
….
char
short int,
int, long int
Float,
double
long double 37
N.Rajkumar
38. integer
A number without a fraction part : integral
number.
C supports three different sizes of the
integer data type :
short int
int
long int
38
N.Rajkumar
39. Floating Point
A floating-point type is a number with a
fractional part, e.g. 56.78
Floating point numbers are stored using
4 Byte.
Types
Float
Double
long double
39
N.Rajkumar
40. character
• Character are generally stored using 8
bits(1 Byte) of the internal storage.
40
N.Rajkumar
41. void
The void type has no values and no
operations.
Both the set of values and the set of
operations are empty.
41
N.Rajkumar
42. Variable Declaration
To create a variable, you must specify the type and
then its identifier :
Integer
int a;
a=10;
int a=10;
int a,b;
a=10;
b=5;
int a=10,b=5;
42
N.Rajkumar
43. Variable Declaration
To create a variable, you must specify the type and
then its identifier :
Float
float a;
a=10.5;
float a=10.5;
float a,b;
a=10.5;
b=5.5;
float a=10.5,b=5.5;
43
N.Rajkumar
44. Variable Declaration
To create a variable, you must specify the type and
then its identifier :
Character
char a;
a=‘x’;
char a=‘x’;
char a,b;
a=‘x’;
b=‘y’;
char a=‘x’,b=‘y’;
44
N.Rajkumar
45. Variable Declaration
To create a variable, you must specify the type and
then its identifier :
String
char a[6];
a=“NAFEES”;
char a[6]=“NAFEES”;
Representation of string a
N
a[0]
A
a[1]
F
a[2]
E
a[3]
E
a[4]
S
a[5]
45
N.Rajkumar
46. Entire Data types in c:
Data type Size(bytes) Range Format string
Char 1 128 to 127 %c
Unsigned char 1 0 to 255 %c
Short or int 2 -32,768 to 32,767 %i or %d
Unsigned int 2 0 to 65535 %u
Long 4 -2147483648 to 2147483647 %ld
Unsigned long 4 0 to 4294967295 %lu
Float 4 3.4 e-38 to 3.4 e+38 %f or %g
Double 8 1.7 e-308 to 1.7 e+308 %lf
Long Double 10 3.4 e-4932 to 1.1 e+4932 %lf 46
N.Rajkumar
47. Basic C programs
#include<stdio.h>//program to display given data
#include<conio.h>
void main()
{
int a=10;
float b=10.88;
char c=‘$’;
char d[6]=“Nafees”;
clrscr();
printf(“Integer value : %dn”,a);
printf(“Float value : %fn”,b);
printf(“Character : %cn”,c);
printf(“String : %s”,d);
getch();
}
OUTPUT
Integer value : 10
Float value : 10.88
Character : $
String : Nafees
47
N.Rajkumar
48. Basic C programs
#include<stdio.h>//program to display given data from user
#include<conio.h>
void main()
{
int a;
float b;
char c;
char d[6];
clrscr();
printf(“Enter a integer value : “);
scanf(“%d”,&a);
printf(“Enter a float value : “);
scanf(“%f”,&b);
printf(“Enter a character : “);
scanf(“%c”,&c);
printf(“Enter a string : “);
scanf(“%s”,d);
OUTPUT
Enter a integer value :12
Enter a float value : 5.34
Enter a character : h
Enter a string : Nafees
Integer value : 12
Float value : 5.34
Character : h
String : Nafees
printf(“Integer value : %dn”,a);
printf(“Float value : %fn”,b);
printf(“Character : %cn”,c);
printf(“String : %s”,d);
getch();
}
48
N.Rajkumar
49. Types of Operator
1. Arithmetic operator
2. Relational operator
3. Logical operator
4. Assignment operator
5. Short hand assignment operator
6. Increment or decrement operator(unary)
7. Comma operator
8. Conditional operator
49
N.Rajkumar
50. Arithmetic operator
• It is used to carry out arithmetic
operations
NAME OPERATOR
ADDITION +
SUBTRACTION -
MULTIPLICATION *
DIVISION /
MODULUS
(REMAINDER)
%
50
N.Rajkumar
51. SAMPLE PROGRAM
#include<stdio.h> // Header File
#include <conio.h>
void main ( ) /* main is the starting of every c program */
{
int a=5,b=2; //Local Declaration
clrscr( );
printf(“Addition : %dn”,(a+b));
printf(“Subtraction : %dn”,(a-b));
printf(“Multiplication : %dn”,(a*b));
printf(“Division : %dn”,(a/b));
printf(“Modulus(remainder): %d”,(a%b));
getch( );
}
OUTPUT
Addition : 7
Subtraction : 3
Multiplication : 10
Division :2
Modulus(remainder):1
51
N.Rajkumar
52. Division operator on Different
Data Type
Operation Result Example
int/int int 5/2 = 2
int/real real 5/2.0 = 2.5
real/int real 5.0/2 = 2.5
real/real real 5.0/2.0 = 2.5
52
N.Rajkumar
53. Relational operator
• It is used to compare two or more
operands and returns 1 if true else return 0
NAME OPERATOR
Less than <
Less than or equal to <=
Greater than >
Greater than or equal to >=
Equal to ==
Not equal to !=
53
N.Rajkumar
57. Assignment operator
• It is used to assign a value or
expression etc to a variable.
• Eg: a =10.(assignment operator ‘=‘
assigns value 10 to variable a.
a = b
a = b + c etc,.
57
N.Rajkumar
59. Increment & Decrement Operator
NAME OPERATOR
Increment operator ++
Decrement operator --
Pre Increment Post Increment
int a=10;
++a
Now value of a is 11
int a=10;
a++
Now value of a is 11
Pre decrement Post decrement
int a=10;
--a
Now value of a is 9
int a=10;
a--
Now value of a is 9
59
N.Rajkumar
60. Increment & Decrement Operator
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“Enter a & b value : n”);
scanf(“%d%d”,&a,&b);
printf(“Value of a & b before increment : %d %dn”,a,b);
printf(“Value of a & b after increment : %d
%dn”,a++,b++);
printf(“Value of a & b after decrement : %d %dn”,a--,b—);
getch();
} 60
N.Rajkumar
61. Increment & Decrement Operator
OUTPUT
Enter a & b value :
6
9
Value of a & b before increment : 6 9
Value of a & b after increment : 7 10
Value of a & b after decrement : 5 8
61
N.Rajkumar
62. Conditional Operator (or)
Ternary Operator
• It is used to checks the condition and
execute the statement depending on the
condition.
• Eg: C = a > b ? a:b
62
N.Rajkumar
66. Expression
• An expression represent data item such as
variable, constant are interconnected
using operators.
• Eg:
Expression C Expression
a + b + c a + b + c
a2+b2 a*a + b*b
66
N.Rajkumar
70. PROGRAM CONTROL STRUCTURES
• Depending upon the sequence of the execution of
statements, the control structures are categorized as
follows:
• Categories:
• Sequential structure
– In which instructions are executed in sequence.
• Selection structure
– In which instruction are executed based on the result
of some condition.
• Iteration structure
– In which instruction are executed repeatedly.
70
N.Rajkumar
71. Sequential STRUCTURE
• It allows the program to flow in
sequence
START
Statement 1
Statement 2
Statement N
STOP
71
N.Rajkumar
72. SELECTION STRUCTURE
• It allows the program to make a choice
from alternative paths.
• C provide the following selection
structures
– if statement
– if … else statement
– Nested if … else statement
72
N.Rajkumar
82. Nested for loop
Syntax
for (initi; cond; Inc/Dec)
{
for (initi; cond; Inc/Dec)
{
Body of the loop
}
}
82
N.Rajkumar
83. DECISION MAKING & BRANCHING
• if statement
• if else statement
• Nested if else statement
• switch statement
• Conditional statement
• goto statement
• break statement
• continue statement
Selection statements
Unconditional
statements
83
N.Rajkumar
84. if statement (Simple if)
Syntax
if (condition is true)
{
Statements;
}
84
N.Rajkumar
85. if statement (Simple if)
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a;
clrscr( );
printf("nEnter a number:");
scanf("%d",&a);
if(a>10)
{
printf(" n a is greater than 10");
}
getch( );
}
OUTPUT
Enter the number: 12
a is greater than 10
85
N.Rajkumar
87. if else statement
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a;
clrscr( );
printf("nEnter a number:");
scanf("%d",&a);
if(a>10)
{
printf(" n a is greater than 10");
}
else
{
printf(" n a is not greater than 10");
}
getch( );
}
OUTPUT
Enter the number: 12
a is greater than 10
Enter the number: 1
a is not greater than10
87
N.Rajkumar
88. Nested if else statement
Syntax
if (condition is true)
{
Statements;
}
else if (condition is true)
{
Statements;
}
else if (condition is true)
{
Statements;
}
else
{
Statements;
}
88
N.Rajkumar
89. Nested if else statement
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3;
float avg;
printf("nEnter the marks:");
scanf("%d%d%d",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
printf("n The average is:%f",avg);
printf("n The Grade is:");
if(avg>=60)
{
printf("First class");
} 89
N.Rajkumar
90. else if(avg>=50)
{
printf("Second class");
}
else if(avg>=35)
{
printf("Third class");
}
else
{
printf("Fail");
}
getch();
}
OUTPUT
Enter the marks:65
75
70
The average is:70.00
The Grade is: First
class
90
N.Rajkumar
91. Nested if else
#include <stdio.h> //Program to find leap year
#include<conio.h>
void main()
{
int year;
printf("Enter a year to check if it is a leap year : n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.n", year);
else
printf("%d is not a leap year.n", year);
getch();
}
OUTPUT
Enter a year to check
if it is a leap year
:2012
2012 is a leap year
91
N.Rajkumar
95. case 3:
c=a*b;
printf("nThe result of Multiplication is:%d",c);
break;
case 4:
exit(0);
break;
default:
printf(“Wrong Choice”);
}
getch();
}
OUTPUT
Enter the value of a,b:
5
6
MENU
1.ADD
2.SUB
3.MULTIPLY
4.EXIT
Enter the choice:1
The result of Addition is:11
95
N.Rajkumar
96. Conditional Statement
• It uses conditional operator (?)
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“Enter a number : “);
scanf(“%d”,&n);
(n%2==0)?printf(“even”):printf(“odd”);
getch();
}
OUTPUT
Enter a number : 3
odd
96
N.Rajkumar
97. goto statement
• When a goto statement is encountered, the control
is transferred to the label.
goto label;
…………
…………
…………
label:
…………
97
N.Rajkumar
99. goto statement
even:
printf(“%d is an even no”,x);
return;
odd:
printf(“%d is an odd no”,x);
return;
OUTPUT
Enter a number to find odd or even no :5
5 is an odd no
99
N.Rajkumar
100. #include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“Enter two value to subtract : n”);
scanf(“n%d%d”,&a,&b);
if(a>b)
{
printf(“Subtracted value is : %dn”,(a-b));
}
else
{
break;
}
}
OUTPUT
Enter two value to subtract :
5
3
Subtracted value is : 2
When break statement is
encountered inside any block or loop,
control automatically passes to the first
statement after the block or loop
break statement
100
N.Rajkumar
102. • Loop:
–A loop is defined as a block of statements
which are repeatedly executed for certain
no of times.
• Types of Loops in C
–while loop
–do while loop
–for loop(simple & nested)
Looping Statements
102
N.Rajkumar
103. • It is an entry controlled loop works only when the condition is
true
• Syntax:
while (condition)
{
.
Body of the loop;
.
}
while loop
103
N.Rajkumar
104. #include<stdio.h> //program to print even no
#include<conio.h>
void main()
{
int n,i;
printf(“Enter the limit : “);
scanf(“%d”,&n);
i=1;
while (i<=n)
{
if(i%2==0)
{
printf(“%dt”,i);
}
i++;
}
}
while loop
OUTPUT
Enter the limit : 10
2 4 6 8 10
104
N.Rajkumar
105. #include<stdio.h> //program to calculate factorial of given no
#include<conio.h>
void main()
{
int i=1,fact=1,n;
printf("nEnter the Number:");
scanf("%d",&n);
while(i<=n)
{
fact =fact *i;
i++;
}
printf("n The value of %d! is:%d",n,fact);
getch();
}
while loop
OUTPUT
Enter the Number:3
The value of 3! is: 6
105
N.Rajkumar
106. #include<stdio.h> //program to reverse a number and to check
#include<conio.h>// whether it is palindrome or not
void main()
{
int n,rev=0;`
clrscr();
printf(“Enter a number to reverse : “);
scanf(“%d”,&n);
while(n!=0)
{
rev=rev*10;
rev=rev+(n%10);
n=n/10;
}
printf(“Reverse of given no is : %dn”,rev);
while loop
OUTPUT
Enter a number to
reverse : 452
Reverse of given
number is : 254
Not Palindrome
if(n==rev)
{
printf(“Palindrome”);
}
else
{
printf(“Not Palindrome”);
}
getch():
}
106
N.Rajkumar
107. #include<stdio.h> //program to find sum of digits
#include<conio.h>
void main()
{
int r=0,sum=0,n;
printf("nEnter the no:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("sum of the digits is:%d",sum);
getch():
}
while loop
OUTPUT
Enter the no:156
sum of the digits is:12
107
N.Rajkumar
108. #include<stdio.h> //program to find armstrong number
#include<conio.h>
void main()
{
int r=0,sum=0,n,a;
printf("nEnter the number:");
scanf("%d",&n);
a=n;
while(n>0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
if(a==sum)
printf("nIt is an armstrong number");
else
printf("nIt is not an armstrong number");
getch();
}
OUTPUT
Enter the number:153
It is an armstrong number
108
N.Rajkumar
109. • It is an exit controlled loop. It proceeds its first iteration without
checking the loop condition and proceeds next iteration by
checking the condition.
• Syntax:
do
{
.
Body of the loop;
.
}while (condition);
do while loop
109
N.Rajkumar
110. #include<stdio.h> //program to print even no
#include<conio.h>
void main()
{
int n,i;
printf(“Enter the limit : “);
scanf(“%d”,&n);
i=1;
do
{
if(i%2==0)
{
printf(“%dt”,i);
}
i++;
}while (i<=n);
}
do while loop
OUTPUT
Enter the limit : 10
2 4 6 8 10
110
N.Rajkumar
111. #include<stdio.h> //program to calculate factorial of given no
#include<conio.h>
void main()
{
int i=1,fact=1,n;
printf("nEnter the Number:");
scanf("%d",&n);
do
{
fact =fact *i;
i++;
}while (i<=n);
printf("n The value of %d! is:%d",n,fact);
getch();
}
do while loop
OUTPUT
Enter the Number:3
The value of 3! is: 6
111
N.Rajkumar
112. do while loop //program to generate fibonacci series
OUTPUT
Enter the range : 10
0
1
1
2
3
5
8
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,r;
clrscr();
printf(“Enter the range : “);
scanf(“%d”,&r);
a=0,b=1;
printf(“%d”,a);
printf(“n%d”,b);
printf(“n”);
c=0;
do
{
c=a+b;
if(c<=r)
printf(“%dn”,c);
a=b;
b=c;
}while(c<=r);
getch();
} 112
N.Rajkumar
114. #include<stdio.h> //program to generate number from 1 to n
#include<conio.h>
void main()
{
int i,n;
printf("nEnter the limit : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf(“%dn”,i);
}
getch();
}
for loop
OUTPUT
Enter the limit : 5
1
2
3
4
5
114
N.Rajkumar
115. #include<stdio.h> //program to calculate factorial of given no
#include<conio.h>
void main()
{
int i,fact=1,n;
printf("nEnter the Number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact =fact *i;
}
printf("n The value of %d! is:%d",n,fact);
getch();
}
for loop
OUTPUT
Enter the Number:3
The value of 3! is: 6
115
N.Rajkumar
116. #include<stdio.h> //program to generate given multiplication table
#include<conio.h>
void main()
{
int i,tab,n,res;
printf("nEnter the multiplication table:");
scanf("%d",&tab);
printf("nEnter the limit of multiplication table:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
res=i*tab;
printf("%d * %d : %dn”,i,tab,res);
}
getch();
}
for loop
OUTPUT
Enter the multiplication table : 2
Enter the limit of multiplication
table : 4
1 * 2 : 2
2 * 2 : 4
3 * 2 : 6
4 * 2 : 8
116
N.Rajkumar
117. #include<stdio.h>//program to calculate student marks & average using for loop
#include<conio.h>
void main()
{
int n,s1,s2,s3,s4,s5,tot;
float avg;
clrscr();
printf(“Enter no of students : ”);
scanf(“%d”,&n);
printf(“Enter marks of five subjects : n”)
for(i=1;i<=n;i++)
{
printf(“Enter student %d marksn”,i);
scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5);
tot=s1+s2+s3+s4+s5;
avg=tot/5;
printf(“Total : %dnAverage : %f”,tot,avg);
}
getch();
}
for loop
117
N.Rajkumar
118. • Definition of prime number:
– A natural number greater than one has not any other divisors except
1 and itself. In other word we can say which has only two divisors 1
and number itself.
– For example: 5. Their divisors are 1 and 5.
for loop
118
N.Rajkumar
119. #include<stdio.h>//program to check whether the given no is prime or not
#include<conio.h>
void main()
{
int n, i, c = 0;
printf("Enter a number :");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
printf(“Given no is a Prime number");
else
printf(“Given no is not a Prime number");
getch();
}
for loop
OUTPUT
Enter a number : 7
Given no is a Prime number
119
N.Rajkumar
120. Nested for loop
Syntax
for (initi; cond; Inc/Dec)
{
for (initi; cond; Inc/Dec)
{
Body of the loop
}
}
120
N.Rajkumar
121. #include<stdio.h> //program to generate numbers in triangle
#include<conio.h>
void main()
{
int i`,j,n;
printf("nEnter the limit : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d”,i);
}
printf(“n”);
}
getch();
}
Nested for loop
OUTPUT
Enter the limit : 5
1
22
333
4444
55555
121
N.Rajkumar
122. #include<stdio.h> //program to generate stars(*) in triangle
#include<conio.h>
void main()
{
int I,j,n;
printf("nEnter the limit : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(“*”);
}
printf(“n”);
}
getch();
}
Nested for loop
OUTPUT
Enter the limit : 5
*
**
***
****
*****
122
N.Rajkumar
124. Swapping without using third
variable
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a,b;
clrscr( );
printf(" nEnter the value of a:");
scanf("%d",&a);
printf(" nEnter the value of b:");
scanf("%d",&b);
124
N.Rajkumar
125. a=a+b;
b=a-b;
a=a-b;
printf(" nThe value of a is:%d",a);
printf(" nThe value of b is:%d",b);
getch( );
}
Output:
Enter the value of a:5
Enter the value of b:6
The value of a is:6
The value of b is:5
125
N.Rajkumar