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

Loops Solved Problems 1

loops solved problems 1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Loops Solved Problems 1

loops solved problems 1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COMP 111: Computer Applications C++

Assignment # 4
1- Which is the value of i after the for statement below?
n = 100;
for ( i = 0; i < n; i++ ) {
...
}
a. 99
b. 100
c. 101

2- Which are the first and last values of i output by this loop?
n = 20;
for ( i = 0; i < n; i++ ) {
cout << i << endl;
}

a. 0 and 19 b. 1 and 19 c. 0 and 20 d. 1 and 20

3- Which is the last value of i output by this loop?


n = 27;
i = 0;
for( i = 0; i <= n; i+= 2 ) {
cout << i << endl;
}

a. 25 b. 28 c. 27 d. 26

4- What are the first and last values of i output by this loop?
n = 10;
i = 0;
while ( ++i < n ) {
cout << i << endl;
}
a. 1 and 9 b. 0 and 9 c. 0 and 10 d. 1 and 10
5- What is the major difference between a while statement and a do-while statement?

In While loop the condition is tested first and then the statements
are executed if the condition turns out to be true if the condition is
false the loop is never executed .

In do while the statements are executed for the first time and then
the conditions are tested, if the condition turns out to be true then
the statements are executed again. if the condition is false the
statements are not executed again ,this means that the do while
loop is executed once even if the condition is false .

What are the first and last values of i output by this loop?
n = 10;
i = 0;
do {
cout << i << endl;
} while ( i++ < n );

a. 1 and 10 b. 0 and 10 c. 1 and 9 d. 0 and 9

6- Write the output of the following segments of code.


a.
int c = 1;
while (c < 20)
{
if (c%4 == 0 && c%3 != 0)
cout << c << "x"; c++;
}

4x8x16x

b.
int d = 28;
while (d%2==0 || d > 1)
{
d = d / 2;
cout << d << "\n";
}

14
7
3
1
7- Write a program that reads a set 20 of integers and then finds and prints the sum and
count of the even and odd integers.
#include <iostream>
using namespace std;
int main()
{
/*. Write a program that reads a set 20 of integers and then finds
and prints the sum and count of the even and odd integers */

int value , odd_count = 0 ,even_count = 0 ,even_sum = 0 ,odd_sum =0 ;

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


{
cout << "enter the value u want to test \n";
cin >> value ;
if ( value % 2 == 0 )
{
even_sum = even_sum + value ;
even_count++;
}
else
{
odd_sum = odd_sum + value ;
odd_count++;
}
}

#include <iostream>

using namespace std;

int main()
{
int i,n;
double answer=0;
cout<<"Enter 'n'\n";
cin>>n;

for(i=1;i<=n;i++)
{
answer = answer + (1.0/i);
}
cout<<"Answer is "<<answer<<"\n";

}
No output , there will be an infinite loop because no parentheses so only one statement in
the while loop (fact*=1)

The output is : -12

#include <iostream>
using namespace std;
int main() {

float num1=0,num2=0,den=0,total=0;

for(int i=7 ; i<=22 ; i+=3) {


num1+=i;
}

for(int i=6 ; i<=31 ; i+=5) {


num2+=i;
}
for(int i=13 ; i<=25 ; i+=2) {
den+=i;
}

total=(num1+num2)/den;
cout << total<<endl;
}
#include <iostream>
using namespace std;
int main() {
int n=20, firstTerm = 1, secondTerm = 0, nextTerm;
for (int i = 1; i <= n; ++i) {
nextTerm = firstTerm + secondTerm;
cout << nextTerm << " + ";
firstTerm = secondTerm;
secondTerm = nextTerm;
}
cout << "Sum of Fibonacci Series: "<< nextTerm<< endl;
}

#include <iostream>
using namespace std;
int main() {
int sum=0;
for(int i=1 ; i<=512 ; i*=2)
{
sum+=i;
}
cout << sum<<endl;
}

You might also like