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

circle-info

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.

  1. Prime Number Checker:

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

circle-info

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