0% found this document useful (0 votes)
239 views6 pages

University of Gondar Institute of Technology Department of Electrical and Computer Engineering

This document contains an introduction to computer programming worksheet for first year engineering students. It includes 8 questions asking students to write short C++ programs to perform tasks like calculating the square of a number, adding digits, finding the area and circumference of a circle using functions, determining if a number is even or odd, and calculating BMI based on height and weight. The programs cover basic concepts like functions, loops, conditional statements, and output. An example of the output for one of the programs is also provided.

Uploaded by

Mulualem Tilahun
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)
239 views6 pages

University of Gondar Institute of Technology Department of Electrical and Computer Engineering

This document contains an introduction to computer programming worksheet for first year engineering students. It includes 8 questions asking students to write short C++ programs to perform tasks like calculating the square of a number, adding digits, finding the area and circumference of a circle using functions, determining if a number is even or odd, and calculating BMI based on height and weight. The programs cover basic concepts like functions, loops, conditional statements, and output. An example of the output for one of the programs is also provided.

Uploaded by

Mulualem Tilahun
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/ 6

University of Gondar

Institute of Technology
Department of Electrical and Computer Engineering

1. Write a C++ program which have an int function square () that returns the square of its
single int formal parameter.

#include<iostream>
using namespace std;
int square(int n)
{
int sq=n*n;
return sq;
}
int main()
{
int n;
cout<<"Please enter the number : ";
cin>>n;
cout<<"The square of "<<n<<" is : "<<square(n);
return 0;
}

2. Write a C++ program which add digits of number.

#include<iostream>
using namespace std;
int main()
{
int n;
int sum=0;
cout<<"please enter the positive integer : ";
cin>>n;
do
{
sum=sum+n%10;
n=n/10;
}
while (n!=0);
cout<<"The sum of the digits of the integer you entered is : "<<sum;
return 0;
}

Introduction to computer programing (ECEg 1052) worksheet


For first year Engineering students (section 5 and 6)
University of Gondar
Institute of Technology
Department of Electrical and Computer Engineering

3. Write a C++ program which helps to find Area and circumference of the circle.
#include<iostream>
#define pi 3.14
using namespace std;
int main()
{
float r,A,C;
cout<<"Please enter the radius of the circle : ";
cin>>r;
if(r>0){
A=pi*r*r;
C=2*pi*r;
cout<<"The area of the circle is : "<<A;
cout<<"\nThe circumfrence of the circle is : "<<C;
}
else
{
cout<<"wrong radius input!!";
}

return 0;
}

Exercise, please do it again by using function

4. write a C++ program to find the Sum and Average of three numbers.
#include<iostream>
using namespace std;
int main()
{
float n1,n2,n3,sum,ave;
cout<<"please enter the three numbers :\n";
cin>>n1>>n2>>n3;
sum=n1+n2+n3;
ave=sum/3;
cout<<"The sum of the three numbers : "<<sum;
cout<<"\nThe average of the three numbers : "<<ave;
return 0;
}

Introduction to computer programing (ECEg 1052) worksheet


For first year Engineering students (section 5 and 6)
University of Gondar
Institute of Technology
Department of Electrical and Computer Engineering

5. Write a C++ program which have a void function called even () that accepts a number
and determine whether the number is even or not.
#include<iostream>
using namespace std;
void even(int n)
{
if(n%2==0)
{
cout<<n<<" is even !!";
}
else
{
cout<<n<<" is not even !!";
}
}
int main()
{
int n;
cout<<"Please enter the number";
cin>>n;
even(n);
return 0;
}

6. Write a C++ program which displays the following pattern.


*
a
**
***
****
*****
*****
*****
*****
b
*****
*****

Introduction to computer programing (ECEg 1052) worksheet


For first year Engineering students (section 5 and 6)
University of Gondar
Institute of Technology
Department of Electrical and Computer Engineering

a.
#include<iostream>
using namespace std;
int main()
{
for (int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
cout<<"* ";
}
cout<<endl;
}
return 0;
}

b.
#include<iostream>
using namespace std;
int main()
{
for (int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
cout<<"* ";
}
cout<<endl;
}
return 0;
}

7. Write a program that inputs a person’s height (meters) and weight (in kilograms) and outputs
one of the messages: underweight, normal, or overweight, using the criteria:
✓ Underweight if BMI<18.5
✓ Normal weight if 18.5<=BMI<=25
✓ Overweight if BMI>25

Introduction to computer programing (ECEg 1052) worksheet


For first year Engineering students (section 5 and 6)
University of Gondar
Institute of Technology
Department of Electrical and Computer Engineering

#include<iostream>
using namespace std;
int main()
{
float height,weight,BMI;
cout<<"please enter the height of the person : ";
cin>>height;
cout<<"please enter the weight of the person : ";
cin>>weight;
if(height<0||weight<0)
{
cout<<"wrong height or weight input!!";

}
else
{
BMI=weight/(height*height);
cout<<"your BMI is : "<<BMI<<"\n";
if(BMI<18.5)
{
cout<<"you are underweight!!";
}
else if(BMI>25)
{
cout<<"you are overweeight!!";
}
else
{
cout<<"you are normal!!";
}
}
return 0;
}

Introduction to computer programing (ECEg 1052) worksheet


For first year Engineering students (section 5 and 6)
University of Gondar
Institute of Technology
Department of Electrical and Computer Engineering

8. What will be the out put of the following code?


#include<iostream>
using namespace std;
int main ()
{
int x=5;
int y=10;
cout<<++x+y++<<endl;
x++;
cout<<"The new value of X: "<<++x <<endl;
cout<<"The new value of y: "<<y++<<endl;
cout<<++x-y- -<<endl;
x*=2;
y*=3;
cout<<++y+x- -<<endl;
cout<<"The updated value of X: "<<x<<endl;
cout<<"The updated value of y: "<<y<<endl;
return 0;
}

Output
16
The new value of X: 8
The new value of y: 11
-3
52
The updated value of X: 17
The updated value of y: 34

Introduction to computer programing (ECEg 1052) worksheet


For first year Engineering students (section 5 and 6)

You might also like