Write a code to print fibonacci series

Q5. Write a code to print fibonacci series

Solution :- This program is about giving a length N and the task is to print the Fibonacci series upto n terms.

Fibonacci Sequence :- The Fibonacci sequence is a sequence consisting of a series of numbers and each number is the sum of the previous two numbers. For Example :- 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….

Algorithm of Fibonacci Series

START
Step 1->Declare variables i, a, b, nextTerm
Step 2->Initialize the variables, a=0, b=1, and nextTerm = 0
Step 3->Enter the number of terms of Fibonacci series to be printed
Step 4->Repeat below steps n times
-> print the value of a
-> nextTerm = a + b
-> a = b
-> b = nextTerm
-> increase value of i each time by 1
STOP

Code of Fibonacci Series

Output

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to find perfect number

Q4.Write a code to find perfect number

Perfect Number :- Perfect number, a positive integer equal to the sum of its proper divisors. The smallest Perfect number is 6, which is the sum of 1, 2 and 3.
For Example:-
Input = 28
Output = 28 is the Perfect Number.
So as we can see that the divisor of 28 is 1, 2, 4, 7, 14, so the sum of all the divisors is 28 (1 + 2 + 4 + 7 + 14 = 28). Therefore, 28 is the Perfect number.

Algorithm for Perfect Number

START
Step 1 – Input the number.
Step 2 – Find all divisors of the number except the number itself.
Step 3 – If the sum of all divisors of the number is equal to the number, then return true. Else, return false.
STOP

Code for Perfect Number

Output

28 is a perfect number.

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to find GCD of two numbers

Q3. Write a code to find GCD of two numbers

GCD :- GCD stands for Greatest Common Divisor, GCD of two numbers is the largest number that can exactly divide both numbers. It is also called as HCF.

For Example :-
Input = 25 and 10
Output = 5 is the GCD.
As we can see that 5 divides both 25 and 10 and we don’t have any larger number that divides both the numbers therefore, the GCD of 25 and 10 is 5.

Algorithm for GCD

START
1. Input 2 Numbers A and B and declare variable GCD which holds the result.
2. Run Loop i from 1 to i <= A and i <=B
Check if A & B are completely divisible by i or not if yes then
Assign GCD = i
Loop End
3. Output GCD
STOP

Code for GCD

Output

GCD of 25 and 10 is 5

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to find sum of digits of a given number

Q2. Write a code to find sum of digits of a given number

Sum of digits :- Here we will discuss how we can find the sum of digits in a given number. Let us understand that with the help of an example.

For Example :-
Input:-  3256
Output:- 16

Explanation:- As you can see the given number is 3256 and we have to calculate the sum of digits in the given number, so here we have the following digits 3, 2, 5, 6, and we will sum all these digits -> 3 + 2 + 5 + 6  and the result we get is 16 that will be our output.

Below you will find its algorithm and program.

Sum Of Digits of a number Algorithm

START
Step 1: Get number by user
Step 2: Get the modulus/remainder of the number
Step 3: sum the remainder of the number
Step 4: Divide the number by 10
Step 5: Repeat the step 2 while number is greater than 0.
STOP

Sum Of Digits of a number Program

Output

Sum Of Digits : 16

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to check whether the given number is prime number or not

Q1.Write a code to check whether the given number is prime number or not

The article will help you write a algorithm and program to Check Whether a Number is Prime or not.

Prime Number:- A number that is divisible by only and only 1 and itself is known as a Prime Number. For example: – 11 is only divisible by 1, so 11 is prime, while 10 is divisible by 1, 2, and 5 so 10 is not a prime number.

Here you will find the algorithm and program to check whether a number is prime or not with explanation.

Prime Number Checking Algorithm

Here’s a simple algorithm to check whether a given positive integer is prime or not:

 

1.Start with the number n to be checked.

2.If n is less than 2, it is not prime. Return false.

3.If n is 2 or 3, it is prime. Return true.

4.If n is even (i.e., divisible by 2), it is not prime (except for 2 itself). Return false.

5.For all odd integers i from 3 to the square root of n (inclusive), do the following:

6.If n is divisible by i, it is not prime. Return false.

7.If none of the above conditions hold, n is prime. Return true.

Program to check whether the number is Prime or not

Output

Enter a positive integer : 11
it is a prime number.
Enter a positive integer : 24
it is not a prime number.

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number