0% found this document useful (0 votes)
22 views

Algorithm Examples

Uploaded by

shoaibmod712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Algorithm Examples

Uploaded by

shoaibmod712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1. Algorithm to calculate the sum of any two numbers.

Step 1: Start
Step 2: Declare variables a, b, sum
Step 3: Input the values of a and b
Step 4: Calculate, sum = a+b
Step 5: Display the value of sum
Step 6: Stop
2. Algorithm to display the larger value between any two numbers.
Step 1: Start
Step 2: Declare variables a,b
Step 3: Read the values of a and b
Step 4: if a>b then
display a is larger
else
display b is larger
Step 5: Stop
3. Write an algorithm and draw flowchart to check whether any integer is exactly divisible
by 3 or not.
Step 1: Start
Step 2: Declare variables n, rem
Step 3: Input the value n
Step 4: Calculate, rem = n mod 3
Step 5: if rem = 0 then
display n is exactly divisible by 3
else
display n is not exactly divisible by 3
Step 6: Stop
4. Algorithm to display the numbers from 1 to n.
Step 1: Start
Step 2: Declare variables n, i
Step 3: Input the value of n
Step 4: Initialize, i = 1
Step 5: Repeat steps 5.1 and 5.2 until i≤n
Step 5.1: Display the value of i
Step 5.2: Increase the value of i by 1 i.e. i = i+1
Step 6: Stop

5. Algorithm to display the odd numbers from 1 to n.


Step 1: Start
Step 2: Declare variables n, i
Step 3: Input the value of n
Step 4: Initialize, i = 1
Step 5: Repeat steps 5.1 and 5.2 until i≤n
Step 5.1: Display the value of i
Step 5.2: Increase the value of i by 2 i.e. i = i+2
Step 6: Stop

6. Algorithm to display the even numbers from 1 to n.


Step 1: Start
Step 2: Declare variables n, i
Step 3: Input the value of n
Step 4: Initialize, i = 2
Step 5: Repeat steps 5.1 and 5.2 until i≤n
Step 5.1:Display the value of i
Step 5.2: Increase the value of i by 1 i.e. i = i+2
Step 6: Stop

7. Algorithm to display the even numbers from 1 to n.


Step 1: Start
Step 2: Declare variables n, i
Step 3: Input the value of n
Step 4: Initialize, i = 1
Step 5: Repeat steps 5.1 and 5.2 until i≤n
Step 5.1: If i mod 2 = 0 then
Display the value of i
Step 5.2: Increase the value of i by 1 i.e. i = i+1
Step 6: Stop

8. Algorithm to display the sum of the numbers from 1 to n.


Step 1: Start
Step 2: Declare variables n, i, sum
Step 3: Input the value of n
Step 4: Initialize, i = 1,sum=0
Step 5: Repeat steps 5.1 and 5.2 until i≤n
Step 5.1: sum = sum + i
Step 5.2: Increase the value of i by 1 i.e. i = i+1
Step 6: Display the value of sum
Step 7: Stop

[n=5,i=1,sum=0
sum=0+1=1
i=2
sum=1+2=3
i=3
sum=3+3=6 , i=4
sum=6+4=10 , i=5, sum=10+5=15 i=6 Exit step 5
sum=15 Ans.]

9. Algorithm to display the factorial of n.


Step 1: Start
Step 2: Declare variables n, i, fact
Step 3: Input the value of n
Step 4: Initialize, i = 1,fact=1
Step 5: Repeat steps 5.1 and 5.2 until i≤n
Step 5.1: fact=fact * i
Step 5.2: Increase the value of i by 1 i.e. i = i+1
Step 6: Display the value of fact
Step 7: Stop

10: Algorithm to display the factors of any integer


Step 1: Start
Step 2: Declare variables n, i=1
Step 3: Input number n
Step 4: Repeat steps 4.1 and 4.2 until i≤n
Step 4.1: if n mod i=0
Display the value of i
Step 4.2: i=i+1
Step 5: Stop

11: Algorithm to display the count of factors in any integer


Step 1: Start
Step 2: Declare variables n, i=1,c=0
Step 3: Input number n
Step 4: Repeat steps 4.1 and 4.2 until i≤n
Step 4.1: if n mod i=0
c=c+1
Step 4.2: i=i+1
Step 5: Display the value of c
Step 5: Stop
12: Algorithm to check whether any integer is prime or not.
Step 1: Start
Step 2: Declare variables n, i=1,c=0
Step 3: Input number n
Step 4: Repeat steps 4.1 and 4.2 until i<=n
Step 4.1: if n mod i=0
c=c+1
Step 4.2: i=i+1
Step 5: If c = 2 then
Display n is prime
else
Display n is not prime
Step 6: Stop

13. Algorithm to calculate the sum of digits present in any integer.


Step 1: Start
Step 2: Declare variables n,rem,sum=0
Step 3: Input the value of n
Step 4: Repeat steps 4.1,4.2 and 4.3 until n≠0
Step 4.1: rem=n mod 10
Step 4.2: sum=sum+rem
Step 4.3: n=n/10 [Integer Division]
Step 5: Display the value of sum
Step 6: Stop

14. Algorithm to calculate the reverse of any integer.


Step 1: Start
Step 2: Declare variables n,rem,rev=0
Step 3: Input the value of n
Step 4: Repeat steps 4.1,4.2 and 4.3 until n≠0
Step 4.1: rem=n mod 10
Step 4.2: rev=rev*10+rem
Step 4.3: n=n/10 [Integer Division]
Step 5: Display the value of rev
Step 6: Stop

You might also like