1.Scala program to print your name.
object ExPrintName {
def main(args:Array[String]){
println("my name is: Manju")
}
}
Out Put:my name is: Manju
2.Scala program to find largest number among two numbers.
object ExampleArray {
def main(args:Array[String]){
var number = Array(10,20,30,40,50);
var N:Int=0;
println("All Array Elemnts");
for(N<-number){
println(N);
}
var sum:Int=0;
for(N<-number){
sum+=N;
}
println("Sum of all array elements:"+sum);
}
}
Out Put:Largest number is:2000
3.Scala program to find a number is positive, negative or positive.
object ExCheckNumber {
def main(args:Array[String]){
var number=(-688880);
if(number==0){
println("Number is zerooooo");
}
else if(number >= 0){
println("Numberis Positive");
}
else
{
println("Number is Negative");
}
}
}
Out Put:Number is Negative
4.Scala program to demonstrate example of multiple variables declarations and assignments.
object exampleVarDecAndAssin {
def main(args:Array[String]){
var (name:String,age:Int)= ("Manju",25);
println("NAME:"+name);
println("AGE:"+age);
var(address,mobile)=("Xyz,ABC","9880000000");
println("Address:"+address);
println("Mobile:"+mobile);
}
}
Out Put:
NAME:Manju
AGE:25
Address:Xyz,ABC
Mobile:98800000000
5.Scala program to create a user define function to return largest number among two numbers.
object ExampleUDFToGetLargestNumber {
def getLargestNumber(x:Int,y:Int):Int= {
var largestnumber:Int=0;
if(x>y)
largestnumber = x;
else
largestnumber = y;
return largestnumber;
}
def main(args:Array[String]){
var a:Int=10;
var b:Int=30;
println("Largest number from "+ a +" and "+ b +" is:"+ getLargestNumber(a,b));
}
}
Out Put:Largest number from 10 and 30 is:30
6.Scala program to declare string variable and print the string.
Object ExampleString {
def main(args:Array[String]){
val text : String = "You are reading the Scala Program";
println("value of text is: " + text);
}
}
Out Put:value of text is: You are reading the Scala Program
7.Scala program to print numbers from 1 to 10 using for loop with until to determine loop range.
object ExampleForLoop2 {
def main(args:Array[String]){
var Counter:Int=0;
for(Counter<-1 until 11)
println(Counter+ " " );
println();
}
}
Out Put:
1
2
3
4
5
6
7
8
9
10
8.Scala program to print numbers from 1 to 100 using for loop.
object ExampleForLoop1 {
def main(args:Array[String]){
var Counter:Int=0;
for(Counter<-1 to 10)
print(Counter+ " " );
println();
}
}
Out Put:1 2 3 4 5 6 7 8 9 10
9.Scala program to demonstrate example of collection list and for loop.
object ExampleForAndCollection {
def main(args:Array[String]){
var N:Int=0;
var number=List(100,200,300,400);
for(N<-number){
println(N);
}
}
}
Out Put:
100
200
300
400
10.Scala program of array - Declare, print and calculate sum of all elements.
object ExampleArray {
def main(args:Array[String]){
var number = Array(10,20,30,40,50);
var N:Int=0;
println("All Array Elemnts");
for(N<-number){
println(N);
}
var sum:Int=0;
for(N<-number){
sum+=N;
}
println("Sum of all array elements:"+sum);
}
}
Out Put: All Array Elemnts
10
20
30
40
50
60
70
Sum of all array elements:280
11.Scala program extends function & override.
import java.io._
class Point1(val xc: Int, val yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println ("Point x location : " + x);
println ("Point y location : " + y);
}
}
class Location(override val xc: Int, override val yc: Int,
val zc :Int) extends Point(xc, yc){
var z: Int = zc
def move(dx: Int, dy: Int, dz: Int) {
x = x + dx
y = y + dy
z = z + dz
println ("Point x location : " + x);
println ("Point y location : " + y);
println ("Point z location : " + z);
}
}
object Manju{
def main(args: Array[String]) {
val loc = new Location(10, 20, 15);
// Move to a new location
loc.move(10, 10, 5);
}
}
OutPut:
Point x location : 20
Point y location : 30
Point z location : 20
12.Scala Program while loop execution.
object Demo1{
def main(args: Array[String]) {
// Local variable declaration:
var a = 15;
// while loop execution
while( a < 20 ){
println( "Value of a: " + a );
a = a + 3;
}
}
}
Out Put:
Value of a: 15
Value of a: 18
No comments:
Post a Comment