ADVERTISEMENT
ADVERTISEMENT

Kotlin program of integer range using (..) operator

Learn how to create integer range using (..) operator in Kotlin?
Submitted by IncludeHelp, on March 26, 2022

(..) Operator

The (..) operator is the simplest way to create a range. It creates a range from the given start and end values. It is the operator form of rangeTo() function. Here, we will demonstrate the example of creating an integer range using the (..) operator.

Example 1:

fun main(args : Array<String>){
	// creating integer ranges
	println("Integer range 1:")	
	for(x in 1..10){
		println(x)
	}
	println()

	println("Integer range 2:")	
	for(x in -5..5){
		println(x)
	}
	println()
}

Output:

Integer range 1:
1
2
3
4
5
6
7
8
9
10

Integer range 2:
-5
-4
-3
-2
-1
0
1
2
3
4
5

Example 2:

fun main(args : Array<String>){
	// creating integer ranges
	println("Integer range 1:")	
	for(x in 1..10 step 2){
		println(x)
	}
	println()

	println("Integer range 2:")	
	for(x in -5..5 step 2){
		println(x)
	}
	println()
}

Output:

Integer range 1:
1
3
5
7
9

Integer range 2:
-5
-3
-1
1
3
5

Kotlin Ranges Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.