Sie sind auf Seite 1von 6

Center for Diploma Studies Page 1/6

DAT10603: Principle of Edition 1


Programming Revision No. 1
Effective Date 23/09/2013
Title: Selection Statements
Ammendment Date 23/09/2013

CENTER FOR DIPLOMA


STUDIES

PRINCIPLE OF PROGRAMMING

LAB INSTRUCTION

Subject Code DAT 10603

Title C STRUCTURES

Course Code 1 DAT

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

1. To understand the concept and basic of IF and SWITCH CASE Statement


2. To write a C programming based on the above concept

2.0 THEORY

EXAMPLE 1

Following example will find out large number from given input:
#include<stdio.h>
int main()
{

int x,y;

printf("Enter value for X :");


scanf("%d",&x);

printf("Enter value for Y :");


scanf("%d",&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;

printf("Type any number (+ve) or (-ve) : ");


scanf("%d",&a);

printf("Type any number again : ");


scanf("%d",&b);

if (a > 0 && b > 0)


printf("\nBoth numbers are positive\n");
if (a == 0 || b == 0)
printf("\nAt least one of the numbers = 0\n");
if (!(a > 0) && !(b > 0))
printf("\nBoth numbers are negative\n");
if ((a > 0) && !(b > 0))
printf("\nThe first number is positive\n");
if (!(a > 0) && (b > 0))
printf("\nThe first number is negative\n");

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;

printf("WELCOME TO MATHEMATICAL PROGRAM\n");


printf("Enter 1 to perform addition\n");
printf("Enter 2 to perform subtraction\n");
printf("Enter 3 to perform multiplication\n");
printf("Please Enter your choice : ");
scanf("%d",&a);

printf("\nPlease Enter two numbers (separated by space): ");


scanf("%d %d",&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

1. Based on your understanding of selection statement (if of switch case), write a C


program when user enter the faculty short name, the program will display the faculty
name in full as per below table.

Singkatan Nama Penuh


FKMP Fakulti Kejuruteraan Mekanikal dan
Pembuatan
FKEE Fakulti Kejuruteraan Elektrik dan Elektronik

FSKTM Fakulti Sains Komputer dan Teknologi


Maklumat
FKAAS Fakulti Kejuruteraan Awam dan Alam
Sekitar
FSTPI Fakulti Sains Teknologi dan Pembangunan
Insan
FPTV Fakulti Pendidikan Teknikal dan Vokasional
FTK Fakulti Teknologi Kejuruteraan
PPD Pusat Pengajian Diploma

Das könnte Ihnen auch gefallen