0% found this document useful (0 votes)
17 views15 pages

Mussaddiq 1038 080153

The document contains 16 Dart programs that demonstrate the use of conditional statements and loops to solve various problems. The programs check if a number is even or odd, compare two numbers, validate a user's age for voting eligibility, determine if a year is a leap year, classify a number as positive, negative or zero, find the largest of three numbers, calculate sums and products within a range, generate passwords, count character occurrences in a string, display multiplication tables, count digits, convert decimals to binary, calculate greatest common divisor (GCD), and print star patterns. The programs are run sequentially with output displayed after each one.

Uploaded by

noorfatima.okit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views15 pages

Mussaddiq 1038 080153

The document contains 16 Dart programs that demonstrate the use of conditional statements and loops to solve various problems. The programs check if a number is even or odd, compare two numbers, validate a user's age for voting eligibility, determine if a year is a leap year, classify a number as positive, negative or zero, find the largest of three numbers, calculate sums and products within a range, generate passwords, count character occurrences in a string, display multiplication tables, count digits, convert decimals to binary, calculate greatest common divisor (GCD), and print star patterns. The programs are run sequentially with output displayed after each one.

Uploaded by

noorfatima.okit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Subject : MAD

Mussaddiq Mahmood
INSTRUCTOR: Imdad Hussain
mussaddiqmahmood@gmail.com

// 1. Write a Dart program to check if a number is even or odd using an if statement.

void one(){

int num = 10; // change this to any number

if (num % 2 == 0) {

print("$num is even");

} else {

print("$num is odd");

// 2. Create a program that compares two numbers and prints the larger one using if-else.

void two() {

int num1 = 15; // change this to any number

1
int num2 = 20; // change this to any number

if (num1 > num2) {

print("$num1 is larger than $num2");

} else if (num1 < num2) {

print("$num2 is larger than $num1");

} else {

print("$num1 and $num2 are equal");

// 3. Implement a Dart program that checks if a user's age is eligible to vote (18 and above).

void three() {

int age = 17; // change this to any age

if (age >= 18) {

print("You are eligible to vote");

} else {

print("You are not eligible to vote");

2
// 4. Write a program to determine if a given year is a leap year or not using if-else.

void four() {

int year = 2020; // change this to any year

if (year % 4 == 0) {

if (year % 100 == 0) {

if (year % 400 == 0) {

print("$year is a leap year");

} else {

print("$year is not a leap year");

} else {

print("$year is a leap year");

} else {

print("$year is not a leap year");

// 5. Create a Dart program that classifies a given number as positive, negative, or zero.

3
void five() {

int num = -5; // change this to any number

if (num > 0) {

print("$num is positive");

} else if (num < 0) {

print("$num is negative");

} else {

print("$num is zero");

void main()

print("\n\nONE");

one();

print("\n\nTWO");

two();

print("\n\nTHREE");

three();

print("\n\nFOUR");

4
four();

print("\n\nFIVE");

five();

// Program 6: Find the largest of three numbers using nested `if` statements.

void six(int a, int b, int c) {

if (a >= b) {

if (a >= c) {

print("$a is the largest number");

} else {

5
print("$c is the largest number");

} else {

if (b >= c) {

print("$b is the largest number");

} else {

print("$c is the largest number");

// Program 7: Find the sum of all even numbers and the product of all odd numbers within
a specified range.

void seven(int start, int end) {

int evenSum = 0;

int oddProduct = 1;

for (int i = start; i <= end; i++) {

if (i % 2 == 0) {

evenSum += i;

6
} else {

oddProduct *= i;

print("The sum of all even numbers in the range $start to $end is $evenSum");

print("The product of all odd numbers in the range $start to $end is $oddProduct");

// Program 8: Generate a random password with varying complexity.

/*String eight(int passwordLength, int passwordComplexity) {

String password = "";

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

switch (passwordComplexity) {

case 1: // low complexity

password += String.fromCharCode(Random().nextInt(26) + 97); // a-z

break;

case 2: // medium complexity

password += String.fromCharCode(Random().nextInt(62) + 33); // a-z, A-Z, 0-9

break;

7
case 3: // high complexity

password += String.fromCharCode(Random().nextInt(95) + 32); // a-z, A-Z, 0-9, symbols

break;

return password;

}*/

// Program 9: Count the occurrence of each character in a given string.

Map<String, int> nine(String inputString) {

Map<String, int> characterCount = {};

for (int i = 0; i < inputString.length; i++) {

String character = inputString[i];

if (characterCount.containsKey(character)) {

characterCount[character] = characterCount[character]! + 1;

} else {

characterCount[character] = 1;

8
return characterCount;

// Program 10: Simulate a basic menu ordering system for a restaurant.

/*void ten() {

// Define the menu items and their prices

Map<String, double> menuItems = {

"Pizza": 10.00,

"Burger": 15.00,

"Fries": 5.00,

"Soda": 2.50,

};

// Get the user's order

print("Welcome to the menu ordering system!");

print("What would you like to order?");

String order = stdin.readLineSync()!;

// Calculate the total bill

9
double totalBill = 0.00;

if (menuItems.containsKey(order)) {

totalBill = menuItems[order]!;

} else {

print("Invalid item");

return;

// Print the total bill

print("Your total bill is $totalBill");

}*/

void main()

print("\n\nSIX");

six(8,6,8);

print("\n\nSEVEN");

seven(5,9);

//printf("\n\nEIGHT");

//eight();

10
print("\n\nNINE");

print(nine("I am Mussaddiq Mahmood"));

//printf("\n\nTEN");

//ten();

Note: Question 8 and 10 are using dart:io but onmine compiler not supportted that.

// Program 11: Display the multiplication table of a given number using loops.

void displayMultiplicationTable(int number) {

for (int i = 1; i <= 10; i++) {

print("$number x $i = ${number * i}");

11
}

// Program 12: Count the number of digits in a given number using loops.

int countDigits(int number) {

int digitCount = 0;

while (number != 0) {

number = number ~/ 10;

digitCount++;

return digitCount;

// Program 14: Convert a decimal number to binary using loops.

String decimalToBinary(int decimalNumber) {

String binaryNumber = "";

while (decimalNumber != 0) {

binaryNumber = (decimalNumber % 2).toString() + binaryNumber;

decimalNumber = decimalNumber ~/ 2;

12
}

return binaryNumber;

// Program 15: Find the greatest common divisor (GCD) of two numbers using loops.

int gcd(int a, int b) {

while (b != 0) {

int remainder = a % b;

a = b;

b = remainder;

return a;

// Program 16: Print a pattern of stars in the form of a right-angled triangle.

void printRightAngledTriangle(int height) {

for (int i = 1; i <= height; i++) {

for (int j = 1; j <= i; j++) {

print("*");

13
}

print("\n");

void main() {

// Call the program functions

print("\n\n11th");

displayMultiplicationTable(5);

print("\n\n12th");

int digitCount = countDigits(12345);

print("The number of digits in 12345 is $digitCount");

print("\n\n14th");

String binaryNumber = decimalToBinary(10);

print("The binary representation of 10 is $binaryNumber");

print("\n\n15th");

int gcdResult = gcd(12, 18);

print("The greatest common divisor of 12 and 18 is $gcdResult");

14
print("\n\n16th");

printRightAngledTriangle(5);

Note: Last Question Ouput not Showing Properply due to online Compiler Behqviour.

End ❤️
Mussaddiq Mahmood

Roll no: F20-BSCS-1038

15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy