Unit1 231CSC201T
Unit1 231CSC201T
UNIT I
C PROGRAMMING BASICS
2 Algorithm T1 50 2
3 Flow Charts T1 5
4 Pseudo Code T1 3
5 Structure of C program T2 14 12
7 Character set T1 2 18
8 Constants T1 4 20
9 Variables T1 3 22
10 Data Types T1 2 19
14 Looping statements T1 22 55
Total Hours 9
Outcome: Students will be able to develop simple applications in C using basic constructs
1
spr_PC_Unit1
Algorithm
2
spr_PC_Unit1
3
spr_PC_Unit1
4
spr_PC_Unit1
5
spr_PC_Unit1
6
spr_PC_Unit1
7
spr_PC_Unit1
8
spr_PC_Unit1
9
spr_PC_Unit1
10
spr_PC_Unit1
11
spr_PC_Unit1
12
spr_PC_Unit1
13
spr_PC_Unit1
14
spr_PC_Unit1
15
spr_PC_Unit1
16
spr_PC_Unit1
17
spr_PC_Unit1
18
spr_PC_Unit1
19
spr_PC_Unit1
20
spr_PC_Unit1
21
spr_PC_Unit1
22
spr_PC_Unit1
23
spr_PC_Unit1
24
spr_PC_Unit1
25
spr_PC_Unit1
26
spr_PC_Unit1
27
spr_PC_Unit1
28
spr_PC_Unit1
29
spr_PC_Unit1
30
spr_PC_Unit1
31
spr_PC_Unit1
32
spr_PC_Unit1
33
spr_PC_Unit1
34
spr_PC_Unit1
35
spr_PC_Unit1
36
spr_PC_Unit1
37
spr_PC_Unit1
38
spr_PC_Unit1
39
spr_PC_Unit1
40
spr_PC_Unit1
41
spr_PC_Unit1
42
spr_PC_Unit1
43
spr_PC_Unit1
DECISION MAKING STATEMENTS
The if statement:
The if statement is a decision making statement. It is used to control the flow of
execution of the statements and also used to test logically whether the condition is true or
false. It is always used in conjunction with condition. This statement is used when a
question requires answer.
Syntax:
if(condition is true)
{
True Statement;
}
Flowchart:
False
Condition
True
True Statement
44
spr_PC_Unit1
If the condition is true, then the True statements are executed. The „True Statement‟
may be a single statement or group of statements.
If the condition is false then the true statements are not executed, instead the
program skip past it. The condition is given by the relational operator like = =, ! =, < =, >
=. etc.,
Example:
#include<stdio.h>
void main()
{
int i;
printf(“Enter the number less than 10..”);
scanf(“%d”,&i);
if(i<=10)
{
printf(“The entered number is less than 10”);
}
}
Output:
Enter the number less than 10…5
The entered number is less than 10
} }
else Flowchart:
{
45
spr_PC_Unit1
True Statement;
False Statement;
True False
Condition
46
spr_PC_Unit1
Example:
#include<stdio.h>
#include<conio.h>
{
int a;
clrscr();
printf(“Enter the value for a:”);
scanf(“%d”,&a);
if(a%2==0)
{
printf(“The entered number is even”);
}
else
{
printf(“The entered number is odd”);
}
getch();
}
Output:
Enter the value for a:5
The entered number is odd
Conditi False
False Statement 1
True
True False
Conditi
48
spr_PC_Unit1
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“Enter the value for a:”);
scanf(“%d”,&a);
if(a==15)
{
printf(“Play Foot Ball”);
}
else
{
if(a==10)
printf(“Play Cricket”);
else
printf(“Don‟t Play”);
}
getch();
}
Output:
Enter the value for a: 10
Play Cricket
50
spr_PC_Unit1
Flowchart:
True False
Condit
True False
Statement 1 Conditi
True False
Statement 2 Condit
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
51
spr_PC_Unit1
printf(“Enter the value for a,b,c:”);
scanf(“%d%d%d”,&a,&b,&c); if(a>b && a>c)
{
printf(“a is big”);
}
else if(b>c)
{
printf(“b is big”);
}
else
{
printf(“c is big”);
}
getch();
}
Output:
Enter the value for a,b,c: 10 20 30
C is big
SWITCH STATEMENT
The switch statement in C language is used to execute the code from
multipleconditions. It is like if else-if ladder statement.
default:
//code to be executed if all cases are not matched;
52
spr_PC_Unit1
}
53
spr_PC_Unit1
Example:
#include<stdio.h>
void main()
{
int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1:
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2:
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3:
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4:
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
}
OUTPUT:
1. Addition
2.Subtraction
3.Multiplication
4.Division
54
spr_PC_Unit1
Enter the values of a & b: 20 15
Enter your Choice : 1
Sum of 20 and 15 is : 35
LOOPING STATEMENTS
In some situations there is a need to repeat a set of instructions in specified number
of times or until a particular condition is being satisfied. This repetitive operations aredone
through a loop control structure.
The loop in a program consists of two parts, one is body of the loop and another one
is control statement.
Any looping includes the following steps:
i) Initialization of a condition variable.
ii) Test the control statement
iii) Executing the body of the loop depending on the condition.
iv) Updating the condition variable.
55
spr_PC_Unit1
}
Flowchart:
False
Conditio
True
#include<stdio.h>
void main()
{
int num,rev=0,digit;
printf(“Enter the number:”);
scanf(“%d”,&num);
while(num!=0)
{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
printf(“Reverse is:%d”,rev);
56
spr_PC_Unit1
}
Output:
Enter the number: 7896
Reverse is: 6987
The do….while loop:
In some situations it may be necessary to execute the body of the loop before the
test condition is performed, such a situation the do…while loop is useful.
It is also repetitive control structure and executes the body of the loop once
irrespective of the condition, then it checks the condition and continues the execution until
the condition becomes false.
Syntax:
do
{
…………
Body of the loop;
…………
}while(condition);
Flowchart:
True Conditio
False
57
spr_PC_Unit1
Example: Addition of numbers upto 10 by using the do…while loop.
#include<stdio.h>
void main()
{
int i=1,sum=0;
do
{
sum=sum+i;
i++;
}
while(i<=10);
printf(“Sum of the numbers upto 10 is…%d”,sum);
}
Output:
Sum of the numbers upto 10 is…55
58
spr_PC_Unit1
5. Eg. Eg.
i=0; i=0;
while( i<5) do
{ {
printf(“%d”,i); printf(“%d”,i);
i++; }while(i<5);
}
The for… loop:
The for loop is another repetitive control structure, and is used to execute set of
instructions repeatedly until the condition becomes false.
The assignment, incrementation or decrementation and condition checking is done
in for statement only, where as other control structures are not offered all these feature in
one statement.
The for loop has three parts
i) Initialize counter is used to initialize counter variable.
ii) Test condition is used to test the condition.
iii) Increment/decrement counter is used to increment or decrement counter variable.
Syntax:
for( initialize counter ; test c ondition ; increment / decrement counter)
Initialization
{
body of the loop
}
Increment/Decrement
Flowchart:
Conditi True
False 59
spr_PC_Unit1
191GES204T
#include
<stdio.h
> void
main()
{
int i,sum=0;
for(i=1;i<=10
;i++)
{
sum=sum+i;
}
printf(“Sum of the numbers upto 10 is…%d”,sum);
}
Output:
Sum of the numbers upto 10 is…55
ITC_PC_SPR Page 60
191GES204T
#include <stdio.h> */
{ return 0;
Example 2:
Example 3:
if(num){ }
reverse_function(num/10);
ITC_PC_SPR Page 61
191GES204T
return 0;
//Calling user defined function to perform
reverse }
Example 4:
Example 5:
scanf("%lf", &num1);
ITC_PC_SPR Page 62
191GES204T
if (num1 >= num2 && num1 >= num3) // num3 is the largest number
Example 6:
else {
printf("Enter first number: ");
if (num2 >= num3)
scanf("%lf", &num1);
printf("%.2lf is the largest number.",
printf("Enter second number: "); num2);
scanf("%lf", &num2); else
printf("Enter third number: "); printf("%.2lf is the largest number.",
scanf("%lf", &num3); num3);
ITC_PC_SPR Page 63
191GES204T
Example 7:
}
// greater number between num1 and num2 is
++max;
stored in max
}
max = (num1 > num2) ? num1 : num2;
return 0;
Example 8:
//storing user input into num1 and num2 //Mathematical formula to calculate LCM
from GCD
scanf("%d %d", &num1, &num2);
lcm = (num1 * num2) / gcd;
ITC_PC_SPR Page 64
191GES204T
Example 9:
{ }
{ }
ITC_PC_SPR Page 65
191GES204T
Example 10:
#include<stdio.h> if ( i <= 1 )
{ else
first_term = second_term;
//Ask user to input number of terms
second_term = next_term;
printf("Enter the number of terms:\n");
}
scanf("%d",&count);
printf("%d\n",next_term);
}
printf("First %d terms of Fibonacci
series:\n",count);
return 0;
for ( i = 0 ; i < count ; i++ )
{ }
Example 11:
{ }
scanf("%d",&num); if(n==0)
return(n*find_factorial(n-1));}
ITC_PC_SPR Page 66
191GES204T
Example 12:
int main() {
{ flag_var=0;
*/ flag_var=1;
printf("%d\n",i);
//Display prime numbers for input range
}
printf("Prime numbers from %d and %d
return 0;
are:\n", num1, num2);
}
Example 13:
#include<stdio.h>
scanf("%d",&num);
ITC_PC_SPR Page 67
191GES204T
/* We are adding cubes of every digit /* If sum of cubes of every digit is equal to
number
* and storing the sum in variable sum
* itself then the number is Armstrong
*/
*/
while (num != 0)
if(copy_of_num == sum)
{
printf("\n%d is an Armstrong
rem = num % 10; Number",copy_of_num);
sum = sum + (rem*rem*rem); else
num = num / 10; printf("\n%d is not an Armstrong
} Number",copy_of_num);
return(0);
Example 14:
int main() }
temp=num; else
ITC_PC_SPR Page 68
191GES204T
Example 15:
#include <stdio.h>
Example 16:
Example 17:
int main() {
printf("Enter the value of n(positive integer): printf("Sum of first %d natural numbers is:
"); %d",n, sum);
Example 18:
int main() */
{ count=1;
}
* or decrement it inside the body of loop
like we did
Example 19:
#include <stdio.h> }
int arr[100],size,sum=0; {
scanf("%d",&size); }
printf("Enter the elements of the array: "); printf("Sum of array elements is: %d",sum);
{ }
scanf("%d",&arr[i]);
ITC_PC_SPR Page 70
191GES204T
Example 20:
int main() }
int numberOfRows = 0; {
scanf("%u",&numberOfRows); }
for(row=1; row<=numberOfRows; ++row) // Move the cursor to new line for next row
{ printf("\n");
for(column=row; column<numberOfRows; }
++column)
Example 21:
#include <stdio.h> }
printf("* ");
ITC_PC_SPR Page 71
191GES204T
Example 22:
Example 23:
Example 24:
#include <stdio.h>
int num1, num2; //%.2f is used for displaying output upto two
decimal places
float avg;
printf("Average of %d and %d is:
printf("Enter first number: "); %.2f",num1,num2,avg);
scanf("%d",&num1); return 0;
printf("Enter second number: "); }
scanf("%d",&num2);
ITC_PC_SPR Page 72
191GES204T
Example 25:
Example 26:
scanf("%d",&num); }
Example 27:
ITC_PC_SPR Page 73
191GES204T
Example 28:
Example 29:
#include <stdio.h> {
{ }
else
scanf("%c",&ch); return 0;
}
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch
=='I' ||ch=='o'||ch=='O'||ch=='u'||ch=='U')
Example 30:
#include<string.h> int i;
ITC_PC_SPR Page 74
191GES204T
scanf("%s",str); }
if(str[i]>=97&&str[i]<=122) return 0;
str[i]=str[i]-32; }
Example 31:
#include<string.h> scanf("%s",str);
int main(){
ITC_PC_SPR Page 75