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

Algorithm 1

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)
20 views

Algorithm 1

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/ 5

Algorithms

Prime Number
Step-1: Start

Step-2: Input n

Step-3: Ini alize r, i, Count=0

Step-4: for i = 2 to i < n do

1. r = n mod i
2. If r = 0
a. Count = Count+1
b. break
3. i = i+1

Step-5: if count = 0 then

1. print “ Prime number. ”

Else

1. print “ Non Prime number. ”

Step-6: End if

Step-7: Stop

Leap Year
Step-1: Start

Step-2: Input year

Step-3: if year mod 400 = 0 then

1. print “ Leap Year ”

else

1. if year mod 100 = 0 then


a. print “ Not Leap Year ”

else

1. if year mod 4 = 0 then


a. print “ Leap Year ”

Step-4: End if

Step-5: Stop

1
Fibonacci series
Step-1: Start

Step-2: Input range R

Step-3: Ini alize first = 0 , second = 1

Step-4: print “The Fibonacci seires : ”

Step-5: print “ first , second ”

Step-6: for i = 0 to i < R do

1. third = first+second
2. print “ third ”
3. first = second
4. second = third
5. i = i+1

Step-7: End loop

Step-8: Stop

Palindrome number
Step-1: Start

Step-2: Input number N

Step-3: Set reverse = 0

Step-4: copy = N

Step-4: while N>0 do

1. digit = N%10
2. reverse = reverse*10+digit
3. N = N/10

Step-5: End Loop

Step-6: if copy = reverse then

1. Print “ Palindrome number .”

else

1. Print “ Not Palindrome number .”

Step-7: End if

Step-8: Stop

2
Largest and Smallest of n numbers
Step-1: Start

Step-2: Ini alize n,num[100],max,min

Step-3: Input n (The array number)

Step-4: for i=0 to i=n-1 do

1. Input num[i]

Step-5: Set max = num[0] and min = num[0]

Step-6: for i=0 to i=n-1 do

1. If num[i] > max then


a. max = num[i]
2. If num[i] < min then
a. min = num[i]
3. i = i +1

Step-7: End loop

Step-8: Print “ max and min ”

Step-9: Stop

Nearest Prime number


Step-1: Start

Step-2: Input n

Step-3: If n is prime then

1. print “ n ”

Step-4: else

1. Set two pointers: one (lower) star ng from n−1 and the other (upper) from n+1.
2. Check if the lower number is prime.
a. If prime then
i. Print “lower”
3. Check if the upper number is prime.
a. If prime then
i. Print “ upper ”
4. If neither is prime then
a. lower = lower -1
b. upper = upper +1
5. The loop con nues un l one of the pointers finds a prime number.

Step-9: End if

Step-10: Stop

3
Decimal to Binary
Step-1: Start

Step-2: Ini alize dec,bin,i

Step-3: Input decimal number dec

Step-4: while dec>0 do

1. bin = bin + (dec mod 2)*i


2. dec = dec/2
3. i = i*10

Step-5: End while

Step-6: Print “binary number bin”

Step-7: Stop

Sin(x) Series
sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
Step-1: Start

Step-2: Ini alize x,n,i,j,sum=0

Step-3: Input the no of terms n

Step-4: Input the value of x (in Radian)

Step-5: If the value of x is in Radian form then convert into Radian

Step-6: for i=0 and j=1 to i<n

1. fact = factorial of j
2. sum = sum + ((-1)^i)*(x^j)/fact

Step-7: print “sum”

Step-8: Stop

4
Goldberg Conjecture
Step-1: Start

Step-2: Ini alize n,i,j,

Step-3: Input a even number n

Step-4: for i = 1 to n

1. if i=prime then
a. if (n-i) = prime then
1. Print “ The number can br expressed as the sum of two prime number ”

Step-5: End if

Step-6: Stop

Armstrong Number
Step-1: Start

Step-2: Ini alize n,s,temp,dig,count.

Step-3: Input n

Step-4: Set the value of s=0 , count=0 and temp=n

Step-5: While n>0 do

1. n = n/10
2. count = count + 1

Step-6: End While

Step-7: n = temp

Step-8: while n>0 do

1. dig = n mod 10
2. s = s + (dig to the power count)
3. n = n/10

Step-9: End while

Step-10: if temp == s then

1. Print “ The number is Armstrong Number. ”

Else

1. Print “ The number is not Armstrong Number. ”

Step-11: End if

Step-12: Stop

You might also like