Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

OBJECT 1:

Write a program in dart to print the following string in a specific format


sample : “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” 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

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'},
};

// Display the examination schedule


print("Examination Schedule:");
examSchedule.forEach((subject, date) {
print("Subject: $subject, Date: $date");
});
}
Output:
Examination Schedule:
Subject: Math, Date: {exam_st_date: 2024-05-20}
Subject: Science, Date: {exam_st_date: 2024-05-22}
Subject: English, Date: {exam_st_date: 2024-05-25}
OBJECT 5:
Write a Dart program to get the difference between a given number
and 17, if the number is greater than 17 return double the absolute
difference.

SOURCE CODE:
void main() {
int num = 25; // replace with your number
int difference = getDifference(num);
print("The difference is: $difference");
}

int getDifference(int num) {


int difference = num - 17;
if (num > 17) {
return difference * 2;
} else {
return difference.abs();}

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()!);

Print("Enter the height of the triangle: ");


double height = double.parse(stdin.readLineSync()!);
double area = calculateTriangleArea(base, height);
area = base * height;
Print("The area of the triangle is: $area");
}
OBJECT 12:
Write a Dart program to compute the greatest common divisor (GCD)
of two positive integers.

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;
}

int lcm(int a, int b) {


return (a * b) ~/ gcd(a, b);
}

void main() {
int num1 = 36;
int num2 = 48;

int result = lcm(num1, num2);

print('The LCM of $num1 and $num2 is $result');}


OBJECT 14:

Write a Dart program to sum of three given integers. However, if two


values are equal sum will be zero.

SOURCE CODE:

int sumofThree(int a, int b, int c) {


if (a == b || b == c || a == c) {
return 0;
}
return a + b + c;
}

void main() {
// Input three integers
int num1 = 10; // Example value
int num2 = 15; // Example value
int num3 = 20; // Example value

// Calculate the sum


int result = sumofThree(num1, num2, num3);

// Output the result


print('The sum of $num1, $num2, and $num3 is $result');
}

OBJECT 15:

Write a Dart program to compute the future value of a specified


principal amount, rate of interest, and a number of years. Test Data :
amt = 10000, int = 3.5, years = 7

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;

// Calculate the distance


double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
Print('The distance between the points ($x1, $y1) and ($x2,
$y2) is $distance');
}
OBJECT 17:
Write a Dart program to list all files in a directory.

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("Enter the total number of members in the group: ");


int totalMembers = int.parse(stdin.readLineSync()!);

List<Map<String, dynamic>> membersInfoBelow60 = [];


List<Map<String, dynamic>> membersInfoAbove60 = [];

for (int i = 0; i < totalMembers; i++) {


String name;
int age;
while (true) {
print("Enter the name of member ${i + 1}: ");
name = stdin.readLineSync()!;
if (name.isEmpty) {
print("Name cannot be empty. Please enter a valid name.");
continue;
}
print("Enter the age of member ${i + 1}: ");
age = int.parse(stdin.readLineSync()!);
if (age < 0) {
print("Age cannot be negative. Please enter a valid age.");
continue;
}
break;
}
if (age < 60) {
membersInfoBelow60.add({"name": name, "age": age});
} else {
membersInfoAbove60.add({"name": name, "age": age});
}
}

int totalAmount = calculateTotalAmount(days,


membersInfoBelow60.length, membersInfoAbove60.length);

print("The total amount the group has to pay is: Rs. $totalAmount");
}

int calculateTotalAmount(int days, int membersBelow60, int


membersAbove60) {
int totalMembers = membersBelow60 + membersAbove60;
int roomRent = 0;

if (totalMembers == 2) {
roomRent = 5000;
} else if (totalMembers == 3) {
roomRent = 6000;
} else if (totalMembers == 4) {
roomRent = 7000;
} else if (totalMembers >= 5) {
roomRent = totalMembers * 1500;
}

int totalRoomRent = roomRent * days;


int perPersonRoomRent = totalRoomRent ~/ totalMembers;
int discount = (perPersonRoomRent ~/ 2) * membersAbove60;

int totalAmount = totalRoomRent - discount;

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:

Write a program in dart to perform basic operation.

SOURCE CODE:

// Perform basic operation


Void main() {
Double n1, n2, n3, result;
String operation;
print (“Enter the first number”);
n1 = double.parse(Stdin.readLineSync()!);
print (“Enter the second number”);
n2 = double.parse(Stdin.readLineSync()!);
print (“Enter the third number”);
n3 = double.parse(Stdin.readLineSync()!);
print (“Enter the operator (+,-,*,/):”);
operator = Stdin.readLineSync()!;
if (operator == ‘+’) {
result = n1 + n2;
print (result);
} else if (operator == ‘-‘) {
result = n1 – n2;
print (result);
} else if (operator == ‘*’) {
result == n1 * n2;
print (result);
} else if (operator == ‘/’) {
if (n2 != 0) {
result == n1 / n2;
print (result);
} else {
print (“Error! Division by zero is not allowed”);
}
} else {
print (“Invalid operator”);
}
}

OUTPUT:

Enter the first number


2
Enter the first number
3
Enter the operator (+,-,*,/):
+
5.0

Enter the first number


3
Enter the second number
4
Enter the operator (+,-,*,/):
*
12.0

OBJECT 26:

Write a program in dart to find the grand total sum of products.

SOURCE CODE:

import ‘dart:io’;
void main() {
String s = “/Users/laibakhaliq/Downloads/SalesDataUNICODE.txt”;
File f = File(p);
Double sum = 0;

List<String> Lines = f.readAsLineSync();


for ( int i = 1; i < lines.length; i++) {
List<String> field = lines[i].split(“\t”);
sum = sum + double.parse(field[8]);
}
Print(“the grand total sum is $sum”);
}

OUTPUT:

The grand total sum is 9534.0

OBJECT 25:

Write a program to calculate the total sum of every product of sales


data.

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:

The total sum of product A is 2340.0


The total sum of product B is 4470.0
The total sum of product C is 1864.0
The total sum of product D is 860.0

OBJECT 24:

Write a Exception handling program in dart.

SOURCE CODE:
import 'dart:io';
import 'dart:math';
void displayFile(String filePath)
{

File c = File (filePath);


print (c.readAsStringSync());
}
void main () {

double n=1, d = 0, q=-10;


List a = [1,2.2,"UBIT",true];
try
{
print ("Enter the neumerator :");
n = double.parse(stdin.readLineSync()!);
print ("Enter the denominator :");
d = double.parse(stdin.readLineSync()!);

print ("Enter index of the value to be fetched ");


int i = int.parse(stdin.readLineSync()!);
print ("Value at index $i is ${a[i]}");

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

You might also like