Assignment # 2
Subject: Programming Fundamentals
Teacher: Ma’am Khola Naseem
Student: Ali Hamza(30198)
Fibonacci Series
Algorithm
Start
Declare and initialize variables
Enter number of terms to be printed
Use loop for
C=num1+num2
num1=num2
num2=c
increase value of ‘a’ every time by 1
‘a’ is less than the number of terms entered
Print value of c
End
Code
int n, c, num1 = 0, num2 = 1, a = 1;
cout << "Enter number of terms: ";
cin >> n;
cout << "Fibonacci series is " << num1 << num2;
do
{
c = num1 + num2;
cout << c;
num1 = num2;
num2 = c;
a++;
} while (a <= n);
cout << endl;
}
Flow Chart
Start
Declaring and Initializing variables
n, first = 0, second = 1, next, a
Input number
Code
c = num1 + num2;
cout << c;
num1 = num2;
num2 = c;
a++;
(a<=n)
Cout<
End
Floyd’s Triangle
Algorithm
Start
Declare variables
Enter number of rows
Initialize ‘a’ with value 1
If ‘a’ is divisible by 2 than value of ‘c’ equal 1 and value of ‘d’ is 0
Otherwise if ‘a’ is not divisible by 2 than value of ‘c’ equal to 0 and value of ‘d’ is equal to 1
Initialize ‘b’ with value of 1
If ‘b’ is divisible by 2 print value of ‘c’
Otherwise print value of ‘d’
Increase ‘b’ by 1 each time
‘b’ less than equal to ‘a’
And ‘a’ less than equal to ‘e’
Code
// fibonacci Series.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include
using namespace std;
int main()
{
int a, b, c, d, e;
cout << "Enter number of rows: ";
cin >> e;
a = 1;
do
{
if (a % 2 == 0)
{
c = 1;
d = 0;
}
else
{
c = 0;
d = 1;
}
b = 1;
do
{
if (b % 2 == 0)
{
cout << c;
}
else
{
cout << d;
}
b++;
}
while (b <= a);
a++;
cout << endl;
}
while (a <= e);
}
Flow Chart
Share with your friends: |