Monday, 6 August 2018

Sum Of Palindromic Numbers

QUESTION DESCRIPTION :

A number is called palindromic if its decimal representation is a palindrome. You are given a range, described by a pair of integers L and R. Find the sum of all the palindromic numbers lying in the range [L,R], inclusive of both the extremes.

Input Format :

The first line contains an integer T, denoting the number of test cases.
The description of T test cases follow:
  • Each line of test cases contains 2 numbers L and R denoting the lower and upper bound of the numbers for which you have to calculate the sum.
Output Format :

For each line of input, print a single integer which is equal to the sum of the palindromic numbers.

TEST CASE 1

INPUT

2
1 10
123 150

OUTPUT

sum is 45
sum is 272

EXPLANATION :

The question asks the programmer to send the sum of all the numbers that are palindromic in nature. So in order to solve this question we will check every number for the palindromic property and add it to the resulting sum if the number is a palindrome.

ALGORITHM :

Although there are quite a few algorithms that can be used to solve this, we will be using a simple one. Instead of checking each half of the number for similarity, we will be reversing the number and then checking if it is equal to the given number. If yes, then the number will be added to the resulting sum.

Steps:
  1. Take the input T for the number of test cases.
  2. Loop T times
    1. For each test case take L an R as input
  3. Start a loop from L till R and check palindrome constaint (done in the isPal function)
    1. For each number of the loop, calculate the reverse of it.
      • Reverse calculation:
        1. Take number and mod (%) it by 10 this gives you the first ones digit
        2. Multiply rev (Intially 0) by 10 and add the above digit to it
        3. Divide the original number by 10 to remove the previous digit.
        4. Goto step 3.1.1 until there is atleast a digit left in the number
    2. Check if the reverse is equal to original number
      • If yes : add to the sum (final result) 
  4. Redo Step 3.1 for all numbers in L and R
  5. Print the result and move to the next line
  6. Goto Step 3
  7. Exit 
Take care of the L and R constraints as they both have to be included in the calculation of the result.

SOLUTION : (Please use this as reference only)


Link to Code : Sum Of Palindromic Numbers
(Please Try To Refrain From Copying The Code)






Saturday, 4 August 2018

Discount

QUESTION DESCRIPTION


Mr. Phileas Fogg is a very perfect man. He saves money even on petty things. One day he heard a discount offer announced in a mall. He wants to purchase lot of items to save his money. The discount is given only when atleast two items are bought. Since each item has different discount prices, he finds it difficult to check the amount he has saved. So he approaches you to device an automated discount calculator to make him easy while billing.

I/O Constraints :  The input consists of two floating point values denoting price of item1 and item2. The third value denotes the discount value in %. The output consists of three floating point values denoting the total amount, discounted price and the amount saved.

Note: Correct the floating point decimals to 2 places.

TEST CASE 1

INPUT

20.50
45.40
10

OUTPUT

Total amount:$65.90
Discounted amount:$59.31
Saved amount:$6.59 

EXPLANATION :

The question simply requests you to accept 3 values (float) and perform trivial operations on them. This is a basic percentage question where you ought to find the discount value of all the items bought by the customer.

Note: The note at the end of the question simply wants the answer correct upto 2 decimal places i.e. 2.567 will result in 2.57 as the answer, this can be easily done using the printf function to print the values


ALGORITHM :

This is a simple algorithm:

Steps :
  •  Take 3 numbers as input (3 float numbers a,b,c)
  •  Print the addition of the first two numbers after the Total amount value
  •  Then calculate the discount value by multiplying the sum by discount/100 as the discount is in percentage.
  • Subtract the discount from the sum and print it as the Discounted amount 
  • Print the discount value as the Saved amount.
  • Exit


Note: Please mind the spaces, the : and the $ symbol

Note 2: Check this link  to understand how printf flags work. To print at most 2 decimal values use the %0.2f flag, here % stands for flag identifier, 0.2 states at max 2 decimal values while the rest will be rounded off and f is the flag for printing the value of float.


SOLUTION :  (Please use this as reference only)




Link to Code : Discount
(Please Try To Refrain From Copying The Code)

Thursday, 2 August 2018

Ascii Name

QUESTION DESCRIPTION


Write a program which reads your name from the keyboard and outputs a list of ASCII codes, which represent your name

TEST CASE 1

INPUT

SRMUNIVERSITY

OUTPUT

The ASCII values of the string are:
83 82 77 85 78 86 69 82 83 73 84 89

EXPLANATION :

This is a simple question expecting you to convert the characters to their respective ASCII representation. That can be easily done by parsing each character to their respective integer representation.


ALGORITHM :

This is a simple algorithm:

Steps :
  • Create a char array to receive the string as input
  • Take the string as input
  • Print the first mandatory line "The ASCII values of the string are:
  • Use a for loop till you find the '\0' character (Why \0)
  • For each character in the string:
    •  Convert it to its integer version and print it with a Space " ".
  • When the '\0' character is found Exit the code.

Remember the spacing in the output and the end of line


SOLUTION :  (Please use this as reference only)


Link to Code : Ascii Name
(Please Try To Refrain From Copying The Code)