Write a program in java to display first 20 numbers of Harsh Series: 1, 2, 5, 12, 29, 70 using following method prototype: void harsh_series()
Hint: The series starts with 1 & 2 and subsequent numbers are the sum of twice the previous number and number previous to previous number.
import java.util.Scanner;
public class Main {
static void harsh_series(){
{
int x,m=1,n=0,y;
System.out.println("First 20 Harsh series: ");
for(x=1; x<=20; x++)
{
y= m + 2*n;
System.out.print(y+" ");
m = n;
n = y;
}
}
}
public static void main(String args[]){
harsh_series();
}}
Comments
Leave a comment