Slide 2
Slide 2
Practice problems
Counter Loop: Single Instruction
We have seen FOR loop used for repeating 1 instruction.
#include<iostream>
using namespace std;
main(){
for(int x = 0; x < 5; x = x + 1)
{
cout << "Welcome to UET!!" << endl;
}
}
Counter Loop: Multiple Instructions
We can also use multiple instructions within the FOR loop
}
}
Counter Loop: Multiple Instructions
Suppose, we want to write a C++ program that repeats
multiple instructions to print the table of 5.
#include <iostream>
using namespace std;
main()
{
int multiple;
int table = 5;
for (int num = 1; num <= 10; num = num + 1)
{
multiple = table * num;
cout << table << " * " << num << " = " << multiple << endl;
}
}
Counter Loop: Multiple Instructions
Write a C++ program that finds sum of numbers between
1 and 100 without using formula use code template given
next
Counter Loop: Multiple Instructions
Write a C++ program that finds sum of numbers between
1 and 100 without using formula.
#include <iostream>
using namespace std;
main()
{
int sum = 0;
for (int x = 1; x <= 100; x = x + 1)
{
}
}
Counter Loop: Multiple Instructions
Write a C++ program that finds sum of numbers between
1 and 100
#include <iostream>
using namespace std;
main(){
int sum = 0;
for (int x = 1; x <= 100; x = x + 1)
{
sum = sum + x;
}
cout << "The sum of Numbers from 1 to 100 is: ";
cout << sum;
}
Counter Loop: Independent Iterations
In first two problems the individual iterations are
Independent of each other.
#include<iostream> #include <iostream>
using namespace std; using namespace std;
main()
{
main(){
int multiple;
int table = 5;
for(int x = 0; x < 5; x = x + 1) for (int num = 1; num <= 10; num = num + 1)
{ {
cout << "Welcome to UET!!" << endl; multiple = table * num;
cout << table <<" * "<<num<<" = "<<multiple<< endl;
}
}
}
}
Counter Loop: Independent Iterations
Independent Iteration means the result of previous
iteration are not required for next iterations.
#include<iostream> #include <iostream>
using namespace std; using namespace std;
main()
{
main(){
int multiple;
int table = 5;
for(int x = 0; x < 5; x = x + 1) for (int num = 1; num <= 10; num = num + 1)
{ {
cout << "Welcome to UET!!" << endl; multiple = table * num;
cout << table <<" * "<<num<<" = "<<multiple<< endl;
}
}
}
}
Counter Loop: Dependent Iterations
However, there are set of problems that require the
result from previous iterations.
#include <iostream>
using namespace std;
main(){
int sum = 0;
for (int x = 1; x <= 100; x = x + 1)
{
sum = sum + x;
}
cout << "The sum of Numbers from 1 to 100 is: ";
cout << sum;
}
Counter Loop: Dependent Iterations
Dependent Iteration means the result of previous
iteration are required for next iterations.
#include <iostream>
using namespace std;
main(){
int sum = 0;
for (int x = 1; x <= 100; x = x + 1)
{
sum = sum + x;
}
cout << "The sum of Numbers from 1 to 100 is: ";
cout << sum;
}
Working Example 2: Fibonacci Series
The Fibonacci numbers are the numbers in the following
integer sequence.
Working Example 2: Fibonacci Series
You are given the first 2 fibonacci numbers.
int n1 = 0;
int n2 = 1;
}
}
Solution: Fibonacci Series
#include <iostream>
using namespace std;
main()
{
int n1 = 0;
int n2 = 1;
int next;
for(int x = 0; x < 5; x = x + 1)
{
next = n1 + n2;
cout << next << ", ";
n1 = n2;
n2 = next;
}
}
Working Example 3: Fibonacci Series
Input: Take three variables, n1, n2, n3 from user
Make a function named
void fibonacciSeries(int n1, int n2, int n3)
Input Output
n1: 2 5, 8, 13, 21, 34, 55, 89, 144,
n2: 3 233
n3: 9
n1: 0 1, 2, 3, 5, 8, 13, 21, 34, 55,
n2: 1 89, 144, 233, 377, 610, 987,
n3: 20 1597, 2584, 4181, 6765, 10946,
Solution: Fibonacci Series
#include <iostream>
using namespace std;
void fibonacciSeries(int n1, int n2, int n3)
{
int next;
for(int x = 0; x < n3; x = x + 1){
next = n1 + n2;
cout << next << ", ";
n1 = n2;
n2 = next;
}
}
Learning Objective
factorial(4) = 4 × 3 × 2 × 1 = 24
factorial(7) = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040
factorial(1) = 1