0% found this document useful (0 votes)
80 views14 pages

C++ Looping and Control Structures Examples

The document contains solutions to programming exercises involving various loop structures in C++ such as for, while, do-while loops. Some key points: 1. Loop structures like for, while and do-while are used to repeatedly execute a block of code a specified number of times or until a condition is met. 2. Break and continue statements can be used to control the flow inside loops. Break exits the entire loop while continue skips the current iteration. 3. Random number generation, truth tables, function evaluation and numerical integration examples are provided using appropriate loop structures. 4. User input is taken for loop limits, seed values and integration bounds to make the programs more dynamic. 5.

Uploaded by

Otep Umam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views14 pages

C++ Looping and Control Structures Examples

The document contains solutions to programming exercises involving various loop structures in C++ such as for, while, do-while loops. Some key points: 1. Loop structures like for, while and do-while are used to repeatedly execute a block of code a specified number of times or until a condition is met. 2. Break and continue statements can be used to control the flow inside loops. Break exits the entire loop while continue skips the current iteration. 3. Random number generation, truth tables, function evaluation and numerical integration examples are provided using appropriate loop structures. 4. User input is taken for loop limits, seed values and integration bounds to make the programs more dynamic. 5.

Uploaded by

Otep Umam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

NAMA: CHOTIBUL UMAM WIRANDA

NIM: F1B020025

EXERCISES

1. How do you write a for loop expression?

Ans:

#include <iostream>

using namespace std;

int main()

int batas = 10; int nomor;

for (nomor =1; nomor <= batas; nomor ++)

cout << nomor << "\n";

2. Give a sample code for endless for loop.

Ans:

We may as well write the above for loop as

int i = 0;

for (; i<10; )

i++;

We may write an endless for loop as below.

for (; ; )

3. Make a program using endless for loop and use break; statement to get out of it.

Ans:

#include<iostream>

using namespace std;

int main()
{ int i =0,sum=0 ;

for(;;)

{ sum += i;

if (sum >25) break;

cout<<“i= ”<<i<<“ sum = ” <<sum<<endl;

i++;}

return 0;

4. Make a small program to illustrate an endless while loop and use exit () function to end the
program.

Ans:

#include <iostream>

using namespace std;

int main( )

{cout<<“i\tx\ty” <<endl;

int x =0,i=0 ,y;

while (true)

x = i*i ;

y = x*x;

if (y>300)

exit(0);

cout <<i<<“\t”<<x<<“\t”<< y<<endl;

i++;

return 0;

5. Write a program using while loop to make a multiplication table of 1.25 from 1×1.25 to 10×1.25.

Ans:
#include <iostream>

using namespace std;


int main()

double x = 1.25, y ;

int i=1;

cout<<"i \tx \ty" <<endl;

while ( i>=1 && i<=10 )

{y = i*x;

cout <<i<< "\t"<<x <<"\t"<< y<<endl;

i =i+1; }

return 0;

6. What is the difference between while loop and do … while loop?

Ans:

The use of do…while loop is similar to while loop except for the fact that the while
condition is evaluated at the end of program, therefore, at least one computation would
be carried out even if the while condition turns out false. The following figure illustrates
the execution of do … while loop.

7. Make a program using for loop to display square roots of numbers from 1 to 5.

Ans:

#include <iostream>

#include <cmath>

using namespace std;

int main()

int n = 1,N,i=0;

for ( i>=1 ; i<=5 ; i++)

{N = i*n;
cout <<"Number = " << N <<"\tSquare root = "<<sqrt(N)<<endl;}

return 0;

8. Write a program to convert inches into millimetres and millimetres into inches.

Ans:

#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
float m,cm,inch;
cout<<"masukkan besaran yang akan di konversi (meter): ";
cin>>m;
cm=m*100;
inch=m*(100/2.54);
cout<<m<<" meter = "<<cm<<" centimeter dan "<<inch<<" inci";
return 0;
}

9. The function 2y + 3xy –6zx + 4z is to be evaluated for x = 1 to 4, for y = 2 to 5 and z = 3 to 6. Write


a program with nested for loops to achieve this.

Ans:

#include<iostream>
using namespace std;
int main()
{
cout<<"soalnya adalah 2y+3xy-6zx+4z"<<endl;
cout<<"jika x = 1 sampai 4 , y = 2 sampai 5, dan z = 3 sampai 4 "<<endl;
for (int x = 1 ; x <=4 ; x++)
{
for (int y = 2 ; y <=5 ; y++)
{
for (int z = 2 ; z <=5 ; z++)
{
int a = 2*y+3*x*y-6*z*x+4*z;
cout<<"Nilai dari a adalah "<<a<<endl;
}
}
10. Write an endless for loop to read a number of integers. It should exit when 000 is entered. The
program should display total number of entries and average value of the entered data.

Ans:

#include <iostream>
using namespace std;
int main ()
{
int N, count, sum;
float rata;
sum = 0; count = 0;
cin >> N;
while (N != 000) {
count++;
sum+=N;
cin >> N;
}
if (count > 0) {
cout << "Banyak bilangan = " << count << endl;
cout << "Jumlah total = " << sum << endl;
rata = (float)sum/(float)count;
cout << "Rata-rata = " << rata << endl;
} else { // count == 0
cout << "Tidak ada data yang diolah" << endl;
}
return 0;
}

11. Make a C++ program to generate random numbers with seed number provided by user.

Ans:

#include <cstdlib>

#include <ctime>

#include <iostream>

using namespace std;

int main()

srand((unsigned)time(0));

int i;
i = (rand()%6)+1;

cout << i << "\n";

12. Make a program with for loop to generate 6 random numbers less than 1000 by using function
time() for generating the seed number.

Answer: Type and run the following program to get a set of random numbers less than 1000.

PROGRAM 6.24 – Illustrates generation of three digit random numbers.

#include<iostream>

#include<cstdlib>

#include<ctime>

using namespace std;

int main()

long Randnumber ;

cout<< “Random Numbers are”<<endl;

long unsigned seed= time( NULL ) ;

srand(seed);

for (int n=1;n<=6;n++)

{Randnumber = rand()%1000;

cout << Randnumber<<“\n”;

return 0;

The following output is obtained by running the program. Random Numbers are

411

809

767

51

671

123
Ans:

#include<iostream>

#include<cstdlib>

#include<ctime>

using namespace std;

int main()

long Randnumber ;

cout<< "Random Numbers are"<<endl;

long unsigned seed= time( NULL ) ;

srand(seed);

for (int n=1;n<=6;n++)

{Randnumber = rand()%1000;

cout << Randnumber<<"\n";

return 0;

13. Find mistakes in the following program. Correct it and run it.

#include <iostream>

using namespace std;

void main()

int x = 0;

do;

cout<< “\tcube of “<< x <<“ = “<<x*x*x <<endl;

x +=1;

while(x<=4)

return 0;

}
Ans:

#include <iostream>

using namespace std;

int main()

int x = 0;

do

cout<< "\tcube of "<< x <<" = "<<x*x*x <<endl;

x +=1;

while(x<=4);

return 0;

14. Write an iteration program with endless while loop. Make use of exit() function to get out of it.

Ans:

#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 5){
cout << "Hello World" << endl;
}

return 0;
}

15. Make a program with do—while loop to calculate the square roots of numbers from 1 to 10.

Ans:

#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
double i=1, x;
cout.precision (3);
do
{
x=sqrt(i);
cout<<""<<i<<"\t| "<<x<<endl;
i++;
}
while (i<=10);
}

16. Write a program with endless while statement and make use of break statement to terminate
the loop.

Ans:

#include<iostream>
using namespace std;
int main()
{
int i=1;
while (i>=1)
{
cout<<i<<endl;++i;
if (i>50)
break;
}
}

17. Make a program to illustrate the use of goto statement.

Ans:

#include <iostream>
using namespace std;
int main()
{
int i = 1;
label:
cout<<"No. "<<i<<endl;i++;
if (i<=20)
goto label;
}

18. Make program to illustrate the use of continue statement.

Ans:

#include <stdio.h>
int main()
{
int i,n=2;
for (int i = 1; i <= 20; i++)
{
if (i == n)
{
n= n+2;
continue;
}
printf(" %d ", i);
}
}

19. Make a program to evaluate the following function for x = 4 to 8, y = 2 to 3 and z = 5. 4x3 z + 5 y z
+ 10 x y z

Ans:

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
cout<<"soalnya adalah 4x^3 z + 5 y z + 10 x y z"<<endl;
cout<<"jika x = 4 sampai 8 , y = 2 sampai 3, dan z = 5 sampai 4 "<<endl;
for (int x = 4 ; x <= 8 ; x++)
{
for (int y = 2 ; y <= 3 ; y++)
{
for (int z = 5 ; z = 5 ; z++)
{
int a = 4*pow(x,3)*z + 5*y*z + 10*x*y*z;
cout<<"Nilai dari a adalah "<<a<<endl;
break;
}
}
}

20. Construct a program with for loop to evaluate a truth table for the Boolean expression. !(A||B)

Answer:

#include <iostream>

using namespace std;

int main()

{ int A , B ;

for ( A =0; A<2 ; A++)

{for(B =0;B<2; B++)

cout<< “A = ”<<A << “ B = ”<<B << “ !( A||B)= ” << !(A||B)<<endl;}

return 0;
}

Ans:

A = 0 B = 0 !( A||B)= 1

A = 0 B = 1 !( A||B)= 0

A = 1 B = 0 !( A||B)= 0

A = 1 B = 1 !( A||B)= 0

A B !(A||B)
0 0 1
0 1 0
1 0 0
1 1 0

21. Make a program for integration of x in the limits specified by user.

Answer: For integration of a function f(x) between the limits a and b, we follow the well known
trapezoidal rule as illustrated below.

PROGRAM 6.25 – Illustrates calculation of integral of x between the limits specified by user.

#include <iostream>

#include <cmath>

using namespace std;

int main () {

double x , Lower_Limit , Upper_Limit , Integral , h, F;

int n = 100;

cout<<“Enter the limits of integral :\n” ;

cout<< “Upper_Limit = “; cin >> Upper_Limit ;

cout<< “Lower_Limit = “ ; cin>> Lower_Limit;

h = (Upper_Limit – Lower_Limit )/n;

Integral = 0.5 * (Lower_Limit + Upper_Limit)*h;

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

{F = (Lower_Limit + i*h)*h ;

Integral += F; };

cout << “Integral of x = “ << Integral <<endl;

return 0 ;
}

The expected output is given below. The user has put in the limits as 8 and 2 so the result is 30.

Enter the limits of integral :

Upper_Limit = 8.0

Lower_Limit = 2.0

Integral of x = 30

Ans:

#include <iostream>

#include <cmath>

using namespace std;

int main ()

double x , Lower_Limit , Upper_Limit , Integral , h, F;

intn=100;

cout<<“Enter the limits of integral :\n” ;

cout<< “Upper_Limit = “; cin >> Upper_Limit ;

cout<< “Lower_Limit = “ ; cin>> Lower_Limit;

h = (Upper_Limit – Lower_Limit )/n;

Integral = 0.5 * (Lower_Limit + Upper_Limit)*h;

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

{F = (Lower_Limit + i*h)*h ;

Integral += F;};

cout << “Integral of x = “ << Integral <<endl;

return 0 ;

}
22. Make a program to integrate xn in the range for x = a to x = b. Also develop a program to
integrate any polynomial in the limits a to b.

Ans:

#include <iostream>

using namespace std;

int main()

int x;

for(x=0;x=10;x=x+1)

puts(“what are you lookin at?”);

return(0);

23. Make a test program to illustrate endless for loop.

Ans:

#include <iostream>

#include <cmath>

using namespace std;

int main()

for(int i=1; i>=1; i++)

cout<<"Nilai i adalah: "<<i<<endl;

24. Make a program with for loop having only middle expression.
Answer: The program is given below.

PROGRAM 6.26 – Illustrates for loop with only middle expression.

#include<iostream>

using namespace std;

main()

{int n = 10,sum=0, i=0 ;

for ( ;i<=10; )

{sum +=i;

i++;}

cout<<“sum = ”<<sum<<endl;

return 0;

The program runs and output is as below. sum = 55s

Ans:

#include<iostream>

using namespace std;

main()

{int n = 10,sum=0, i=0 ; for ( ;i<=10; )

{sum +=i;

i++;}

cout<<“sum = ”<<sum<<endl; return 0;

You might also like