Hacker rank solutions
1)Given an integer, , perform the following conditional actions:
If n is odd, print Weird
If n is even and in the inclusive range of to , print Not Weird
If n is even and in the inclusive range of to , print Weird
If n is even and greater than , print Not Weird
Input Format
A single line containing a positive integer,n.
Constraints
l<=n<=100
Output Format
Print Weird if the number is weird. Otherwise, print Not Weird.
Sample Input 0
3
Sample Output 0
Weird
Solution:
import math
import os
import random
import re
import sys
if __name__ == '__main__':
n = int(input().strip())
if n % 2 != 0:
print("Weird")
elif n % 2 == 0 and 2 <= n <= 5:
print("Not Weird")
elif n % 2 == 0 and 6 <= n <= 20:
print("Weird")
elif n % 2 == 0 and n > 20:
print("Not Weird")
2)The provided code stub reads two integers from STDIN, and . Add code to print
three lines where:
The first line contains the sum of the two numbers.
The second line contains the difference of the two numbers (first - second).
The third line contains the product of the two numbers.
Example
a = 3, b = 5
if __name__ == '__main__':
a = int(input().strip())
b = int(input().strip())
# Calculate the sum, difference, and product
sum_ab = a + b
diff_ab = a - b
prod_ab = a * b
# Print the results
print(sum_ab)
print(diff_ab)
print(prod_ab)
3)The provided code stub reads two integers, and , from STDIN.
Add logic to print two lines. The first line should contain the result of integer
division, a//b
The second line should contain the result of float division, a/b.
if __name__ == '__main__':
a = int(input())
b = int(input())
# Calculate integer division and float division
int_division_result = a // b
float_division_result = a / b
# Print the results
print(int_division_result)
print(float_division_result)
4) The provided code stub reads and integer, , from STDIN. For all non-negative
integers i<n , print i square.
Example: n = 3
The list of non-negative integers that are less than n = 3 is [0,1,2] . Print the
square of each number on a separate line
if __name__ == '__main__':
n = int(input())
# Iterate through non-negative integers less than n
for i in range(n):
# Print the square of each number
print(i ** 2)