home / study / engineering / computer science / questions and answers / for each
ID: 3857904 • Letter: H
Question
home / study / engineering / computer science / questions and answers / for each of the problems listed below write the ...
Question: For each of the problems listed below write the C+...
Bookmark
for each of the problems listed below write the C++ program using for loop structures
I. A program will calcuate and display the fibonacci sequence
"1 1 2 3 5 8 13...."
II. A program will calculate and display a square matrix multiplication table. For instance a sample output for a 4x4 table is listed below. (Hint. Nested for loops are needed).
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
Explanation / Answer
Please refer below codes
1)
#include<iostream>
using namespace std;
int main()
{
int number_of_terms,first=0,second=1,term;
cout<<"Enter number of terms"<<endl; //asking number of terms of fibonacci series
cin>>number_of_terms;
cout<<"Fibonacci series is"<<endl;
//logic is 0,1 are fixed and next term is calculated by sum of previous terms
for(int i=0; i < number_of_terms; i++)
{
cout<<first<<" ";
term = first + second;
first = second;
second = term;
}
return 0;
}
Please refer below output
Enter number of terms
10
Fibonacci series is
0 1 1 2 3 5 8 13 21 34
Process returned 0 (0x0) execution time : 4.752 s
Press any key to continue.
II) Please refer below code
#include<iostream>
using namespace std;
int main()
{
int rows,r,c;
int **mat;
cout<<"Enter number of rows :";
cin>>rows;
mat = new int*[rows];
for(int i=0; i < rows; i++)
mat[i] = new int[rows];
cout<<"Output matrix is :"<<endl;
for(int i=0; i < rows; i++)
{
r = i + 1;
for(int j=1; j <= rows; j++)
{
c = j;
mat[i][j] = r * c; //multiplying row and column at any position of element of matrix
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Please refer below output
Enter number of rows :10
Output matrix is :
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Process returned 0 (0x0) execution time : 2.269 s
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.