CSC Computer Education, M.K.B.Nagar
CSC Computer Education, M.K.B.Nagar
Decision
Making &Branching
Statement
Jumping
Statement
Looping
Statement
Test TRUE
Condition
Executable Statements
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
Syntax :
if(condition)
{
True Block Statement(s);
}
else
{False Block statement(s);
}
Initialize
FALSE
Test
Condition
TRUE STOP
Body of the loop
Increment or
Decrement
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
Syntax :
Initialization counter;
do
{
Body of the Loop;
Increment/Decrement
counter ;
} while(condition);
Start
Initialize
Increment or Decrement
TRUE Test
Condition
FALSE
CSC COMPUTER EDUCATION,
M.K.B.NAGAR Stop
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
Syntax : For(initialization;condition;increment/decrement)
{
Body of the Loop;
}
Start
Initialize
FALSE
Test
Condition
TRUE STOP
Body of the loop
Increment or
Decrement
20
A is greatest 100
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
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
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
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();
}
}
Output Output
1 1
12 23
123 456
1234 7 8 9 10
12345 11 12 13 14 15
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
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
** * * *
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
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();
}
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
The continue statement causes the next iteration
break;
• Default is Optional
• It is useful while writing menu driven programs
• Break statement transfers the control to the end of
switch..case statement.
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
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;
The break & continue statements used inside a for, while and do..while loops
The loop does does not terminate when a continue statement is encountered
The goto statement is used to transfer the control in a loop or function from one point
8. Write a program to count the number of digits in an integers using while loop?
CSC COMPUTER EDUCATION,
M.K.B.NAGAR
9. Write a program to calculate the sine series?
EXERCISES
10. Write a program to print the following Outputs using for while and do..while loops?