Java Program to print 1,4,7,10,13… series up to N

import java.util.Scanner;

public class Program 
{
    	public static void main( String[] args ) 
    	{
		Scanner reader = new Scanner(System.in);
		int a;
 		int i;

		System.out.print("\nEnter Last Number : ");
 		a = reader.nextInt();
 		for(i=0;i<=a;i++)
 		{
 			if(i%3==1)
 			{
				System.out.print(i);	
				if(i<a)
 					System.out.print(",");
			}
	 	}		
    	}
}

Output :
Enter Last Number : 25
1,4,7,10,13,16,19,22,25,



Leave a comment