Lecture 4 - Math

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Fibonacci Series

• The Fibonacci sequence is a series where the next term


is the sum of pervious two terms.

• The first two terms of the Fibonacci sequence is 0


followed by 1.

• The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21


Program to Display Fibonacci Series
• #include <iostream> •{
• using namespace std; • cout << " " << t1;
• continue;
• int main() • }
•{ • if(i == 2)
• int n, t1 = 0, t2 = 1, nextTerm = 0; • {
• cout << t2 << " ";
• cout << "Enter the number of • continue;
terms: "; • }
• cin >> n; • nextTerm = t1 + t2;
• t1 = t2;
• cout << "Fibonacci Series: "; • t2 = nextTerm;
• for (int i = 1; i <= n; ++i) •
• { • cout << nextTerm << " ";
• // Prints the first two terms. • }
• if(i == 1) • return 0;
• •}
To find Largest Number among Three Numbers
• To find the largest number among three numbers using
if, if else and nested if else statements.

• In this program, user is asked to enter three numbers.

• Then this program finds out the largest number among


three numbers entered by user and displays it with
proper message.
Program to find Largest Number among Three
Numbers
• #include <iostream>
• using namespace std; • if(number2 >= number1
&& number2 >= number3)
• int main() • {
•{
• cout << "Largest
• float number1, number2, number: " << number2;
number3;
• }
• cout << "Enter three
numbers: "; • if(number3 >= number1
• cin >> number1 >> number2 && number3 >= number2)
>> number3; {
• cout << "Largest
• if(number1 >= number2 && number: " << number3;
number1 >= number3) • }
• {
• cout << "Largest number:
" << number1; • return 0;
• } •}
To find Quotient and Reminder

• To find the quotient and remainder of a given dividend and divisor.

• In this program, user is asked to enter two integers (divisor and


dividend) and computes the quotient and remainder.

• To compute quotient and remainder, both divisor and dividend should


be integers.

• The division operator / is computes the quotient (either between float


or integer variables).

• The modulus operator % computes the remainder when one integer is


divided by another (modulus operator cannot be used for floating-
type variables).
Program to find Quotient and Reminder
• #include <iostream> • cout << "Enter divisor: ";
• cin >> divisor;
• using namespace std;
• quotient = dividend /
• int main() divisor;
• remainder = dividend %
•{ divisor;
• int divisor, dividend,
quotient, remainder; • cout << "Quotient = " <<
quotient << endl;
• cout << "Remainder = " <<
• cout << "Enter remainder;
dividend: ";
• cin >> dividend; • return 0;
•}
Program to perform Matrix Multiplication
• #include<conio.h>
• #include<iostream>
• using namespace std;
• int main()
• {
• int a[10][10], b[10][10], c[10][10];

• int x, y, i, j, m, n;

• cout << "\nEnter the number of rows and columns for Matrix A:::\n\n";

• cin >> x >> y;


• // x denotes number rows in matrix A
• // y denotes number columns in matrix A
• cout << "\n\nEnter elements for Matrix A :::\n\n";
• for (i = 0; i < x; i++)
• {
• for (j = 0; j < y; j++)
• {
• cin >> a[i][j];
• }
• cout << "\n";
Continue…
• }
• cout << "\n\nMatrix A :\n\n";
• for (i = 0; i < x; i++)
• {
• for (j = 0; j < y; j++)
• {
• cout << "\t" << a[i][j];
• }
• cout << "\n\n";
• }
• cout << "\n-----------------------------------------------------------\n";
• cout << "\nEnter the number of rows and columns for Matrix B:::\n\n";
• cin >> m >> n;
• // m denotes number rows in matrix B
• // n denotes number columns in matrix B
• cout << "\n\nEnter elements for Matrix B :::\n\n";
• for (i = 0; i < m; i++)
• {
• for (j = 0; j < n; j++)
• {
• cin >> b[i][j];
• }
• cout << "\n";
Continue…
• }
• cout << "\n\nMatrix B :\n\n";
• for (i = 0; i < m; i++)
• {
• for (j = 0; j < n; j++)
• {
• cout << "\t" << b[i][j];
• }
• cout << "\n\n";
• }
• if (y == m)
• {
• for (i = 0; i < x; i++)
• {
• for (j = 0; j < n; j++)
• {
• c[i][j] = 0;
• for (int k = 0; k < m; k++)
• {
• c[i][j] = c[i][j] + a[i][k] * b[k][j];
• }
• } Continue…
• }
• cout << "\n-----------------------------------------------------------\n";
• cout << "\n\nMultiplication of Matrix A and Matrix B :\n\n";
• for (i = 0; i < x; i++)
• {
• for (j = 0; j < n; j++)
• {
• cout << "\t" << c[i][j];
• }
• cout << "\n\n";
• }
• }
• else
• {
• cout << "\n\nMultiplication is not possible";
• }
• getch();
• return 0;
• }
Arithmetic Means
• In order to calculate arithmetic mean of numbers in C+
+ Programming, we have to ask to the user to enter
number size then ask to enter the numbers of that size.
• To calculate arithmetic mean of all numbers, first
perform addition of all the numbers, then make a
variable responsible for the arithmetic mean and place
addition/size in a variable say armean (arithmetic
mean), then display the result on the output screen.
Program to find Arithmetic Mean
• #include<iostream> • for(i=0; i<n; i++)
• #include<conio.h> • {
• using namespace std; • cin>>arr[i];
• int main() • sum=sum+arr[i];
•{
• }
• int n, i, arr[50], sum=0;
• int armean=sum/n;
• cout<<"How many
number you want to • cout<<"Arithmetic
enter ?\n"; Mean = "<<armean;
• cin>>n; • getch();
• cout<<"Enter "<<n<<" • return 0;
Numbers :";
•}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy