MeetCode
Print star pattern count
C
C++
Computer Mathematics
CSS
DSA
HTML
Java
JavaScript
NodeJS
PHP
Programming Interview
Python
ReactJS
SQL
Run
Submit
Dark
Problem Statement
Solutions
Submissions
Print star pattern count
Easy
Java
Control Flow
23 views
Explanation
Complexity
Problem Statement
Task: return total stars printed in a triangle of height n (1+2+...+n).
Input Format
An integer n (height of the triangle).
Output Format
An integer: total number of stars printed.
Example
Input
4
Output
10
Constraints
• n ≥ 1
Concept Explanation
A triangle of height n prints:
1 + 2 + 3 + ... + n stars.
This is the sum of first n natural numbers.
Step-by-Step Explanation
1.Read integer n.
2.Initialize total = 0.
3.Loop i from 1 to n.
4.Add i to total.
5.After loop ends, return total.
Concept Explanation
A triangle of height n prints:
1 + 2 + 3 + ... + n stars.
This is the sum of first n natural numbers.
Step-by-Step Explanation
1.Read integer n.
2.Initialize total = 0.
3.Loop i from 1 to n.
4.Add i to total.
5.After loop ends, return total.
Input / Output Format
Input Format
An integer n (height of the triangle).
Output Format
An integer: total number of stars printed.
Constraints
• n ≥ 1
Examples
Input:
4
Output:
10
Example Solution (Public)
Java
static long totalStars(int n){return 1L*n*(n+1)/2;}
Official Solution Code
Wrap
Copy
static long totalStars(int n){return 1L*n*(n+1)/2;}
Please
login
to submit solutions.
Editor
C
C++
Computer Mathematics
CSS
DSA
HTML
Java
JavaScript
NodeJS
PHP
Programming Interview
Python
ReactJS
SQL
Test Cases
Output
Submission Result
Input
4
Expected Output
10
Compile Output
Error
Output
Please
login
to submit solutions.