how print numbers from 10 to 0 in dart

94 views
Skip to first unread message

vinay vishnu

unread,
Feb 12, 2020, 4:56:15 AM2/12/20
to Dart Misc
how print numbers from 10 to 0 in dart ..., can anybody suggest me pls  guys

Nidhin Mahesh

unread,
Feb 12, 2020, 5:09:03 AM2/12/20
to mi...@dartlang.org
hello Vinay,

please be familiar with basic algorithm. 

On Wed, Feb 12, 2020 at 3:26 PM vinay vishnu <vinayvi...@gmail.com> wrote:
how print numbers from 10 to 0 in dart ..., can anybody suggest me pls  guys

--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/322b2495-b8a3-478f-a2a3-0148481c3031%40dartlang.org.

Sarath Mohan

unread,
Feb 12, 2020, 7:58:57 AM2/12/20
to Dart Misc
main() {
  
  int l = 10;
  for (int i = 10; i>=0; i--) {
    print(i);
  }
}

杨志

unread,
Feb 13, 2020, 12:13:32 AM2/13/20
to mi...@dartlang.org
void main() {
int i = 10;
while (i >= 1) {
print(i);
i--;
}
}
or
void main() {
int i = 10;
do {
print(i);
i--;
} while (i >= 1);
}
Sarath Mohan <sar...@tiltlabs.io> 于2020年2月12日周三 下午8:58写道:
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Sarath Mohan

unread,
Feb 13, 2020, 1:44:13 AM2/13/20
to Dart Misc
main() {
  reverseOrder(10);
}

reverseOrder(int number){
  print(number);
  if(number != 0)
    reverseOrder(number-1);
}
  

On Wednesday, February 12, 2020 at 3:26:15 PM UTC+5:30, vinay vishnu wrote:

vinay vishnu

unread,
Feb 13, 2020, 2:50:50 AM2/13/20
to Dart Misc
Thank you all guys...

Jacob Bang

unread,
Feb 13, 2020, 7:38:04 AM2/13/20
to mi...@dartlang.org
And since we already are on way to some code golfing:

void main() => Iterable.generate(11, (i) => 10 - i).forEach(print);

Den tor. 13. feb. 2020 kl. 08.50 skrev vinay vishnu <vinayvi...@gmail.com>:
>
> Thank you all guys...
>
> --
> For more ways to connect visit https://www.dartlang.org/community
> ---
> You received this message because you are subscribed to the Google Groups "Dart Misc" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
> To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/fbe50aa6-644d-4b7c-9f01-56e62c84241a%40dartlang.org.



--
Jacob Bang / julemand101

Bob Nystrom

unread,
Feb 13, 2020, 1:22:04 PM2/13/20
to General Dart Discussion
On Thu, Feb 13, 2020 at 4:38 AM Jacob Bang <julem...@gmail.com> wrote:
And since we already are on way to some code golfing:

void main() => Iterable.generate(11, (i) => 10 - i).forEach(print);

A little shorter:

main() => [for (var i = 10; i >= 0; i--) i].forEach(print);

– bob
 

Den tor. 13. feb. 2020 kl. 08.50 skrev vinay vishnu <vinayvi...@gmail.com>:
>
> Thank you all guys...
>
> --
> For more ways to connect visit https://www.dartlang.org/community
> ---
> You received this message because you are subscribed to the Google Groups "Dart Misc" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
> To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/fbe50aa6-644d-4b7c-9f01-56e62c84241a%40dartlang.org.



--
Jacob Bang / julemand101

--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Jake Macdonald

unread,
Feb 13, 2020, 1:46:51 PM2/13/20
to Dart Misc, Bob Nystrom
Or print during the generation, cause why not :P

  void main() => List.generate(11, (i) => print(10 - i));

بهترین ها

unread,
Feb 13, 2020, 3:06:13 PM2/13/20
to mi...@dartlang.org
Hi, all answers are correct, but with the intent of educating those who study this material, I also share this method with you:
You can create your own generator method or use the Stream if you want to run asynchronously:

,,,

import 'dart:async';


Stream<int> reverseAsync({int count}) async* {
  count ??= 10;
  while(!count.isNegative)
    yield count--;
}

Iterable<int> reverse([int count = 10]) sync* {
  while(!count.isNegative)
    yield count--;
}

Iterable<int> reverseByList([int count = 10]) sync* {
  yield* List.generate(count + 1, (i) => count - i);
}


void main() {
  
  print('Generator Method Using List.generate: [sync]');
  reverseByList().forEach(print);
  
  print("Custom Generator Method: [sync]");
  reverse().forEach(print);
  
  print("Stream: [async]");
  reverseAsync().listen(print);
}


,,,


On Wed, Feb 12, 2020 at 1:26 PM vinay vishnu <vinayvi...@gmail.com> wrote:
how print numbers from 10 to 0 in dart ..., can anybody suggest me pls  guys

--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
--
با احترام
پوریا قمری، ریاست کل هلدینگ بیستک
Reply all
Reply to author
Forward
0 new messages