0% found this document useful (0 votes)
9 views

Programming Assignment by Samuel Mesfin

Uploaded by

danielmesfin40
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Programming Assignment by Samuel Mesfin

Uploaded by

danielmesfin40
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Samuel Mesfin

ID.NUMBER: UGR/5939/15
Section: 2

COMPUTER PROGRAMMING ASSIGNMENT

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() {

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


for(int j = 10;j < 20; j++) {
cout<<j<<" ";
}
cout<<"\n";
for(int k = 20; k < 30; k++ ) {
cout<<k<<" ";
}
cout<<"\n";
for(int z = 30; z < 40; z++) {
cout<<z<<" ";
}
cout<<"\n";
for(int u = 40;u < 50; u++) {
cout<<u<<" ";
}
cout<<"\n";
}

return 0;
}

Question #4:
#include <iostream>
using namespace std;
int main() {
int numofterms, y, x, sum(0);
cout<<"input number of terms: ";
cin>>numofterms;

for(int y = 1;y <= numofterms; y++) {


sum = sum + y;
if(y != numofterms)
cout<<y<<"+";
else
cout<<y<<" is ";
}
cout<<sum;

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;
}

You might also like