0% found this document useful (0 votes)
30 views10 pages

CHAPTER 3 ML FUNDAMENTALS OF PROGRAMMING With C++ HANDOUT v1 0

Uploaded by

Endush G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views10 pages

CHAPTER 3 ML FUNDAMENTALS OF PROGRAMMING With C++ HANDOUT v1 0

Uploaded by

Endush G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

COVER PAGE TABLE OF CONTENTS

TABLE OF CONTENTS ......................................................................................................................................... 1


PART ONE........................................................................................................................................................... 4
CHAPTER ONE – PRELIMINARY OF PROGRAMMING ............................................................................. 5
General Introduction to Computer System ....................................................................................... 5
What is a program?.......................................................................................................................... 6
Why Study C++? .................................................................................................................................... 11

Fundamentals of Programming with C++ Our First C++ Program .......................................................................................................................... 12


1.1.1. The Program Development Cycle ................................................................................................ 13
- Handout [ COCS-1013 ] Step 1: De ne the Problem .............................................................................................................. 13
Step 2: Design the Program .............................................................................................................. 13
Step 3: Code the Program ................................................................................................................. 14
Step 4: Compile and Link the Program............................................................................................. 14
Step 5: Execute the Program ............................................................................................................ 15
Step 6: Test and Debug the Program ................................................................................................ 15
1.1.2. Errors in the Cycle ....................................................................................................................... 15
CHAPTER TWO – INTRODUCTION TO C++ PROGRAMMING ................................................................. 17
The Origins of C++ .......................................................................................................................... 17
How Compilation of C++ program works ........................................................................................ 17
Structure of C++ Program ............................................................................................................... 18
Variable ......................................................................................................................................... 19
Identifiers ...................................................................................................................................... 21
Fundamental Data Types ............................................................................................................... 21
Constants ...................................................................................................................................... 22
1. Literal constants ............................................................................................................................... 23
2. De ned constants ............................................................................................................................. 23
3. Symbolic Constants........................................................................................................................... 23
Comments ..................................................................................................................................... 24
Simple Input Output ............................................................................................................................. 25
Modifying the output ........................................................................................................................... 26
Expressions .................................................................................................................................... 27
Eyasu G.Z. ( MSc, MA ) 1. Arithme c Operators ....................................................................................................................... 27
@joshkiyakoo : telegram
[email protected] : email 2. Rela onal Operators ........................................................................................................................ 28
3. Logical Operators ............................................................................................................................. 30
4. Increment/Decrement Operators .................................................................................................... 30
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)
5. Assignment Operator ....................................................................................................................... 31 Default Arguments ........................................................................................................................ 59
5.1 Simple assignment: ..................................................................................................................... 32 Variable Number of Arguments / Overloaded functions ................................................................. 60
5. 2 Compound assignment: ............................................................................................................. 33 Inline Functions ............................................................................................................................. 61
6. Condi onal Operator ....................................................................................................................... 34 Recursion ...................................................................................................................................... 62
7. The sizeof Operator .......................................................................................................................... 34 CHAPTER FOUR - EXERCISES ........................................................................................................... 64
Operator Precedence ..................................................................................................................... 35 PART TWO........................................................................................................................................................ 67
CHAPTER TWO - EXERCISES ............................................................................................................ 37
CHAPTER THREE – STATEMENTS ........................................................................................................ 38
STATEMENTS (PROGRAM CONTROL STRUCTURE STATEMENTS) ..................................................... 38
1. Simple statement.............................................................................................................................. 38
2. Compound Statement ...................................................................................................................... 39
3. Selec on / Condi onal Statement ................................................................................................... 39
3.1 Two-way selec on ...................................................................................................................... 39
3.2 Mul ple-way Selec on ............................................................................................................... 42
4. Itera ve / Looping Statements ........................................................................................................ 43
4.1 The for loop ................................................................................................................................. 43
4.2 The while statement ................................................................................................................... 45
4.3 The do...while statement ........................................................................................................... 46
4.4 Nested Loop Statements ............................................................................................................ 47
4. Jump statements .............................................................................................................................. 48
4.5.1 Break statement ...................................................................................................................... 48
4.5.2 The con nue statement .......................................................................................................... 49
4.5.3 The goto statement ................................................................................................................. 49
4.5.4 The return statement .............................................................................................................. 49
CHAPTER THREE - EXERCISES .......................................................................................................... 51
CHAPTER FOUR – FUNCTIONS ............................................................................................................ 53
FUNCTIONS (PROGRAM BLOCK OF STATEMENTS)........................................................................... 53
Function Declaration / Prototype Declaration ................................................................................ 53
Function Definition ........................................................................................................................ 53
Calling functions ............................................................................................................................ 54
A Simple Func on ................................................................................................................................. 55
Parameters and Arguments ........................................................................................................... 56
Global and Local Scope .................................................................................................................. 58
Scope Operator.............................................................................................................................. 59

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 2 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 3 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)

CHAPTER THREE – STATEMENTS

STATEMENTS (PROGRAM CONTROL STRUCTURE STATEMENTS)


Statements represent the lowest-level building blocks of a program. Each statement represents a
computa onal step which has a certain side-e ect. (A side-e ect can be thought of as a change in the
program state, such as the value of a variable changing because of an assignment.) Statements are useful
because of the side-e ects they cause, the combina on of which enables the program to serve a speci c
purpose (e.g., sort a list of names).

A statement causes an action to be performed. In C++, a statement controls the sequence of execution,
evaluates an expression, or does nothing (the null statement). The basic structure of any program can be
represented like this:

Begin
Statement
Statement

PART ONE Statement


.....
End
A running program spends all of its me execu ng statements. The order in which statements are executed
is called ow control (or control ow). This term re ect the fact that the currently execu ng statement has
the control of the CPU, which when completed will be handed over ( ow) to another statement.
C++ insists that all statements end with a semicolon. The reason is that C++ is tolerant of different layouts.
If you chose to put several statements on one line, then the semicolons would be the only way in which C++
would be able to separate them.
Like many procedural languages, C++ provides different forms of statements:

Statements
Simple Compound Selection Iterative Jump
Statement Statement Statement Statement Statement

do-while
simple if for loop while loop goto
loop

simple simple simple do-


if-else break
for-loop while-loop while-loop

nested nested nested do-


if-else-if continue
for-loop while-loop while-loop

nested if

1. Simple Statement
A simple statement is a computa on terminated by a semicolon. Variable de ni ons and semicolon- terminated
expressions are representa ves pf this category.

int i; //declara on statement

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 4 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 38 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)
x++; // this has a side-e ect First expression is evaluated. If the outcome is nonzero then statement is executed. Otherwise, nothing
m+3; //useless statement b/c it has no side-e ect; result is just discarded
happens. For example, when dividing two values, we may want to check that the denominator is nonzero:

The simplest statement is the null statement which consists of just a semicolon: if (count != 0)
average = sum / count;
; // null statement To make mul ple statements dependent on the same condi on, we can use a compound statement:
2. Compound Statement if (balance > 0) {
Compound statement is a unit of code consis ng of zero or more statements; hence the name compound. interest = balance * creditRate;
This consists of an opening brace, an op onal declara on and de ni on sec on, and an op onal statement balance += interest;
sec on, followed by a closing brace. }
For example: ✓ A variant form of the if statement allows us to specify two alterna ve statements: one which is
executed if a condi on is sa s ed and one which is executed if the condi on is not sa s ed. This is
{ int min, i = 10, j = 20; called the if-else statement and has the general form:
if (expression)
min = (i < j ? i : j);
statement1;
cout << min << '\n'; else
statement2;
}
✓ First expression is evaluated. If the outcome is nonzero then statement1 is executed. Otherwise,
Compound statements are useful in two ways: statement2 is executed.
For example:
(i) They allow us to put mul ple statements in places where otherwise only single statements are
allowed, and if (balance > 0) {
(ii) They allow us to introduce a new scope (part of the program text within which a variable remains interest = balance * creditRate;
de ned in the program) in the program. For example, the scope of min, i, and j in the above balance += interest;
example is from where they are de ned ll the closing brace of the compound statement. Outside }
the compound statement, these variables are not de ned. else {
Notes:  Because a compound statement may contain variable de ni ons and de nes a scope for them, it interest = balance * debitRate;
is also called a block. balance += interest;
 A block does not need a semi colon. }

3. Selec on / Condi onal Statement


E.g. 1
Selec on/Condi onal statements are used for specifying
#include <iostream >
alternate paths of execu on, depending on the outcome of a int main ( ) {
logical condi on. It is some mes desirable to make the int x;
execu on of a statement dependent upon a condi on being cout << "Please guse a number I have inmind from 1-10:";
sa s ed. C++ provides such facili es. The if statement enables cin >> x;
you to test for a condi on (such as whether two variables are if (x > 10)
equal) and branch to di erent parts of your code, depending on cout << "That number is too big!" << endl;
the result. The modern languages provide two selec on if (x < 1)
constructs: two-way and mul ple-way selec ons. cout << "That number is too small!" << endl;
if (x != 5)
3.1 Two-way selec on cout << "You didn't get the right number!" << endl;
It is some mes desirable to make the execu on of a statement if (x==5)
dependent upon a condi on being sa s ed. The if statement cout << "You didn't get the right number!" << endl;
provides a way of expressing this, the general form of which is: cout << "Thank you and goodbye!" << endl;
}
if (expression)
statement; E.g. 2 #include<iostream>

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 39 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 40 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)
int main ( ) { else if (ch >= 'a' && ch <= 'z')
cout << "Please type the number 8 : "; cout<<"the character "<<ch<<"Small le er le er";
cin >> x; else
if (x == 8) cout<<"the character "<<ch<<"It is a special character";
cout << "Thank you! I appreciate that." << endl;
else { 3.2 Mul ple-way Selec on
cout << "Why can't you follow simple instructions?<< endl; The switch statement provides a way of choosing between a set of alterna ves, based on the value of an expression.
} The general form of the switch statement is:
}
switch (expression) {
Note: If statements may be nested by having an if statement appear inside another if statement. For case constant 1:
example: statements;
.....
if (callHour > 6) {
case constant n:
if (callDura on <= 5)
statements;
charge = callDura on * tarrif1;
default:
else
statements;
charge = 5 * tarrif1 + (callDura on - 5) * tarrif2;
}
}
✓ First expression (called the switch tag or case selector) is evaluated, and the outcome is compared to
else
each of the numeric constants (called case labels), in the order they appear, un l a match is found.
charge = atFee;
The statements following the matching case are then executed. Note the plural: each case may be
followed by zero or more statements (not just one statement). Execu on con nues un l either a
✓ A frequently-used form of nested if statements involve the else part consis ng of another if-else break statement is encountered or all intervening statements un l the end of the switch statement
statement like:
are executed. The nal default case is op onal and is exercised if none of the earlier cases provide a
void main( ) {
match.
Char ch;
Cout<<"Enter a character"<<endl;
Cin>>ch; E.g.1 The following example prompts user to enter an operator and 2 operands and generate the
expression result with the selected operator on the operands.
if (ch >= 0 && ch <= 9) int main () {
cout<<"the character "<<ch<<"is a number"; int operand1, operand2;
else { char operator;
if (ch >= 'A' && ch <= 'Z') cout <<"enter two numbers"<<endl;
cout<<"the character "<<ch<<"capital le er"; cin >> operand1>>operand2;
else { cout <<"enter one of the these operators ‘+’ , ’*’ , ‘/’ , ‘%’ "<<endl;
if (ch >= 'a' && ch <= 'z') cin >> operator;
cout<<"the character "<<ch<<"Small le er le er"; switch (operator) {
else case '+':
cout<<"the character "<<ch<<"It is a special character"; cout<<operand1 + operand2;
} break;
} case '-':
} cout<<operand1 - operand2;
For improved readability, it is conven onal to format such cases as follows: break;
if (ch >= '0' && ch <= '9') case '*':
cout<<"the character "<<ch<<"is a number"; cout<<operand1 * operand2;
else if (cha >= 'A' && ch <= 'Z') break;
cout<<"the character "<<ch<<"capital le er"; case '/':

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 41 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 42 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)
cout<<operand1 / operand2; for (expression1; expression2; expression3)
break;
for (ini aliza on; expression; variable update)
default:
cout << "unknown operator: " << operator << ‘\n'; ✓ First expression1 is evaluated. Each me round the
break; loop, expression2 is evaluated. If the outcome is
} nonzero then statement is executed and expression3
} is evaluated. Otherwise, the loop is terminated.
It should be obvious that any switch statement can also be wri en as mul ple else-if statements. The above ✓ The most common use of for loops is for situa ons
statement, for example, may be wri en as: where a variable is incremented or decremented
with each itera on of the loop.
int main () {
int operand1, operand2;
//calculates the sum of numbers from 1 through n
char operator;
double result; void main ( ) {
cout <<" Enter two Integers "<<endl; int sum = 0, n;
cin >> operand1>>operand2; cout << "Enter the maximum number" << endl;
cout <<"Enter one of the arithme c operators ‘+’ , ’*’ , ‘/’ , ‘%’ "<<endl; cin >> n;
cin>> operator; for (int i = 1; i <= n; ++i)
if (operator == '+') sum += i;
cout<<operand1 + operand2;
cout << "the sum of numbers from 1 to << n << " is =: " << sum;
else if (operator == '-')
cout<< operand1 - operand2; }
else if (operator == 'x' || operator == '*')
E.g. 1
cout<< operand1 * operand2;
else if (operator == '/') #include<iostream >//displays the squares of the numbers [1,20]
cout<< operand1 / operand2; #include<iomanip.h> // for setw( ),
else void main ( ) {
cout << "unknown operator: " << operator << '\n'; int n;
} for (n=1; n<=20; n++) {
cout << setw(4) << n;
int sq = n*n;
However, the switch version is arguably neater in this case. In general, preference should be given to the
cout<<setw (6) << sq<<endl;
switch version when possible. The if-else approach should be reserved for situa on where a switch cannot
} //end of loop
do the job (e.g., when the condi ons involved are not simple equality expressions, or when the case labels
} // end of program.
are not numeric constants).

4. Itera ve / Looping Statements


E.g. 2
A loop is a way of repea ng a series of instruc ons several mes. The loop is set up either to repeat a certain
#include <iostream.h>
number of mes or to go round and round un l some condi on is met. Either way there should be some
void main ( ) {
condi on that makes the loop terminate. int x, limit, sum;
The loop can be arranged in one of three ways: cout << "Please enter a number bigger than 1 : ";
cin >> limit;
• for statement sum = 0;
• while statement for (x = 1; x <= limit; x++) {
• do…while statement cout << "I am adding " << x << endl;
sum = sum + x;
4.1 The for loop }
This is the simplest and straight-forward looping construct that has the following general look: cout << endl;

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 43 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 44 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)
cout << "The sum of all the numbers from 1 to "; E.g 2
cout << limit << " is " << sum; // An example of a while loop
return 0; #include <iostream>
}
Using namespace std:
int main () {
4.2 The while statement int number;
The while statement (also called the while loop) provides a cout << "Please type a number bigger than 10 : ";
way of repea ng a statement while a condi on holds. cin >> number;
Syntax: while (number <= 10) {
while ( expression ) cout << "No, bigger than 10! Try again: ";
Statements;
cin >> number;
}
✓ First expression (called the loop condition) is }
evaluated. If the outcome is nonzero then statement
(called the loop body) is executed and the whole
process is repeated. Otherwise, the loop is 4.3 The do...while statement
terminated. The do statement (also called do loop) is similar to the while statement, except that its body is executed first
and then the loop condition is examined.
//the while logic for summing numbers from 1 through n Syntax: do
void main ( ) { statement;
int sum =0, i=1, n; while (expression);
cout << "Enter the maximum number" << endl;
cin >> n; ✓ First statement is executed and then expression is evaluated. If the outcome of the latter is nonzero
while ( i <= n ) then the whole process is repeated. Otherwise, the loop is terminated.
sum += i++; ✓ The do loop is less frequently used than the while loop. It is useful for situations where we need the
cout << "The sum of numbers from 1 to << n << " is =: " << sum; loop body to be executed at least once, regardless of the loop condition.
} //the DO while logic for summing numbers from 1 through n
void main() {
E.g. 1 int sum = 0,i=1,n;
cout<<"Enter the maximum number"<<endl;
//a program to display numbers from 100 to 1
cin>>n;
#include<iostream.h>
do
#include <iomanip.h>
sum += i++;
void main ( ) {
cout<<"the sum of numbers from 1 to <<n<<"is =:"<<sum;
int x = 100;
}
while (x>=1) {
cout<< setw(4)<<x<<endl; E.g.1
x--; //the following program displays “Hello’’ un l one presses ‘N’
} //end of while loop
return; #include<iostream.h>
} //end of main prog #include<conio.h>
void main ()
{
char ch;
Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 45 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 46 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)
do { cout << "Correct!" << endl;
else
cout<<"Hello!\n"; cout << "No, try again!" << endl;
cout<<"Do you want to display more Hello's (Y/N) "; }
while (answer != 2*x + 30 - x);
cin >>ch; }
}
} while (ch != 'N');
getch(); 4. Jump statements
} 4.5.1 Break statement
This statement causes an exist from a loop or decision block. It takes the control from the inner block to the
E.g. 2 outer (out of the following closing brace). In switch statement it is used to exit from the switch statement. A
/* this prog asks the user to type a number from 1 to 10 (inclusive) and refuses to accept any number break statement only applies to the loop or switch immediately enclosing it. It is an error to use the break
outside that range. */
statement outside a loop or a switch.
#include <iostream.h>
void main () { E.g. /* this prog converts characters from small case to upper case if they are wri en in small
int my_number; cases. The loop body will be prematurely terminated by the break statement if ‘N’ is
int valid; // 1 if the number is valid, pressed,
// 0 otherwise
cout << "Please enter a number from 1 to 10 : "; */
do
#include<iostream.h>
{ cin >> my_number;
valid = 1; // by default #include<conio.h>
if (my_number < 1)
valid = 0; #include<ctype.h>
if (my_number > 10) void main( ) {
valid = 0;
if (valid == 0) char ch;
cout << "I said from 1 to 10! Try again : ";
} for( int i=1; i<=26;i++) {
while (valid == 0); cout<<"Enter a character ";
cout << "Thank you." << endl;
} cin>>ch;
char up=toupper(ch); // converts character from
4.4 Nested Loop Statements
We can stick/nest one loop statement inside another one. In such cases, the resulting loop statement is cout<<"upper case "<<up; // lower case to upper one
called a nested loop statement. In principle, you can have as many loops nested inside each other as you
cout<<"\nCon nue [Y/N] ";
like.
cin>>ch;
//this prog demonstrates the nesting of loop statements
#include <iostream.h> if (ch = = 'N')
void main () {
break; // exit out of loop.
int x, answer;
for (x = 1; x <= 10; x++) { }
cout << "Question " << x << endl;
cout << "What is the answer to " << 2*x cout<<"Thanks";
<< " + " << (30 - x) << " ? : "; getch();
do {
cin >> answer; }
if (answer == 2*x + 30 - x)
Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 47 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 48 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)
4.5.2 The con nue statement ✓ For a function whose return type is void, expression should be empty:
The continue statement terminates the current itera on of a loop and instead jumps to the next itera on. It
applies, just like the break statement, to the loop immediately enclosing the con nue statement. In while int main (void) {
and do loops, the next itera on commences from the loop condi on. In a for loop, the next itera on cout << "Hello World\n";
return 0;
commences from the loop’s third expression. }
E.g. //the following program displays even natural numbers below 100
#include<iostream>
#include<iomanip.h>
#include<conio.h>
void main () {
for (int i=1; i<=100; i++) {
if (i%2!= 0)
con nue;
else
cout<<setw(4)<<i<<endl;
}
getch();
}

4.5.3 The goto statement


The goto statement provides the lowest-level of jumping. It has the general form:
goto label;
where label is an identifier which marks the jump destination of goto. The label should be followed by a
colon and appear before a statement within the same function as the goto statement itself. But most
programmers these days avoid using it altogether in favor of clear programming.

for (i = 0; i < attempts; ++i) {


cout << "Please enter your password: ";
cin >> password;
if (Verify(password)) // check password for correctness
goto out; // drop out of the loop
cout << "Incorrect!\n";
}

out:
//etc...

4.5.4 The return statement


The return statement enables a function to return a value to its caller. It has the general form:
return expression;
where expression denotes the value returned by the function. The type of this value should match the return
type of the function. For a function whose return type is void, expression should be empty:

return;
Note:
✓ The only function we have discussed so far is main, whose return type is always int. The return
value of main is what the program returns to the operating system when it completes its
execution.
Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 49 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 50 of 68
Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC) Fundamentals of Programming with C++ MicroLink Informa on Technology College 2016 (2024 GC)

CHAPTER THREE - EXERCISES y =3;


case 1 : x =4;
1. Write a program which inputs a person’s height (in centimetres) and weight (in kilograms) and outputs Default:
one of the messages: underweight, normal, or overweight, using the criteria: y = 3;
x = 1;
Underweight: weight < height/2.5 }
Normal: height/2.5 <= weight <= height/2.3
Overweight: height/2.3 < weight 9. If the variable divisor is not zero, divide the variable dividend by divisor, and store the result in quotient.
2. Write a program which inputs a date in the format dd/mm/yy and outputs it in the format month dd, year. If divisor is zero, assign it to the quotient. Then print all the variables. Assume the dividend and divisor
For example, 25/12/61 becomes: December 25, 1961 are integer and quotient is a double.
10. write a program that create the following number pattern.
3. Write a program which produces a simple multiplication table of the following format for integers in the 1 2 3 4 5 6 7 8 9
range 1 to 9: 1 2 3 4 5 6 7 8
1x1=1 1 2 3 4 5 6 7
1x2=2 1 2 3 4 5 6
... 1 2 3 4 5
9 x 9 = 81 1 2 3 4
1 2 3
4. Write a program which inputs an integer value, checks that it is positive, and outputs its factorial, using 1 2
the formulas: 1
factorial(0) = 1
factorial(n) = n × factorial(n-1) 11. Write a program that accepts student mark out of 100, and return the corresponding letter grade based
. on the following condition:
5. Write a program that display numbers from 0 to 10 using three loops. if mark is greater than or equal to 90 A
if mark is greater than or equal to 80 and less than 90 B
6. write for loop that will produce each of the following sequence if mark is greater than or equal to60 and less than 80 C
a. 2, 4, 6, ….44 if mark is greater than or equal to 40 and less than 60 D
b. 5, 7, 9,…...45 if mark is less than 40 F
c. The sum of numbers between 2 to 44 inclusive for other cases NG
d. The sum of the first 20 numbers in the series 1, 4, 7, 10… N.B write the program using both switch and else-if.

7. Re write the following code fragment using one switch statement


if (ch = = ‘E’|| ch= = ‘e’)
cout<<" this is either the value of ‘E’ or ‘e’";
else if (ch = = ‘A’|| ch= = ‘a’)
cout<<" this is either the value of ‘A’ or ‘a’";
else if (ch = = ‘r’|| ch= = ‘i’)
cout<<" this is either the value of ‘i’ or ‘r’";
else
cout<<" Enter the correct choice";

8. If the originally x=2 ,y=1 and z=1, what are the value of x, y, z after executing the following code?
Switch(x) {
case 0 : x = 2;

Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 51 of 68 Prepared by: Eyasu G.Z. (MSc, MA) Department of Computer Science, Arada Giorgis Campus, A.A. Page 52 of 68

You might also like