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

Loops Solved Problems 2

loops solved problems 2
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)
27 views

Loops Solved Problems 2

loops solved problems 2
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/ 5

COMP 111: Computer Applications C++

Assignment # 5
1. How many times will each of the following loops execute? What is the output in
each case?
2. Write a program (starting from #include) that repeatedly collects positive integers
from the user, stopping when the user enters a negative number or zero. After that,
output the largest positive number entered. You may not use any library other than
<iostream>. A sample run should appear on the screen like the text below.

#include <iostream>
using namespace std;
int main ( )
{
int maxNum = 0;
int x = 1;
while ( x > 0 )
{
cout << "Enter a number: ";
cin >> x;
if ( x > maxNum && x > 0 )
maxNum = x;
}
cout << "The largest positive number you entered was " << maxNum <<
".\n";
}
3. Write a full program (starting from #include) that asks the user how many integer-
valued numbers she wants to input, reads the integers one at a time with an
appropriate prompt, and then outputs the average. An example output is shown
below, where the user chose to enter 4 numbers.

#include <iostream>
using namespace std;
int main()
{
cout << "How many numbers? ";
int num;
cin >> num;
int sum = 0;
int value;
int x = 1;
while (x <= num)
{
cout << "Enter number #" << x << ": ";
cin >> value;
sum += value;
x++;
}
cout << "The average is " << (double) sum / num;

}
4. Write C++ program to produce an n times multiplication table (n is less than or equal
to 10). For example, if n is equal to four the table should appear as follows:

Use nested for loops and pay attention to formatting your output so that it is displayed neatly.
#include <iostream>
using namespace std;
int main (){

{
int n ;
cout<< "enter the value of n \n";
cin >> n;
cout <<"\t";
for(int x = 1; x <= n; x++)
{
cout << x << "\t";
}
cout << endl;
for(int c = 1; c <= n; c++)
{
cout << c << " |\t";
for(int i = 1; i <= n; i++)
{
cout << i * c << '\t';
}
cout << endl;
}

}
5. Write C++ programs to print using loops:

#include <iostream>
using namespace std;
int main()
{
int rows,i,j,space;
cout << "Enter the number of rows: ";
cin >> rows;
for(i=rows; i>=1; i--)
{
for(space=0; space<rows-i; space++)
cout << " ";
for(j=1; j<=2*i-1; j++)
{
cout << "* ";
}

cout << endl;


}

#include <iostream>
using namespace std;
int main()
{ *
int i,j,rows; **
cout << "Enter the number of rows: "; ***
cin >> rows; ****
for(i=1;i<=rows;++i)
{
for(j=1;j<=i;++j)
{
cout << "* ";
}
cout << endl;
}
}

int main()
{
int i,j,rows;
cout << "Enter the number of rows: "; ****
cin >> rows; ***
**
for(i=rows;i>=1;--i)
*
{
for(j=1;j<=i;++j)
{
cout << "* ";
}
cout << endl;
}
6.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double p,q, sum = 0, Euclidean;
int n ;
cout << " Enter the number of n space \n";
cin >> n;
for ( int i = 1 ; i <= n ; i++)
{
cout << " enter the value of p " << i << " :";
cin >> p;
cout << " enter the value of q " << i << " :";
cin >> q;
sum = sum + pow ( p - q, 2 ) ;
}
Euclidean = pow ( sum, 0.5 ) ;
cout << "The Euclidean distance between these two points " <<
Euclidean << endl;
}

You might also like