Showing posts with label Python Loops. Show all posts
Showing posts with label Python Loops. Show all posts

Sunday, 10 August 2025

LCM of two numbers without built-in functions

LCM of Two Numbers

LCM (Least Common Multiple) of two given numbers without using built-in math functions or user-defined functions.

# LCM of two numbers without use of built-in functions.

# Input two numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

# Store the maximum of num1 and num2
if num1 > num2:
    max_num = num1
else:
    max_num = num2

# Find LCM using loop
while True:
    if (max_num % num1 == 0) and (max_num % num2 == 0):
        lcm = max_num
        break
    max_num += 1

# Display the result
print("LCM of", num1, "and", num2, "is", lcm)

Output of program

Enter first number: 5

Enter second number: 7

LCM of 5 and 7 is 35


Thursday, 11 July 2024

Python program to generate 100 random numbers between 1 to 100

Random Number Generation in Python

Following python program will generate 100 random numbers between 1 to 100.

import random

# Define a blank list to store the random numbers
random_numbers = []

# Loop 100 times to generate 100 random numbers
for _ in range(100):
  # Generate a random integer between 1 and 100 (inclusive)
  number = random.randint(1, 100)
# Append the random number to the list
  random_numbers.append(number)

# Print the list of random numbers
print("Generated 100 random numbers in range 1 to 100:")
print(random_numbers)

Output

Generated 100 random numbers in range 1 to 100:

[31, 48, 64, 37, 69, 53, 75, 24, 34, 19, 5, 2, 44, 12, 51, 96, 49, 92, 54, 99, 78, 68, 87, 74, 92, 53, 68, 41, 76, 13, 59, 89, 52, 99, 17, 41, 37, 43, 47, 32, 13, 10, 91, 87, 46, 3, 29, 46, 11, 71, 74, 26, 21, 63, 67, 80, 10, 33, 4, 4, 30, 89, 63, 93, 90, 24, 2, 46, 73, 91, 88, 94, 5, 93, 18, 5, 31, 93, 66, 54, 28, 8, 11, 39, 17, 96, 91, 60, 38, 4, 77, 98, 7, 45, 48, 64, 51, 40, 38, 34]

* * *

< Back to Home >


Tuesday, 17 August 2021

Program to count total number of characters in a given String

Following Python program will count total number of characters in a given string. 

Expected Output: If user enters string = Hello Students, output should be

Total Number of Characters in a String = 14

Program Code:

# Python program to count total number of characters in a given String
 
str1 = input("Enter your string: ")
count = 0  #initialize counter to 0
 
for i in str1:
    count = count + 1
 
print("Total Number of Characters in a String = ", count)

Output of Program:

Enter your string: Hello Students Total Number of Characters in a String = 14


* * * * *

< Back to Python Loops >