is this is right way to find nth term of an arithmetic progression

Viewed 39

is there any other way to solve it? in some tutorials they put more code above the main function like this

           import java.util.Scanner;
           public static int term(int a , int d , int n) {
           int ans = a+(n-1)*d;
           return ans;
           }
           public static void main(String [] args){
           Scanner sc = new Scanner(System.in);
           int a = sc.nextInt();
           int n = sc.nextInt();
           int d = sc.nextInt();
           
           System.out.println(term(a,d,n));
           }
           } 



        

or the below one is also right ??

     import java.util.Scanner;
     public static void main (String [] args){
     Scanner sc = new Scanner(System.in);
     int a = sc.nextInt();
     int n = sc.nextInt();\
     int d = sc.nextInt();
    
     int z = a+(n-1)*d;
     System.out.println(z);
     }

  
0 Answers
Related