ASSIGNMENT 2 OF FLUTTER (ANSWER ALL
QUESTIONS AS THIS IS MY ASSIGNMENT)
Q1. Write a Dart program to print Fibonacci series up to n terms.
Q2. Create a Dart function to calculate the factorial of a number recursively.
Q3. Demonstrate the use of List and Map in Dart with custom data.
Q4. Write a Dart program to reverse a number and check if it is a palindrome.
Q5. Build a Dart class named 'BankAccount' with deposit and withdraw methods.
Q6. Write a Dart program to check whether a given string is a palindrome.
Q7. Create a Dart class hierarchy using inheritance (Animal → Dog).
Here are concise, ready-to-run Dart solutions for all seven tasks, written in clear
idiomatic Dart and suitable for use in Flutter projects or standalone Dart scripts .
[1]
Q1 Fibonacci series
This prints the first n Fibonacci numbers iteratively starting from 0 and 1, following the
recurrence F (n)=F (n −1)+ F(n− 2) with the usual base values F (0)=0 , F (1)=1 [2][3][4]. The
iterative approach avoids the exponential cost of naive recursion and is a common,
efficient pattern in Dart for sequence generation .
[3][1]
import 'dart:io';
void printFibonacci(int n) {
int a = 0, b = 1;
for (int i = 0; i < n; i++) {
stdout.write('${a}${i < n - 1 ? ' ' : ''}');
final next = a + b;
a = b;
b = next;
}
stdout.writeln();
}
void main() {
final n = 10; // change as needed
printFibonacci(n);
}
Q2 Recursive factorial function
This defines a recursive factorial with a base case n ≤ 1⇒ 1 , and the recurrence
n ⋅factorial (n− 1), which is a standard demonstration of recursion in Dart and is widely
taught in Dart tutorials . Negative inputs are rejected to keep the domain to
[5][6]
nonnegative integers, mirroring typical examples .
[5][1]
int factorial(int n) {
if (n < 0) {
throw ArgumentError('Factorial is undefined for negative numbers');
}
return (n <= 1) ? 1 : n * factorial(n - 1);
}
void main() {
print(factorial(5)); // 120
}
Q3 Using List and Map with custom data
This example shows a List of custom objects, derives a List of names via map/toList, and
constructs a Map keyed by name with age values—patterns that align with common Dart
collection usage in app code . The code uses Dart language features for classes, lists,
[7][8]
maps, and collection-for, as covered in the language tour .
[1]
class Person {
final String name;
final int age;
Person(this.name, this.age);
Map<String, dynamic> toMap() => {'name': name, 'age': age};
}
void main() {
final people = <Person>[
Person('Asha', 30),
Person('Ravi', 25),
Person('Meera', 28),
];
// List from custom data
final names = people.map((p) => p.name).toList();
print(names); // [Asha, Ravi, Meera]
// Map from custom data
final ageByName = {for (final p in people) p.name: p.age};
print(ageByName); // {Asha: 30, Ravi: 25, Meera: 28}
// Convert back to List of maps (e.g., for JSON-like structures)
final peopleAsMaps = people.map((p) => p.toMap()).toList();
print(peopleAsMaps);
}
The transformation patterns (map, toList, Map.fromIterable/for) are standard for
converting between List and Map in Dart .
[8][7][1]
Q4 Reverse a number and check palindrome
This reverses an integer using arithmetic (modulo/division) and compares the reversed
value to the original to determine if it is a numeric palindrome, as outlined in classic Dart
palindrome-number examples . Negative numbers are marked non-palindromic here by
[9]
convention, consistent with common practice in such exercises .
[9]
bool isNumericPalindrome(int number) {
if (number < 0) return false;
int original = number;
int reversed = 0;
while (number > 0) {
final digit = number % 10;
reversed = reversed * 10 + digit;
number ~/= 10;
}
return original == reversed;
}
void main() {
print(isNumericPalindrome(54545)); // true
print(isNumericPalindrome(12345)); // false
}
Q5 BankAccount class with deposit/withdraw
This defines a BankAccount class with constructor, private balance field, getter, and
validated deposit/withdraw methods, following idiomatic Dart class syntax, getters, and
exceptions per the language tour . The underscore prefix scopes _balance to the library,
[1]
which is Dart’s standard privacy mechanism for fields .
[1]
class BankAccount {
final String accountNumber;
double _balance;
BankAccount({required this.accountNumber, double initialBalance = 0})
: _balance = initialBalance;
double get balance => _balance;
void deposit(double amount) {
if (amount <= 0) {
throw ArgumentError('Deposit amount must be positive');
}
_balance += amount;
}
void withdraw(double amount) {
if (amount <= 0) {
throw ArgumentError('Withdrawal amount must be positive');
}
if (amount > _balance) {
throw StateError('Insufficient funds');
}
_balance -= amount;
}
@override
String toString() => 'BankAccount($accountNumber, balance: $_balance)';
}
void main() {
final acc = BankAccount(accountNumber: 'IN-001', initialBalance: 5000);
acc.deposit(1500);
acc.withdraw(2000);
print(acc.balance); // 4500
}
Q6 Palindrome string check
Two common approaches are shown in Dart: a two-pointer comparison from both ends,
and a direct reverse-equality check using split/reversed/join, both of which are standard
patterns in community examples . The example trims and lowercases the input to do
[10][11]
a simple normalized comparison for typical palindrome checks, which mirrors simple
Flutter/Dart tutorials on palindrome checkers .
[12][10]
bool isPalindromeTwoPointer(String input) {
int i = 0, j = input.length - 1;
while (i < j) {
if (input[i] != input[j]) return false;
i++;
j--;
}
return true;
}
bool isPalindromeReverse(String input) {
return input == input.split('').reversed.join();
}
void main() {
final s = 'Madam';
final normalized = s.toLowerCase().replaceAll(RegExp(r'[^a-z0-9]'), '');
print(isPalindromeTwoPointer(normalized)); // true
print(isPalindromeReverse(normalized)); // true
}
The two-pointer loop mirrors the widely cited StackOverflow solution, and the reverse-
equality method uses Dart’s split + reversed + join idiom for simple string reversal .
[10][11]
Q7 Inheritance: Animal → Dog
This demonstrates single inheritance with Dog extending Animal and overriding a method
—exactly how Dart implements class inheritance with extends, as covered in standard
tutorials . The example also shows calling the superclass constructor via super and
[13][14]
method overriding, which are part of Dart’s core OOP features .
[1]
class Animal {
final String name;
Animal(this.name);
void speak() => print('$name makes a sound');
}
class Dog extends Animal {
Dog(String name) : super(name);
@override
void speak() => print('$name barks');
}
void main() {
final pet = Dog('Bruno');
pet.speak(); // Bruno barks
}
This pattern illustrates single-level inheritance and method overriding in Dart, consistent
with common examples such as Human/Person or Car/Toyota in tutorials .
[13][14][1]
1. https://dart.dev/language
2. https://pub.dev/documentation/fibonacci/latest/
3. https://www.linkedin.com/pulse/understanding-fibonacci-sequence-dart-recursive-vs-code-githinji-
5uzrf
4. http://progopedia.com/example/fibonacci/488/
5. https://www.tutorialkart.com/dart/dart-program-factorial/
6. https://www.youtube.com/watch?v=uJOM86jOCoE
7. https://www.dbestech.com/tutorials/understanding-dart-map-and-list-for-flutter-development-
explained-step-by-step-when-and-how-to-use
8. https://www.dhiwise.com/post/dart-list-to-map-a-comprehensive-guide-to-data-conversion
9. https://protocoderspoint.com/palindrome-program-dart/
10. https://stackoverflow.com/questions/13111321/compare-a-string-left-to-right-right-to-left-in-dart
11. https://www.educative.io/answers/how-to-reverse-a-string-in-dart
12. https://www.geeksforgeeks.org/flutter/flutter-palindrome-checker-app/
13. https://www.tutorialspoint.com/inheritance-in-dart-programming
14. https://dart-tutorial.com/object-oriented-programming/inheritance-in-dart/
15. https://ijmra.in/v7i11/16.php
16. https://iopscience.iop.org/article/10.1088/1361-6463/ad2aaf
17. https://www.tandfonline.com/doi/full/10.1080/03772063.2023.2269120
18. https://ieeexplore.ieee.org/document/10420209/
19. https://journalofinequalitiesandapplications.springeropen.com/articles/10.1186/s13660-024-03090-
9
20. https://link.springer.com/10.1007/s13370-023-01108-x
21. https://wseas.com/journals/equations/2024/a08equations-004(2024).pdf
22. https://link.aps.org/doi/10.1103/PhysRevE.111.025103
23. https://pubs.aip.org/adv/article/14/1/015233/3043795/More-generalized-k-Fibonacci-sequence-
series-and
24. https://ieeexplore.ieee.org/document/9864978/
25. http://arxiv.org/pdf/2310.04439.pdf
26. https://arxiv.org/pdf/1107.1858.pdf
27. https://arxiv.org/pdf/2409.02946.pdf
28. http://arxiv.org/pdf/2407.04409.pdf
29. https://nntdm.net/papers/nntdm-30/NNTDM-30-1-067-080.pdf
30. https://nntdm.net/papers/nntdm-29/NNTDM-29-4-789-793.pdf
31. http://arxiv.org/pdf/1909.01938.pdf
32. http://arxiv.org/pdf/2309.14501.pdf
33. http://arxiv.org/pdf/2409.00229.pdf
34. https://www.mdpi.com/2227-7390/9/16/1928/pdf
35. https://www.youtube.com/shorts/zdrpIiwUCnM
36. https://programming-idioms.org/idiom/301/recursive-fibonacci-sequence/5576/dart
37. https://www.geeksforgeeks.org/dart/dart-recursion/
38. https://ieeexplore.ieee.org/document/10271062/
39. https://arxiv.org/abs/2210.02000
40. http://journals.sagepub.com/doi/10.1177/0148333116658854
41. https://dsr.ju.edu.jo/djournals/index.php/Hum/article/view/5836
42. https://dl.acm.org/doi/10.1145/3558481.3591090
43. https://studia.reviste.ubbcluj.ro/index.php/subbmusica/article/view/2881
44. https://akjournals.com/view/journals/6/62/3-4/article-p291.xml
45. https://dmtcs.episciences.org/12385
46. https://link.springer.com/10.1007/s00453-022-01066-z
47. https://pubs.aip.org/aip/acp/article-lookup/doi/10.1063/5.0192296
48. https://arxiv.org/pdf/1608.05550.pdf
49. http://arxiv.org/pdf/2406.04507.pdf
50. http://arxiv.org/pdf/1308.3466.pdf
51. https://arxiv.org/pdf/1703.08931.pdf
52. https://arxiv.org/pdf/2206.12600.pdf
53. http://arxiv.org/pdf/2401.07351.pdf
54. https://arxiv.org/pdf/1609.03000.pdf
55. https://arxiv.org/html/2503.16781v2
56. https://arxiv.org/pdf/2210.02067.pdf
57. https://arxiv.org/pdf/1506.04862.pdf
58. https://www.linkedin.com/posts/aditya-kumar-software-developer-engineer_flutter-dart-flutterjobs-
activity-7309999678505238529-caLr
59. https://www.youtube.com/watch?v=kicWR_7Sbuc
60. https://dart-tutorial.com/dart-how-to/reverse-a-list-in-dart/
61. https://pub.dev/documentation/dartutilities/latest/strings_is_palindrome/isPalindrome.html