0% found this document useful (0 votes)
3 views5 pages

Coding Questions For Practice

The document contains a series of shell scripting coding questions and their solutions, focusing on various programming concepts such as input validation, loops, and mathematical operations. It includes examples like generating multiplication tables, printing star patterns, counting digits, and checking for Armstrong numbers. Each question is accompanied by a brief description and a code listing demonstrating the solution.
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)
3 views5 pages

Coding Questions For Practice

The document contains a series of shell scripting coding questions and their solutions, focusing on various programming concepts such as input validation, loops, and mathematical operations. It includes examples like generating multiplication tables, printing star patterns, counting digits, and checking for Armstrong numbers. Each question is accompanied by a brief description and a code listing demonstrating the solution.
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

Coding Questions for Practice

Shell Scripting  Bash


Contents
Q-1: Multiplication Table with Input Validation . . . . . . . . . . . . . . . . . 1
Q-2: Right-Angled Triangle of Stars . . . . . . . . . . . . . . . . . . . . . . . . 1
Q-3: Inverted Right-Angled Triangle Pattern . . . . . . . . . . . . . . . . . . . 2
Q-4: Right-Angled Triangle with Odd Numbers . . . . . . . . . . . . . . . . . 2
Q-5: Count the Number of Digits . . . . . . . . . . . . . . . . . . . . . . . . . 3
Q-6: Fibonacci Series up to N Terms . . . . . . . . . . . . . . . . . . . . . . . 3
Q-7: Reverse a Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Q-8: Armstrong Number Check . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Q-1: Multiplication Table with Input Validation

Write a shell script that takes an integer number from the user and prints its table. If
the input is not a valid integer or is less than or equal to zero, display a proper message
to the user.
Listing 1: Q-1 Solution
1 # !/ bin / bash
2
3 read -p " Enter an integer : " num
4
5 # Check valid integer
6 if ! [[ " $num " =~ ^ -?[0 -9]+ $ ]]; then
7 echo " Error : Please enter a valid integer ."
8 exit 1
9 fi
10
11 # Check positive
12 if [ " $num " -le 0 ]; then
13 echo " Error : Number must be greater than 0. "
14 exit 1
15 fi
16
17 echo " Table of $num :"
18 for (( i =1; i <=10; i ++) )
19 do
20 echo " $num x $i = $ (( num * i) )"
21 done

Q-2: Right-Angled Triangle of Stars

Write a shell script to print a right-angled triangle of stars for a given number of rows.
1
Listing 2: Q-2 Solution
1 read -p " Enter number of rows : " rows
2
3 for (( i =1; i <= rows ; i ++) )
4 do
5 for (( j =1; j <= i; j ++) )
6 do
7 echo -n " *"
8 done
9 echo
10 done

Q-3: Inverted Right-Angled Triangle Pattern

Write a shell script that asks the number of rows from the user and prints the inverted
right-angled triangle pattern.
Listing 3: Q-3 Solution
1 # !/ bin / bash
2
3 read -p " Enter number of rows : " rows
4
5 for (( i= rows ; i >=1; i - -) )
6 do
7 for (( j =1; j <= i; j ++) )
8 do
9 echo -n " *"
10 done
11 echo
12 done

Q-4: Right-Angled Triangle with Odd Numbers

Write a shell script to print the right-angled triangle pattern using odd numbers. The
script must ask two things from the user  the number of rows and the starting odd
number. If the entered number is not an odd number, print a proper message to the user.
Listing 4: Q-4 Solution
1 # !/ bin / bash
2
3 read -p " Enter number of rows : " rows
4 read -p " Enter starting odd number : " num
5
6 # Check odd
7 if (( num % 2 == 0 )) ; then
8 echo " Error : Starting number must be ODD . "
9 exit 1
10 fi
11

2
12 current = $num
13
14 for (( i =1; i <= rows ; i ++) )
15 do
16 for (( j =1; j <= i; j ++) )
17 do
18 echo -n " $current "
19 current = $ (( current + 2) )
20 done
21 echo
22 done

Q-5: Count the Number of Digits

Write a shell script to print the number of digits in a number entered by the user.
Listing 5: Q-5 Solution
1 # !/ bin / bash
2
3 read -p " Enter a number : " num
4
5 # Remove negative sign if present
6 num = ${ num # -}
7
8 count =$ {# num }
9
10 echo " Number of digits : $count "

Q-6: Fibonacci Series up to N Terms

Write a shell script to print the Fibonacci series up to the given number of terms N.
Listing 6: Q-6 Solution
1 # !/ bin / bash
2
3 echo " Enter number of terms :"
4 read n
5
6 a =0
7 b =1
8
9 echo " Fibonacci Series :"
10 for (( i =0; i <n; i ++) )
11 do
12 echo -n " $a "
13 fn = $ (( a + b))
14 a= $b
15 b= $fn
16 done

3
Explanation

The Fibonacci series is a sequence of numbers where each number is the sum of the
two preceding numbers. The series starts with 0 and 1, and each subsequent number
is computed as fn = a + b.
Q-7: Reverse a Number

Write a shell script to print the reverse of a number entered by the user.
Listing 7: Q-7 Solution
1 # !/ bin / bash
2
3 echo " Enter a number :"
4 read num
5
6 temp = $num
7 rev =0
8
9 while [ $temp - gt 0 ]
10 do
11 digit = $ (( temp % 10) )
12 rev =$ (( rev * 10 + digit ) )
13 temp =$ (( temp / 10) )
14 done
15
16 echo " Reversed number is : $rev "

Explanation

The algorithm uses three key operations in each iteration:


ˆ digit=$((temp % 10))  selects the last digit of the number.
ˆ rev=$((rev * 10 + digit))  appends that digit to the reversed number.
ˆ temp=$((temp / 10))  removes the last digit from the original number.
Step-by-step trace for N = 1234 :

Initial values: $N = 1234, temp = 1234, rev = 0


Iteration 1:
digit = 1234 mod 10 = 4, rev = (0 × 10) + 4 = 4, temp = 1234/10 = 123
Iteration 2:
digit = 123 mod 10 = 3, rev = (4 × 10) + 3 = 43, temp = 123/10 = 12
Iteration 3:
digit = 12 mod 10 = 2, rev = (43 × 10) + 2 = 432, temp = 12/10 = 1
Iteration 4:
digit = 1 mod 10 = 1, rev = (432 × 10) + 1 = 4321, temp = 1/10 = 0
Final result: reverse = 4321

Q-8: Armstrong Number Check

Write a shell script to check whether a given number is an Armstrong number.


4
Listing 8: Q-8 Solution
1 # !/ bin / bash
2
3 echo " Enter a number :"
4 read num
5
6 temp = $num
7 sum =0
8
9 # Count digits
10 digits = ${# num }
11
12 while [ $temp - gt 0 ]
13 do
14 digit = $ (( temp % 10) )
15 sum =$ (( sum + digit ** digits ))
16 temp =$ (( temp / 10) )
17 done
18
19 if [ $sum -eq $num ]
20 then
21 echo " Armstrong Number "
22 else
23 echo " Not an Armstrong Number "
24 fi

Explanation

An Armstrong number (also called a narcissistic number) is a number that is equal


to the sum of its own digits, each raised to the power of the number of digits.
Example: N = 153
Number of digits = 3 Digits: 1, 5, 3
The algorithm uses three key operations in each iteration:
ˆ digit=$((temp % 10))  selects the last digit.
ˆ sum=$((sum + digit ** digits))  adds the digit raised to the power of the
total digit count.
ˆ temp=$((temp / 10))  removes the last digit.
Step-by-step trace for N = 153:
Step temp digit Calculation sum

1 153 3 3 = 27
3
27
2 15 5 5 = 125
3
152
3 1 1 1 =1 3
153
Since sum = 153 = N , the number is an Armstrong number.

You might also like