Posts

Showing posts with the label python code

Program to check whether a given number is prime or not using a while loop in Python:

 # While loop in Python - Checking if a number is prime num = int(input("Enter a number: ")) is_prime = True i = 2 while i <= num//2:     if num % i == 0:         is_prime = False         break     i += 1 if is_prime:     print(num, "is a prime number") else:     print(num, "is not a prime number")

Check if a number is even or odd in python

Check if a number is even or odd This program takes a number (num) and checks if it is even or odd using the modulo operator (%). If the remainder of num divided by 2 is 0, then it is even. Otherwise, it is odd. num = 7 if num % 2 == 0:     print(num, "is even") else:     print(num, "is odd")

Addition of Two numbers in Python

num1 = 5 num2 = 7 sum = num1 + num2 print("The sum of", num1, "and", num2, "is", sum) This program adds two numbers (num1 and num2) and stores the result in a variable called sum. The print statement is used to display the result to the console.

Hello World Python Code with Explanation

here's an example of a "Hello, World!" program in Python:pythonCopy code print("Hello, World!") This program simply prints the message "Hello, World!" to the console. This program is a simple example of a Python program. The print statement is used to output text to the console. In this case, the text being output is "Hello, World!", which is enclosed in quotation marks. When the program is executed, the text "Hello, World!" will be printed to the console. The program does not take any input or perform any calculations - it simply prints the message and then exits. This program is often used as a simple starting point for learning how to write Python code, as it demonstrates the basic syntax of the language and how to output text to the console.