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

C++ Programming Exercise

Uploaded by

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

C++ Programming Exercise

Uploaded by

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

1.

Design a program that


reads three numbers and
prints the smallest number.
#include <iostream>
using namespace std;
int main() {
double num1, num2, num3;
cout << "Enter the first
number: ";
cin >> num1;
cout << "Enter the second
number: "; cin >> num2;
cout << "Enter the third
number: ";
cin >> num3; double
smallest = min(num1,
num2, num3);
cout << "The smallest
number is: " << smallest <<
endl;
return 0; }
2. Design a program that
keeps on reading numbers
until the user enters a zero
value.
#include <iostream>
using namespace std;
int main() {
double sum = 0;
while (true) { cout << "Enter
aor 0 to stop): ";
double num;
cin >> num;
if (num == 0) { break; } sum
+= num; } cout << "The sum
of the numbers is: " << sum
<< endl;
return 0; }

3. Design a program that


keeps on reading numbers
until the user enters -1 and
then determines and
outputs number.
#include <iostream>
using namespace std;
int main() {
double largest =
numeric_limits<double>::mi
n(); while (true) {
cout << "Enter a number (or
-1 to stop): ";
double num;
cin >> num; if (num == -1) {
break; } if (num > largest)
{ largest = num; } } cout <<
"The largest number is: " <<
largest << endl;
return 0; }
4. Design a program that
reads five numbers and
computes and outputs their
arithmetic average. Then
generalize your algorithm
so that it can handle an
arbitrary number of
numbers.
#include <iostream>
using namespace std;
int main() {
double num1, num2,, num4,
num5; cout << "Enter the
first number: ";
cin >> num1; cout <<
"Enter the second number:
";
cin >> num2;
cout << "Enter the third
number: "; cin >> num3;
cout << "Enter the fourth
number: "; cin >> num4;
cout << "Enter the fifth
number: "; cin >> num5;
double average = (num1 +
num2 + num3 + num4 +
num5) / 5; cout <<
"The arithmetic average is: "
<< average << endl; int n;
cout << "Enter the number
of numbers: ";
cin >> n; double sum = 0;
for (int i = 0; i < n; ++i) {
cout << "Enter a number: ";
cin >> num; sum += num; }
average = sum / n;
cout << "The arithmetic
average is: " << average <<
endl;
return 0; }

5. Design a program that


accepts a positive integer n
as input and computes and
outputs the factorial of the
number i.e. the product of
all integers from 1 to n.
#include <iostream>
using namespace std;
int main() {
int n; cout << "Enter a
positive integer: ";
cin >> n; int factorial = 1; for
(int i = 1; i <= n; ++i)
{ factorial *= i; }
cout << "The factorial of "
<< n << " is: " << factorial
<< endl;
return 0; }

6. Design a program that


reads a positive integer and
then computes and outputs
all the positive factors of
that integer. For example,
the positive factors of 33
are 1, 3, 11,33, which are
integers that divide 33
evenly.
#include <iostream>
using namespace std;
int main() {
int num; cout << "Enter a
positive integer: ";
cin >> num; for (int i = 1; i
<= num; ++i) { if (num % i
== 0) {
cout << i << endl; } }
return 0; }

You might also like