6. Write a Java program that will print the following series until it reaches 100.
1 1 2 3 5 8 13 …………………………
7. Write a java program to find the sum of the series up to the nth term where n is input by the user.
1+(1+n)+(1+n+n)+(1+n+n+n)……………….nth Term
Example: input n = 6
Output: 1+7+13+19+25+31 = 96
public class App {
/**
* The start point of the program
*
* @param args
*/
public static void main(String[] args) {
int[] fibonacciSequence = new int[100];
fibonacciSequence[0] = 1;
fibonacciSequence[1] = 1;
for (int i = 2; i < 12; i++) {
fibonacciSequence[i] = fibonacciSequence[i - 1] + fibonacciSequence[i - 2];
}
System.out.println("Fibonacci sequence: ");
for (int i = 0; i < 12; i++) {
System.out.print(fibonacciSequence[i] + " ");
}
}
}
import java.util.*;
class App {
static int sumN(int n, int k) {
int sum = 1;
for (int i = 0; i < k; i++) {
sum += n;
}
return sum;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter n: ");
int n = keyboard.nextInt();
int sum = 1;
int k;
System.out.print(sum + "+");
for (int i = 1; i < n - 1; i++) {
k = sumN(n, i);
sum += k;
System.out.print(k + "+");
}
k = sumN(n, n - 1);
sum += k;
System.out.print(k + " = " + sum);
keyboard.close();
}
}
Comments
Leave a comment