Computer Science, asked by atrs7391, 6 hours ago

WAP to input a string, then output positive integer or throw exception depending on the following:

"-6" => 6 (Make positive then convert to integer)
"-6.3" => 6 (Make positive then convert to nearest integer)
"6" => 6 (Convert to integer)
"6.9" => 7 (Convert to nearest integer)
"0" => throw appropriate exception
"abc~!`~;'swhwd" (Anything non numeric) => throw appropriate exception

* If the converted integer becomes 0 from 0.1 or something, throw exception.

Language: Any (Python preferred)

Answers

Answered by anindyaadhikari13
12

\textsf{\large{\underline{Solution}:}}

The given problem is solved using language - Python.

x=input('Enter any string: ')

try:

   y=round(abs(eval(x)))

   if y==0:

       raise Exception

   print('Result:',y)  

except:

   print('Exception Raised.')

\textsf{\large{\underline{Sample I/O}:}}

#1

Enter any string with numbers: -6

Result: 6

———————————————————————

#2

Enter any string: -6.3

Result: 6

———————————————————————

#3

Enter any string: 6

Result: 6

———————————————————————

#4

Enter any string: 6.9

Result: 7

———————————————————————

#5

Enter any string: 0

Exception Raised.

———————————————————————

#6

Enter any string: good

Exception Raised.

———————————————————————

#7

Enter any string: 0.2

Exception Raised.

Attachments:
Similar questions