## Display a pyramid

prompt the user to enter number of line (1 – 15) prints a number pyramid number = eval(input(“Enter the number of lines(only 1 – 15): “)) if (number >= 1) and (number <= 15): for i in range(number): nToPrint = i + 1 spaces = number – nToPrint for j in range(spaces): print(” “,end=””) n … Read moreDisplay a pyramid

## Find the factors of an integer

reads an integer and displays all its smallest factors number = eval(input(“Enter an Integer: “)) divisor = 2 while number != 1: if number % divisor == 0: print(divisor,end=” “) number = number // divisor else: divisor += 1

## Greatest common divisor (GCD) another way

First find d to be the minimum of n1 and n2, and then check whether d, d – 1, d – 2, …, 2, or 1 is a divisor for both n1 and n2 in this order. The first such common divisor is the greatest common divisor for n1 and n2. n1 = eval(input(“Enter first … Read moreGreatest common divisor (GCD) another way

## count positive, negative numbers and compute Average of numbers

number = eval(input(“Enter an integer, the input ends if it is 0: “)) count = 0 total = 0 numberOfPositive = 0 numberOfNegative = 0 while number != 0: count += 1 if number > 0: numberOfPositive += 1 else: numberOfNegative += 1 total = total + number number = eval(input(“Enter an integer, the input … Read morecount positive, negative numbers and compute Average of numbers

## simulate a random walk

This program that simulates a random walk in a lattice import turtle from random import randint turtle.speed(1) #Set turtle speed to sloweast # Draw 16-by-16 lattice turtle.color(“gray”) # Color for lattice x = -80 for y in range(-80, 80 + 1, 10): turtle.penup() turtle.goto(x, y) # Draw a horizontal line turtle.pendown() turtle.forward(160) y = 80 turtle.right(90) … Read moresimulate a random walk

## Show Prime Numbers

this program show 50 prime numbers in 5 x 10 format NUMBER_OF_PRIMES = 50 # Number of primes to display NUMBER_OF_PRIMES_PER_LINE = 10 # Display 10 per line count = 0 # Count the number pf prime numbers number = 2 # A number to be tested for primeness print(“The first 50 prime numbers are”) … Read moreShow Prime Numbers