Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sum of natural numbers class SumOfNaturalNumbers {

Similar presentations


Presentation on theme: "Sum of natural numbers class SumOfNaturalNumbers {"— Presentation transcript:

1 Sum of natural numbers class SumOfNaturalNumbers {
public static void main (String arg[]) { int m = 3, n = 10; if (m > n) { System.out.println (“Invalid input!”); } else { System.out.println (“Sum of numbers from ” + m + “ to ” + n + “ is ” + Sum(m, n));

2 Sum of natural numbers public static int Sum (int m, int n) {
int x, y; if (m==n) return m; x = Sum(m, (m+n)/2); y = Sum((m+n)/2+1, n); return (x+y); } } // end class

3 Fibonacci series class fib { public static long fibonacci(long n){
if (n==1) fib = 0; else if (n==2) fib = 1; else fib = fibonacci(n-1) + fibonacci(n-2); return fib; } public static void main (String args[]) { long i, x; long n = 17; for (i=1; i<=n; i++) { x = fibonacci(i); System.out.println(x);


Download ppt "Sum of natural numbers class SumOfNaturalNumbers {"

Similar presentations


Ads by Google