Dat10603 - Lab 3 (Control Structures)
Dat10603 - Lab 3 (Control Structures)
PRINCIPLE OF PROGRAMMING
LAB INSTRUCTION
Title C STRUCTURES
No. of Experiment 3
Center for Diploma Studies Page 2/6
DAT10603: Principle of Edition 1
Programming Revision No. 1
Effective Date 23/09/2013
Title: Selection Statements
Ammendment Date 23/09/2013
AIM:
To write a simple C Programs on selection statements.
1.0 OBJECTIVES
2.0 THEORY
EXAMPLE 1
Following example will find out large number from given input:
#include<stdio.h>
int main()
{
int x,y;
if ( x > y )
{
printf("\nThe larger number is X which is - %d\n\n",x);
}
else
{
printf("\nThe larger number is Y which is - %d\n\n",y);
}
return 0;
}
Center for Diploma Studies Page 3/6
DAT10603: Principle of Edition 1
Programming Revision No. 1
Effective Date 23/09/2013
Title: Selection Statements
Ammendment Date 23/09/2013
EXAMPLE 2
Following example will ask user to key in positive or negative number as input:
#include <stdio.h>
int main()
{
int a,b;
return 0;
}
Center for Diploma Studies Page 4/6
DAT10603: Principle of Edition 1
Programming Revision No. 1
Effective Date 23/09/2013
Title: Selection Statements
Ammendment Date 23/09/2013
EXAMPLE 3
Following program will asks user, which mathematical operation to perform and it prints the
result after performing the desired mathematical operation.
#include<stdio.h>
int main(void)
int a,b,c;
switch(a)
{
case 1:
printf("The Addition of %d and %d is %d",b,c,(b+c));
break;
case 2:
printf("The Substraction of %d and %d is %d",b,c,(b-c));
break;
case 3:
printf("The Multiplication of %d and %d is %d",b,c,(b*c));
break;
default:
printf("You have chosen the wrong choice!!!");
}
return 0;
}
Center for Diploma Studies Page 5/6
DAT10603: Principle of Edition 1
Programming Revision No. 1
Effective Date 23/09/2013
Title: Selection Statements
Ammendment Date 23/09/2013
EXAMPLE 4
Following program will asks user, to choose their favorite fruits based on the option given.
#include<stdio.h>
int main()
{
char fruit;
printf("Which one is your favourite fruit:\n");
printf("a) Apples\n");
printf("b) Bananas\n");
printf("c) Cherries\n");
scanf("%c",&fruit);
switch (fruit)
{
case 'a':
printf("You like apples\n");
break;
case 'b':
printf("You like bananas\n");
break;
case 'c':
printf("You like cherries\n");
break;
default:
printf("You entered an invalid choice\n");
}
return 0;
}
Center for Diploma Studies Page 6/6
DAT10603: Principle of Edition 1
Programming Revision No. 1
Effective Date 23/09/2013
Title: Selection Statements
Ammendment Date 23/09/2013
3.0 QUESTION