diff --git a/6thJune_Dyang.java b/6thJune_Dyang.java new file mode 100644 index 000000000..a7096a1d4 --- /dev/null +++ b/6thJune_Dyang.java @@ -0,0 +1,44 @@ +// PROGRAM TO: Print Fibonacci numbers up to 'n' numbers and replacing multiples of 5 and prime numbers with 0 +//test + + +import java.util.*; + +public class fibonacci +{ + public static void main(String args[]) + { + Scanner inp = new Scanner(System.in); + + int n, sum, n1 = 1, n2 = 1, marker; + System.out.println("Enter the limit:"); + n = inp.nextInt(); + System.out.print(n1); + System.out.print(" "); + System.out.print(n2); + System.out.print(" "); + for (int i = 2; i < n; i++) { + marker = 0; + sum = n1+n2; + if (sum %5 ==0) + System.out.print("0 "); + else { + for (int j = 2; j < sum /2; j++) + { + if (sum % j == 0) + { + System.out.print(sum+" "); + marker = 1; + break; + } + } + if (marker == 0) + System.out.print("0 "); + } + + n1 = n2; + n2 = sum; + } + } +} +//test diff --git a/fibonacci-6thJune.cpp b/fibonacci-6thJune.cpp new file mode 100644 index 000000000..fea67ee72 --- /dev/null +++ b/fibonacci-6thJune.cpp @@ -0,0 +1,52 @@ +//Fibonacci Series with a Twist + +#include +using namespace std; + +bool prime(int n) +{ + if (n <= 1) + return false; + + + for (int i = 2; i <= sqrt(n); i++) + if (n % i == 0) + return false; + + return true; +} + +int fibonacci(int n) +{ + int a = 1, b = 1, c, i; + //Corner case + if( n == 0) + return 0; + else if ( n == 1 ) + { + cout << "1 "; + return 0; + } + cout << "1 1 "; + for(i = 3; i <= n; i++) + { + c = a + b; + //Condition check + if ( c % 5 != 0 && !prime( c )) + cout << c << " "; + else + cout << "0 "; + a = b; + b = c; + } + return b; +} + +int main() +{ + int n = 0; + cout << " Please enter the value of n : "; + cin >> n; + fibonacci(n); + return 0; +} \ No newline at end of file