Showing posts with label Logical programs. Show all posts
Showing posts with label Logical programs. Show all posts

Sunday, August 24, 2014

String palindrome

Write a program to to calculate string palindrome.

Monday, August 4, 2014

Count number of bits are set to 1

Write a program to count number of bits are set to 1 in an integer.

Calculate daily expenditure if monthly expenditure is given

Write a program to calculate daily expenditure if monthly expenditure is given using loop.

Add between any two numbers using loop

Write a program to add between any two numbers using loop.

Palendrome number

Write a program to check given number is palendrome or not.

Armstrong number

Write a program to check given number is armstrong or not.

Prime factors of a number

Write a program in C language to calculate the prime factors of a number.

Monday, January 13, 2014

Java Calculator

Create a java based GUI calculator as shown below ...
Implement only the following functionalities ...
  • Addition
  • Difference
  • Multiplication
  • Division
  • CLR button
  • Back Space button

Friday, January 3, 2014

Snake Game

Snake is an old computer game. There are many versions, but all involve maneuvering a "snake" around the screen, trying to avoid running the snake into itself or an obstacle.
Simulate the game through the application, though it is very simplified version.

Consider a 10 X 10 board/matrix (refer the following diagram, where every integer value is indicating the cell number). The snake move Suppose the snake starts to move from cell number 15. The snake can move to 4 possible directions: north (N), east (E), south (S) and west (W). If the snake moves to N, then it actually moves to cell number 5. If it moves to S, then it actually moves to cell number 25. Similarly if the snake moves to E or W, which means snake is moving to cell number 26 or 24 respectively. So, with every move, the snake covers a cell and consequentially it leaves a trail along the path.

Now consider a sequence of moves, NEEESSWWNNN. That means the snake first moves from its initial position (cell number 15) to cell number 5, then takes a right turn and covers the cell number 6, 7 and 8. After that it takes a down turn and covers 18 and 28, before it takes a left turn and covers cell number 27 and 26. At last it takes again upward turn and covers cell number 16 first and meets the same trail, i.e., cell number 6. It means the snake has run on to itself. In this case, the snake made 9 successful moves and on the 10th move it runs into itself.
Again consider the sequence of moves, SSEEEEEEEE. That means from starting position (cell number 15, the snake moves downward till cell 35 and then takes a right turn to cover cell number 36 to 40, before it runs out of board. Before running out of board it made 7 moves.



Again consider sequence of moves: WWSSSEEEESSSS. That means the snake first made a left run from start position (cell number 15) and covered cell number14 and 13 and then a downward move and continued till cell number 43. Then it took a right turn to go till cell number 47 and again moved downwards and continued till cell number 87. This time the snake neither did run onto itself nor run out of board. It completed all the 13 moves successfully.


Description: Complete the given method to return the number of moves as well as the result of the moves (i.e., “SUCCESS” OR “RANOUTOFBOARD” OR “RANONTOITSELF”) for a given sequence of moves in string format.

Input: Start position as integer value and sequence of moves as string value

Output: Number of moves (made successfully or made before it ran out of board or before it ran onto itself) along with the result of the moves (i.e., “SUCCESS” OR “RANOUTOFBOARD” OR “RANONTOITSELF”).

UTC
Input
Output
UTC04_01
53 and SSSSEEENNEEEEESS
{13, RANOUTOFBOARD }
UTC04_02
3 and SSSSEESSSSWNNNNNNNN
{14, RANONTOITSELF}
UTC04_03
15 and WWSSSEEEESSSS
{13, SUCCESS}

Monday, December 30, 2013

Sort Names by First Name

Names are stored in the pattern last name first pattern [lastName, firstName].
For example given the name “Sharma, Rakesh”, “Rakesh” is first name and “Sharma” is last name.
Complete the method provided to return the collection of strings sorted based on their first name.

Method:
public static String[ ] SortByFirstName(string[ ] names)
{ 
   // code here 
}
Sample Input & Output: 
UTC
Sample Input
Sample Output
01
“Sharma, Rakesh”, “Patil, Parthiv”, “Gowda, Anil”,
“Prasad, Vishnu”, “Khan, Amir”

“Khan, Amir”, “Gowda, Anil”, “Patil, Parthiv”, “Sharma, Rakesh”, “Prasad, Vishnu”

02
“Krishna, Gopal”, “Page,
Larry”, “King, Gavin”,
“Swamy, Krishna”

“King, Gavin” , “Krishna, Gopal”, “Swamy, Krish-na”, “Page, Larry”


Consecutive Characters count

Complete the method to print the consecutive characters and the number of times appearing in a String.
public static void PrintConsecutiveCharacters(string input)
{ 
   //write code
}
Input & Output: 
UTC
Sample Input
Sample Output
01
“I saw a CD player and a modem in ccd”

CD 2
DE 1 
02
“Student List do not exist in system”


ST 4
DE 1
NO 1

Triplets

Triplets are a set of three similar things.
Complete the function to print all the triplets such that A+B = C 
public static void PrintTriplets(int[ ] data) 
{ 
    //write code here
}
Input & Output
UTC
Sample Input
Sample Output
01
data = {2,3,4,5,7}

<2,3,5>
<2,5,7>
<3,4,7>
02
array = {1,2,3,4,5,7,9}

<1,2,3>
<1,3,4>
<1,4,5>
<2,3,5>
<2,5,7>
<3,4,7>
<4,5,9>

Friday, December 27, 2013

Sum of consecutive elements in a subsequence

Complete the method to find the minimal length of the sub sequence of consecutive elements of the sequence, sum of which is greater or equal to the specified number.

Example:

 5   1   3   5   10   7   4   9   2   8 

If the specified number is 15 in the sequence
5 + 1 + 3 + 5 + 10 > 15 (length is 5)
1+3+5+10 >= 15 (length is 4)
3+5+10 >= 15 (length is 3)
5 + 10 >= 15 (length is 2)
10 + 7 >=15(length is 2)
There is no element in the array whose value is >= 15 and hence the minimal length is 2
public static int FindMinimalLength(int[] array, int value)
{
        //write code here
}
UTC Sample Input Sample Output
01  array = {5,1,3,5,10,7,4,9,2,8}
value = 15
2
02 array = {1,2,3,4,5}
value = 11
3

Wednesday, November 27, 2013

Dot Net: Collections

Count the frequency of different words in a sentence.

Objective:
To choose appropriate collection class for storing key/value pair.

Problem Statement:
Write a program to create a list of words present in a sentence passed from command line. The list also contains count of the appearance of each word in that sentence. The program has to display the words in sorted order and its frequency.

If the input is: “DotNet is technology and DotNet is interoperable and DotNet is simple”.


Output should be:  [and = 2, DotNet =3, interoperable = 1, is = 3, simple =1, technology=1]

Monday, November 25, 2013

Dot Net: Matrix Arrangement

Write a function to return an array where the midst position contains the smallest value followed by the next smallest value on the right of the midst position, followed by the next smallest value to the left of the midst position and the rest of numbers continue in this format.

Function signature:
public static int[ ] ArrangeElements(int[,] inputArray) 
{ 
   // write the code here
}



Saturday, November 23, 2013

Java decimal numbers to comma-punctuated numbers Conversion

Write a method to convert strings containing decimal numbers into comma-punctuated numbers, with a comma after every third digit from the right.For example, given the string 1234567 , the method should return 1,234,567

Java HashSet Example

Create a class Employee with class members firstName(String), lastName(String) and salary(int).Do the following:

  • Create a HashSet of Employees.
  • Print the employee details.
  • Sort the employees in HashSet by salary and print the result.
  • Print the employee with the least salary.
  • Print the employee with the highest salary.
  • Search for an employee.

Java String to Character array ( toCharArray() method )

Write a JAVA Program which accepts text of words seperated by commas and gives the output of words printed on seperated lines with commas removed.

Sample Input & Output:

Enter the string to parse
Monday,Tuesday,Wednesday,Thursday,Friday,Sunday

Monday
Tuesday
Wednesday
Thursday
Friday
Sunday

Monday, November 18, 2013

Java HshMap using StringTokenizer

Write a program that gets an array of words and does the following:

  • Prints each word and it's frequency
  • Prints only the words
  • Prints only the frequency of all words

Here's a sample run assuming the array of words as {i , came , i , saw , i , conquered}
came : 1
conquered : 1
i : 3
saw : 1
[came, conquered, i, saw]
[1, 1, 3, 1]

Note: Print in the same order as shown above.

Java HashMap Example (Phone Book)

Implement a PhoneBook using HashMap.
Hint: Create the following classes:

  1. Person Class contains firstName and surName.
  2. PhoneNumber Class contains areaCode and number.
  3. BookEntry contains Person object and PhoneNumber object.
  4. PhoneBook contains a HashMap<Person,BookEntry>.
  5. TestPhoneBook Class contains main() to do the following:

The user should be prompted with the following menu

  • Enter 1 to add a new phonebook entry
  • Enter 2 to find the number for a name.
  • Enter 3 to find name for a number
  • Enter 9 to quit