First Steps Into The C' Programming
First Steps Into The C' Programming
Programming.
Brief History of “C”.
“C” programming language was developed by
Dennis Ritchie and Brian Kernighan, at Bell
Telephone Laboratories (now AT&T
incorporation US) in 1972.
It was modified from BCPL (Basic Combined
Programming Language), which was
developed by Martin Richards.
Later modified by Ken Thompson as B
Language. Finally to C programming language.
“C” character sets
C language can identify.
A-Z : upper case Alphabets.
a-z : lower case Alphabets.
0-9 : Digits.
+,-,*,/,% : 5 Basic mathematical
operators.
,,.,”,:,;,$,#,>,<,?,! etc. : Special
characters & punctuation symbols.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 2
Keywords(Reserved words)
“C” language has 32 keywords or reserved words.
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 3
Constants
Constants are those whose value is
fixed, and the value does not change.
There are 2 types of constants.
Integer constant and
Real constant.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 4
Variables
Variables are those whose values can
be changed.
In computer science, variables are place
holders, where a quantity can be
stored.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 5
Identifiers
Identifiers are the names given to the
program elements such as variables,
arrays, strings, functions, structures,
unions etc.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 6
Rules for forming a “C”
identifier.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 7
Datatypes in “C” language
Datatype keyword Memory(Bytes)
1. Integer int 2
2. Real numbers float 4
3. Double precision real nos. double 8
4. Character char 1
5. Void or Null void 0
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 8
General format of a “C”
program.
Preprocessor statements.
Global declarations (if any)
main( )
{
local declarations;
executable statement/s;
}
user_defined functions ( )(if any)
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 9
My first “C” program
Program to display the statement.
“Programming is Fun”
#include<stdio.h>
void main( )
{
printf(“Programming is Fun”);
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 10
Compiling and Running the
program
1. Enter your program in the Turbo-C IDE.
2. Save your program code giving a relevant name with the
extension .c. This is called the source code.
3. Compile your program by selecting the This is called the
source code.
4. Compile your program by selecting the compile drop down
menu and click on compile. You get a dialog box and error
messages if any. After successful compilation, It creates an
object file with the extension .obj
5. Click on link in the compile drop down menu. After successful
linking, It creates an executable file with the extension .exe.
6. Now Run your program by selecting the run drop down menu
and click on run.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 11
Escape sequences
\n new line character
\t horizontal tab
\v vertical tab
\a alarm
\b backspace
\” prints the double quote
\’ prints the single quote.
\\ prints the backslash character itself.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 12
Program 3
Write a “C” program to add 200 and 300 and display the output as :
“THE SUM OF 200 AND 300 IS = 500.”
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
int sum;
clrscr();
sum=200+300;
printf(“\n THE SUM OF 200 AND 300 IS = %d”,sum);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 13
Program 4
Write a “C” program to add two integer numbers and display the result.
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,sum;
clrscr();
printf(“\nEnter the value of two integer numbers : “);
scanf(“%d %d”,&num1,&num2);
sum=num1+num2;
printf(“\nThe sum of %d and %d is = %d”,num1,num2,sum);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 14
Program 5
Write a “C” program to accept the area of the circle, calculate the area of
the circle and display the result.
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
float area,radius;
clrscr();
printf(“\nEnter the radius of the circle : “);
scanf(“%f”,&radius);
area=3.14*radius *radius;
printf(“\nThe area of the circle is = %d”,area);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 15
Program to display the ASCII code of the symbol entered.
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
int code;
char symbol;
clrscr();
printf(“\nEnter a character from the keyboard: “);
scanf(“%c”,&symbol);
code=symbol;
printf(“\nThe ASCII code of the %c is = %d”,symbol,code);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 16
Operators and Expressions
Unary Operators:
The operation done on the single operand is
called as a Unary operator.
The Unary operators available in “C” language
are:
Unary plus: ‘+’
Unary minus: ‘-’
Increment operator: ‘++’
Decrement operator: ‘--’
Size of operator: sizeof
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 17
Operators and Expressions
(Contd.)
Increment Operator:
There are two types of increment operator:
1. Pre-increment Operator.
2. Post Increment Operator.
Pre-increment Operator:
If the operator is used before the operand, then it is
called as pre-increment operator.
Ex: ++a;
Here, If the value of a is initially, 5, then, the value is
first incremented by 1, and then the value 6 is
fetched.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 18
Operators and Expressions
(Contd.)
Program to illustrate the working of a Pre-increment operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=15;
clrscr();
printf(“\nThe initial value of a = %d”,a);
printf(“\nNow the value of a = %d”,++a);
printf(“\nLater the value of a = %d”,a);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 19
Operators and Expressions
(Contd.)
Post-increment Operator:
If the operator is used after the operand,
then it is called as Post-increment
operator.
Ex: a++;
Here, If the value of a is initially, 5, then,
the value a i.e., 5 is first fetched and
later the value of a is incremented by 1.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 20
Operators and Expressions
(Contd.)
Program to illustrate the working of a Post-increment operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=25;
clrscr();
printf(“\nThe initial value of a = %d”,a);
printf(“\nNow the value of a = %d”,a++);
printf(“\nLater the value of a = %d”,a);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 21
Operators and Expressions
(Contd.)
sizeof Operator:
The sizeof operator returns the number of bytes allocated by the compiler for the
variables or the data type.
Ex: sizeof(int) will return 2.
Program to illustrate the sizeof operator:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\nThe Size of \”int \” is = %d bytes”,sizeof(int));
printf(“\nThe size of \”float\” is = %d bytes”,sizeof(float));
printf(“\nThe Size of \”char \” is = %d bytes”,sizeof(char));
printf(“\nThe size of \”double\” is = %d bytes”,sizeof(double));
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 22
Operators and Expressions
(Contd.)
Relational operators:
Less than : ‘<‘
Greater than : ‘>’
Less than or equal to : ‘<=‘
Greater than or equal to : ‘>=‘
If the value of the operation is true, then
the compiler, will return 1 else will
return 0.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 23
Program to illustrate the relational operators:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
a=15;
b=20;
printf(“\nThe value of a>b is = %d”,a>b);
printf(“\nThe value of a<b is = %d”,a<b);
printf(“\nThe value of a>=b is = %d”,a>=b);
printf(“\nThe value of a<=b is = %d”,a<=b);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 24
Operators and Expressions
(Contd.)
Equality operator:
Equal to : ‘==‘
Not equal to : ‘!=‘
If the value of the operation is true, then
the compiler, will return 1 else will
return 0.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 25
Operators and Expressions
(Contd.)
Logical Operators:
Logical AND: ‘&&’
Logical OR: ‘||’
Logical NOT: ‘!’
Logical AND: The expression will evaluate true
only if both the conditions are true.
Logical OR: The expression will evaluate true, if
at least one of the conditions is true.
Logical NOT: The expression will return true, if
the condition is false and vice versa.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 26
Operators and Expressions
(Contd.)
Assignment Operators:
Syntax:
Identifier= value of an expression.
Ex: A=10; Num=x;
Sum=a+b;
Multiple assignment operators:
identifier1=identifier2=expression;
Other assignment operators:
+=, -=, *=, /=, %=
identifier1+=identifier2;
identifier1=identifier1+identifier2;
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 27
Operators and Expressions
(Contd.)
If, i=5, j=7
Expression Equivalent value
expression
i+=5; i=i+5;
j*=(i-3);
i%=(j-2);
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 28
Operators and Expressions
(Contd.)
If, f=5.5, g=-3.25
Expression Equivalent value
expression
f+=g;
f-=g;
f*=g;
f/=g;
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 29
Operators and Expressions
(Contd.)
Conditional Operators:
Identifier=(test expression)? Expr1 : expr2;
If a and b is 10 and 3 respectively.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 30
Operators and Expressions
(Contd.)
If two integer numbers a and b are 10 and 3
respectively.
Expression value
a+b
a-b
a*b
a/b
a%b
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 31
Operators and Expressions
(Contd.)
If f1 and f2 are two floating point
variables with the value 12.5 and 2.0
respectively:
Expression value
f1+f2
f1-f2
f1*f2
f1/f2
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 32
Operators and Expressions
(Contd.)
If c1 and c2 are two character variables
with value ‘P’ and ‘T’ respectively.
Expression value
c1
c1+c2
c1+c2+5
c1+c2+’5’
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 33
Operators and Expressions
(Contd.)
Absolute.c
#include<stdio.h>
#include<conio.h>
void main()
{
int num,abs;
clrscr();
printf(“\nEnter a signed number : “);
scanf(“%d”,&num);
abs=(num<0)?-num:num;
printf(“\nAbsolute value = %d”,abs);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 34
Operators and Expressions
(Contd.)
Program to find the biggest of two numbers using ternary operator:
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,large;
clrscr();
printf(“\nEnter the value of two numbers : “);
scanf(“%d %d”,&num1,&num2);
large=(num1>num2)?num1:num2;
printf(“\nThe largest of two numbers is = %d”,large);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 35
Operators and Expressions
(Contd.)
A C program to illustrate sizeof operator:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\nSize of short int is %d”,sizeof(short int));
printf(“\nSize of int is %d”,sizeof(int));
printf(“\nSize of long int is %d”,sizeof(long int));
getch():
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 36
Operators and Expressions
(Contd.)
#include<stdio.h>
#include<conio.h>
void main()
{
int i=25;
clrscr();
printf(“\nInitial value of i = %d”,i);
printf(“\n%d”,i++);
printf(“\n%d”,i-=4);
printf(“\n%d”,i+=7);
printf(“\n%d”,i--);
printf(“\n%d”,i+4);
printf(“\nNow i value = %d”,i);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 37
Operators and Expressions
(Contd.)
Bitwise Operators:
Data stored in memory location is in the form of 0’s and 1’s. called
Binary digITS(BITS).
Manipulation of these bits, C provides 6 bitwise operators:
These operators work only with int and char datatypes.
Operator Symbol Meaning
& Ampersand Bitwise AND
| Pipeline Bitwise OR
^ Caret Exclusive-OR
~ Tilde 1’s complement
<< Double less than Left shifting of bits
>> Double greater than Right shifting of bits
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 38
Operators and Expressions
(Contd.)
int a=5,b=3;
Considering an 8-bit computer:
The equivalent binary number of 5= 0000 0101
The equivalent binary number of 3= 0000 0011
The result of bitwise ANDing of a and b is:
a = 0000 0101
b = 0000 0011
a&b = 0000 0001
The result of bitwise ORing of a and b is:
a = 0000 0101
b = 0000 0011
a|b = 0000 0111
The result of bitwise Exclusive-ORing of a and b is:
a = 0000 0101
b = 0000 0011
a^b = 0000 0110
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 39
Operators and Expressions
(Contd.)
Bitwise Complement:
If a=5; Binary equivalent is 0101
b=~a;
= 1010 which is 1’s complement of a.
Bitwise Left shifting:
Consider the Binary number 1001 1011
Before Left shifting by one bit: 1001 1011
After Left shifting by one bit : 0011 0110
Bitwise Right shifting:
Consider the Binary number 1010 0101
Before Right shifting by one bit : 1010 0101
After Right shifting by one bit : 0101 0010
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 40
Control Statements
Normally program statements gets executed one after
the other sequentially, from the first statement to the
last statement one after the other.
The statements which alter the sequence of execution
of the program are called control statements.
The control statements are classified as:
Conditional control statements:
if statement
if-else statement
nested if statements
switch statement
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 41
Control Statements
Unconditional control statements:
goto statement
break statement
continue statement
Looping/Repetitive/Iteration control statements:
for statement
while statement
do-while statement
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 42
The if statement
The if statement:
Syntax:
if(condition)
{
statement/s;
}
next statements(if any)
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 43
if statement (flow chart)
false
if(condition
)
true
Statement/s;
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 44
Control Statements
Program to illustrate the simple if statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf(“\nEnter a positive number : “);
scanf(“%d”,&num);
if(num<0)
printf(“\n\aERROR IN INPUT VALUE !!”);
printf(“\nThe number entered is : %d”,num);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 45
Control Statements
Program to print the maximum number of days in February.
#include<stdio.h>
#include<conio.h>
void main()
{
int month,day,year,feb_max;
clrscr();
printf("\nEnter month and year : ");
scanf("%d %d",&month,&year);
feb_max=28;
if(month==2)
{
printf("\nMonth is February");
if((year%4)==0)
{
printf("\nLeap year");
feb_max=29;
}
}
printf("\nMax days in February month in %d year is %d",feb_max,year);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 46
Control Statements
Program to find the biggest of three numbers using simple if statements.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,num3,large;
clrscr();
printf(“\nEnter the value of 3 numbers : “);
scanf(“%d %d %d”,&num1,&num2,&num3);
large=num1;
if(num2>large)
large=num2;
if(num3>large)
large=num3;
printf(“\nThe largest number is = %d”,large);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 47
Control Statements
The if-else construct:
Syntax:
if(condition)
{
statement1/s;
}
else
{
statement2/s;
}
Next statements (if any)
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 48
if-else construct (flow chart)
true false
if(condition
)
Statement1/s; Statement2/s;
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 49
Control Statements
Program to find whether the given number is odd or even.
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf(“\nEnter a number : “);
scanf(“%d”,&num);
if(num%2==0)
printf(“\nThe number %d entered is EVEN NUMBER”,num);
else
printf(“\nThe number %d entered is ODD NUMBER”,num);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 50
Control Statements
Write a C program to check whether the entered character is an alphabet or not.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf(“\nEnter an alphabet : “);
ch=getchar();
if((ch>=‘a’ &&ch<=‘z’)||(ch>=‘A’&&ch<=‘Z’)
printf(“\n%c entered is an alphabet”,ch);
else
printf(“\n%c entered is not an alphabet”,ch);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 51
Control Statements
The if-else-if construct : (nested if)
Syntax : if(condition1)
{
if(condition2)
statement1;
else
statement2;
}
else if(condition3)
statement3;
else
statement4;
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 52
Control Statements
The other forms of two-layer nesting:
1) if(condition1)
statement1;
else if(condition2)
statement2;
2) if(condition1)
statement1;
else if(condition2)
statement2;
else
statement3;
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 53
Control Statements
3) if(condition1)
{
if(condition2)
statement1;
else
statement2;
}
else
statement3;
4) if(condition1)
{
if(condition2)
statement1;
else
statement2;
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 54
Control Statements
Write a C program to accept a character and display the corresponding messages .
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf(“\nEnter a character : “);
ch=getchar();
if(ch>=‘a’ && ch<=‘z’)
printf(“\NLOWER CASE ALPHABET”);
else if(ch>=‘A’ && ch<=‘Z’)
printf(“\nUPPER CASE ALPHABET”);
else if(ch>=‘0’ && ch<=‘9’)
printf(“\nDIGITS”);
else if(ch==‘+’||ch==‘-’||ch==‘*’||ch==‘/’||ch==‘%’)
printf(“\nMATHEMATICAL OPERATOR”);
else
printf(“\nSPECIAL SYMBOL”);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 55
Control Statements
Write a C program to find whether an alphabet entered is a vowel or a consonant.
#include<stdio.h>
#include<conio.h>
Void main()
{
char ch;
clrscr();
printf(“\nEnter an alphabet : “);
ch=getchar();
if(ch>=‘A’ && ch<=‘Z’)
ch+=32;
if(ch==‘a’||ch==‘e’||ch==‘i’||ch==‘o’||ch==‘u’)
printf(“\nVOWEL”);
else
printf(“\nCONSONANT”);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 56
Control Statements
The switch-case statement:
Syntax:
switch(expression)
{
case constant1 :
statement/s;
break;
case constant2 :
statement/s;
break;
….
….
….
case constantn :
statement/s;
break;
default :
statement/s;
break;
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 57
switch – case statement
(flowchart)
switch(expression)
Case 1:
Match ? Statement/s; break;
case 2:
Match ? Statement/s; break;
case n:
Match ? Statement/s; break;
default Statement/s;
Next statement
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 58
Looping:
Iteration: Iteration(looping) is repeated
execution of a group of statements until some
logical condition has been satisfied.
The while construct:
Syntax:
while(condition)
{
statement/s;
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 59
Flowchart: false
while
(condition)
true
Statement/s;
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 60
Looping(contd.)
Here, the condition is evaluated first. If
it is true (non-zero), statement is
executed and the condition is re-
evaluated. This cycle continues until the
condition becomes false (zero).
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 61
Looping(contd.)
Write a C program to find the sum of the first 10 natural numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,n=1;
clrscr();
while(n<=10)
{
sum+=n;
n++;
}
printf(“\nThe sum of the first 10 numbers is = %d”,sum);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 62
Looping(contd.)
Write a C program to find the sum of the
first 50 even numbers.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 63
Looping(contd.)
The do-while loop construct:
Syntax:
do
{
statement/s;
}while(condition);
In a do-while loop, unlike the while statement, the
check is made at the bottom of the loop instead of at
the top. Therefore, the body of the loop always gets
executed at least once. The loop executes as long as
the condition remains true.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 64
Looping(contd.)
Flowchart: do
Statement/s;
true
while
(condition);
false
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 65
Looping(contd.)
Write a C program to print the first 10 multiples of 5 one below the other.
#include<stdio.h>
#include<conio.h>
void main()
{
int n=1;
clrscr();
printf(“\nThe first 10 multiples of 5 is as follows :”);
do
{
printf(“\n%d”,5*n);
n++;
}while(n<=10);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 66
Looping(contd.)
Write a C program to find the average of n numbers:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1;
float num,sum=0,avg;
clrscr();
printf(“\nEnter the value of n: “);
scanf(“%d”,&n);
do
{
printf(“\nEnter the value of %d number : “,i);
scanf(“%f”,&num);
sum+=num;
i++;
}while(i<=n);
avg=sum/n;
printf(“\nThe average is = %f”,avg);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 67
Looping(contd.)
The for loop:
Syntax:
for (initialization expression; test
expression; update expression)
{
statement/s;
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 68
Looping(contd.)
Initialization expression
Flowchart:
false
Test expression
true
Update expression
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 69
Looping(contd.)
The for loop is useful while executing the statement/s a number of
times. For example, a program that displays the first 10
multiples of 3 on a single line, is given below:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
printf(“\t%d”,3*i);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 70
Looping(contd.)
The for keyword is followed by three components within
round braces, ( and ). These three components are
separated by semicolons. In the above example, the
three components are:
i=1
i<=10
i++
The first component, i=1 is executed only once prior to
the statements within the for loop. This is called the
initialization expression.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 71
Looping(contd.)
The second component i<=10 is evaluated
once before every execution of the
statements within the loop. It is called the
test expression. If this expression is true,
the statements within the loop executes. If it
is false, the loop terminates and the control
of execution is transferred to the statement
following the for loop.
The third component i++ is executed once after
every execution of the statements within the
loop. It is called as Update expression.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 72
break statement:
break:
This control statement can be used in loops and
switch statements. It is used to terminate the
loop when a specified condition is satisfied.
The control comes out of the loop and the
following statements are executed. In switch
statements, the control comes out of the
switch statements and the following
statements are executed.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 73
continue statement:
continue:
This control statements are used in the loops.
During execution if a continue statement is
encountered all the following statements will
be terminated and the control will go the next
iteration of the loop without executing the
following statements in the loop. In the while
and do-while the test condition is executed
and in the for loop, the control goes to the
update expression.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 74
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while(i<=10)
{
if(i==6)
break;
printf("\n%d",i);
}
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 75
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,num;
clrscr();
printf("\nEnter a number : ");
scanf("%d",&num);
while(i++<num)
{
if(i%3!=0)
continue;
else
printf("\n%d",i);
}
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 76
goto:
This is an unconditional control statement
to tell the compiler from where the next
statements has to be executed. (It is
not a good programming practice to use
goto statements).
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 77
Write a C program to reverse a given number.
#include<stdio.h>
#include<conio.h>
void main()
{
long int num,rev=0;
int r;
clrscr();
printf("\nEnter a number : ");
scanf("%ld",&num);
while(num!=0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
printf("\nReversed number = %ld",rev);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 78
Write a C program to check whether a given number is a palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
long int num,temp,rev=0;
int r;
clrscr();
printf("\nEnter a number : ");
scanf("%ld",&num);
temp=num;
while(num!=0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
printf("\nReversed number = %ld",rev);
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 79
if(rev==temp)
printf("\n%ld IS PALINDROME",temp);
else
printf("\n%ld IS NOT A PALINDROME",temp);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 80
Write a C program to find the GCD and LCM of two numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,a,b,gcd,lcm,prod;
clrscr();
printf("\nEnter two numbers : ");
scanf("%d %d",&a,&b);
m=a;
n=b;
prod=n*m;
while(n!=m)
if(n>m)
n-=m;
else
m-=n;
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 81
gcd=n;
lcm=prod/gcd;
printf("\nGCD of %d and %d is = %d",a,b,gcd);
printf("\nLCM of %d and %d is = %d",a,b,lcm);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 82
Write a C program to find the factorial of a given number.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
long int fact=1;
clrscr();
printf("\nEnter the number whose factorial has to be found : ");
scanf("%d",&n);
for(i=n;i>0;i--)
fact=fact*i;
printf("\nThe factorial of %d is = %ld",n,fact);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 83
Write a C program to check whether a given number is a prime or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,h,flag;
clrscr();
printf("\nEnter a number : ");
scanf("%d",&num); i=2; flag=1; h=num/2;
while(i<=h)
{
if(num%i==0)
{
flag=0;
printf("\nNumber %d is not a prime number",num);
break;
}
i++;
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 84
if(flag)
printf("\n%d is a prime number",num);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 85
Write a C program to find the largest, smallest and second largest of three numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,lar,small,seclar;
clrscr();
printf("\nEnter the value of 3 numbers : ");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
lar=a;
small=b;
}
else
{
lar=b;
small=a;
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 86
if(lar<c)
lar=c;
else if(small>c)
small=c;
printf("\nLargest number is = %d",lar);
printf("\nSmallest number is = %d",small);
seclar=(a+b+c)-(lar+small);
printf("\nSecond largest number = %d",seclar);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 87
Write a C program to generate prime numbers between the range of two numbers.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int m,n,k,i,flag,count=0;
clrscr();
printf("\nEnter the range of numbers to generate prime numbers : ");
scanf("%d %d",&m,&n);
printf("\nPrime numbers between %d and %d\n",m,n);
for(k=m;k<=n;k++)
{
flag=1;
for(i=2;i<=sqrt(k);i++)
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 88
if(k%i==0)
{
flag=0;
break;
}
if(flag)
{
count++;
printf("\t%d",k);
}
}
printf("\nNumber of prime numbers between %d and %d is %d",m,n,count);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 89
ARRAYS
One Dimensional Array:
An array is a group of elements (data items) that have common
characteristics (Ex. Numerical data, character data etc.) and
they share a common name.
The elements of an array are differentiated from one another by
their positions within the list of items.
Each array element (i.e., each individual data item) is referred to
by specifying the array name followed by the subscript enclosed
in square brackets.
The subscript indicates the position of a particular element with
respect to the rest of the elements. The subscript must be a
non negative integer.
For ex., in the n element array, x, the array elements are
x[1], x[2], x[3], x[4], ….. x[n-1], x[n].
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 90
ARRAYS (Contd.)
Single and multi dimensional arrays are available in the C
language. The number of subscripts determines the
dimensionality of the array.
Declaration of an Array:
An array must be declared in the same manner as an ordinary
variables, except that each array name must be accompanied
by a size specification. This is necessary because the compiler
will have to know how much memory to reserve for this array. A
single dimensional array is declared as follows:
datatype array_name[n]; size of an array must be an integer
constant.
The integer array declaration int x[100]; creates an array that is
100 elements long with the first element being 0 and the last
being 99.
Ex: int i[100]; char text[80]; float n[12];
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 91
ARRAYS (Contd.)
An individual element in an array can be
referred to by means of the subscript, the
number in brackets following the array name.
A subscript is the number that specifies the
element’s position in an array.
i[20]=1234;
Note: i[2] is not the second element of the
array I but the third.
The following program illustrates how to enter
data into an array and also reading data from
an array.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 92
ARRAYS (Contd.)
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5],n;
clrscr();
printf("\nEnter 5 numbers : ");
/*Input the 5 integer numbers into array "arr" */
for(n=0;n<5;n++)
{
printf("\nEnter integer # %d : ",n+1);
scanf("%d",&arr[n]);
}
/*Print out the integer stored in array "arr" */
for(n=0;n<5;n++)
printf("\nInteger # %d = %d",n+1,arr[n]);
/* n+1 because the C subscript starts from 0 */
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 93
ARRAYS (Contd.)
When an array of certain data type is declared, initially they are stored as
a contiguous set of these data types. The following program illustrates
this:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, arr[5];
clrscr();
printf("\nSize of int is = %d",sizeof(int));
printf("\nSize of \"arr\" is = %d",sizeof(arr));
for(i=0;i<5;i++)
printf("\n&arr[%d] = %u",i,&arr[i]);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 94
ARRAYS (Contd.)
Initializing array:
An array can be initialized when declared by specifying
the values of some or all of its elements. Arrays can
be initialized at the time of declaration when their
initial values are known in advance. The values to
initialize an array must be constants – never
variables or function calls.
The array can be initialized as follows:
int array[5]={4,6,5,7,2};
float x[6]={0,0.25,23.45,-0.9,456.3,12.34};
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 95
ARRAYS (Contd.)
The array size need not be specified explicitly when initial values
are included as a part of an array declaration. With a numerical
array, the array size will automatically be set equal to the
number of initial values included within the declaration.
int digits[]={1,2,3,4,5,6}; six element integer array
float x[]={0,0.25,-56.34,78.45}; four element real array.
Array overflow:
It is illegal to access a non-existent element of the array. C
language will not check for array overflow. It is programmers
responsibility to ensure that any subscripting performed does not
cross the upper as well as the lower bounds of the array.
int array[5];
array[5]=105; /* Illegal */
Some other variables that the program uses are being over written.
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 96
ARRAYS (Contd.)
Processing an Array:
Single operations involving entire arrays are not permitted in C. If a and b are two
similar arrays of the same data type, same dimensionality and same size,
assignment operations, comparison operations etc., must be carried out on an
element-by-element basis.
This is done within a loop, where each pass through the loop is used to process
each element of an array. The number of passes through the loop will therefore
be equal to the number of array elements to be processed and the value of the
index (subscript) should be incremented from 0 to n-1.
Write a C program to find the largest element in an array and position of it’s
occurrence.
#include<stdio.h>
#include<conio.h>
void main()
{
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 97
ARRAYS (Contd.)
int a[100], lar,pos,n,i;
clrscr();
printf("\nEnter the number of elements in the array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
lar=a[0]; pos=0;
for(i=1;i<n;i++)
{
if(lar<a[i])
{
lar=a[i];
pos=i;
}
}
printf("\nLargest element in the array is %d",lar);
printf("\nLargest element position in the array is %d",pos+1);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 98
ARRAYS (Contd.)
Write a C program to read N student marks and find the class Average using arrays.
#include<stdio.h>
#include<conio.h>
void main()
{
int marks[100],i,n;
float avg,sum=0;
clrscr();
printf("\nEnter the number of students in the class : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&marks[i]);
sum+=marks[i];
}
avg=sum/n;
printf("\nClass Average = %.2f",avg);
getch();
}
07/12/21 ¨˜”°º•Calypso•º°”˜¨ 99
ARRAYS (Contd.)
Write a C program to find the largest and smallest elements in an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n,lar,small;
clrscr();
printf("\nEnter the number of elements in the array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
lar=a[0]; small=a[0];
for(i=0;i<n;i++)
{
if(lar<a[i]) lar=a[i];
if(small>a[i]) small=a[i];
}
printf("\nThe largest element in the array is = %d",lar);
printf("\nThe smallest element in the array is = %d",small);
getch();
}
BINARY SEARCH
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,item,n,beg,end,mid;
clrscr();
printf("\nEnter the number of elements : ");
scanf("%d",&n);
printf("\nEnter the elements into the array in descending order :\n ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
getch();
}
C o m p u t e r \0
strlen( ) function:
This function counts and returns the number of characters in a
string. The length does not include the NULL character.
Syntax:
n=strlen(string);
where n is an integer variable which receives the value of the length
of the string.
strlwr() function
This function converts all characters in a string from uppercase to lowercase.
Syntax: strlwr(string);
Write a C program to accept a string and convert all characters in a string from uppercase to
lowercase without using library function.
Write a C program to convert all the lowercase characters into the uppercase
characters in the given string without using library function.
Write a C program to extract a substring from the given string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
if(a>b)
return a;
else
return b;
}
void main()
{
int large;
clrscr();
large=largest();
printf(“\nThe largest number is = %d”,large);
getch();
}
ptr = #
24560 is placed here
{ int acct_no;
int acct_type;
char name[30];
char address[50];
long balance;
long last_payment;
};
struct account vendor={1001,1,”Aneesh”,”No.123, 4th main, Vijaynagar, B’lore”, 54603,5000};
printf(“\nEnter account number : “);
scanf(“%d”,&acct_no);