A so-called “star number”, s, is a number defined by the formula: s = 6n(n-1) + 1 where n is the index of the star number. Thus the first six (i.e. for n = 1, 2, 3, 4, 5 and 6) star numbers are: 1, 13, 37, 73, 121, 181

In contrast a so-called “triangle number”, t, is the sum of the numbers from 1 to n: t = 1 + 2 + … + (n-1) + n. Thus the first six (i.e. for n = 1, 2, 3, 4, 5 and 6) triangle numbers are: 1, 3, 6, 10, 15, 21

Write a Java application that produces a list of all the values of type int that are both star number and triangle numbers.

Hey, I have some questions how to write this program, I have started it and there's what I have so far, what I want to ask is should I create an endless loop? I want to find star numbers first , then to find triangular numbers and just try if it works, then I thought of creating a function where both will be displayed. Am I on the right track?
Thanks)

import java.util.Scanner;

public class triangular_stars {

    public static void main(String[] args) {

        int stars = findStarNumber(0);
        int triangular=findTriangularNumber(0);
    }

    public static int findStarNumber(int stars) {

        int ending_number = 10;
        for (int i= 1; i <ending_number; i++) {


        int s=0;
        for(int n=1; n>i; n++){
            s= 6 * n * (n - 1) + 1;

        }
        System.out.println(i +" = " + s);


    }
        return ending_number;
    }

    public static int findTriangularNumber(int t){

        int starting_number = 1;

                int ending_number = 100;

                for (int i = starting_number; i <= ending_number; i++) {

                    int triangular = 0;


                    for (int j = 1; j <= i; j++) {

                        triangular = triangular + j;

                    }
              System.out.println(i + " = " + triangular);
                }
                return ending_number;
    }
}

I found issue in generating StarNumber in the function findStarNumber.
You just need to modify the code at line 14

14 for(int n=1; n>i; n++){ // ..should be .....> for(int n=-1; n<=i; n++){

When i ran the program for number range 1 to 100 there is only one instance where "triangular number" is equal to "star-Number" which is 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.