You are on page 1of 15

UNITY UNIVERSITY

DEPARTMENT OF COMPUTER SCIENCE


WORKSHEET ON ALL CHAPTERS OF FUNDAMENTALS OF PROGRAMMING I
USING C++

UNIT ONE: - FUNDAMENTALS OF PROGRAMMING USING C++


1. Explain the difference between program and programming languages?
2. List and explain the levels of Programming Languages by giving example?
3. List and explain the five stages of program development life cycle?
4. Define algorithm and mention and explain the algorithm design and analysis stages?
5. Draw a flow chart and write the pseudo code for the problem to determine whether the
number is Positive or Negative.
6. Use two numbers (a and b). Draw a flow chart and Write a pseudo code program to
determine whether the number is smaller or larger than each other
7. The perimeter P and Area of a triangle whose sides have lengths a, b and c are given by:
P=a+b+c √
Area = s( s−a)( s−b)(s−c )
 Where s=(a+b+c)/2. Draw a flow chart or write a pseudo code program with input a, b, c
and output P and Area.
8. Write the difference between structural and object oriented programming paradigms by
giving example and mention some of the advantages of object oriented programming
paradigm over structural programming paradigm?
9. List down and explain the six phases that C++ program passes through?
10. Write the general form or structure of C++ program?
11. State whether each name in this exercise is valid identifier or invalid identifier. If the
name of identifier is invalid, explain why it is?

sum_of_credits _type_of_car
while SECTION_6
bingo-square 3_4_87
initial tree final_#
sum 2nd_total
secondsum long
TOTAL-PAY column 1
total_pay abcde3456
if switch
num1 number_two?
12. Identify either the following C++ statement is valid or invalid

long x; sum=x+y, cout<<“average”;

cin>>x>>y; cout<<“Sum=“<<sum; int x; y; z; sum,

main(); cout<<“x=“<<x<<endl, float x,y;

1
13. Determine the output of the following C++ Program

//Program to read a String


Output
#include<iostream.h>

main()

char name[20];

cout<<“Enter a name…”;

cin>>name;

cout<<“The name you entered was “<<name;

UNIT TWO: -DATA TYPES, VARIABLES, EXPRESSIONS AND OPERATORS


1. Variables are declared before they are using in a program and the data type is included
during the declaration. Describe when we use the following data types in the variable
declaration in C++? (4 point).

N Data Types When we use this data types?


O
1 int

2 float

3 double

4 char

2. Determine the output of the following C++ program


#include<iostream.h>
// program 1 to illustrate use or char data type
main()
{ Output
char ltr_1='C',
ltr_2=' ',
ltr_3='i',
ltr_4='s',
ltr_5=' ',
ltr_6='g',
ltr_7='r',
ltr_8='e',
ltr_9='a',
ltr_10='t',
ltr_11='!';
cout<<"\n"<<ltr_1<<ltr_2<<ltr_3<<ltr_4<<ltr_5<<ltr_6<<ltr_7<<ltr_8<<ltr_9<<ltr_10<<ltr_11;

2
}
3. Write C++ Program to display the following output

I Love ETHIOPIA!

4. Write C++ program to display the following output using char data type and character strings:

A) C D E F

B) Her Unearth Beauty Blinded me that

I can’t see and appreciate the beauty of others

Leave a line between the output of the above two statements.

C) P R O G R A M

5. The following C++ program has its own errors and violates some rules of C++
programming. Determine the errors of the program and rewrite the program to correct the
errors.

#include <iostream.h> Syntax Errors

main ( ) {

float a =248, b=132.79, answer;

answer=a+b

cout<<"\n"<<"Answer =<<a+b="<<; Rewrite the correct Program

answer=a-b

cin<<"\n"<<"Answer="<<"a-b="<<answer;

answer=a*b;

cout<<"\n"<<"Answer="<<"a*b="<<answer;

6. What is the output of the following two C++ program? Write the output these two
program inside the box below and indicate that why the output of these two program is
different each other?.

3
Output of program one
#include<iostream.h>

//program one to illustrate variable Declatation


int main() {
int myAge;
cout<<”myAge is”<<myAge<<endl;
return 0; Output of program Two
}
==================================
//Program Two toillustrate initialization of a variable
#include<iostream.h>
int main()
{
int myAge=22; Difference between the two program
cout<<"myAge is"<<" "<<myAge<<endl;
return 0;
}

7. Determine the following variables named are valid or invalid and reason out why the
variable named are invalid or valid? (4 point).

No Variable named Is it Valid or Why it is valid or invalid?


Invalid?
1 netsalay

2 % deduction

3 10number

4 switch

8. Identify which of the following variable declarations are valid or invalid?

charmiddle_initial, union_code;

intnum_dependents;

floatreg_hours, ot_hours, rate_of_pay;

doublegross_pay, net_pay;

Integer num_dependents;

….. reg_hours, ot_hours;


Double gross_ pay, net_pay;
char union;

4
char ‘a’,
9. Observe the following program and determine its output.

#include<iostream.h>
main()
{ Output
int x;
x=65;
cout<< x<<endl;
x= 90;
cout<<endl<<x;
}

10. Determine the output of the following C++ program

#include <iostream.h> Output


#include<conio.h>
void main() {
intnum;
clrscr ();
cout<<"Enter an integer number:";
cin>>num;
cout<<"num<10 is "<<(num<10)<<endl;
cout<<"num>10 is"<<(num>10)<<endl;
cout<<"num==10 is"<<(num==10)<<endl;
cout<<"num==20 is"<<(num==20)<<endl;
getch ();
}
11. Determine the output of the following C++ program

#include<iostream.h>
int main()
{ // shows the difference between m++ and ++m:
intm,n;
m = 44;
n= ++m; // the pre-increment operator is applied to m
cout<< "m="<<m<<",n="<<n<<endl;
m = 44;
n= m++; // the post-increment operator is applied to m
cout<< "m="<<m<<",n="<<n<<endl;
}
12. Write C++ program to display the following output. Use assignment operator to write
your program?

5
The sum of C=a+b is 120
13. Write algorithm expression or pseudo code program and C++ program for the following
mathematical expression.
x+ y x− y
A) – B) x=√ a2 +b 2
z z−x

p−q
C) + m*n D) F= (9/5 * C) +32
r−s

UNIT THREE: - FLOW OF CONTROL OF PROGRAM


1. Define control statement and Loop Control variable and describe its purpose?
2. List down the two most important alternative statement or structure that changes the
normal flow of control in C++ programming and identify the types of each of these
statements?
3. Write a program that displays “Hello World” on the screen based on the user input such
as the user press y from the keyboard
4. Write C++ program using if-statement to display the following output. Suppose the if
condition is, if(code ==1)

The water was too warm


The water were all fished out
It was too late in the season
***End of Fishing Excuses ***
5. Write the syntax for the control structure of, if-----else, if—else-- if ladder and switch
statements (3 point).

No Types of Control Syntax for the control structure


Structure
1 if----else

if----else if ladder
2

3 switch

6
6. Write C++ program to determine whether the number is positive or not?
7. Write C++ program to check either a given number is odd or even using if-else statement
8. Write C++ program to display the following output using if-else statement.
i=3, j=10
i is not greater than 4
k set equal to i
9. Write C++ program that performs the following input and output operation .
Input Output
A Excellent
B Very Good
C Satisfactory
D Not Satisfactory
F Failure
otherwise Invalid Input
10. Write C++ program to display a letter grade when users enters different range of marks. Use
the following input and output information for your work.

Input Output
(marks>=90) and (marks<=100) A
(marks>=70) and (marks<90) B
(marks>=55)and (marks<70) C
(marks>=35)and (marks<55) D
(marks>=1) and (marks<35) F
otherwise I
11. Refer Question no. 10. Write the above program again using switch statement and test the
output of the program. Rewrite this program again assuming that the program accepts input
of upper and lower case letters and display the same result when accepts both upper and
lower case letters as an input?
12. Write C++ program using if-else-if or switch statement to calculate Income Tax, pension and
net payment of an employees. The program is expected to accept salary of an employee as
input and displays income tax, pension, total deduction and net payment of a salary as output.
Use 6% of basic salary to compute pension and use the following information for tax
competition.
Salary Tax
<=150 0
150-650 10%
650-1400 15%
1400-2350 20%
2350-3550 25%
3550-5000 30%
>5000 35%

7
14. Write the syntax for the control structure of, FOR-----LOOP, While----Loop and do---
while Loop statements

4 for----Loop

5 while----Loop

6 do----while Loop

15. Determine the output of the following C++ program


#include<iostream.h>
int main ()
{ Output
for(int n=10;n>0;n--)
{
cout<<n<<“,”;
}
cout<<“FIRE!”;
return 0;
}

16. Write C++ program using for_loop to determine sum and average of the first n positive
numbers, assuming that the program accepts input from the user?
17. Write C++ program using for loop to print “Welcome to C++ Programming Course”
statement ten(10) times
18. Determine the output of the following simple C++ program
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
int main() { Output
clrscr ();
for (int x=1; x <= 6; x++){
for (int y=1; y <= 6; y++)
if (y > x) {
break;
8
}
else {
cout<<setw(4) << x*y;
}
cout<<endl;
}
return 0;
}
19. What is the output of the following simple C++ program?
#include<iostream.h>
#include<conio.h>
void main() {
clrscr(); Output
for(int i=0;i<=4;i++) {
for(int j=0;j<=4;j++) {
if (i==j)
cout<<'*';
else
cout<<'#';
}
cout<<endl;
}
getch();
}
20. Write C++ program to display the following output using for loop?

21. A certain company plan to give a 40% of bonus to each of its employees at the end of
every year for those who are working more than two years in the company. If an
employee has been working ten or more years of the company, she/he to get an additional
birr 100 in addition to bonus. Analyze and design the algorithm of this problem and write
C++ program to calculate (compute) a bonus of an employee
22. Write a program to display the following shapes
a) **** b) * c) ****
**** ** ***
**** *** **
**** *

d) * e) *
*** **
***** ***
******* ****
23. Write C++ program to count the number down starting from 8 using while loop.
Assuming that users enter the number 8 while running the program?
24. Write a program to calculate sum of the first 10 natural numbers using while loop

9
25. Write C++ Program to display the following output?

26. Determine the output of the following C++ program?


#include<iostream.h>
main ()
{
int n; Output
cout<<“Enter an integer (negative number to quite):”;
cin>>n; //get first number
while (n>=0)
{
cout<<n <<“Squared is”<<(n*n);
cout<<“\n Enter an integer (negative to quit):”;
cin>>n; //Get next number
}
}
27. What is the output of the following C++ program implemented using do—while loop?
#include<iostream.h>
int main() Output
{
intn,i=0;
cout<< "Enter a positive integer: ";
cin>> n;
long sum=0;
do {
sum += i++;
}
while (i <= n);
cout<< "The sum of the first " << n << " integers is " << sum;
}

28. Write C++ program to display (prints) the following output using do----while loop?

10
UNIT FOUR: - FUNCTIONS
1. Define function and describe why we use function to write a real world program
2. Identify the two categories of functions in C++ and explain with examples
3. Write C++ program to display or print the following output

4. Write the syntax to define user defined function. And explain what each part of the
functions clearly
5. Determine the output of the following C++ program
//Program to define a function by the name addition.
#include<iostream.h>
#include<conio.h>
int addition (int a, int b){
int r;
r= a+b;
return (r);
}
int main (){
clrscr();
int z;
z= addition(5,3);
cout<<"The result is:"<<z;
return 0;
}
6. Define a function by the name skipthree(), to display “Title Line 1” and “Title Line 2”
respectively by skipping three lines between these two outputs when the skipthree()
function is called?
7. Write the syntax used for function prototype declaration in C++ programming language
and explain the difference with function definition and describe the reason why we use
function prototype while you are writing a program?
8. Define a function that calculates the Area of a Circle and Volume of a Box by
considering the necessary variables used to perform area of a circle and volume of a box.
9. Define a function that prints your first and last name in a separate line but a function
prints the name after one blank line.
10. Determine the output of the following C++ program?
// FunctionFactorial

// Program to calculate factorial of a number with function call

#include <iostream.h>

// Function declaration (prototype)

11
int Factorial(int M);

int main() {

int number=0, result;

// User input must be an integer number between 1 and 10

While ((number<1) || (number>10)) {

cout<<"Enter Integer number = ";

cin>> number;

// Function call and assignment of return value to result

result = Factorial(number);

//Output result

cout<< "Factorial = " << result <<endl;

return 0;

// Function definition (header and body)

// An integer, M, is passed from caller function.

int Factorial(int M) {

int factorial=1;

// Calculate the factorial with a FOR loop

for(int i=1; i<=M; i++) {

factorial = factorial*i;

return factorial; // This value is returned to caller

11. Identify the syntax errors you observed for the following C++ program and write your
answer inside the box provided below

12
#include <iostream.h>
float Convert();
int main()
{
cout<< "Please enter the temperature in Fahrenheit: ";
cin>>TempFer;
TempCel = Convert(TempFer);
cout<< "\n The temperature in Celsius: "<<TempCel<<endl;
return 0;
}
float (float TempFer) Syntax Errors
{
TempCel = ((TempFer - 32) * 5) / 9;
returnTempCel;
}

12. The following C++ program has syntax errors and violates some rules of C++
programming. Determine the errors of the program and rewrite the program to correct the
errors
Syntax Errors
#include <iostream.h>
#include <conio.h>
void myFunction()
int x = 5, y = 7;
int main() {
cout << "x from main: " << x << "\n";
cout << "y from main: " << y << "\n\n";
cout << "Back from myFunction!\n\n";
cout << "x from main: " << x << "\n";
cout << "y from main: "
return 0;
}
void myFunction() //Function Definition
{
int y = 10;

13
cout << "x from myFunction: " << x << "\n";
cout << "y from myFunction: " << y << "\n\n";
13. What is the output of the following two C++ program? Write the output of the program
inside the box below and indicate that what is the implementation of the program?
#include <iostream.h>
#include <conio.h>
voidmyFunc();
int main()
{
Output
int x = 5;
cout<< "\n In main x is: " << x;
myFunc();
cout<< "\n Back in main, x is: " << x;
return 0;
}
voidmyFunc() {
int x = 8;
cout<< "\n In myFunc, local x: " << x <<endl;
{
cout<< "\n In block in myFunc, x is: " << x;
int x = 9;
cout<< "\n Very local x: " << x;
}
cout<< "\n Out of block, in myFunc, x: " << x <<endl;
}
14. Determine the output of the following C++ program

#include<iostream.h>
#include<conio.h> Output
void duplicate(int &a, int &b, int &c);
int main() {
clrscr();
int x=2, y=5, z=10;
duplicate(x, y, z);
cout<<"x= "<<x<<", y="<<y<<" , z= "<<z;

return 0;
}
void duplicate(int &a, int &b, int &c) {
a*=3;
b*=3;
c*=3;
}

14
15. Write the difference between local and global variables and show the difference using
examples
16. Explain the difference between argument passed by value and argument passed by
references and show the difference using examples
17. Write C++ program to calculate volume of the box to implement argument passed by
value when a function is called by defining the necessary function
18. Write C++ program to perform multiplication and division of two numbers by defining
your own function to implement local and global variables and also use variables visible
to only within the block.

15

You might also like