Delete duplicate elements from array

Q24. Write a code to delete duplicate elements from array

Delete Duplicate Elements :- In this, we will perform the delete operation in the array and after performing the delete operation, the array must contain only unique integer values.
For Example :-
Input = 5,7,8,2,4,5,6,5,4,4
Output = 5,7,8,2,4,6
So as we can see that the input consists of 10 elements ( 5,7,8,2,4,5,6,5,4,4) which contain some duplicate elements. So we have to remove duplicate elements from the array, and after performing the delete operation the output will be 5,7,8,2,4,6

Algorithm For Delete Duplicate Elements

START
Step 1 -> Input the number of elements of the array.
Step 2 -> Input the array elements.
Step 3 -> Repeat from i = 1 to n
– if (arr[i] != arr[i+1])
– temp[j++] = arr[i]
– temp[j++] = arr[n-1]
Step 4 -> Repeat from i = 1 to j
– arr[i] = temp[i]
Step 5 -> return j.
STOP

Code For Delete Duplicate Elements

Output

Array elements after deleting duplicates :5,7,8,2,4,6,

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

Write a code to find LCM of two numbers

Q23. Write a code to find LCM of two numbers.

LCM :- LCM (Least Common Multiple) of numbers is defined as the smallest possible multiple of two or more numbers.

For Example :-
Input = 6 and 5
Output = 30 is the LCM.
As we can see 30 is the smallest possible multiple of 6 and 5 therefore, 30 is the least common multiple (LCM) of two numbers 6 and 5

Algorithm For LCM

START
Step 1 → Initialize num1 and num2 with positive integers
Step 2 → Store maximum of num1 & num2 to max
Step 3 → Check if max is divisible by num1 and num2
Step 4 → If divisible, Display max as LCM
Step 5 → If not divisible then increase max by 1 and then goto step 3
STOP

Code For LCM

Output

LCM of 5 and 6 is 30

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

string is substring of given string or not.

Q22.Write a code to check whether the string is substring of given string or not.

String :- The string is defined as an array of characters. The difference between a character array and a string is the string ending with a special character ‘\0’.Given two strings S1 and S2, The task is to find if S1 is a substring of S2. If yes, return the index of the first occurrence, else return -1.

For Example :- Input : s1 = “Alfa”, s2 = “Alfatechlab”
Output : 0

check whether substring in a string Algorithm 

START

STOP

check whether substring in a string code

Output

Substring Found Successfully!

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

check whether a character is vowel or consonant

Q21. Write a code to check whether a character is vowel or consonant

Vowel :- In English, five alphabets A, E, I, O, and U are called as Vowels.
Consonant :- In English, all alphabets other than vowels are Consonant.
For Example :-
Input = E
Output = E is Vowel.
So as we can see that E is alphabets which is Vowel. So, the output is “E is Vowel”.

check whether a character is vowel or consonant Algorithm 

START
Step 1 – Input the alphabets.
Step 2 – Check if the alphabets is (a, e, i, o, u) if alphabet is among these then it is vowel.
Step 3 – If the alphabets is vowel than print Vowel otherwise print Consonant.
STOP

check whether a character is vowel or consonant code

Output

M is a consonant.

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

Write a code to check armstrong number

Q20. Write a code to check armstrong number

Armstrong Number :- A Armstrong Number is a number that is equal to sum of cubes of each digits.
For Example :- 370
So as we can see that the sum of cubes of each digit (3^3=27, 7^3=343, 0^3=0 => 27+343+0=370) is equal to it’s number. So 370 is an Armstrong number.

Algorithm for Armstrong Number

START
step 1 : read number
step 2 : set sum=0 and duplicate=number
step 3 : reminder=number%10
step 4 : sum=sum+(reminder*reminder*reminder)
step 5 : number=number/10
step 6 : repeat steps 4 to 6 until number > 0
step 7 : if sum = duplicate
step 8 : display number is armstrong
step 9 : else
step 10 : display number is not armstrong
STOP

Code  for Armstrong Number

Output

The number 370 is an Armstrong number.

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