Day 5: Factorial Flex & Sum of Numbers: The Kanye Code Way
Multiply like Kanye, add up the wins. Coding’s easy when you’re keeping it real😎
Yo, what’s good, fam? It's ya boy Kanye here, ready to break down some classic Java code like it’s a banger track. So grab your laptop, turn the volume up, and let’s dive into two lit programs: the Factorial and the Sum of Numbers. These are your go-to codes for impressing the squad, but let’s keep it simple, ‘cause ain’t nobody got time for complicated stuff, right?
1. Factorial Program
Alright, let’s talk about factorials. You know how when you try to do too much at once and it’s like "this is too extra"? Yeah, factorial is like that. It's the number that multiplies by every number below it.
So if we got 5, the factorial (5!) would be:
5! = 5 × 4 × 3 × 2 × 1 = 120
Let’s drop some code:
Step-by-Step Breakdown:
->The program takes an input number (num) from the user using Scanner.
->It calculates the factorial by multiplying all numbers from 1 to num using a for loop.
->Finally, it prints the result, which is the calculated factorial.
2. Sum of Numbers Program
Now, if you’re just tryna keep it chill and add up some numbers like you’re counting the likes on your latest TikTok post, we got the sum of numbers. Basically, you add all numbers from 1 to whatever number you throw at it.
For example, if you got 5:
1 + 2 + 3 + 4 + 5 = 15
Here’s how we code that:
Step-by-Step Breakdown:
The program takes an input number (
num
) from the user, representing the range of numbers to sum up (from 1 tonum
).It uses a
for
loop to add all numbers from 1 tonum
and stores the result in thesum
variable.Finally, it prints the total sum of all numbers from 1 to
num
.
Too Long; Didn't Read:
Factorial: Multiply all numbers from the input down to 1.
Sum of Numbers: Add up all numbers from 1 to the input.
Code's easy if you keep it simple, and you’re one step closer to flexing that coding muscle.
Now go ahead, drop these programs like they’re your next viral hit😊