Uploaded by rugved.v

Programs

advertisement
1. Add all digits and find if even or not program – Explain
Number=int(input());
If(number%2)==0:
Print (“even”)
Else
Print(“odd”)
2. array, Searching an unsorted array
list.sort() , list.sort(reverse=TRUE)
3. Detection of a number is binary or not
B={‘0’,’1’}
StringA=’0101010101’
T=set(stringA)
If b==t or t=={‘0’} or t=={‘1’}
Print (“binary”
Else:
Print(“nonbinary”)
Stringb=’012320102’
u=Set(stringb)
if b==u or b=={‘0’} or b=={1’}
print(“binary”)
else:
print(“nonbinary”)
4. to search any duplicate element in sorted array.
//Initialize an array
Use two loops :
Outer loop will iterate from 0 to length of array
Inner loop will be used to compare with selected element
If match is found, duplicate element found
Arr=[1,34,2,4,3,4];
For I in range(0,len(arr)):
For j in range(i+1,len(arr)):
If(arr[i]==arr[j]):
Print(arr[j]);
5. WAP to find the length of last word in a string.
//As all the words are separated by spaces
Remove the spaces by using spilt() and store it in list
Go to the last element of list and count the lenght
6. WAP to reverse a number.
Initialize a number
Consider revno=0
Get the remainder of number
Revno=multiply rev no * 10 and add remainder
Number=number/10
7. WAP which tells the frequency of each digit in a number.
Initialize a no
Cnt =0
Take no you want to check x=5
While<0
Extract one digits one by one and compare with 5
Match is found then increment count++
8. WAP which tells me whether the number I entered is binary or not.
Set operator stores only unique elements
It will store {‘0,1,2,3’}
Take a string and apply set function to it
A=’1020302’
T=set(a)
Create another set having elements 0,1
B={‘0’,’1’}
If both the sets A=B or 1 or 0 are equal the binary
9. Length of last word in a string
Idea here is … words are separated by spaces
Convert string to list
Use split function to separate words
Access last word of list
Use while loop to count the len of string
10. Remove duplicates from array
1)Using sets
2) while >0
Extract each word and compare with element in temp variable and store it in x
And then print x
11. Find if the string is the rotation of another string
String 1 , string 2 : check length if they are equal
String1=String1 + string 1
Check index of string 2 in string 1
If yes then rotation
12. Given an integer array, how will you print only odd numbers in reverse order?
13. Give a use case of finally block
Try , catch , finally
14. Prime number program in Java / Python / C++
Idea
Num > 1
Iterate from 2 to num/2
If no is divisible by any no between 2 to n/2 it is not prime.
15. Recursion in Python / Java / C++
Function calling itself for example factorial of a number
16. String length program without using string fucntions
Download