Name: Mohammed Alquraish: What The Question Is Asking

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

NAME: MOHAMMED ALQURAISH

WHAT THE QUESTION IS ASKING:

Write a Program to print 1st 40 Fibonacci numbers & print numbers that are
divisible by 5 and 7?

REFERENCE:

I found about this series on MATHISFUN.com

https://www.mathsisfun.com/numbers/fibonacci-sequence.html

ASSUMPTION AND GENERAL OBSERVATION:

I observe that this series is working on some formula then I searched and I tried to
make it.

CALCULATION AND OTHER WORK:

A number adds with previous number which is a number before that like:

0 (0) before zero and if we add it with an answer of that num


1 (0+1) zero is ans of previous num and adding current num
1 (0+1) zero was before previous num and added with current ans.
2 (1+1)
3 (1+2)
5 (2+3)
8 (3+5)
13 (5+8)
.
.
.
.
.

Code:

import java.awt.*;
import java.util.ArrayList;

public class ListFibonacci {


public static void main(String[] args) {
int n = 40, ans = 0, fb = 1;
ArrayList<Integer> arr = new ArrayList<Integer>();// Made Array For Saving
Fibonacci Series

System.out.print("First " + n + " terms: \n");


for (int i = 1; i <= n; ++i)
{
System.out.print(i+". \t"+ans + "\n");
arr.add(i-1,ans); //placing value of ans in array

int add = ans + fb;


ans = fb;
fb = add;

System.out.println("\n\nNumbers Divisible by 5.");


System.out.println("Numbers are: \n");
for (int i = 1; i <= n ; i++) {
int t = arr.get(i-1); //saving current array-index value in 't'
if (t % 5 == 0) //If number divide by 5 gives remainder 0
{
System.out.println(t);
}
}

System.out.println("\nNumbers Divisible by 7.");


System.out.println("Numbers are: \n");
for (int i = 1; i <= n ; i++) {
int t = arr.get(i-1);
if (t % 7 == 0)
{
System.out.println(t);
}
}

System.out.println("\nNumbers Divisible by 5 & 7.");


System.out.println("Numbers are: \n");
for (int i = 1; i <= n ; i++) {
int t = arr.get(i-1);
if (t % 5 == 0)
{
System.out.println("By 5: \t"+t);
}
else if (t % 7 == 0)
{
System.out.println("\t\t\t\t\tBy 7: \t"+t);
}
else
{

}
}
}

Output:
DISCUSSION OF SOLUTION:

Here I made formula and also made list for storing number of Fiboacci series to
find or to check which numbers are divisible by 5 and 7.
Sign of %MOD use to check remainder, to declare list type I searched on net so for
‘int’ wo write ‘Integer’ to declare its type, used ‘t’ as temporary variable to store
current value of array/list like ‘arr.get(i)’ here ‘i’ is used for index number to get
value of that index & get is built-in function of list. And arr.add(i-1, ans) I used ‘i-
1’ because I started loop from ‘1’ and array/list started index is ‘0’ so ‘1-1 = 0’ and
its index number of array/list to where to store and ‘ans’ is variable used for
Fibonacci Series. And then I did swapping to change value of variable ‘ans’ and
did addition for formula in variable ‘add’.

You might also like