Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First 3 Questions' Solutions of Assignment-1 #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions session_1/solution/question_1.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//Printing First N Fibonacci Numbers

import 'dart:io';

void fibo(int n) {
int a = 0, b = 1;
int c = a + b;
print(a);
print(b);
print(c);

for (int i = 4; i <= n; i++) {
a = b;
b = c;
c = a + b;
print(c);
}
}

void main() {
print('Please enter a value for n: ');
int? n = int.parse(stdin.readLineSync()!);
print('This is the corresponding Fibonacci Series:');
fibo(n);
}
32 changes: 32 additions & 0 deletions session_1/solution/question_2.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//Checking if the given number is a Semiprime or not

import 'dart:io';

bool prime(int n) {
if (n == 1) return false;
for (int i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}

bool sempr(int n) {
if (prime(n)) return false;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
if (!(prime(i)))
return false;
else if (!(prime((n / i).floor()))) return false;
}
}
return true;
}

void main() {
print('Please enter a value for n: ');
int? n = int.parse(stdin.readLineSync()!);
if (sempr(n))
print('${n} is a SemiPrime Number');
else
print('${n} is not a SemiPrime Number');
}
36 changes: 36 additions & 0 deletions session_1/solution/question_3.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Checking whether the sum of prime elements of the array is prime or not

import 'dart:io';

bool prime(int n) {
if (n == 1) return false;

for (int i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}

void main() {
print('Please enter the number of values in the List: ');
int? siz = int.parse(stdin.readLineSync()!);
var ary = [];

for (int i = 0; i < siz; i++) {
int? k = int.parse(stdin.readLineSync()!);
ary.add(k);
}

int sum = 0;
for (int i = 0; i < siz; i++) {
if (prime(ary[i])) {
int k = ary[i];
sum += k;
}
}

if (prime(sum))
print('The Sum of Prime Elements of the entered array is PRIME!');
else
print('The Sum of Prime Elements of the entered array is NOT PRIME!');
}