diff --git a/HackerRank1 b/HackerRank1 new file mode 100644 index 0000000..daf4adc --- /dev/null +++ b/HackerRank1 @@ -0,0 +1,6 @@ +if __name__ == '__main__': + a = int(input()) + b = int(input()) + print(a+b) + print(a-b) + print(a*b) \ No newline at end of file diff --git a/HackerRank2 b/HackerRank2 new file mode 100644 index 0000000..9a5528d --- /dev/null +++ b/HackerRank2 @@ -0,0 +1,9 @@ +arr=[2,3,6,6,5] +arr.sort() +arr.reverse() +for num in arr: + if num < arr[0]: + print(num) + break + + \ No newline at end of file diff --git a/HackerRank3 b/HackerRank3 new file mode 100644 index 0000000..de3a275 --- /dev/null +++ b/HackerRank3 @@ -0,0 +1,5 @@ +n = int(input()) +string="" +for i in range(1,n+1): + string=string+str(i) +print(string) \ No newline at end of file diff --git a/HackerRank4 b/HackerRank4 new file mode 100644 index 0000000..dcd6929 --- /dev/null +++ b/HackerRank4 @@ -0,0 +1,9 @@ +k = int(input()) +student_marks = {} +for _ in range(k): + name, *line = input().split() + scores = list(map(float, line)) + student_marks[name] = scores +query_name = input() +Average=sum(student_marks[query_name])/3 +print(f"{Average:.2f}") \ No newline at end of file diff --git a/Question1.py b/Question1.py new file mode 100644 index 0000000..e6d45c1 --- /dev/null +++ b/Question1.py @@ -0,0 +1,5 @@ +# Write a python code that prints numbers from 1 to 10 on the screen. + +for i in range(1, 11): #for i in means we loop through those numbers one by one. + #range(1, 11) generates numbers starting from 1 up to, but not including, 11 + print(i) \ No newline at end of file diff --git a/Question10.py b/Question10.py new file mode 100644 index 0000000..2b4342f --- /dev/null +++ b/Question10.py @@ -0,0 +1,17 @@ +#Write the code that calculates the person's weight index and returns the result as underweight, overweight or overweight according to the index value. (You can look on the internet for the weight index calculation) To do this, ask the user for their weight and height measurements. weight index If it is below 25, it is weak, Between 25-30 is normal, If you are over 30-40, you are overweight. If you are over 40, you are overweight +weight = float(input("Enter your weight in kilograms: ")) #float(input(...)) function is used to allow decimal inputs for weight and height +height = float(input("Enter your height in meters; ")) + +bmi = weight /(height ** 2) + +if bmi < 18.5: + category = "Underweight" +elif 18.5 <= bmi <25: + category = "Normal weight" +elif 25 <= bmi <30: + category = "Overweight" +else: + category = "Obesity" + +print(f"Your BMI is: {bmi:.2f}") #:.2f formatting in the print statement rounds the BMI to two decimal places for readability. +print(f"You are classified as: {category}") diff --git a/Question11.py b/Question11.py new file mode 100644 index 0000000..c6282b8 --- /dev/null +++ b/Question11.py @@ -0,0 +1,16 @@ +#How to write a Python program that finds the largest of three numbers entered by a user? +num1 = float(input("Enter the first number: ")) #Use float() so it works with both integers and decimal numbers +num2 = float(input("Enter the second number: ")) +num3 = float(input("Enter the third number: ")) + +if num1 >= num2 and num1 >= num3: + largest = num1 +elif num2 >= num1 and num2 >= num3: + largest = num2 +else: + largest = num3 + +print("The largest number is: ", largest) + + +#largest = max(num1, num2, num3) Python also has a built-in function called max() \ No newline at end of file diff --git a/Question12.py b/Question12.py new file mode 100644 index 0000000..b92f5c9 --- /dev/null +++ b/Question12.py @@ -0,0 +1,15 @@ +#Get Midterm and Final grades from a student for any course. The sum of 40% of the midterm exam grade and 60% of the final grade will give the year-end average. +#If the average is below 50, "FAILED" will appear on the screen, and if it is 50 or above, "SUCCESSFUL" will be displayed on the screen. This printing process is 4 lessons. and the lessons will be written one after the other. +for course in range(1,5): # Repeat the process for 4 courses + print("Course", course) + + midterm = float(input("Enter the midterm grade: ")) + final = float(input("Enter the final grade: ")) + + average = (midterm * 0.4) + (final * 0.6) #Calculate the average + + if average >= 50: + print("Result: Succesful") + else: + print("Result: Failed") + \ No newline at end of file diff --git a/Question2.py b/Question2.py new file mode 100644 index 0000000..30fe530 --- /dev/null +++ b/Question2.py @@ -0,0 +1,17 @@ +#Take a number input from the user and write a Python program that prints even numbers up to this number on the screen. Do this first with 'for' and then with 'while' loops. + +# Ask the user for a number +num = int(input("Enter a number: ")) + +for i in range(1, num + 1): #loops from 1 up to the number inclusive. + if i % 2 == 0: # checks if the number is divisible by 2 + print(i) + + +num = int(input("Enter a number: ")) + +i = 1 # starts counting from 1 +while i <= num: + if i % 2 == 0: # checks if it's even + print(i) + i += 1 # increases the counter each time \ No newline at end of file diff --git a/Question3.py b/Question3.py new file mode 100644 index 0000000..26b6cce --- /dev/null +++ b/Question3.py @@ -0,0 +1,6 @@ +#Write a Python code that receives a start and end value from the user and prints all the numbers between these values ​​(including the end value) on the screen. +start = int(input("Enter the start value: ")) +end = int(input("Enter the end value: ")) # int(..) converts the input (a string) to a number + +for i in range(start, end + 1): # range() stops before the end number, so +1 includes the last number + print(i) \ No newline at end of file diff --git a/Question4.py b/Question4.py new file mode 100644 index 0000000..1366b16 --- /dev/null +++ b/Question4.py @@ -0,0 +1,6 @@ +#Get a number from the user and write a Python code that prints whether this number is odd or even. +num = int(input("Enter a number: ")) +if num % 2 == 0: #Check if the number is even + print("The number is even.") +else: + print("The number is odd.") diff --git a/Question5.py b/Question5.py new file mode 100644 index 0000000..2da4b7e --- /dev/null +++ b/Question5.py @@ -0,0 +1,7 @@ +#Write a Python program that takes a positive integer input from the user and calculates its factorial. Factorial is the product of all positive integers between a number itself and 1. +#For example: if the user entered 5, the program should give the following output: Enter a number from the user: 5 Factorial: 120 +num = int(input("Enter a number: ")) #int(..) converts the input to an integer +factorial = 1 #start with 1 because multiplying by 0 would make everything 0 +for i in range(1, num + 1): #Use a for loop to calculate factorial + factorial *= i #each loop multiplies the current factorial by i + print("Factorial:", factorial) \ No newline at end of file diff --git a/Question6.py b/Question6.py new file mode 100644 index 0000000..330a5e6 --- /dev/null +++ b/Question6.py @@ -0,0 +1,16 @@ +#Write a Python code that receives a number from the user and checks whether this number is prime. +#A prime number is a number greater than 1 that is only divisible by 1 and itself. +num = int (input("Enter a number: ")) +is_prime = True #assume it is prime until we find a reason to say otherwise +if num <= 1: + is_prime = False #1 and any number below it is not prime +else: + for i in range(2, int(num ** 0.5) + 1): #for i in range(2, num): check all the numbers between 2 and (num - 1) to see if any of them divide your number evenly + #num ** 0.5 = square root of the number + if num % i == 0: + is_prime = False + break +if is_prime: + print(num, "is a prime number.") +else: + print(num, "is not a prime number.") diff --git a/Question7.py b/Question7.py new file mode 100644 index 0000000..874605e --- /dev/null +++ b/Question7.py @@ -0,0 +1,9 @@ +#How to create a loop that calculates the Fibonacci sequence and returns the result as a list containing numbers up to a certain limit? +limit = int(input("Enter a limit: ")) +fib_list = [0, 1] #start with the first two Fibonacci numbers +while True: #we use a while loop so we can keep generating numbers until we decide to stop. + next_number = fib_list[-1] + fib_list[-2] #this adds the last two numbers in the list to get the next one. + if next_number > limit: + break #stop the loop if next number is over the limit + fib_list.append(next_number) #add it to the list +print("Fibbonacci sequence up to", limit, ":", fib_list) \ No newline at end of file diff --git a/Question8.py b/Question8.py new file mode 100644 index 0000000..16bf199 --- /dev/null +++ b/Question8.py @@ -0,0 +1,4 @@ +#Write a Python code that takes a word from the user and prints the reverse of this word on the screen. +word = input("Enter a word: ") +reversed_word = word[::-1] #reverse the word using slicing, word[::-1] gives you the word in reverse +print("Reversed word:", reversed_word) \ No newline at end of file diff --git a/Question9.py b/Question9.py new file mode 100644 index 0000000..2df6c6d --- /dev/null +++ b/Question9.py @@ -0,0 +1,8 @@ +#How to create a combination of loop and conditional statement that takes a word input from the user and checks whether that word is a palindrome (the same when read backwards)? +#A palindrome is a word (or phrase) that reads the same forwards and backwards. For example, the word "level" is a palindrome, but "hello" is not. +word = input("Enter a word: ") +reversed_word = word[::-1] +if word == reversed_word: + print(f"{word} is a palindrome!") #f-string, the part inside {} will be evaluated and inserted into the string +else: + print(f"{word} is not a palindrome.") \ No newline at end of file