Session Objectives
Explain Types of Control Statements
Explain If, If..else statements
Understand Looping concepts
Explain Switch..Case statement
Explain Break,continue Statements
Explain the Goto Statement
• Jumping
• Decision making&
Statement
Branching
• Looping
Statement
Decision
Making &Branching
Statement
Jumping
Statement
Looping
Statement
Syntax :
if(condition)
{
True Block Statement(s);
}
Executable statement(s);
Test TRUE
Condition
FALSE True Block Statements
Executable Statements
Syntax :
if(condition)
{
True Block Statement(s);
}
else
{False Block statement(s);
}
FALSE Test TRUE
Condition
False Block Statements True Block Statements
Syntax :
Initialization counter;
while(condition)
{
Body of the Loop;
Increment/Decrement
counter ;
}
Start
Initialize
FALSE
Test
Condition
TRUE STOP
Body of the loop
Increment or
Decrement
Syntax :
Initialization counter;
do
{
Body of the Loop;
Increment/Decrement
counter ;
} while(condition);
Start
Initialize
Body of the loop
Increment or Decrement
TRUE Test
Condition
FALSE
Stop
Syntax : For(initialization;condition;increment/decrement)
{
Body of the Loop;
}
Start
Initialize
FALSE
Test
Condition
TRUE STOP
Body of the loop
Increment or
Decrement
Multiple Initialization / Increments
The following loop is used for multiple initialization -
The for loop will be termed as a nested for loop when
it is written like this : -
Write a “C” Program swapping two
numbers Decrement Operator Example
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int a,b,c; int i;
printf("enter two number="); i=2;
scanf ("%d %d",&a,&b); printf("\n The value of i is %d",i);
b=a+b; printf("\n The value of i is %d",--i);
a=b-a; printf("\n The value of i is %d",i);
b=b-a; }
printf("%d %d",a,b);
} Write a program to convert farenheit into
celsius = (far - 32) *5/9
Increment operator(++) Example
#include<stdio.h> #include<stdio.h>
void main() void main()
{
{
float faren,celsius;
int i=2; printf("\n Enter the farenheit = ");
printf("\n The value of i is %d",i); scanf(" %f", &faren);
printf("\n The value of i is %d",++i); celsius = (faren -32) *5/9;
printf("The Celsius is = %f",celsius);
printf("\n The value of i is %d",i);
}
}
Write a program to accept your name,address & city and print it one by one
#include<stdio.h>
void main()
{
char name[20],add[30],city[20];
printf("\n Enter the name=");
scanf("%s",&name);
printf("\n Enter the Address=");
scanf("%s",&add);
printf("\n Enter the City=");
scanf("%s",&city);
printf("The Name is = %s\n",name);
printf("The Address is = %s\n",add);
printf("The City is = %s\n",city);
}
Enter the Name = CSCComputer The Name is = CSCComputer
Enter the Address= Meenambalsalai The Address is= Meenambalsalai
Enter the city = Chennai The city is = Chennai
How to print a Special characters
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf("\n This is how to print a double quote : \"\n");
printf("\n This is how to print a backslash : \\\n");
c='\'';
printf("\n c now contains %c\n",c);
printf("\n This is how to print a percent - sign : %%\n");
c='\007'; /* beep sound code is 7 */
printf("\n c now contains %d\n",c);
getch();
}
Write a program to print employee name, Write a program to find the greatest among two
basic pay,pf,lic & calculate the net salary numbers a,b
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
char name[20]; int a,b,c;
int basic,pf,lic,net; printf("\n Enter 2 nos. =");
printf("\n Enter the name="); scanf("%d %d",&a,&b);
scanf("%s",name); if (a>b)
printf("\n Enter Basic Bpay ="); {
scanf("%d",&basic); printf("A is greatest %d“,a);
printf("\n Enter PF ="); }
scanf("%d",&pf); else
printf("\n Enter LIC ="); {
scanf("%d",&lic); printf(" B is greatest %d“,b);
net = basic – (pf + lic); }
printf("Net Salary is = %d ",net); getch();
} }
Enter 2 nos. = 100
20
A is greatest 100
Write a program to find the greatest of
Three numbers Write a program to check whether the given number
#include<stdio.h> is positive or negative.
void main() #include<stdio.h>
{ void main()
int a,b,c; {
printf("\n Enter 3 nos. ="); int a;
scanf("%d %d %d",&a,&b,&c); printf("Enter the number=");
if ((a>b) && (a>c)) scanf("%d",&a);
{ if (a>0)
printf("A is greatest %d“,a); {
} printf( "Given number is positive %d“.a);
elseif (b>c) }
{ Elseif(a<0)
printf(" B is greatest %d“,b); {
} printf("Given number is negative %d“,a);
else }
{ Else
printf("C is greatest %d“.c); {
} printf("Given number is Zero %d“,a);
getch(); }
} }
Enter 3 nos. = 10
20
5 Enter the number = 10
B is greatest The Given Number is positive
Write a progrm input the given Number id Odd or Even
Write a progrm input the given
Number
year is LEAP YEAR or Not
#include<stdio.h>
#include<stdio.h>
void main()
void main()
{
{
int no;
int year;
printf("Enter the Number =\n");
printf("Enter the year =\n");
scanf("%d",&no);
scanf("%d",&year);
if ((no%2)==0)
if ((year%4)==0)
{
{
printf("\n The Number is Even Number %d“,no);
printf("\n It is a leap year");
}
}
else
else
{
{
printf("\n The Number is Odd Number %d“,no);
printf("\n It is not a leap year");
}
}
getch();
getch();
}
}
Enter the number = 12
Enter the Year 2000 The Number is Even Number 12
It is a leap year
Write a program to print the FIBONACCI PASCAL TRIANGLE
SERIES upto N Numbers
#include<stdio.h>
#include<stdio.h>
void main() void main()
{ {
int n,i,f1,f2,f3; int i,j;
printf("enter the number = "); clrscr();
scanf("%d",&n); for(i=1;i<=5;i++)
f1=-1; {
f2=1; for(j=1;j<=i;j++)
for(i=0;i<=n;i++)
{
{
f3=f1+f2; printf("%d\n",i);
printf("%d\n",f3); }
f1=f2; printf("\n");
f2=f3; }
}
} Output
1
Enter the number = 5 22
0 333
1
1 4444
2
3 55555
5
PASCAL TRIANGLE PASCAL TRIANGLE
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int i,j; int i,j,c=1;
clrscr(); clrscr();
for(i=1;i<=5;i++) for(i=1;i<=5;i++)
{ {
for(j=1;j<=i;j++) for(j=1;j<=i;j++)
{ {
printf("%d\n",j); printf("%d\n",c);
} c++;
printf("\n"); }
} printf("\n");
}
Output Output
1 1
12 23
123 456
1234 7 8 9 10
12345 11 12 13 14 15
PASCAL TRIANGLE to print *
#include<stdio.h> PASCAL TRIANGLE
void main() #include<stdio.h>
{ void main()
int i,j; {
clrscr(); int i,j;
for(i=1;i<=5;i++) clrscr();
{ for(i=0;i<5;i++)
for(j=1;j<=i;j++) {
{ for(j=i;j>0;j--)
printf(“*"); {
} printf("%d\n",j);
printf("\n"); }
} printf("\n");
}
Output
Output
* 1
21
** 321
** * 4321
** * * 54321
** * * *
To print the word in reverse
#include<stdio.h>
#include<conio.h>
#define size 10
void main()
{ Enter Any String : raja
char name[size+1];
The Given String is raja
int i=1; The Reversed String is ajar
clrscr();
printf("\n Enter Any String");
scanf("%s",name);
printf("\n The Given string is %s\n",name);
for(i=0;name[i]!='\0';i++);
printf("\n\n The Reversed String is");
for(i=size-1;i>=0;i--)
{
printf("%c",name[i]);
}
getch();
}
label
The goto statement transfers control to any other
statement within the same function in a C program.
They reduce program reliability and make program
difficult to maintain.
goto Labelname:
Statements;
--
Labelname:
---
Statements;
Syntax :
switch(Expression)
{
case 1: Statements;
break;
case 2: Statements;
break;
case 3: Statements;
break;
default :Statements;
break;
}
• Default is Optional
• It is useful while writing menu driven programs
• Break statement transfers the control to the end of
switch..case statement.
Write a menu type program to solve arithmetic calculation case 4:
using switch case stmts. #include<stdio.h> tot = a/b;
void main()
printf("\n The Division = % d",tot);
{
int a,b,choice,tot;
break;
printf("\n 1. Addition"); default:
printf("\n 2. Subtraction"); printf("\n invalid");
printf("\n 3. Multiplication"); break;
printf("\n 4. Division"); }
printf("\n Enter the Values="); }
scanf("%d %d",&a,&b);
printf("enter your choice=");
scanf("%d",&choice);
switch(choice)
{
case 1:
tot = a+b;
printf("\n The Addition of 2 nos. % d",tot);
break;
case 2:
tot = a-b;
printf("\n The Subtraction of 2 nos. % d",tot);
break;
case 3:
tot = a*b;
printf("\n The Multiplication of 2 nos. % d",tot);
break;
Write a program to count the number of vowels and
consonants in a string
#include<stdio.h>
void main()
{
int c=0,v=0;
char x;
printf("enter any string \n");
do
{
switch(x=getchar())
{
case 'a':
case 'e':
case 'i':
case 'o': Enter Any String : welcome
case 'u': [Link] Vowels : 3
v++; [Link] Consonants : 4
break;
case '\n':
break;
default:
c++;
break;
}
}while (x!='\n');
printf("no. of vowels is %d \n",v);
printf("no. of consonants is %d", c);
getch();
}
Write a “C” Program to generate Armstrong
No.s from 1 to 1000 Write a program print the given number as
#include<stdio.h> reverse format.
void main() #include<stdio.h>
{ void main()
int a,b,s,n,i; {
long int a,b,c=0,n;
printf("\n ENter the Limit"); printf("\n Enter the Number");
scanf("%d",&n); scanf("%ld",&a);
printf("\n The armstrong Numbers are"); while(a>0)
for(i=0;i<=n;i++) {
{ n=a%10;
a=i; a=a/10;
s=0; c=b+c*10;
}
while(a>0) printf("\n The reversed numeral is %ld",c);
{ }
b=a%10;
b=b*b*b;
s=s+b;
a=a/10;
}
if(i==s)
{ Enter the Number : 345
The reversed Numeral is 543
printf("\t %d",i);
}
}
}
Write a program to convert the number from binary to
decimal 'break' statement Example
#include<stdio.h> 'break' stmt is used to terminate loops
#include<math.h> or exit from a [Link] can be used within a do-while,
void main() for and switch statement
{
int i=0,j=0,sum=0; #include<stdio.h>
long int n,x; void main()
printf("\n Enter Binary Number"); {
scanf("%ld",&n); int i;
if(n!=0) printf("The values are\n");
{ for(i=1;i<=100;i++)
i=n%10; {
if(i==0 || i==1) printf("They are %d \n",i);
{ if(i==25)
while(n!= 0) {
{ break;
i=n%10; }
sum=sum+i*pow(2,j); }
n=n/10; }
j++;
}}}
if(sum==0)
printf("\n The no is not a binary number");
else
printf("\n The equivalent decimal number is %d",sum);
}
Print the Numbers from 1…25
Enter Binary Number : 101
The Equivalent Decimal No : 5
Convert the character from lower to upper and vice
versa.
#include<stdio.h>
void main()
{
char x,a;
Do {
printf("\n Enter any character");
fflush(stdin);
scanf("%c",&x);
if(x>='A' && x<='Z')
printf("\nLower case %c",x+('a' - 'A'));
else if(x>='a' && x<='z')
printf("\n Upper case is %c",x - ('a' - 'A'));
else
printf("\n That's not a Letter");
printf("\n Another Data[Y/N]?");
fflush(stdin);
scanf("%c",&a);
}while(a=='y');
THANK YOU
BY INDIRA - 22D21A0533
TARUNI - 22D21A0548
SUSHMA - 22D21A0549