Download presentation
Presentation is loading. Please wait.
1
Dr. Joe Anderson September 6, 2017
COSC 117 Lab 2 Dr. Joe Anderson September 6, 2017
2
Modulus operator Modulus operator: “%”
Performs division of two numbers and returns only the remainder Ex: 11 % 5 == 1 10 % 5 == 0 23 % 3 == 2 Common usage: to test if a number is even or odd! If n is even, then n % 2 == 0 If n is odd, then n % 2 == 1
3
Lab: 3N + 1 Problem Given: a positive integer ”n”
If n is even, divide it by 2 If n is odd, multiply it by 3 and add 1 Stop when n is 1, otherwise, repeat Ex: Start with 3 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 Your tasks: Ask user for a positive int Perform 3N + 1 operation, until it reaches 1, and print each step to the terminal
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.