Write a code to find sum of array elements

Q16.Write a code to find sum of array elements

Array :- An array is a collection of data items, all of which are of the same type, using a common name.
For Example :-
input : arr[]={ 2, 5, 3, 6, 4 }
output : 20

So as we can see that the sum of elements in arr[] = { 2, 5, 3, 6, 4 } is 20 so the output is 20.

Algorithm for Array Sum

START
Step 1 : Take an array Arr and define its values
Step 2 : Loop for each value of Arr
Step 3 : Add each element to ‘sum’ variable
Step 4 : After the loop finishes, display ‘sum’
STOP

Code for Array Sum

Output

Sum of array is = 20

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

Write a code to check whether the two strings are anagram of each other or not.

Q15. Write a code to check whether the two strings are anagram of each other or not.

Anagram :- When two strings contain the same characters irrespective of the order of occurrence then such strings are known as Anagram.
For Example :-
String1 = eleven plus two
String2 = twelve plus one
So as we can see that both strings have same characters, only the order of characters is different so this is an Anagram

Algorithm for Anagram

START
Step->1 : Define two strings.
Step->2 : Check for their lengths. If the lengths are not equal, then strings are not an anagram.
Step->3 : Else, convert the string to lower case character to make the comparison easy.
Step->4 : Some language allows the strings to provide inbuilt function for sorting of string. If not then convert them to character array for sorting.
Step->5 : Sort the array.
Step->6 : Finally, check for the equality of content.
STOP

Code for Anagram

Output

The two strings are anagram of each other

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

Write a code to check whether the given number is palindrome or not

Q14. Write a code to check whether the given number is palindrome or not

Palindrome Number:- A Palindrome Number is a number that remains same when its digits are reversed or we can say that the Palindrome number is a number that is symmetric.

For Example:- 121
So as we can see that the number 121 remains the same when it is reversed so it is a palindrome number.

Algorithm for Palindrome Number

START
Step 1 : Get the number to check for palindrome
Step 2 : Hold the number in temporary variable
Step 3 : Reverse the number
Step 4 : Compare the temporary number with reversed number
Step 5 : If both numbers are same, print “palindrome number”
Step 6 : Else print “not palindrome number”
STOP

Code for Palindrome Number

Output

The number 121 is a Palindrome number

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

Write a code to find length of a string

Q13. Write a code to find length of a string

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’.

For Eg :- If the string is “alfatechlab”, the output is 11.

Algorithm for find length of string

START
Step 1:Length = 0
Step 2: Repeat step 3 while S1 [Length] ≠ NULL
Step 3: Length = Length + 1
Step 4: Return Length
STOP

Code for find length of string

Output

Length of the string =11

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

Write a code to sort the characters in a given string

Q12. Write an code and algorithm to sort string of characters in dictionary order

Solution : Given a string contain lowercase character from a to z and our task is to sort them in dictionary order.

String :- A string is a sequence of characters terminated with a null character \0 .
For example: char lfc[] = “alfatechlab”;

Algorithm for sort string of characters in dictionary order

//Algorithm To Sort String Of Characters
START
Step 1 -> Take string as input str.
Step 2 -> Store the count of each character in one array ARR, index 0 corresponding to a and so on.
Step 3 -> Now repeat loop I till the end of array ARR
Step 4 -> Repeat inner loop on the basis of value of ARR[I]
Step 5 -> Print (‘a’ + I)
Step 6 -> End Inner loop
Step 7 -> End Loop
STOP

Code  for sort string of characters in dictionary order

Output

aaabcefhllt

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