0% found this document useful (0 votes)
5 views

ALL CONDITIONAL PROGRAMS

The document contains a Dart program that implements various mathematical functions using conditional statements. These functions include calculating factorial, sum of digits, reversing a number, finding GCD, calculating simple interest, determining quadratic roots, checking if three sides form a triangle, and identifying perfect squares and perfect numbers. Additionally, it generates a Fibonacci series and demonstrates the usage of these functions in the main method.

Uploaded by

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

ALL CONDITIONAL PROGRAMS

The document contains a Dart program that implements various mathematical functions using conditional statements. These functions include calculating factorial, sum of digits, reversing a number, finding GCD, calculating simple interest, determining quadratic roots, checking if three sides form a triangle, and identifying perfect squares and perfect numbers. Additionally, it generates a Fibonacci series and demonstrates the usage of these functions in the main method.

Uploaded by

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

ALL CONDITIONAL PROGRAMS

import 'dart:math';

// Function to calculate factorial using if-else

int factorial(int n) {

if (n == 0 || n == 1) {

return 1;

} else {

int fact = 1;

int i = 2;

while (i <= n) {

fact *= i;

i++;

return fact;

// Function to find sum of digits using if-else

int sumOfDigits(int num) {

int sum = 0;

while (num > 0) {

if (num != 0) {

sum += num % 10;

num ~/= 10;

return sum;
}

// Function to reverse a number using if-else

int reverseNumber(int num) {

int reversed = 0;

while (num > 0) {

if (num != 0) {

reversed = reversed * 10 + num % 10;

num ~/= 10;

return reversed;

// Function to find GCD using if-else

int gcd(int a, int b) {

if (b == 0) {

return a;

} else {

return gcd(b, a % b);

// Function to calculate simple interest using if-else

double simpleInterest(double principal, double rate, double time) {

if (principal > 0 && rate > 0 && time > 0) {

return (principal * rate * time) / 100;

} else {

return 0;
}

// Function to find roots of quadratic equation using if-else

List<double> quadraticRoots(double a, double b, double c) {

double discriminant = b * b - 4 * a * c;

if (discriminant < 0) {

return []; // No real roots

} else if (discriminant == 0) {

double root = -b / (2 * a);

return [root];

} else {

double root1 = (-b + sqrt(discriminant)) / (2 * a);

double root2 = (-b - sqrt(discriminant)) / (2 * a);

return [root1, root2];

// Function to check if three sides form a triangle using if-else

bool isTriangle(int a, int b, int c) {

if ((a + b > c) && (a + c > b) && (b + c > a)) {

return true;

} else {

return false;

// Function to check if a number is a perfect square using if-else

bool isPerfectSquare(int num) {


int sqrtNum = sqrt(num).toInt();

if (sqrtNum * sqrtNum == num) {

return true;

} else {

return false;

// Function to check if a number is a perfect number using if-else

bool isPerfectNumber(int num) {

int sum = 0;

for (int i = 1; i <= num ~/ 2; i++) {

if (num % i == 0) {

sum += i;

if (sum == num) {

return true;

} else {

return false;

// Function to generate Fibonacci series using if-else

List<int> fibonacci(int n) {

List<int> series = [];

if (n >= 1) {

series.add(0);

}
if (n >= 2) {

series.add(1);

int i = 2;

while (i < n) {

series.add(series[i - 1] + series[i - 2]);

i++;

return series;

void main() {

int num = 5;

int numDigits = 123;

int numReverse = 4567;

int num1 = 48, num2 = 18;

double principal = 1000, rate = 5, time = 2;

double a = 1, b = -3, c = 2;

int side1 = 3, side2 = 4, side3 = 5;

int perfectSquareNum = 25;

int perfectNumber = 28;

int fibTerms = 7;

print("Factorial of $num is: ${factorial(num)}");

print("Sum of digits of $numDigits is: ${sumOfDigits(numDigits)}");

print("Reverse of $numReverse is: ${reverseNumber(numReverse)}");

print("GCD of $num1 and $num2 is: ${gcd(num1, num2)}");

print(
"Simple Interest for Principal = \$$principal, Rate = $rate%, Time = $time years is: \$
{simpleInterest(principal, rate, time)}",

);

print(

"Roots of quadratic equation ${a}x^2 + ${b}x + ${c} are: ${quadraticRoots(a, b, c)}",

);

print(

"Can sides $side1, $side2, and $side3 form a triangle? ${isTriangle(side1, side2, side3)}",

);

print(

"Is $perfectSquareNum a perfect square? ${isPerfectSquare(perfectSquareNum)}",

);

print(

"Is $perfectNumber a perfect number? ${isPerfectNumber(perfectNumber)}",

);

print("Fibonacci series up to $fibTerms terms: ${fibonacci(fibTerms)}");

You might also like