You are on page 1of 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