Assignment 1
Assignment 1
SOURCE CODE:
Void main(){
String poem = “Twinkle, twinkle, little star, How I wonder what
you are! Up above the world so high , Like a diamond in the sky.
Twinkle, twinkle, little star, How I wonder what you are”;
List <String> lines = poem.split(‘!’);
for (int i = 0; i < lines.length; i++){
lines[i] = lines[i].trim();
if (lines[i] != ‘ ‘)
print (‘${i+2}.${lines[i]}’); }
}
}
Output: 1. Twinkle, twinkle, little star,
2. How I wonder what you are,
3. Up above the world so high,
4. like a diamond in the sky.
5. Twinkle, twinkle, little star,
6. How I wonder what you are
OBJECT 2:
Write a program in dart which accepts the radius of a circle from the
user and the compute the area.
SOURCE CODE:
Import ‘dart:io’;
Void main(){
Print (“enter the radius of a circle”);
Double radius = double.parse(stdin.readLineSync()!);
Double area = 3.142 * radius * radius;
Print (“The area of a circle is : $area”);
}
Output: 227.0095
OBJECT 3:
Write a dart program which accepts a sequence of comma- separated
numbers from user and generate a list and a tuple with those numbers.
Sample data: 3,5,7,23 Output : List : [‘3’,’5’,’7’,’23’] Set: (‘3’,’5’,’7’,’23’)
SOURCE CODE:
Import ‘dart:io’;
Void main(){
Print (“enter a sequence of comma-separated numbers”);
String input = stdin.readLineSync()!;
list <String> list = input.split(‘,’);
list = list.map((str)str.trim()).toList;
var tuple = Tuple.fromList(list);
Print(“List: $list”);
Print(“Tuple: $tuple”);
Output: List : [‘3’,’5’,’7’,’23’]
Tuple: (‘3’,’5’,’7’,’23’)
OBJECT 4:
Write a Dart program to display the examination schedule. (Extract the
date from exam_st_date).
SOURCE CODE:
void main() {
// Define the examination schedule
var examSchedule = {
'Math': { 'exam_st_date': '2024-05-20'},
'Science': { 'exam_st_date': '2024-05-22'},
'English': { 'exam_st_date': '2024-05-25'},
};
SOURCE CODE:
void main() {
int num = 25; // replace with your number
int difference = getDifference(num);
print("The difference is: $difference");
}
OBJECT 6:
Write a Dart program to calculate the sum of three given numbers, if
the values are equal then return three times of their sum.
SOURCE CODE:
void main() {
int num1 = 5;
int num2 = 5;
int num3 = 5;
int sum = num1 + num2 + num3;
if (num1 == num2 && num2 == num3) {
print("The sum is: ${sum * 3}");
} else {
print("The sum is: $sum");
}
}
OBJECT 7:
Write a Dart program to get a new string from a given string where "Is"
has been added to the front. If the given string already begins with "Is"
then return the string unchanged.
SOURCE CODE:
void main() {
String str = "Code"; // replace with your string
String newStr = str.startsWith("Is") ? str : "Is " + str;
print(newStr);
}
OBJECT 8:
Write a Dart program to find whether a given number (accept from the
user) is even or odd, print out an appropriate message to the user.
SOURCE CODE:
import 'dart:io';
void main() {
print("Enter a number: ");
int? num = int.parse(stdin.readLineSync()!);
if (num % 2 == 0) {
print("$num is even.");
} else {
print("$num is odd.");
}
}
OBJECT 9:
Write a Dart program to get the n (non-negative integer) copies of the
first 2 characters of a given string. Return the n copies of the whole
string if the length is less than 2.
SOURCE CODE:
void main() {
String str = "Hello"; // replace with your string
int n = 3; // replace with the number of copies
String result = str.length < 2 ? str * n : (str.substring(0, 2)) * n;
print(result);
}
OBJECT 10:
Write a Dart program to print all even numbers from a given numbers
list in the same order and stop the printing if any numbers that come
after 237 in the sequence.
SOURCE CODE:
void main() {
List<int> numbers = [1, 2, 3, 4, 237, 6, 8, 10]; // replace with
your list of numbers
numbers.takeWhile((num) => num != 237).where((num) =>
num % 2 == 0).forEach(print);
}
OBJECT 11:
void main() {
Print("Enter the base of the triangle: ");
double base = double.parse(stdin.readLineSync()!);
SOURCE CODE:
int gcd(int a, int b)
{ while (b != 0) {
var remainder = a % b;
a = b; b = remainder;
} return a;
} void main() { // Input two positive integers
int num1 = 36; int num2 = 48; // Calculate the GCD
int result = gcd(num1, num2); // Output the result
print('The GCD of $num1 and $num2 is $result'); }
OBJECT 13:
Write a Dart program to get the least common multiple (LCM) of two
positive integers.
SOURCE CODE:
int gcd(int a, int b) {
while (b != 0) {
int remainder = a % b;
a = b;
b = remainder;
}
return a;
}
void main() {
int num1 = 36;
int num2 = 48;
SOURCE CODE:
void main() {
// Input three integers
int num1 = 10; // Example value
int num2 = 15; // Example value
int num3 = 20; // Example value
OBJECT 15:
SOURCE CODE:
Void main(){
double amt = 1000; double rate = 3.5; int years = 7;
futurevalue = amt;
for (int i = 0; i < years; i ++){
futurevalue += futurevalue *rate/100;
}
Print (‘future value of investment is $futurevalue);
}
OBJECT:
Write a Dart program to compute the distance between the points (x1,
y1) and (x2, y2).
SOURCE CODE:
import 'dart:math';
void main() {
// Coordinates of the points
double x1 = 3;
double y1 = 4;
double x2 = 7;
double y2 = 1;
SOURCE CODE:
import 'dart:io';
void main() { // make a directory path first
String directoryPath = 'c:\Users\Dell\Desktop\lab task';
// Create a Directory object
Directory directory = Directory(directoryPath);
// Check if the directory exists
if (directory.existsSync()) {
// List all entities in the directory
List<FileSystemEntity> entities = directory.listSync();
for (var entity in entities) {
if (entity is File)
{ print(entity.path); }
}
} else { print('Directory does not exist.'); } }
Output:
OBJECT 18:
Create a program that ask the user to enter their name and their age.
Print out the message that tell how many years they have to be 100
years old.
SOURCE CODE:
import 'dart:io';
void main() {
// Ask the user to enter their name
Print('Enter your name: ');
String? name = stdin.readLineSync();
// Ask the user to enter their age
Print('Enter your age: ');
String? ageinput = stdin.readLineSync();
int age = int.parse(ageInput!);
int years = 100 - age;
Print('$name, you have $years years until you turn 100 years
old.'); }
Output: ‘laiba, you have 82 years until you turn 100 years old’
OBJECT 19:
Take a list, for example a = (1, 2, 3, 5, 8, 13, 21, 34, 55, 89) and write a
program out all elements of the list that are less than 5.
SOURCE CODE:
void main() {
List<int> a = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
for (int num in a) {
if (num < 5) {
print(num); }
}
}
OBJECT 20:
Create a program that ask the user for a number and then print out a
list of all the divisors of that numbers.
SOURCE CODE:
import 'dart:io';
void main() {
// Ask the user for a number
stdout.write('Enter a number: ');
int number = int.parse(stdin.readLineSync()!);
// Print out the divisors of the number
Print('The divisors of $number are:');
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
print(i); }
}
}
OBJECT 21:
Take two list, for example a = (1, 2, 3, 5, 8, 13, 21, 34, 55, 89) & b = (1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13). Write a program that return a list
that contain only the elements that are common them (without
duplicate). Make sure your program works on two list of different sizes.
SOURCE CODE:
void main() {
List<int> a = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
List<int> b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
// Create a set to store common elements
Set<int> commonElements = {};
// Iterate through list a
for (int num in a) {
if (b.contains(num))
{ commonElements.add(num); }
}
List<int> result = commonElements.toList(); print(result); }
NAME : LAIBA KHALIQ
SECTION : A
COURSE : PROGRAMMING
FUNDAMENTAL
YEAR : 1st
Semester : 1
st
OBJECT 22:
Write a dart program that asks for number of days a tourist group stays
then it keeps taking information about each member of the group that
comprises of his/her name and age. Your program should print total
amount that the group has to pay. Conditions for calculations are as
follows:
1)Hotel’s room rent policy;
a. 2 people for 5000 rupees.
b. 3 people for 6000 rupees.
c. 4 people for 7000 rupees.
d. 5 or more people Rs. 1500 per person.
2) There is a 50% discount for senior citizen (age 60 or above).
SOURCE CODE:
import 'dart:io';
void main() {
print("Enter the number of days the group stays: ");
int days = int.parse(stdin.readLineSync()!);
print("The total amount the group has to pay is: Rs. $totalAmount");
}
if (totalMembers == 2) {
roomRent = 5000;
} else if (totalMembers == 3) {
roomRent = 6000;
} else if (totalMembers == 4) {
roomRent = 7000;
} else if (totalMembers >= 5) {
roomRent = totalMembers * 1500;
}
return totalAmount;
}
Output:
Enter the number of tourists:
2
Enter the number of days:
5
Enter the name of tourist 1:
Ali
Enter the age of tourist 1:
23
Enter the name of tourist 2:
Usman
Enter the age of tourist 2:
75
The total amount the group has to pay is:18750 rs.
OBJECT 23:
SOURCE CODE:
OUTPUT:
OBJECT 26:
SOURCE CODE:
import ‘dart:io’;
void main() {
String s = “/Users/laibakhaliq/Downloads/SalesDataUNICODE.txt”;
File f = File(p);
Double sum = 0;
OUTPUT:
OBJECT 25:
SORCE CODE:
import ‘dart:io’;
void main() {
String s = “/Users/laibakhaliq/Downloads/SalesDataUNICODE.txt”;
File f = File(p);
double sum1 = 0;
double sum2 = 0;
double sum3 = 0;
double sum4 = 0;
List<String>lines = f.readAs LinesSync();
for ( int i = 1; i < lines.length; i++) {
List<String> field = lines[i].split(“\t”);
if ( field[7] == ‘product A’ ) {
sum1 = sum + double.parse(field[8]);
} else if ( field[7] == ‘product B’ ) {
sum2 = sum + double.parse(field[8]);
} else if ( field[7] == ‘product C’ ) {
sum3 = sum + double.parse(field[8]);
} else if ( field[7] == ‘product D’ ) {
sum4 = sum + double.parse(field[8]);
}
}
print(“The total sum of product A is $sum1”);
print(“The total sum of product B is $sum2”);
print(“The total sum of product C is $sum3”);
print(“The total sum of product D is $sum4”);
OUTPUT:
OBJECT 24:
SOURCE CODE:
import 'dart:io';
import 'dart:math';
void displayFile(String filePath)
{
displayFile("C:\\temp\\yyyyyyyy.txt");
}
on RangeError
{
print ("index number should be from 0 to 3");
}
on FormatException
{
print ("Enter some valid double value");
}
catch (x, y)
{
print ("Some error occured");
print ("\n\n\n$x\n\n\n");
print ("\n\n\n$y\n\n\n");
}
q = n /d;
print ("$n/$d = $q");
}
OUTPUT:
Enter the neumerator:
3
Enter the denominator:
12
Enter index of the value to be fetched:
3
Value at index 3 is true
2.0/12.0 = 0.16666666666666666