HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
Unit 1 algorithms practise
1. for
Open Educational Resource
on
Logic Development and Programming (EC221)
by
Dr. Piyush Charan
Assistant Professor
Department of Electronics and Communication Engg.
Integral University, Lucknow
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
2. Algorithm to reverse a Number
Input: 123
Output: 321
Step 1: Start
Step 2: Accept n
Step 3: [Initialize] rev=0
Step 4: Repeat steps 4 to 7 until n!=0
Step 5:rem=n%10
Step 6: rev=rev*10+rem
Step 7: n=n/10
Step 8: Display rev
Step 9: Stop
01/SEP/2020 2Dr. Piyush Charan, Dept. of ECE, IU Lucknow
3. Dry Run- Reverse a Number
n rem rev
123 3 0
12 2 3
1 1 32
0 321
For n=123
rem= 123%10=3
rem=12%10=2
rem=1%10=1
rev= 0×10+3=3
rev= 3×10+2=32
rev=32×10+1=321
n=123/10=12
n=12/10=1
n=1/10=0
01/SEP/2020 3Dr. Piyush Charan, Dept. of ECE, IU Lucknow
Input: 123
Output: 321
4. Algorithm to check for a
Palindrome Number
Step 1: Start
Step 2: Accept n
Step 3: [Initialize] rev=0, a=n
Step 4: Repeat steps 4 to 7 until n!=0
Step 5: rem=n%10
Step 6: rev=rev*10+rem
Step 7: n=n/10
Step 8: if rev==a
Step 9: print “It is a palindrome number”
Step 10: else print “It is not a palindrome number”
Step 11: Stop
01/SEP/2020 4Dr. Piyush Charan, Dept. of ECE, IU Lucknow
5. Dry Run- Reverse a Number
n a rem rev
121 121 1 0
12 2 1
1 1 12
0 121
For n=121
rem= 121%10=1
rem=12%10=2
rem=1%10=1
rev= 0×10+1=1
rev= 1×10+2=12
rev=12×10+1=121
n=121/10=12
n=12/10=1
n=1/10=0
01/SEP/2020 5Dr. Piyush Charan, Dept. of ECE, IU Lucknow
Input: 121
Output: It is a palindrome number
6. Algorithm to check Armstrong Number
Input: 153
Output: It is an Armstrong Number
Logic for Armstrong Numbers: 1^3+5^3+3^3=1+125+27=153
Step 1: Start
Step 2: Accept n
Step 3: [Initialize] sum=0, a=n
Step 4: Repeat steps 4 to 7 until n!=0
Step 5: rem=n%10
Step 6: sum=sum+ rem*rem*rem
Step 7: n=n/10
Step 8: Display sum
Step 9: if sum==a
Step 10: Print “It is an Armstrong Number”
Step 11: else Print “It is not an Armstrong Number”
Step 12: Stop
01/SEP/2020 6Dr. Piyush Charan, Dept. of ECE, IU Lucknow
7. Dry Run- Armstrong Number
n a rem sum
153 153 3 0
15 5 27
1 1 152
0 153
For n=153
rem= 153%10=3
rem=15%10=5
rem=1%10=1
sum= 0+3×3×3=27
sum= 27+5×5×5=152
sum=152+1×1×1=153
n=153/10=15
n=15/10=1
n=1/10=0
01/SEP/2020 7Dr. Piyush Charan, Dept. of ECE, IU Lucknow
8. Algorithm to generate Fibonacci
series using Iterative Logic
Step 1: Start
Step 2: Accept n
Step 3: Declare a, b, fib, i
Step 4: [Initialize] a=0, b=1, i=1
Step 5: Display a, b
Step 6: Repeat steps 6 to 11 until i<=n-2
Step 7: fib=a+b
Step 8: a=b
Step 9: b=fib
Step 10: Display fib
Step 11: i=i+1
Step 12: Stop
01/SEP/2020 Dr. Piyush Charan, Dept. of ECE, IU Lucknow 8
9. Dry Run- Fibonacci Series
up to ‘n’ terms
01/SEP/2020 Dr. Piyush Charan, Dept. of ECE, IU Lucknow 9
n a b fib i
4 0 1 1 1
1 1 2 2
1 2 3
For n=4
Output Window:
0 1 1 2
10. Algorithm to check whether a
number is Prime or not
Step 1: Start
Step 2: Accept n
Step 3: [Initialize] i=1, c=0
Step 4: Repeat steps 4 to 7 until i<=n
Step 5: if n%1==0 && n%i==0
Step 6: c=c+1
Step 7: i=i+1
Step 8: if c==2
Step 9: print “It is a Prime Number”
Step 10: else print “It is not a Prime Number”
Step 11: Stop
01/SEP/2020 Dr. Piyush Charan, Dept. of ECE, IU Lucknow 10
11. Dry Run- To check for
Prime Number
01/SEP/2020 Dr. Piyush Charan, Dept. of ECE, IU Lucknow 11
n i c
3 1 0
2 1
3 2
4
For n=3
Output Window:
It is a Prime Number
3%1==0 && 3%1==0
3%1==0 && 3%2!=0
3%1==0 && 3%3==0
Note: The counter ‘c’
increments only when
both the conditions are
TRUE together.