# make a sum variable and initialize it
sum = 0
# for-loop
# range = start-1, end 101 (not including 101-- so, 100), iterator = 1
for i in range(1,101):
# sum equals to the sum plus i
sum += i
# print out the sum
print (sum)
Category: Programming
Python Program to Print out every odd number from 1-100
# for - loop
# start value = 1, end value = 101 (not including 101), iterator = 2
for i in range(1,101,2):
# print i
print (i)
How to Draw an n-sided Polygon in Python Using the Turtle Library
# import turtle library
import turtle
# get screen
Window = turtle.Screen()
# name turtle
bob = turtle.Turtle()
# prompt user for num of sides they want their polygon to be
sidesInput = int(input("Enter a number greater than or equal to 3: "))
# if the user inputed a number less than 3 for the number of sides they want in their polygon, print ERROR because a polygon cannot have less than 3 sides
if sidesInput < 3 :
print('ERROR')
# initialize variables
i = 0
forward = ''
left = ''
# using a while-loop
while i in range (sidesInput):
# make forward variable equal to 100
forward = 100
# make left variable 360 degrees divided by the number of sides inputed by user to get the angle at which to rotate to make the figure
left = 360/sidesInput
# while condition- make i = i + 1
i += 1
# turtle
# make bob go forward by forward variable
bob.forward(forward)
# make bob go left by left variable
bob.left(left)
Fibonacci Sequence Using Methods in Java
import java.util.Scanner;
/* AniBlogger12
* Methods
*/
public class methods7 {
// method for returning fibonacci sequence
public static int fibonacci(int num) {
// if the number is 0, return 0
if (num == 0) {
return 0;
// or else if the number is 1, return 1
} else if (num == 1) {
return 1;
// else return the sum of the 2 last numbers to get the next term in the
// sequence
} else {
return fibonacci(num - 1) + fibonacci(num - 2);
}
}
// main
public static void main(String[] args) {
// get keyboard
Scanner kb = new Scanner(System.in);
// prompt user
System.out.println("Please enter the number of terms you want to display the fibonacci series:");
// user input
int num = kb.nextInt();
// result variable
int result = 0;
// for loop
for (int i = 0; i < num; i++) {
// result is equal to the fibonacci of (i)
result = fibonacci(i);
// print out result
System.out.println(result);
}
}
}
Binary Addition
import java.util.Scanner;
/*AniBlogger12
* Binary Addition Program
*/
public class binaryAddition {
public static void main(String[] args) {
// get keyboard
Scanner keyboard = new Scanner(System.in);
// prompt user
System.out.println("Enter a binary number.");
// user integer number input
int num1 = keyboard.nextInt();
// convert the int number to a string
String number1 = Integer.toString(num1);
// make length variable and set it equal to the length of the number entered
int length1 = number1.length();
// prompt user asking for another binary number
System.out.println("Enter another binary number.");
// user input for another binary number
int num2 = keyboard.nextInt();
// convert the int number to a string
String number2 = Integer.toString(num2);
// make length variable and set it equal to the length of the number entered
int length2 = number2.length();
// add the two binary numbers like adding normal numbers
int add = num1 + num2;
// convert sum to a string (this is not the answer)
String notanswer = Integer.toString(add);
// add a zero to the string variable
notanswer = "0" + notanswer;
// find length of this
int length = notanswer.length();
// make a string digit variable
String digit = "";
// make an answer variable
String answer = "";
// make and initialize a carry variable
int carry = 0;
// if the length of the first num is greater than the length of the second num
if (length1 > length2) {
// for loop
// iterate forward
for (int i = 0; i <= length1; i++) {
// take part of the digit from length - i - 1th position to length -ith position
digit = notanswer.substring(length - i - 1, length - i);
// if first digit is 0 and there is nothing to carry over
if (digit.equals("0") && carry == 0) {
// add a 0 to the answer
answer = "0" + answer;
// carry stays 0
carry = 0;
}
// or else if the first digit is 0 and carry is 1
else if (digit.equals("0") && carry == 1) {
// add 1 to the answer
answer = "1" + answer;
// carry becomes 0
carry = 0;
}
// or else if the first digit is 1 and carry is 0
else if (digit.equals("1") && carry == 0) {
// add 1 to the answer
answer = "1" + answer;
// carry over 0
carry = 0;
}
// else if the first digit is 1 and carry is 1
else if (digit.equals("1") && carry == 1) {
// add 0 to answer
answer = "0" + answer;
// carry over 1
carry = 1;
}
// else if the first digit is 2 and carry is 0
else if (digit.equals("2") && carry == 0) {
// add 0
answer = "0" + answer;
// carry over 1
carry = 1;
}
// else if the first digit is 2 and carry is 1
else if (digit.equals("2") && carry == 1) {
// add 1 to answer
answer = "1" + answer;
// carry over 1
carry = 1;
}
}
}
// or else
// if first number is smaller than second number
else if (length1 < length2) {
// for-loop
for (int i = 0; i <= length2; i++) {
// same as last time
digit = notanswer.substring(length - i - 1, length - i);
// same as before
if (digit.equals("0") && carry == 0) {
answer = "0" + answer;
carry = 0;
}
else if (digit.equals("0") && carry == 1) {
answer = "1" + answer;
carry = 0;
}
else if (digit.equals("1") && carry == 0) {
answer = "1" + answer;
carry = 0;
}
else if (digit.equals("1") && carry == 1) {
answer = "0" + answer;
carry = 1;
}
else if (digit.equals("2") && carry == 0) {
answer = "0" + answer;
carry = 1;
}
else if (digit.equals("2") && carry == 1) {
answer = "1" + answer;
carry = 1;
}
}
}
// or else if both are equal
else if (length1 == length2) {
// for-loop
for (int i = 0; i <= length2; i++) {
// same as before
digit = notanswer.substring(length - i - 1, length - i);
if (digit.equals("0") && carry == 0) {
answer = "0" + answer;
carry = 0;
}
else if (digit.equals("0") && carry == 1) {
answer = "1" + answer;
carry = 0;
}
else if (digit.equals("1") && carry == 0) {
answer = "1" + answer;
carry = 0;
}
else if (digit.equals("1") && carry == 1) {
answer = "0" + answer;
carry = 1;
}
else if (digit.equals("2") && carry == 0) {
answer = "0" + answer;
carry = 1;
}
else if (digit.equals("2") && carry == 1) {
answer = "1" + answer;
carry = 1;
}
}
}
// print out the final sum
System.out.println(answer);
}
}
Pizza Order – If-Else Statements & Switch Statements
import java.util.Scanner;
/*AniBlogger12
* Pizza Order
*/
public class PO {
// main
public static void main(String[] args) {
// get keyboard for user input
Scanner kb = new Scanner(System.in);
// prompt user
System.out.println("Please enter the size of the pizza (12, 14, or 16 inch)");
// make size variable
double size = kb.nextDouble();
// prompt user asking for number of toppings
System.out.println("Please enter the number of toppings");
// user input for number of toppings
int toppings = kb.nextInt();
// deduct $5.50 away from toppings
double newtoppings = toppings - 5.5;
// toppings price variable
double tprice;
// switch statement
switch (toppings) {
// first case, no toppings price
case 0:
tprice = 0;
break;
// second case, $2
case 1:
tprice = 2;
break;
// third case, $3
case 2:
tprice = 3;
break;
// if not the above, price is $3 plus $.50 ^ # of toppings
default:
tprice = 3 + Math.pow(.50, newtoppings);
}
// pizza prices for size 12, 14, and 16 pizzas
double price1 = 9.00;
double price2 = 11.00;
double price3 = 14.00;
// if the pizza size is 12, add $9.00 to the toppings price and print out
if (size == 12) {
price1 += tprice;
System.out.println("The cost of the pizza is " + price1);
}
// if the pizza size is 14, add $11.00 to the toppings price and print out
if (size == 14) {
price2 += tprice;
System.out.println("The cost of the pizza is " + price2);
}
// if the pizza size is 16, add $14.00 to the toppings price and print out
if (size == 16) {
price3 += tprice;
System.out.printf("The cost of the pizza is " + price3);
}
}
}
Odds and Evens – Arrays
import java.util.Arrays;
import java.util.Scanner;
/*AniBlogger12
* Odds and Evens- Arrays
*/
public class oddsAndEvens {
public static void main(String[] args) {
// get keyboard
Scanner kb = new Scanner(System.in);
// initialize array, specify number of integers you want typed
int[] array = new int[10];
// prompt user
System.out.println("Please enter a list");
// for 10 numbers entered
for (int i = 0; i < 10; i++) {
// put it into an array
array[i] = kb.nextInt();
}
// loop through the numbers one by one
for (int i = 0; i < array.length; i++) {
//if number is divisible by 2
if (array[i] % 2 == 0) {
//it is even and is replaced with 0
array[i] = 0;
//or else:
} else {
//it is odd and is replaced with 1
array[i] = 1;
}
}
//print out the new array
System.out.print("The new array is " + Arrays.toString(array) + "\n");
}
}
Conversion of Time
import java.util.Scanner;
/* AniBlogger12
* Program to convert elapsed time in the form of hours, minutes, seconds to seconds.
*/
public class TP {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the time as three integers: hours, minutes, and seconds");
// Define hours, minutes, and seconds
int hours = keyboard.nextInt();
int minutes = keyboard.nextInt();
int seconds = keyboard.nextInt();
//Convert hours to seconds: hours*3600= seconds
//Convert minutes to seconds: minutes*60=seconds
//Add hours+ minutes+seconds to get total seconds
int total = hours * 3600 + minutes * 60 + seconds;
System.out.println(hours + ":" + minutes + ":" + seconds + " elapsed time converts to " + total + " seconds.");
}
}
Program to Find the Average of 5 Grades
import java.util.Scanner;
/* AniBlogger12
* Finding the letter grade based on the percent grade entered
*/
public class AGRP {
public static void main(String[] args) {
System.out.println("Please enter 5 of your grades in integers:");
Scanner kb = new Scanner(System.in);
// 5 grades inputed
int g1 = kb.nextInt();
int g2 = kb.nextInt();
int g3 = kb.nextInt();
int g4 = kb.nextInt();
int g5 = kb.nextInt();
//average grade
double grade = (g1 + g2 + g3 + g4 + g5) / 5;
//grade rounded to nearest integer: use Math.rint and cast it to make it an int.
int rgrade = (int) Math.rint(grade);
// grade rounded and printed out to the nearest 10th, use %.1f.%n to round it one decimal place to the left
System.out.printf("The grade rounded to the nearest tenth is %.1f.%n", grade);
// If the grade is greater than or equal to(>=) 90, the grade is an A.
if (Math.rint(rgrade) >= 90) {
System.out.println("The grade is an A.");
}
// If it isn't, then if the grade is greater than or equal to(>=) 80
else if (rgrade >= 80 ) {
System.out.println("The grade is a B.");
}
//If it isn't, then if the grade is greater than or equal to(>=) 70
else if (rgrade >= 70 ) {
System.out.println("The grade is a C.");
}
//If it isn't, then if the grade is greater than or equal to(>=) 60
else if (rgrade >= 60 ) {
System.out.println("The grade is a D.");
}
//If it isn't, then if the grade is less than(<) 60, it is an F.
else if (rgrade < 60) {
System.out.println("The grade is an F.");
}
}
}
Looping Examples-Graphics with the Turtle Library
# Example #1:
# import the turtle method
import turtle
# get the Screen
window = turtle.Screen
# initialize Turtle Variable
bob = turtle.Turtle()
# Make speed fast (0 is the fastest)
bob.speed(0)
# for loop
# start = 0, stop = 250, iteration = 1 (you don't have to put this because if no iteration is typed in the range, it assumes the iteration is 1.
for i in range(0,250):
# move bob forward and iterate by 1 to make bob loop
bob.forward(i+1)
# we are trying to make the base hexogonal and iterate it
# we use bob.left(60) to turn bob 60 degrees (the angle of each
side of a hexagon)
bob.left(60)
Output:

# Example #2:
# import Turtle Method
import turtle
# get Screen
window = turtle.Screen
# Initialize Turtle (I named mine 'bob')
bob = turtle.Turtle()
# make speed fast (zero = fastest)
bob.speed(0)
# for-loop
# start = 0, end = 500, iteration = 1
for i in range(0,500):
# move bob forward and iterate by 1
bob.forward(i+1)
# move bob left by 65 degrees (always go a couple of degrees off from the angle of a side of a hexagon if you want to create the hexagonal spiral like shape
bob.left(65)
Output:
