Programming Assignment by Samuel Mesfin
Programming Assignment by Samuel Mesfin
ID.NUMBER: UGR/5939/15
Section: 2
Question #1:
#include <iostream>
using namespace std;
int main() {
int n(0) ;
double avg, k, sum(0);
do {
cout<<"enter a number: ";
cin>>k;
n++;
sum = sum + k;
}
while(n < 10);
avg = (double)sum/n;
cout<<"the average of the numbers is "<<avg;
return 0;
}
QUESTION #2:
A. #include <iostream>
using namespace std;
int main() {
int sum(0);
for(int i = 1;i <= 100;i++) {
sum = sum +i;
}
cout<<"the sum of numbers from one to hundred is "<<sum;
return 0;
}
B . #include <iostream>
using namespace std;
int main() {
int sum(0);
for(int i = 5;i <= 50;i += 5) {
sum = sum +i;
}
cout<<"the sum of the numbers is "<<sum;
return 0;
}
C.#include <iostream>
using namespace std;
int main() {
int product = 1;
for (int i = 1; i <= 20; i++) {
product *= i;
}
cout << "The product of the numbers from 1 to 20 is " << product << endl;
return 0;
}
Question #3
#include <iostream>
using namespace std;
int main() {
return 0;
}
Question #4:
#include <iostream>
using namespace std;
int main() {
int numofterms, y, x, sum(0);
cout<<"input number of terms: ";
cin>>numofterms;
return 0;
}
Question #5
A .Using while loop
#include <iostream>
using namespace std;
int main() {
int i = 1, product = 1;
while (i <= 20) {
if (i % 2 == 0) {
product *= i;
}
i++;
}
cout << "The product of even numbers from 1 to 20 is " << product << endl;
return 0;
}
B.Using for loop
#include <iostream>
using namespace std;
int main() {
int product = 1;
for (int i = 2; i <= 20; i += 2) {
product *= i;
}
cout << "The product of even numbers from 1 to 20 is " << product << endl;
return 0;
}
Question #6:
#include <iostream>
using namespace std;
int main() {
int x, factorial(1);
cout<<"enter a number to find factorial for: ";
cin>>x;
for(int i = 1; i <= x ; i++) {
factorial *= i;
}
cout<<"factorial of "<<x<<" is "<<factorial;
return 0;
}