Exercises

  1. Factorial Calculation:

Create two programs that calculates the factorial of a given integer. The first solution should have a while loop; the other solution should use a for loop

The factorial function (symbol: !) says to multiply all whole numbers from our chosen number down to 1.

Examples:

  • 4! = 4 × 3 × 2 × 1 = 24

  • 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040

  • 1! = 1

  1. Sum of Numbers:

Write a program that calculates the sum of all integers from 1 to N, where N is a user-entered positive integer.

  1. Longest Name:

Create a program that takes an unknown amount of names and outputs the longest name. You will know that the inputs are done when the user has inputted a string of X.

Sample Input:
Kelsey
Tyree
X

Output:
Kelsey
  1. Prime Number Checker:

Create a program that checks whether a given number is prime or not.

Prime Numbers

  • 1 is not considered a prime number.

  • A prime number is a number that is either 2 or greater.

  • A prime number only has two distinct factors, 1 and itself.

Some examples: 2, 3, 5, 7, 11, 13

  1. Reverse a Number:

Write a program that reverses a given integer. For example, if the input is 12345, the program should output 54321.

Last updated