void main()
{
int num=3;
if(num>0)
{
print('$num is positive');
}
}
void main()
{
int num=5;
if(num%2==0){
print('Number is Even');
}
else{
print('Number is Odd');
}
}
void main() {
int number = 15;
if (number % 3 == 0) {
if (number % 5 == 0) {
print('$number is divisible by both 3 and 5.');
} else {
print('$number is divisible by 3 but not 5.');
}
} else {
print('$number is not divisible by 3.');
}
}
void main()
{
int a=20, b=50, c=7;
if(a>b){
if(a>c){
print('$a is largest');
}
else{
print('$c is largest');
}
}
else{
if(b>c){
print('$b is largest');
}
else{
print('$c is largest');
}
}
}
import 'dart:io';
void main()
{
print("Enter marks!!!!!!");
int nm = int.parse(stdin.readLineSync()!);
if(nm >= 90 && nm <= 100)
{
print("First Div");
}
else if(nm >= 75 && nm <= 90)
{
print("Second div");
}
else if(nm >= 45 && nm <= 75)
{
print("Third div");
}
else
{
print("Better luck next time");
}
}
void main() {
int number = -5;
if (number > 0) {
print('$number is positive.');
} else if (number < 0) {
print('$number is negative.');
} else {
print('$number is zero.');
}
}
void main() {
int age = 20;
if (age >= 18) {
print('You are eligible to vote.');
} else {
print('You are not eligible to vote.');
}
}
void main() {
String character = 'e';
if (character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u') {
print('$character is a vowel.');
} else {
print('$character is a consonant.');
}
}
void main() {
int number = 75;
if (number > 100) {
print('$number is greater than 100.');
} else if (number > 50) {
print('$number is between 51 and 100.');
} else if (number > 0) {
print('$number is between 1 and 50.');
} else {
print('$number is zero or negative.');
}
}
void main() {
int year = 2024;
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.');
}
}
void main(){
int i=16;
if(i>15)
{
print("$i is Greater than 15");
}
print('i am not in the if condition, Thank you!!!!!');
}
Ex= with input
import 'dart:io';
void main() {
print('Enter you score');
int score = int.parse(stdin.readLineSync()!);
if (score != null) {
print("Great job! You scored $score % marks");
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
void main() {
int temperature = 35;
if (temperature > 30) {
print("It's hot outside.");
} else {
print("The weather is cool.");
}
}
Ex=
import 'dart:io';
void main()
{
print('Enter marks !!!');
int nm=int.parse(stdin.readLineSync()!);
if(nm>=60 && nm<=100)
{
print('first Division');
}
else if(nm>=40 && nm<=60)
{
print('Third Division');
}
else if(nm>=40 && nm<=33)
{
print('Third Division');
}
else if(nm<33)
{
print('Failed !!');
}
else
{
print('Invalid input');
}
}
--------------------------------------------------------------------------------------------------------------------------------------
Ex-2
import 'dart:io';
void main() {
print('Enter a number');
int i = int.parse(stdin.readLineSync()!);
if (i % 2 == 0) {
print('$i is Even number');
}
else {
print('$i is odd number');
}
}
WADP to test whether a number is within 100 of 1000 or 2000.
Code -
import 'dart:io';
main() {
var number = int.parse(stdin.readLineSync()!);
if ((1000-number).abs() <= 100 || (2000-number).abs() <= 100) {
print('True');
}
else {
print('False');
}
}
WADP to convert height (in feet and inches) to centimetres.
```dart
import 'dart:io';
main() {
print('Enter the size:\t');
var size = double.parse(stdin.readLineSync()!);
print('Choose any one:\n1. Feet to Centimeter\n2. Inches to Centimeter');
var choice = int.parse(stdin.readLineSync()!);
if (choice == 1) {
print('The size in Centimeter is: ${size * 30.48}');
} else if (choice == 1) {
print('The size in Centimeter is: ${size * 2.54}');
}
}
void main() {
int age = 5;
if (age >= 18) {
print("You are an adult.");
} else if (age >= 13) {
print("You are a teenager.");
} else {
print("You are a child.");
}
}
Ex=
import 'dart:io';
void main()
{
print('Enter marks !!!');
int nm=int.parse(stdin.readLineSync()!);
if(nm>=60 && nm<=100)
{
print('first Division');
}
else if(nm>=40 && nm<=60)
{
print('Third Division');
}
else if(nm>=40 && nm<=33)
{
print('Third Division');
}
else if(nm<33)
{
print('Failed !!');
}
else
{
print('Invalid input');
}
}
void main() {
int number = 75;
if (number > 100) {
print('$number is greater than 100.');
} else if (number > 50) {
print('$number is between 51 and 100.');
} else if (number > 0) {
print('$number is between 1 and 50.');
} else {
print('$number is zero or negative.');
}
}
Check Leap Year:-
void main() {
int year = 2024;
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.');
}
}
Check if a number is positive, negative and zero:-
void main() {
int number = -5;
if (number > 0) {
print('$number is positive.');
} else if (number < 0) {
print('$number is negative.');
} else {
print('$number is zero.');
}
}
Largest of three numbers:-
void main()
{
int a=20, b=50, c=7;
if(a>b){
if(a>c){
print('$a is largest');
}
else{
print('$c is largest');
}
}
else{
if(b>c){
print('$b is largest');
}
else{
print('$c is largest');
}
}
}
Check a number divisible by zero:-
void main() {
int number = 15;
if (number % 3 == 0) {
if (number % 5 == 0) {
print('$number is divisible by both 3 and 5.');
} else {
print('$number is divisible by 3 but not 5.');
}
} else {
print('$number is not divisible by 3.');
}
}
void main() {
int number = -5;
if (number > 0) {
print('$number is positive.');
} else if (number < 0) {
print('$number is negative.');
} else {
print('$number is zero.');
}
}
void main()
{
int num=5;
if(num%2==0){
print('Number is Even');
}
else{
print('Number is Odd');
}
}
void main() {
int a = 10, b = 20, c = 15;
if (a > b && a > c) {
print("The largest number is $a.");
} else if (b > a && b > c) {
print("The largest number is $b.");
} else {
print("The largest number is $c.");
}
}
void main() {
String character = 'a';
if ('aeiou'.contains(character.toLowerCase())) {
print("$character is a vowel.");
} else {
print("$character is a consonant.");
}
}
void main() {
int age = 17;
if (age >= 18) {
print("You are eligible to vote.");
} else {
print("You are not eligible to vote.");
}
}
void main() {
int year = 2024;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
print("$year is a leap year.");
} else {
print("$year is not a leap year.");
}
}
void main() {
int number = 15;
if (number % 5 == 0 && number % 3 == 0) {
print("The number is divisible by both 5 and 3.");
} else {
print("The number is not divisible by both 5 and 3.");
}
}
void main() {
int age = 65;
if (age >= 60) {
print("You are eligible for a senior citizen discount.");
} else {
print("You are not eligible for a senior citizen discount.");
}
}
void main() {
double temperature = 28.0;
if (temperature > 30) {
print("The weather is hot.");
} else if (temperature < 15) {
print("The weather is cold.");
} else {
print("The weather is moderate.");
}
}
void main() {
int age = 25;
if (age <= 12) {
print("You are a child.");
} else if (age <= 19) {
print("You are a teenager.");
} else if (age <= 59) {
print("You are an adult.");
} else {
print("You are a senior.");
}
}
Check if You are eligible to vote:
void main() {
int age = 20;
String result = (age >= 18) ? "You are eligible to vote." : "You are not eligible to vote.";
print(result);
}
Ex-
void main()
{
int a=9, b=88;
a>b?print("a is grater"):print("b is greater");
}
Ex=
void main()
{
int a=10;
int b=1,i,j;
(i=a+b)??(j=a-b);
}
Here are 10 Dart programs using the ternary (conditional) operator condition ? expression_if_true : expression_if_false, demonstrating different scenarios.
void main()
{
int a=4;
String result= (a%2==2)?'Even':'Odd';
print('The number is $result');
}
void main()
{
int a=2, b=3;
String Large=(a>b)?'$a is large':'$b is large';
print('$Large is Larger');
}
void main() {
int age = 20;
String result = (age >= 18) ? "You are eligible to vote." : "You are not eligible to vote.";
print(result);
}
void main() {
int year = 2024;
String leapYear = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? "a leap year" : "not a leap year";
print("$year is $leapYear.");
}
void main()
{
int a=-9;
String result=(a>0)?'$a is positive':((a<0)?"$a is Nagative":"$a is Zero" );
print(result);
}
void main() {
int age = 65;
String discount = (age >= 60) ? "eligible for a senior citizen discount" : "not eligible for a senior citizen discount";
print("You are $discount.");
}
void main() {
String character = 'e';
String result = ('aeiou'.contains(character.toLowerCase())) ? "a vowel" : "a consonant";
print("$character is $result.");
}
void main()
{
int a=2, b=5, c=-9;
int smallest=(a<b)?((a<c)?a:c):((b<c)?b:c);
print('Smallest number $smallest');
}
void main()
{
int temp=33;
String weather=(temp>30)?"hot":((temp<15)?'cold':'Moderate');
print('weather');
}
void main() {
int age = 25;
String stage = (age <= 12) ? "a child" : (age <= 19 ? "a teenager" : (age <= 59 ? "an adult" : "a senior"));
print("You are $stage.");
}
void main() {
String grade = "A";
switch (grade) {
case "A":
print("Excellent!");
break;
case "B":
print("Good job!");
break;
case "C":
print("Fair.");
break;
default:
print("Needs improvement.");
}
}
Ex-
import 'dart:io';
void main()
{
var m1 = 10;
var m2 = 3;
// m1~/=m2;
// print("Result is ${m1~/=m2}");
print('Enter Day');
int nm = int.parse(stdin.readLineSync()!);
switch(nm) {
case 1:
print("Monday");
break;
case 2:
print('Tuesday');
break;
case 3:
print("Wednusday");
break;
case 4:
print('thursday');
break;
case 5:
print('friday');
break;
case 6:
print('saturday');
break;
case 7:
print('sunday');
break;
default:
print('Not a valid date');
break;
}
}
Ex=
import 'dart:io';
void main()
{
// int a=1;
print('Enter between 1 to 5 number');
int b=int.parse(stdin.readLineSync()!);
switch(b)
{
case 1:{
print('one');
break;
}
case 2:
{
print('two');
break;
}
case 3:
{
print('three');
break;
}
case 4:
{
print('four');
}
default:
{
print('wrong entry');
}
}
}
-------------------------------------------------------------
import 'dart:io';
void main()
{
var m1 = 10;
var m2 = 3;
// m1~/=m2;
// print("Result is ${m1~/=m2}");
print('Enter Day');
int nm = int.parse(stdin.readLineSync()!);
switch(nm) {
case 1:
print("Monday");
break;
case 2:
print('Tuesday');
break;
case 3:
print("Wednusday");
break;
case 4:
print('thursday');
break;
case 5:
print('friday');
break;
case 6:
print('saturday');
break;
case 7:
print('sunday');
break;
default:
print('Not a valid date');
break;
}
}
void main()
{
int month=3;
switch(month)
{
case 1:
print('January');
break;
case 2:
print('Fabraury');
break;
case 3:
print('March');
break;
case 4:
print("April");
break;
case 5:
print("May");
break;
case 6:
print("June");
break;
case 7:
print("July");
break;
case 8:
print("August");
break;
case 9:
print("September");
break;
case 10:
print("October");
break;
case 11:
print("November");
break;
case 12:
print("December");
break;
default:
print("Invalid month");
}
}
void main()
{
int a=3, b=5;
String operation='+';
switch(operation){
case '+':
print('Sum= ${a+b}');
break;
case '-':
print('Sub=${b-a}');
break;
case '*':
print('Mul=${a*b}');
break;
case '/':
print('Div=${b/a}');
break;
default:
print('Invalid Operation');
}
}
void main()
{
String character='o';
switch(character.toLowerCase()){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
print('Charater is vowel');
default:
print('Character is consonents');
}
}
void main() {
int score = 85;
switch (score ~/ 10) {
case 10:
case 9:
print("Grade: A");
break;
case 8:
print("Grade: B");
break;
case 7:
print("Grade: C");
break;
case 6:
print("Grade: D");
break;
default:
print("Grade: F");
}
}
void main() {
int month = 7;
switch (month) {
case 12:
case 1:
case 2:
print("Winter");
break;
case 3:
case 4:
case 5:
print("Spring");
break;
case 6:
case 7:
case 8:
print("Summer");
break;
case 9:
case 10:
case 11:
print("Fall");
break;
default:
print("Invalid month");
}
}
void main() {
String signal = "Red";
switch (signal) {
case "Red":
print("Stop");
break;
case "Yellow":
print("Caution");
break;
case "Green":
print("Go");
break;
default:
print("Invalid signal");
}
}
void main()
{
int month=2;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
print("31 days");
case 6:
case 4:
case 9:
case 11:
print("30 days");
case 2:
print('28 days or 29 days');
default:
print("Invalid Month");
}
}
Check User Access Level (Admin, Moderator, User)
void main() {
String role = "Admin";
switch (role) {
case "Admin":
print("Access: Full");
break;
case "Moderator":
print("Access: Limited");
break;
case "User":
print("Access: Basic");
break;
default:
print("Invalid role");
}
}
void main() {
int side1 = 5, side2 = 5, side3 = 5;
switch (side1 == side2 && side2 == side3
? "Equilateral"
: (side1 == side2 || side2 == side3 || side1 == side3)
? "Isosceles"
: "Scalene") {
case "Equilateral":
print("The triangle is Equilateral");
break;
case "Isosceles":
print("The triangle is Isosceles");
break;
case "Scalene":
print("The triangle is Scalene");
break;
}
}
import 'dart:io';
void main() {
String? name;
// Use ?? operator to provide a default value if `name` is null
print("Hello, ${name ?? "Guest"}");
}
--------------------------------------------
Here are 10 Dart programs showcasing the use of null-aware if (if with null-aware operators like ?? or ?.). These examples handle scenarios where null values might arise and demonstrate how to use null-aware operators effectively.
dart
Copy code
void main() {
String? name;
if (name == null) {
print('The value is null.');
}
}
dart
Copy code
void main() {
String? name;
String result = name ?? 'Default Name';
print(result); // Output: Default Name
}
dart
Copy code
void main() {
String? name = 'John';
if (name?.isNotEmpty == true) {
print('The name is $name.');
}
}
dart
Copy code
void main() {
int? number;
number ??= 42;
print('The number is $number.'); // Output: The number is 42
}
dart
Copy code
void main() {
List<int>? numbers;
if (numbers?.isEmpty ?? true) {
print('The list is either null or empty.');
}
}
dart
Copy code
void main() {
Map<String, String>? user = {'name': 'Alice'};
if (user?['name'] != null) {
print('User name is ${user?['name']}');
}
}
dart
Copy code
void main() {
int? age;
if ((age ?? 0) > 18) {
print('You are an adult.');
} else {
print('You are not an adult.');
}
}
dart
Copy code
void main() {
String? greeting = 'Hello';
if (greeting?.contains('Hello') == true) {
print('The greeting contains "Hello".');
}
}
dart
Copy code
void main() {
String? firstName;
String? lastName = 'Doe';
if ((firstName ?? lastName ?? 'Anonymous') == 'Anonymous') {
print('The name is Anonymous.');
} else {
print('The name is ${firstName ?? lastName}.');
}
}
dart
Copy code
void main() {
List<int>? numbers;
numbers = numbers ?? [1, 2, 3]; // Assign default list if null
if (numbers.isNotEmpty) {
print('Numbers: $numbers');
}
}
??: Provides a default value if the variable is null.
??=: Assigns a value to a variable only if it is null.
?.: Accesses a member only if the object is not null.
?. combined with if: Ensures safe checks and operations on nullable types.