Write a code to check whether the number is even or odd

Q6. Write a code to check whether the number is even or odd

Here you will find an algorithm and program to check whether a number is an even number or an odd number. we
know that :-

Even Number :- Even numbers are the numbers which are divisible by 2. For Example :- 2, 4, 6, …

Odd Number :- Odd numbers are the numbers which are not divisible by 2. For Example :- 1, 3, 5, …

Now let us understand what logic we need to use in this –
As we know that, to know number is even or odd we used to check its divisibility with 2, so here we will check the remainder we get on dividing the input number with 2 if we get remainder as 0 then it is divisible by 2 and hence it is even number else it is odd number.

For Example :-

Input : 12
Output : Even Number
Explanation : As we can see that the number given in input, which is 12, on dividing with 2 we get remainder 0 therefore 12 is Even Number.

Algorithm to check given number  is even or odd

\\Algorithm to check number is even or odd.
START
Step 1: [ Take Input ] Read: Number N
Step 2: Check: If N%2 == 0 Then
Print : N is an Even Number.
Else
Print : N is an Odd Number.
STOP

Code to check given number is even or odd

Output

12 is an Even number

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

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