DEV Community

Coder
Coder

Posted on

Problem realted to condition and loop and pattern

1.Question
=> Find n is +ve,-ve or 0?
code:-

import 'dart:io';

void main() {
  print('Om Namah Shivay');
  int n = int.parse(stdin.readLineSync()!);
  if (n > 0) {
    print('$n is positive');
  } else if (n == 0) {
    print('$n is zero');
  } else {
    print('$n is positive');
  }
}

Enter fullscreen mode Exit fullscreen mode

2.Question
=>which one is greater in a and b?
code:-

import 'dart:io';

void main() {
  print('Om Namah Shivay');
  print(greater());
}

int greater({int? a, int? b}) {
  a = int.parse(stdin.readLineSync()!);
  b = int.parse(stdin.readLineSync()!);
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

Enter fullscreen mode Exit fullscreen mode

3.Question
Find that the alphabet is in lowercase or uppercase?
code:-

void main() {
  print('Om Namah Shivay');
  print(Case(alpphabet: 'om'));
}

String Case({required String alpphabet}) {
  if (alpphabet == alpphabet.toUpperCase()) {
    return 'upperCase';
  } else {
    return 'lowerCase';
  }
}

Enter fullscreen mode Exit fullscreen mode

loops
4.print 1 to n
code:-

void main() {
  print('Om Namah Shivay');
  number(n: 10);
}

void number({required int n}) {
  int i = 1;
  while (i <= n) {
    print(i);
    i++;
  }
}

Enter fullscreen mode Exit fullscreen mode

5.print sum of 1 to n?

void main() {
  print('Om Namah Shivay');

  print(sum(n: 5));
}

int sum({required int n}) {
  int i = 1, sum = 0;

  while (i <= n) {
    sum += i;
    i++;
  }
  return sum;
}

Enter fullscreen mode Exit fullscreen mode

6.Find sum of even numbers in 1 to n?

code:-

void main() {
  print('Om Namah Shivay');

  print(sum(n: 5));
}

int sum({required int n}) {
  int i = 1, sum = 0;

  while (i <= n) {
    i % 2 == 0 ? sum += i : sum += 0;

    i++;
  }
  return sum;
}

Enter fullscreen mode Exit fullscreen mode

7.convert f to c(temp.)
code:-

void main() {
  print('Om Namah Shivay');

  print(celsiusConvertor(f: 5.23));
}

double celsiusConvertor({required double f}) {
  return (f - 32) * 5 / 9;
}

Enter fullscreen mode Exit fullscreen mode

8.n is prime or not?

code:-

void main() {
  print('Om Namah Shivay');

  print(prime(n: 56));
}

String prime({required double n}) {
  int i = 2;
  String ans = 'prime';
  while (i < n) {
    if (n % i == 0) {
      ans = 'non-prime';
      break;
    }
  }
  return ans;
}

Enter fullscreen mode Exit fullscreen mode

9.Make this pattern

Image description
code:-

void main() {
  print('Om Namah Shivay');

  print(pattern());
}

String pattern() {
  int i = 1;
  String star = '*';
  while (i <= 3) {
    print(star * 4);
    i++;
  }
  return '';
}

Enter fullscreen mode Exit fullscreen mode

10.Make this pattern

111
222
333

code:-

void main() {
  print('Om Namah Shivay');

  print(pattern());
}

String pattern() {
  int i = 1;
  while (i <= 3) {
    String star = i.toString();

    print(star * 3);
    i++;
  }
  return '';
}

Enter fullscreen mode Exit fullscreen mode

I understood

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay