22.3.21

Hw-7: C++ OOP to print a fibonacci series.

 

Fibonacci Series in C++

Fibonacci Series in C++: In case of fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.

Do the following two ways:

  • Fibonacci Series without recursion
  • Fibonacci Series using recursion

Hw-6: Write a c++ OOP to find factorial using loop in C++

In this program, we will learn how to find factorial of a given number using C++ program? Here, we will implement this program with and without using user define function.

Logic:

  1. Input a number
  2. Initialize the factorial variable with 1
  3. Initialize the loop counter with N (Run loop from number (N) to 1)
  4. Multiply the loop counter's value with factorial variable

Program to find factorial using loop in C++


Enter an integer number: 6
Factorial of 6 is = 720    
What is factorial?
Answer: 

Factorial !

Example: 4! is shorthand for 4 × 3 × 2 × 1

Factorial Symbol

The factorial function (symbol: !) says to multiply all whole numbers from our chosen number down to 1.

Examples:

  • 4! = 4 × 3 × 2 × 1 = 24
  • 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040
  • 1! = 1

We usually say (for example) 4! as "4 factorial", but some people say "4 shriek" or "4 bang"

Calculating From the Previous Value

We can easily calculate a factorial from the previous one:

factorial multiply

As a table:

nn!  
1111
22 × 1= 2 × 1!= 2
33 × 2 × 1= 3 × 2!= 6
44 × 3 × 2 × 1= 4 × 3!= 24
55 × 4 × 3 × 2 × 1= 5 × 4!= 120
6etcetc 

  • To work out 6!, multiply 120 by 6 to get 720
  • To work out 7!, multiply 720 by 7 to get 5040
  • And so on

Example: 9! equals 362,880. Try to calculate 10!

10! = 10 × 9!

10! = 10 × 362,880 = 3,628,800

So the rule is:

n! = n × (n−1)!

Which says

"the factorial of any number is that number times the factorial of (that number minus 1)"

So 10! = 10 × 9!, ... and 125! = 125 × 124!, etc.

What About "0!"

Zero Factorial is interesting ... it is generally agreed that 0! = 1.

It may seem funny that multiplying no numbers together results in 1, but let's follow the pattern backwards from, say, 4! like this:

24/4=6, 6/3=2, 2/2=1, 1/1=1

And in many equations using 0! = 1 just makes sense.


Example: how many ways can we arrange letters (without repeating)?

  • For 1 letter "a" there is only 1 way: a
  • For 2 letters "ab" there are 1×2=2 ways: ab, ba
  • For 3 letters "abc" there are 1×2×3=6 ways: abc acb cab bac bca cba
  • For 4 letters "abcd" there are 1×2×3×4=24 ways: (try it yourself!)
  • etc

The formula is simply n!

Now ... how many ways can we arrange no letters? Just one way, an empty space:


So 0! =
1


20.3.21

Spring 2021 c++: hw-5

Create a OOP that counts the number of times a particular letter shows up in the word search. Count how many times it came in the string. Use Inheritance.

Examples

letterCounter([
  ["D", "E", "Y", "H", "A", "D"],
  ["C", "B", "Z", "Y", "J", "K"],
  ["D", "B", "C", "A", "M", "N"],
  ["F", "G", "G", "R", "S", "R"],
  ["V", "X", "H", "A", "S", "S"]
], "D") ➞ 3

// "D" shows up 3 times: twice in the first row, once in the third row.

letterCounter([
  ["D", "E", "Y", "H", "A", "D"],
  ["C", "B", "Z", "Y", "J", "K"],
  ["D", "B", "C", "A", "M", "N"],
  ["F", "G", "G", "R", "S", "R"],
  ["V", "X", "H", "A", "S", "S"]
], "H") ➞ 2

9.3.21

Spring 2021 C+: hw-4

Write a C++ program that will read an integer number (up to four digits) and convert it into words.

Sample Input/Output:


Spring 2021 c++ : hw-3

C++ OOP to display name and age


You have to read name and age of a person and display them on the output screen. Here, you will learn how to read string (name) with spaces in C++ language?

Here, you have to  declare a string (character array) variable named name that will store name of the person and integer variable named age that will store the age of the person.

Use Constructor, Destructor and Method in C++

Spring 2021 C++ : hw-2

1. Write a OOP to read string using cin function in c ++ . Count the number of character in that string without using built-in function.

2. Write a OOP to read a line of string using getline() function in c++. Count the number of word and number of character from that string. 

Spring 2021 C++ : hw-1

1.       The Farming Problem:

In this problem, a person is asking you to tell him how many legs can be counted among all his animals. The farmer has given three types sample:

A.)   Chicken: 2 legs

B.)    Cows: 4 legs

C.)    Goat : 4 legs

The farmer has counted his animals and he gives you a subtotal for each sample. You have to implement a function in OOP that returns the total number of legs of all the animals.

Rules: Don’t for get to return the result. Remember that the farmer wants to now the total number of legs and not the total number of animals.

Hints:

Farmer said that total number of animals is: 150

Farmer mentioned that there are 100 chickens, 25 cows and 25 goats