Day 1: 21/06/25:
Scenario:
A school teacher is creating a classroom game where students clap once for each number
starting from 1 up to the number of the current round. For example, in round 1 they clap
once, in round 2 they clap twice (1 2), and so on.
🔍 Task:
Write a program to simulate the first 5 rounds of this game. The output should show the
numbers clapped in each round like this:
Output:
import [Link].*;
public class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <=i; j++) {
[Link](j+" ");
}
[Link]("");
}
}
}
Input:
12345
1234
123
12
1
Output:
import [Link].*;
public class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (int i = n; i>=1; i--) {
for (int j = 1; j <=i; j++) {
[Link](j + " ");
}
[Link]("");
}
}
}
Alphabet Pyramid
Print alphabets row-wise starting from 'A'.
Input:3
Output:
AB
ABC
import [Link].*;
public class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (int i = 1; i <= n; i++) {
for (char ch = 'A'; ch<'A'+i;ch++) {
[Link](ch+" ");
}
[Link]();
Odd Number Triangle
A student wants to practice counting odd numbers only. Print the
triangle using odd numbers.
Input: 4
Output:
13
135
1357
import [Link].*;
public class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (int i = 1; i<=n; i++) {
int odd =1;
for(int j =1;j<=i;j++) {
[Link](odd+" ");
odd+=2;
[Link]();
Day 2: 22/06/25:
Reverse Alphabet Pyramid
Print a pattern that starts with A to some letter and shrinks.
Input:3
Output:
ABC
AB
A
import [Link].*;
public class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (int i =n;i>=1;i--) {
for(char ch ='A';ch<'A'+i;ch++){
[Link]( ch +" ");
[Link]("");
Letter Repeat Pattern
Print rows like:
Input: 3
Output:
BB
CCC
import [Link].*;
public class LetterRepeatPattern {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
char ch = 'A'; // start with A
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
[Link](ch + " ");
ch++; // move to next letter
[Link]();
Another Method :
import [Link].*;
public class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (char ch = 'A';ch< 'A'+n;ch++) {
for(int j ='A';j<=ch;j++){
[Link]( ch+" ");
[Link]("");
}
Right-Angled Number Pyramid
Align numbers to the right:
Input:4 Output:
12
123
1234
import [Link].*;
class program{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
int n = [Link]();
for(int i=1;i<=n;i++){
for(int j=1;j<=n-i;j++){
[Link](" ");
for(int k=1;k<=i;k++){
[Link](k+" ");
[Link]();
}
Diagonal 1s in Square
Print 1 on diagonals, 0 elsewhere.
Input:4
Output:
1000
0100
0010
0001
import [Link].*;
class program{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
int n = [Link]();
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++){
if(i==j){
[Link]("1 ");
else {
[Link]("0 ");
[Link]("");
}
Checkerboard Pattern (0 & 1)
Alternate 0 and 1 in a grid.
Input:4 Output:
1010
0101
1010
0101
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if ((i + j) % 2 == 0) {
[Link]("1 ");
} else {
[Link]("0 ");
}
}
[Link]();
Another Method:
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
for (int j = 1; j <= n; j++) {
if (j % 2 != 0) {
[Link]("1 ");
} else {
[Link]("0 ");
else{
for (int j = 1; j <= n; j++) {
if (j % 2 != 0) {
[Link]("0 ");
} else {
[Link]("1 ");
[Link]("");
Hollow Square
Print a square with * on border only.
Input:4
Output:
****
* *
* *
****
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i==1 || j == 1 || i==n || j==n) {
[Link](" * ");
else {
[Link](" ");
[Link](" ");
Half Pyramid Numbers from n to 1
Example if n=4:
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (int i = 1; i <= n; i++) {
int value =n;
for (int j=1;j<=i;j++) {
[Link](value+" ");
value--;
[Link](" ");
Alphabet Count Triangle
Show characters increasing like this:
Input:3
Output:
BC
DEF
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
char ch= 'A';
for (int i = 1; i <= n; i++) {
for (int j='A';j<'A'+i;j++) {
[Link](ch+" ");
ch++;
}
[Link](" ");
Pascal’s Triangle
🧠 What is Pascal’s Triangle?
Each number is the sum of the two numbers directly above it.
The triangle starts with 1 at the top, and each row grows with symmetry.
Sample Input:
n=5
Expected Output:
11
121
1331
14641
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (int i = 1; i <= n; i++) {
int number =1;
for(int j=0;j<=i;j++){
[Link](number +" ");
number = number*(i-j)/(j+1);
[Link](" ");
Count the digits in a number using while loop
Input: 12345
Output: 5
(Count how many digits are in the given number.)
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int count=0;
if(n==0){
count=1;
while(n>0){
n=n/10;
count++;
[Link](count);
Reverse a number using while loop
Input: 1234
Output:4321
(Reverse the digits using logic, not string conversion.)
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int rem = 0;
while(n > 0) {
int digit = n%10;
rem = rem*10 + digit;
n /= 10;
[Link](rem);
}
Check if a number is a palindrome → Input: 121 → Output: Palindrome
Input: 123 → Output: Not a Palindrome
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int >
int rem = 0;
if (n < 0) {
[Link]("Negative Numbers cannot be a palindrome");
} else {
while (n > 0) {
int digit = n % 10;
rem = rem * 10 + digit;
n /= 10;
[Link](rem);
if ( rem) {
[Link]("Palindrome");
} else {
[Link]("Not a palindrome");
}
Display all prime numbers between 1 and N
Input: 10
Output: 2 3 5 7
(Use nested loop: check divisibility up to sqrt(n))
import [Link].*;
import static [Link];
class program{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
for (int i = 2; i <= num; i++) {
boolean isprime =true;
for (int j= 2; j <=sqrt(i); j++) {
if (i % j == 0) {
isprime = false;
if(isprime){
[Link](i+" ");
We can also use j*j<=i for instead of sqrt(i);
Multiplication Table from 1 to 10 (Nested Loop)
✅ What is being asked?
You need to print multiplication tables for numbers from 1 to 10, like:
1x1=1
1x2=2
...
1 x 10 = 10
2x1=2
2x2=4
...
2 x 10 = 20
...
10 x 10 = 100
import [Link].*;
class program{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
for (int i=1;i<=num;i++){
for(int j=1;j<=10;j++){
[Link](i+" * "+j+" = "+i*j);
}
[Link]("");
Print Floyd’s Triangle using nested loops
Input: 4
Output:
23
456
7 8 9 10
import [Link].*;
class program{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int ans =1;
for (int i=1;i<=num;i++){
for(int j=1;j<=i;j++){
[Link](ans+" ");
ans++;
}
[Link]("");
Factorial:
Input: 5
Output:120
Using For loop:
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int ans=1;
for (int i=num;i>=1;i--){
ans=ans*i;
[Link](ans);
}
Using While loop:
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int ans=1;
while(num>0){
ans*=num;
num--;
[Link](ans);
Check if a number is a Strong Number in Java 🔥
💡 What is a Strong Number?
A Strong number is a number in which sum of the factorials of its digits equals the
number itself.
✅ Example:
✅ Strong Number
145 →
1! + 4! + 5! = 1 + 24 + 120 = 145 →
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int >
int sum=0;
while(onum>0){
int digits = onum%10;
int fact =1;
for(int i=1;i<=digits;i++){
fact*=i;
sum+=fact;
onum/=10;
if(num==sum){
[Link]("Strong Number");
else{
[Link]("Not a Strong number");
}
Count the Sum of Digits Until a Single Digit
📝 Input: A number
🔁 Task: Repeatedly sum its digits until a single-digit result is obtained.
📌 Eg: 9875 → 9+8+7+5 = 29 → 2+9 = 11 → 1+1 = 2 → Output = 2
Using Method:
import [Link];
class program{
static int reminder(int rem) {
int rems = 0;
while (rem > 0) {
int digit = rem % 10;
rems += digit;
rem /= 10;
return rems;
public static void main(String[] args){
Scanner input = new Scanner([Link]);
int num =[Link]();
int rem=0;
while(num>0){
int digits = num%10;
rem +=digits;
num/=10;
if(rem>=10){
int ans = reminder(rem);
if(ans<10) {
[Link](ans);
else {
int ans2 = reminder(ans);
[Link](ans2);
else {
[Link](rem);
Optimize Method:
import [Link];
class program{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
while (num >= 10) {
int temp =0;
while (num > 0) {
temp+=num%10;
num /= 10;
}
num=temp;
[Link](num);
Check if a Number is an Armstrong Number
📝 Input: A number
🔁 Task: Sum of each digit’s cube = number
📌 Eg: 153 → 1³ + 5³ + 3³ = 153 → Armstrong ✅
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int >
int length =[Link](num).length();
int rem = 0;
while (num > 0) {
int digit = num % 10;
rem +=[Link](digit,length);
num /= 10;
[Link](rem);
if ( rem) {
[Link]("Armstrong Number");
} else {
[Link]("Not Armstrong Number");
Count Total Zeros in a Number
📝 Input: A number like 10502070
🔁 Task: Count how many zeros it has
📌 Output: 4
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int rem=0,count=0;
while(num>0){
int digits=num%10;
rem = digits;
num/=10;
if(rem==0){
count++;
}
[Link](count);
Reverse a Number Without Using String
📝 Input: A number
🔁 Task: Print its reverse
📌 Eg: 12345 → 54321
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int reverse = 0;
while (num > 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
[Link](reverse);
Check if a Number is Harshad (Niven) Number
📝 Input: A number
🔁 Task: If the number is divisible by the sum of its digits
📌 Eg: Input: 18 → 1+8=9 → 18 % 9 == 0 → Harshad ✅
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int >
int rem = 0;
while (num > 0) {
int digit = num % 10;
rem += digit;
num /= 10;
if (onum % rem == 0) {
[Link]("Harsad Number");
} else {
[Link]("Not Harsad Number");
}
Print All Perfect Squares Less Than N
📝 Input: N
🔁 Task: Print all numbers less than N which are perfect squares
📌 Eg: Input: 30 → Output: 1, 4, 9, 16, 25
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int ans=0;
for(int i=1;i*i<num;i++){
ans =i*i;
[Link](ans+" ");
Sum of Series: 1² + 2² + 3² + … + N²
📝 Input: N
🔁 Task: Print the sum of squares of first N numbers
📌 Eg: Input: 3 → Output: 1 + 4 + 9 = 14
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int ans=0;
for(int i=1;i<=num;i++){
ans+=i*i;
[Link](ans);
Print First N Fibonacci Numbers
📝 Input: N
🔁 Task: Print the first N numbers in Fibonacci sequence
📌 Eg: 0 1 1 2 3 5 8 …
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int a=0;
int b=1;
for(int i=0;i<num;i++){
[Link](a +" ");
int next=a+b;
a=b;
b=next;
}
}
Day 3: 23/06/25:
Find GCD of Two Numbers (Using While Loop)
📝 Input: Two integers
🔁 Task: Find their Greatest Common Divisor using Euclidean Algorithm
📌 Eg: Input: 24, 36 → Output: 12
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int a = [Link]();
int b= [Link]();
while(b!=0){
int temp =b;
b=a%b;
a=temp;
[Link]("GCD:"+a);
}
Find LCM of Two Numbers
📝 Input: Two integers
🔁 Task: Find their Least Common Multiple using loop
📌 Eg: Input: 4, 5 → Output: 20
import [Link];
class program{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int x=a;
int y=b;
while(b!=0){
int temp=b;
b=a%b;
a=temp;
[Link]("GCD:"+a);
int gcd=a;
int Lcm= (x*y)/gcd;
[Link]("LCM:"+Lcm);
}
Day 4:24/06/25:
👉
1️⃣ Count Even and Odd Digits in a Number
Input: 3456 → Output: Even: 2, Odd: 2
import [Link].*;
class program{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
int n = [Link]();
int ecount=0,ocount=0;
while(n>0){
int digit = n%10;
int rem = digit;
n/=10;
if(rem%2==0){
ecount++;
else{
ocount++;
[Link](ecount);
[Link](ocount);
}
👉
2️⃣ Sum of Digits at Even and Odd Positions
Input: 12345 → Output: Even position sum: 6, Odd: 9 (1st = Odd, 2nd = Even...)
import [Link].*;
class program{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
int n = [Link]();
int ecount=0,ocount=0;
while(n>0){
int digit = n%10;
int rem = digit;
n/=10;
if(rem%2==0){
ecount+=rem;
else{
ocount+=rem;
[Link](ecount);
[Link](ocount);
}
👉
3️⃣ Check if a Number is a Strong Number
👉 ✅
A strong number = sum of factorial of digits equals original number
Eg: 145 → 1! + 4! + 5! = 145
import [Link].*;
class program{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
int n = [Link]();
int >
int sum=0;
int rem=0;
while(n>0){
int digit = n%10;
int fact=1;
rem = digit;
for(int i=1;i<=rem;i++){
fact*=i;
sum+=fact;
n/=10;
[Link](sum);
if(>
[Link]("Strong Number");
else{
[Link]("Not a Strong Number");
👉
4️⃣ Product of Digits of a Number
Input: 123 → Output: 6
import [Link].*;
class program{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
int n = [Link]();
int mul=1;
while(n>0){
int digit =n%10;
int rem = digit;
mul*=rem;
n/=10;
[Link](mul);
👉
5️⃣ Count Prime Digits in a Number
Eg: Input: 23753 → Prime digits = 2, 3, 5, 3 → Count = 4
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int ans=0;
int count = 0;
while (n > 0) {
int digit = n % 10;
if(prime(digit)){
count++;
n /= 10;
[Link](count);
public static boolean prime(int a) {
if(a<2) return false;
for (int i = 2; i <=a / 2; i++) {
if (a % i == 0) {
return false;
return true;
}
👉
6️⃣ Check if Number is a Magic Number
👉 ✅
Sum of digits until single digit = 1
Eg: 199 → 1+9+9 = 19 → 1+9 = 10 → 1+0 = 1
import [Link];
class program{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
while (num >=10) {
int temp=0;
while (num> 0) {
temp+=num%10;
num /= 10;
num=temp;
[Link](num);
if(num==1){
[Link]("Magic NUmber");
else{
[Link]("Not a Magic Number");
}
👉
7️⃣ Reverse the Number without using Extra Variables
Logic-level test (you can only use 2 variables max)
import [Link];
class program{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int rem=0;
while(n>0){
int digit = n%10;
rem = (rem*10)+digit;
n/=10;
[Link](rem);
👉
8️⃣ Check if a Number is a Neon Number
👉 ✅
Neon = square of the number → sum of digits of square = number
Eg: 9 → 9² = 81 → 8 + 1 = 9
import [Link];
class program{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n= [Link]();
int sq = n*n;
int digits=0;
while(sq>0){
digits+=sq%10;
sq/=10;
if(digits==n){
[Link](digits+" Neon Number");
else{
[Link]("Not a Neon Number");
👉
9️⃣ Print All Armstrong Numbers from 1 to n
Input: n = 1000 → Output: 1, 153, 370, 371, 407
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (int i = 1; i <= n; i++) {
int num = i;
int length = [Link](num).length();
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += [Link](digit, length);
num /= 10;
if (sum == i) {
[Link](i + " is an Armstrong Number");
🔟 Sum of Digits Raised to Power of Count of Digits
👉 Input: 89 → 8² + 9² = 64 + 81 = 145 ❌
👉 Input: 135 → 1³ + 3³ + 5³ = 1 + 27 + 125 = 153 ✅
import [Link];
class program{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n= [Link]();
int > int temp =n;
int length=0,mul=0;
while(temp>0){
length++;
temp/=10;
while(n>0){
int digit=0;
digit =n%10;
mul += (int) [Link](digit,length);
n/=10;
[Link](mul);
if(mul==onum){
[Link]("Valid Number");
else{
[Link]("It is not a Valid Number");
} for 10
Sum of Even Positioned Digits
👉 Input: 123456
👉 Output: Sum of digits in even positions (from right): 2 + 4 + 6 = 12
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int position =1;
int sum=0;
while(n>0){
int digits = n%10;
if(position%2==0){
sum+=digits;
[Link](digits + "-"+position);
n/=10;
position++;
[Link](sum);
}
Product of Odd Positioned Digits
👉 Input: 987654
👉 Output: Product of 9 × 7 × 5 = 315
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int length = [Link](n).length();
int position =length;
int mul =1;
while(n>0){
int digits = n%10;
if(position%2!=0){
mul*=digits;
[Link](digits + "-"+position);
n/=10;
position--;
[Link](mul);
}
Check if a Number is a Duck Number
👉 A number with zero(s) in it (but not starting with 0)
👉 Input: 1023 → Output: ✅ Duck Number
👉 Input: 0123 → Output: ❌ Not Duck Number
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int >
boolean ans = false;
while(n>0){
int digit =n%10;
if(digit==0){
ans=true;
n/=10;
if([Link](onum).charAt(0)=='0'){
[Link]("Not Duck Number");
else if(ans){
[Link]("Duck Number");
else{
[Link]("Not Duck Number");
}
Count Digits Dividing the Number
👉 Input: 124
Digits: 1, 2, 4
→ 124 is divisible by 1, 2, and 4 → Output: 3
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int >
int length = [Link](n).length();
int count=0;
while(n>0){
int digit = n%10;
if(digit!=0 && onum%digit==0){
count++;
n/=10;
[Link](count);
}
}
Count Digits Not Dividing the Number
👉 Input: 125
Digits: 1, 2, 5
→ Only 1 and 5 divide 125 → Output: 1 (2 doesn't divide)
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int >
int length = [Link](n).length();
int count=0;
while(n>0){
int digit = n%10;
if(digit!=0 && onum%digit!=0){
count++;
n/=10;
[Link](count);
}
Check if the Number is a Spy Number
👉 Sum of digits = Product of digits
👉 Example: 1124 → 1+1+2+4=8 & 1×1×2×4=8 → Spy Number ✅
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int >
int sum =0,mul=1;
while(n>0){
int digit = n%10;
sum+=digit;
mul*=digit;
n/=10;
if(sum==mul){
[Link]("Spy Number");
else {
[Link]("Not Spy Number");
}
17. Harshad Numbers from 1 to N
👉 Input: N=50
👉 Print all Harshad (N divisible by sum of its digits) between 1 to 50
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
for (int i = 1; i <= n; i++) {
int temp=i;
int sum = 0;
while (temp>0) {
int digit = temp % 10;
sum += digit;
temp/=10;
if (i%sum == 0) {
[Link](i);
}
Reverse + Add until Palindrome (Bonus Logic)
👉 Input: 87 → 87 + 78 = 165 → 165 + 561 = 726 → ...
👉 Repeat until you get a palindrome
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
while (true) {
int >
int reverse = 0;
while (onum > 0) {
int digit = onum % 10;
reverse = (reverse * 10) + digit;
onum /= 10;
if (reverse == n) {
[Link]("Palindrome "+n);
break;
} else {
n =n+reverse;
}
}
Count Number of Zeros in a Number
👉 Input: 102030
👉 Output: 3
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int length = [Link](n).length();
int count=0;
while(n>0){
int digit = n%10;
if(digit ==0){
count++;
n/=10;
[Link](count);
}
Day 5:25/06/25:
Automorphic Number Check
👉 A number whose square ends with the same number
👉 Ex: 25² = 625 → ends with 25 → ✅ Automorphic
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int sq=n*n;
int length = [Link](n).length();
int last = (int) [Link](10,length);
if(sq%last==n){
[Link]("Automorphoic Number");
else{
[Link]("Not Automorphoic Number");
Another Method:
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int sq=n*n;
int mod=1;
int temp=n;
while(temp>0){
mod*=10;
temp/=10;
if(sq%mod==n){
[Link]("True");
else{
[Link]("False");
}
Check for Trimorphic Number
👉
A number is called Trimorphic if its cube ends with the number itself.
Input: 24 → 24³ = 13824 → Ends with 24 → Output: Trimorphic
Number
import [Link];
class program{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
int n =[Link]();
int sq= n*n*n;
int length =[Link](n).length();
if(n<=0){
[Link]("Invalid Input");
return;
int digit =(int)[Link](10,length);
if(sq%digit==n){
[Link]("Trimorphoic Number ");
else{
[Link]("Not Trimorphoic Number ");
}
Count Digits Greater than 5 in a Number
👉 Input: 983452 → Digits greater than 5 are: 9, 8 → Output: 2
import [Link];
class program{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
int n =[Link]();
int count=0;
while(n>0){
int digit = n%10;
if(digit>5){
count++;
n/=10;
[Link](count);
}
Check if a Number is a Tech Number
A number is a tech number if it has an even number of digits and the square of the
👉
sum of its two halves is equal to the number itself.
Input: 2025 → 20 + 25 = 45 → 45² = 2025 → Output: Tech Number
import [Link];
class program{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
int n =[Link]();
int digit =[Link](n).length();
if(length%2!=0){
[Link](“Invalid Number”);
return;
int divisor = (int) [Link](10,digit/2);
int Fh = n/divisor;
int Lh=n%divisor;
int sum=Fh+Lh;
int sq = sum*sum;
if(sq==n){
[Link]("Tech Number "+sq);
else{
[Link]("Not a Tech Number "+sq);
}}
}
Check if Sum of Even Digits is Divisible by 4
👉 Input: 123456 → Even digits: 2, 4, 6 → Sum = 12 → 12 % 4 == 0 → Output:
YES
import [Link];
class program{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int sum=0;
while(n>0){
int digit = n%10;
if(digit%2==0){
sum+=digit;
n/=10;
if(sum%4==0){
[Link]("Yes");
else{
[Link]("No");
}
Find Difference Between Product of Even and Odd Digits
👉 Input: 1234 → Even = 24=8, Odd = 13=3 → Output: 8 - 3 = 5
import [Link];
class program{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int emul=1,omul=1;
while(n>0){
int digit = n%10;
if(digit%2==0){
emul*=digit;
else{
omul*=digit;
n/=10;
int sub= emul-omul;
[Link](sub);
}
Find Sum of Prime Digits Present in a Number
👉 Input: 5931 → Prime digits: 5, 3 → Sum = 8
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int sum=0;
while(n>0){
int digit = n%10;
if(isPrime(digit)){
sum +=digit;
n/=10;
[Link](sum);
public static boolean isPrime(int d){
return d ==2|| d ==3|| d ==5|| d ==7;
}
Count Total Digits Before First 0 Appears
👉 Input: 123045 → Output: 3 (since 0 appears after 3 digits)
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
String n = [Link]();
int count = 0;
for (int i = 0; i <[Link](); i++) {
if ([Link](i) == '0') {
break;
count++;
if (count == [Link]()) {
[Link]("No zeros are found");
} else {
[Link](count);
Check if Number is Perfect Square Without Using [Link]()
👉 Input: 49 → Output: Yes (Use loop)
import [Link];
class program{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
int n = [Link]();
boolean ps = false;
for (int i=0;i*i<=n;i++){
if(i*i==n){
ps =true;
break;
if(ps){
[Link]("Prefect Square");
else{
[Link]("Not Prefect Square"); }
Another Method:( Math .sqrt):
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
double sqrt = [Link](n);
if (sqrt == (int) sqrt) {
[Link]("Perfect Square");
} else {
[Link]("Not a Perfect Square");
Check if a Number is a Twisted Prime
👉
A number is twisted prime if both the number and its reverse are prime.
Input: 13 → Reverse = 31 → Both prime → Output: Twisted Prime
import [Link];
class program{
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int reverse = reverse(n);
[Link](reverse);
if(isPrime(n)==true && isPrime(reverse)==true){
[Link]("Twisted Prime");
else{
[Link]("Not Twisted Prime");
}
public static int reverse(int b){
int rev=0;
while(b>0) {
int digit = b % 10;
rev = rev * 10 + digit;
b /= 10;
return rev;
public static boolean isPrime(int a) {
if (a < 2) {
return false;
} else {
for (int i = 2; i <= [Link](a); i++) {
if (a % i == 0) {
return false;
return true;
}
Kaprekar Number Check
If a number’s square can be split into two parts that sum to the number, it's a
👉
Kaprekar number.
Input: 45 → 45² = 2025 → 20 + 25 = 45 → Output: Kaprekar Number
import [Link];
class program{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
int n =[Link]();
int sq = n*n;
int digit =[Link](n).length();
int divisor = (int) [Link](10,digit);
int Fh = sq/divisor;
int Lh=sq%divisor;
int sum=Fh+Lh;
if(n==sum){
[Link]("Kaprekar Number "+sum);
else{
[Link]("Not a Kaprekar Number "+sum);
}}
}
Another Method Using reverse:
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int original = n;
int reverse = 0;
// Step 1: Reverse the number
while (n > 0) {
int digit = n % 10;
reverse = reverse * 10 + digit;
n /= 10;
// Step 2: Count digits until first 0 appears
int count = 0;
while (reverse > 0) {
int digit = reverse % 10;
if (digit == 0) {
break;
count++;
reverse /= 10;
}
[Link]("Digits before first 0: " + count);
👉
Count Total Digits Before First Zero Appears (from right side)
Input: 1234056 → Output: 2
(From right: 6, 5, 0 → count = 2 digits before 0 appears)
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int len = [Link](n).length();
int count = 0;
while (n > 0) {
int digit = n % 10;
if (digit==0) {
break;
count++;
n /= 10;
if (count == len) {
[Link]("No zeros are Found");
}
else{
[Link](count);
👉
13. Check if Number is a Perfect Number
A number is perfect if the sum of its proper divisors equals the
👉
number.
👉
Input: 28 → Output: Perfect Number
Input: 12 → Output: Not a Perfect Number
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int sum=0;
for(int i=1;i<=n/2 or i*i<=n ;i++){
if(n%i==0){
sum+=i;
if(sum==n){
[Link]("Perfect Number");
}
else {
[Link]("Not Perfect Number");
👉
14. Print Digits in Even Positions (from left to right)
Input: 123456 → Output: 2 4 6
(2nd, 4th, 6th digit)
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int >
int rev=0;
while(n>0){
int digit =n%10;
rev=(rev*10)+digit;
n/=10;
int position =1;
[Link](rev);
while(rev>0){
int digit =rev%10;
if(rev%2==0){
[Link](digit+" ");
rev/=10;
position++;
Another Method :(Using String):
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
String n = [Link]();
for (int i = 1; i < [Link](); i += 2) {
[Link]([Link](i) + " ");
}
👉
15. Check if Product of Digits is Prime
👉
Input: 235 → Product = 2×3×5 = 30 → Output: Not Prime
👉
Input: 23 → Product = 6 → Output: Not Prime
👉
Input: 37 → Product = 21 → Output: Not Prime
Input: 23 → Output: Not Prime
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int mul=1;
while(n>0){
int digit = n%10;
mul*=digit;
n/=10;
[Link](mul);
boolean ans = isPrime(mul);
if(isPrime(mul)) {
[Link]("Prime number");
else{
[Link]("Not Prime number");
public static boolean isPrime(int a){
if(a<2){
return false;
else{
for(int i=2;i<a/2;i++){
if(a%i==0){
return false;
return true;
👉
16. Print Digits Whose Square is Even
Input: 248159 → Output: 2 4 8
(Squares: 4, 16, 64 → All even)
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int rev = 0, temp = n;
while (temp > 0) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
while(rev>0){
int digit =rev%10;
int sq= digit*digit;
rev/=10;
if(sq%2==0){
[Link](digit+" ");
👉
17. Find Number of Digits Which Are Multiple of 3
Input: 3695 → Output: 3
(Digits 3, 6, 9 are multiples of 3)
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int count=0;
while(n>0){
int digits = n%10;
if(digits%3==0 && digits>0){
count++;
n/=10;
[Link](count);
👉
18. Find Digits Which Are Same as Position (from left)
👉
Input: 123456 → Output: 1 2 3 4 5 6
Input: 124365 → Output: 1 2 3
(Position starts from 1)
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int rev = 0, temp = n;
while (temp > 0) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
int position =1;
while(rev>0){
int digit =rev%10;
if(position==digit){
[Link](digit+" ");
rev/=10;
position++;
👉
19. Count Number of Times Highest Digit Appears
Input: 8989234 → Highest digit = 9 → Output: 2
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int temp = n;
int max=0;
while (temp > 0) {
int digit=temp%10;
if(max<digit){
max=digit;
temp /= 10;
[Link](max);
int count=0;
while ( n> 0) {
int digit=n%10;
if(max==digit){
count++;
n/= 10;
[Link](count);
Another Method :
import [Link];
class Program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]([Link]()); // Handle negatives
int max = 0;
int count = 0;
while (n > 0) {
int digit = n % 10;
if (digit > max) {
max = digit;
count = 1; // Reset count since new max found
} else if (digit == max) {
count++;
n /= 10;
[Link]("Highest digit: " + max);
[Link]("Occurrences: " + count);
👉
20. Count Consecutive Repeating Digits
Input: 1223334444 → Output: 3
(Repeating groups: 22, 333, 4444 → Total = 3)
Day6: 26/06/25:
Check if a Number is a Lucky Number
A lucky number is a number where the sum of digits is even, and
👉
the product of digits is odd.
👉
Input: 123 → Sum = 6, Product = 6 → Output: Not Lucky
👉
Input: 124 → Sum = 7, Product = 8 → Output: Not Lucky
Input: 135 → Sum = 9, Product = 15 → Output: Lucky
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int sum = 0, mul = 1;
while (n > 0) {
int digit = n % 10;
sum += digit;
mul *= digit;
n /= 10;
if (sum % 2 == 0 && mul % 2 != 0) {
[Link]("Lucky Number");
} else {
[Link]("Not Lucky Number");
}}
}
👉
2️⃣. Count Digits Whose Cube is a Multiple of 3
Input: 236 → Cubes: 8, 27, 216 → Count = 2 (3 and 6)
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int count=0;
while(n>0){
int digit = n%10;
if((int)[Link](digit,3)%3==0){
count++;
n/=10;
[Link](count);
3️⃣. Check if the Digits of a Number are in Increasing Order (Left to
👉
Right)
👉
Input: 1234 → Output: Yes
Input: 421 → Output: No
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int prevDigit =10;
boolean ans = true;
while (num>0){
int curDigit = num%10;
if(curDigit>=prevDigit){
ans=false;
break;
prevDigit=curDigit;
num/=10;
if(ans){
[Link]("Yes");
else{
[Link]("No");
}
👉
4️⃣. Find Sum of Digits at Prime Positions (Left to Right)
Input: 123456 → Prime positions: 2, 3, 5 → Digits: 2, 3, 5 →
Sum = 10
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int rev=0;
while(num>0){
int digit = num%10;
rev =rev*10+digit;
num/=10;
int position =1;
int sum=0;
while(rev>0){
int digit = rev%10;
if(isPrime(position)){
sum+=digit;
position++;
rev/=10;
[Link](sum);
}
static boolean isPrime(int a){
if(a<2){
return false;
else{
for(int i=2;i<=[Link](a);i++){
if(a%i==0){
return false;
return true;
👉
5️⃣. Count Number of Digits Which Are Perfect Squares
Input: 239145 → Perfect squares: 1, 4, 9 → Count = 3
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link]();
int count=0;
while(n>0){
int digit = n%10;
for (int i = 1; i*i<=digit; i++) {
if (i * i == digit) {
count++;
[Link](digit);
break;
n/=10;
[Link]("Count: "+count);
👉
6️⃣. Print Digits in Fibonacci Positions (1, 2, 3, 5, 8, ...)
Input: 123456789 → Positions: 1, 2, 3, 5, 8 → Output: 1 2 3 5 8
👉
7️⃣. Check if the Number is an Evil Number
A number is Evil if it has an even number of 1s in its binary
👉
representation.
👉
Input: 9 → Binary = 1001 → Two 1s → Output: Evil
Input: 7 → Binary = 111 → Three 1s → Output: Not Evil
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
String binary="";
int count=0;
while(num>0){
int bit = num%2;
if(bit==1){
count++;
binary = bit +binary;
num/=2;
if(count%2==0){
[Link]("Evil Number");
else{
[Link]("Not a Evil Number");
}
👉
8️⃣. Find the Difference Between First and Last Digit of a Number
Input: 5832 → First = 5, Last = 2 → Output: 3
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int len = [Link](num).length();
int pow = (int) [Link](10,len-1);
int ldigit= num%10;
int fdigit= num/pow;
int digit =[Link](fdigit-ldigit);
[Link](digit);
👉
9️⃣. Count the Digits that are Powers of 2 (i.e., 1, 2, 4, 8)
Input: 231486 → Output: 5
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int count=0;
while(num>0){
int digit = num%10;
if(digit==1||digit==2||digit==4||digit==8){
count++;
num/=10;
[Link](count);
🔟. Check if a Number is a Disarium Number
👉 A number is Disarium if the sum of its digits powered to their
👉 Input: 135 → 1¹ + 3² + 5³ = 135 → Output: Disarium
positions equals the number.
👉 Input: 89 → 8¹ + 9² = 89 → Output: Disarium
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int >
int rev = 0;
while (num > 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
int pow = 1;
int sum = 0;
while (rev > 0) {
int digit = rev % 10;
sum += (int) [Link](digit, pow);
rev /= 10;
pow++;
if ( sum) {
[Link](sum+"-Disarium Number");
else{
[Link](sum+"-Not Disarium Number");
}
CONVERTING DECIMAL INTO BINARY:
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
String binary="";
while(num>0){
int bit = num%2;
binary = bit +binary;
num/=2;
[Link](binary);
Day 10: 30/06/2025:
👉
Count the Digits Whose Cube is a Prime Number
Example: Input: 239 → Cubes: 8, 27, 729 → Only 8 is a
prime? → Count = ?
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int count=0;
while(num>0){
int digit = num%10;
int cube = digit*digit*digit;
if(isPrime(cube)){
count++;
num/=10;
[Link](count);
static boolean isPrime(int a ){
if(a<2){
return false;
else{
for(int i=2;i<=a/2;i++){
if(a%i==0){
return false;
}
}
return true;
👉
2️⃣ Check if All Digits Are Same
Example: Input: 5555 → Output: Yes
👉 Example: Input: 525 → Output: No
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int ldigit=num%10;
boolean allSame =true;
while (num > 0) {
int digit = num % 10;
if(digit!=ldigit){
allSame =false;
break;
num /= 10;
if(allSame){
[Link]("Yes");
}
else{
[Link]("No");
👉
3️⃣ Find the Maximum Digit at an Even Position (from left)
Input: 235147 → Even positions: 3, 1, 7 → Output: 7
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int rev =0;
while(num>0){
int digit = num%10;
rev= rev*10+digit;
num/=10;
int position =1;
int max=0;
while(rev>0){
int digit = rev%10;
if(position%2==0){
if(max<digit){
max=digit;
rev/=10;
position++;
[Link](max);
👉
4️⃣ Check if a Number is a Harshad Number
👉
A number is Harshad if it is divisible by the sum of its digits.
Example: Input: 18 → 1 + 8 = 9 → 18 % 9 == 0 → Output:
Harshad
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int >
int sum=0;
while(num>0){
int digit = num%10;
sum+=digit;
num/=10;
}
if(onum%sum==0){
[Link]("Harsad Number");
else{
[Link]("Not Harsad Number");
👉
7️⃣ Check if Number is Centered Hexagonal
A number is Centered Hexagonal if: N = 3n(n−1)+1 for some
👉
integer n.
Example: 1, 7, 19, 37, 61, …
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
boolean isHex = false;
for (int i = 1; ; i++) {
int ans = 3 * i * (i - 1) + 1;
if (ans == num) {
isHex = true;
break;
if (ans > num) {
break;
if (isHex) {
[Link]("Number is Centered Hexagonal");
} else {
[Link]("Number is Not Centered Hexagonal");
👉
9️⃣ Count the Number of 0s in Even Positions (from left)
Input: 102040506 →Even positions: 0s at 2, 4, 6..→ Output: 4
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int rev =0;
while(num>0){
int digit = num%10;
rev= rev*10+digit;
num/=10;
int position =1;
int count=0;
while(rev>0){
int digit = rev%10;
if(digit==0 && position%2==0){
count++;
rev/=10;
position++;
[Link](count);
🔟 Check if a Number is a Buzz Number
👉 A number is Buzz if it is divisible by 7 or ends with 7.
👉 Example: Input: 27 → ends with 7 → Output: Buzz
👉 Example: Input: 35 → divisible by 7 → Output: Buzz
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
[Link]();
int ldigit = num%10;
if(num%7==0 || ldigit==7 ){
[Link]("Buzz");
else{
[Link]("Not Buzz");
👉
2️⃣ Find the Second Highest Digit in the Number
Input: 53921 → Digits: 5, 3, 9, 2, 1 → Second highest: 5
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int max1 =-1,max2=-1;
while(num>0){
int digit = num%10;
if(digit> max1){
max2=max1;
max1=digit;
}
else if(digit!=max1 && digit>max2){
max2=digit;
num/=10;
[Link](max2);
👉
5️⃣ Check if Digits are in Non-Decreasing Order (Left to Right)
Input: 1123456 → Output: Yes
👉 Input: 321 → Output: No
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int rev =0;
while(num>0){
int digit = num%10;
rev = rev *10 + digit;
num/=10;
int max=0;
while(rev>0){
int digit = rev%10;
if(digit>=max){
max=digit;
else{
[Link]("No");
return;
rev/=10;
[Link]("Yes");
👉
7️⃣ Check if Sum of First Half Digits = Second Half Digits
Input: 123321 → 1+2+3 == 3+2+1 → Output: Yes
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
int len = [Link](num).length();
if (len % 2 != 0) {
[Link]("Odd digit count - not valid for this check.");
return;
int div = (int) [Link](10,len/2);
int lDigit = num%div;
int fDigit = num/div;
int lsum=0;
while(lDigit>0){
int digit=lDigit%10;
lsum+=digit;
lDigit/=10;
int fsum=0;
while(fDigit>0){
int digit=fDigit%10;
fsum+=digit;
fDigit/=10;
if(lsum==fsum){
[Link]("Yes");
else {
[Link]("No");
}
twin prime upto upr limit, diff b/w the prime should be
[Link],output:(3,5),(11,13):
import [Link].*;
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int num = [Link]();
for (int i = 2; i <= num-2; i++) {
if (isPrime(i ) && isPrime((i+2))) {
[Link]("("+i+ "," +(i+2)+")");
public static boolean isPrime(int n){
if (n <=1) return false;
for (int j = 2; j <=[Link](n); j++) {
if (n % j == 0) return false;
return true;
}
You have n coins and you want to build a staircase with these coins.
The staircase consists of k rows where the ith row has exactly i coins.
The last row of the staircase may be incomplete.
Given the integer n, return the number of complete rows of the
staircase you will build.
import [Link];
class program {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int n = [Link](); // Total number of coins
int row = 0;
while (n >= row + 1) {
row++;
n -= row;
[Link](row);