condition

11
Exercise # ___ Pageno. Title: Turbo C/C++ Conditional Statements Date:___ Objectives: At the End of this exercise you should be able to understand the Turbo C/C++ Conditional statement syntax and to be able to create a program using conditional statements. Background Conditional Statements Format Single Statement Option 1 if(condition) statement; Option 2 if(condition) statement; else statement; Example 1: if( age >= 18) printf(“\nQualified to vote”); Example 2: if( age >= 18) printf(“\nQualified to vote”); else printf(“\nToo Young to vote”); Example 3: Option 3 if(condition) statement; else if(condition1) statement; . . .

Upload: kim-karlo

Post on 16-Oct-2014

267 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Condition

Exercise # ___ Pageno.Title:Turbo C/C++ Conditional Statements Date:___

Objectives: At the End of this exercise you should be able to understand the Turbo

C/C++ Conditional statement syntax and to be able to create a program using conditional statements.

Background

Conditional Statements FormatSingle StatementOption 1

if(condition) statement;

Option 2if(condition) statement;else statement;

Example 1:

if( age >= 18)printf(“\nQualified to vote”);

Example 2:

if( age >= 18)printf(“\nQualified to vote”);

else printf(“\nToo Young to vote”);

Example 3:

if( age >= 18) && (age <=100)printf(“\nQualified to vote”);

else if( age>=1) && (age <=17)printf(“\nToo Young to vote”);

elseprintf(“\nOut of range”);

Option 3if(condition) statement;

else if(condition1) statement;...else if(conditionn) statement;

Page 2: Condition

Exercise no. 7: Turbo C/C++ Conditional statementsNAME: ____________________________________ SECTION: _______________

DATE: __________________

1. Write a program that determines if the input letter is a VOWEL or CONSONANT. The vowels are: A E I O U. Your program must be able to handle a capital or small input letter.

2. Create a program flowchart for the Air Force to label an aircraft as military or civilian. The program is to be given the plane’s observed speed in km/h and its estimated length in meters. For Planes traveling in excess of 1100 km/h, and longer than 52 meters, you should label them as “civilian” aircraft, and shorter such as 500 km/h and 20 meters as “military” aircraft. For planes traveling at more slower speeds, you will issue an “It’s a bird” message.

Page 3: Condition

3. Write a program to assist a teacher in calculating student’s grade at the end of the semester. It accepts a numerical grade as input, then it will display character grade as output, based on the given scale:

Range Grade90 and above A80 – 89 B70 – 79 C60 – 69 DBelow 60 F

Page 4: Condition

Exercise # ___ Pageno.Title:Turbo C/C++ Switch Case Statements Date:___

Objectives: At the End of this exercise you should be able to understand the Turbo

C/C++ Switch Case statement syntax and to be able to create a program using switch case statements.

Background

Switch Case StatementThe switch case statement is a multi-way decision that tests whether an expression matches one of a number of constant integer or character values and branches accordingly. If the case matches the expression value, execution starts at the case and end with a break statement.

Format:

switch(expression)case const-value1: statement1;

statement;break;

case const-value2: statement1;statement;break;

.

.

.case const-valuen: statement1;

statement; break;

default: statements;

Page 5: Condition

Sample Problem 1:Write a program that will ask the user to input two numbers and let him/her choose what to compute, such as 1]sum 2]diff 3]product. Solution:

#include<stdio.h>#include<conio.h>main(){

int a,b;int sum,diff,product;int choice;clrscr()printf(“\nInput 2 nos:”);scanf(“%d%d”,&a,&b);clrscr();printf(“\n1]sum\n2]difference\n3]product”);printf(“\nchoice:”);scanf(“%d”,&choice);

switch(choice){

case 1: sum = a + b;printf(“\nThe sum is %d”,sum);break;

case 2: diff = a - b;printf(“\nThe diff is %d”,diff);break;

case 3: product = a * b;printf(“\nThe product is %d”,product);break;

}getch();}

Page 6: Condition

Sample Problem 2:Write a program that will ask the user to input two numbers and let him/her choose what to compute, such as A]sum B]diff C]product. Solution:

#include<stdio.h>#include<conio.h>main(){

int a,b;int sum,diff,product;char choice; clrscr()printf(“\nInput 2 nos:”);scanf(“%d%d”,&a,&b);clrscr();printf(“\n1]sum\n2]difference\n3]product”);printf(“\nchoice:”);scanf(“%c”,&choice);choice = getchar();switch(choice){

case ‘a’:case ‘A’: sum = a + b;

printf(“\nThe sum is %d”,sum);break;

case ‘b’case ‘B’: diff = a - b;

printf(“\nThe diff is %d”,diff);break;

case ‘c’case ‘C’: product = a * b;

printf(“\nThe product is %d”,product);break;

}getch();}

Page 7: Condition

Exercise no. 7: Turbo C/C++ Conditional statementsNAME: ____________________________________ SECTION: _______________

DATE: __________________

1. Write a program that determines if the input letter is a VOWEL or CONSONANT. The vowels are: A E I O U. Your program must be able to handle a capital or small input letter.

2. Create a program for the Air Force to label an aircraft as military or civilian. The program is to be given the plane’s observed speed in km/h and its estimated length in meters. For Planes traveling in excess of 1100 km/h, and longer than 52 meters, you should label them as “civilian” aircraft, and shorter such as 500 km/h and 20 meters as “military” aircraft. For planes traveling at more slower speeds, you will issue an “It’s a bird” message.

Page 8: Condition

1. Write a program that accepts a number and outputs its equivalent in words.(use extra sheet of paper if needed)

Sample input/output dialogue:Enter a number : 1380One thousand three hundred eighty

Take note that the maximum input number is 3000.

Page 9: Condition