Credits

Saturday, February 21, 2009

Java's Loop Sample Programs

Here are more Java sample loop structures. You can add nested loops or loops inside another loop. Just don't get all tangled!!!!



[+/-] show/hide




//Multiplication table
class SquareCube{
public static void main(String args[])
{
int num,square,cube;
for(num=1;num<10;num++)
{
square=num*num;
cube=num*num*num;
System.out.println(num+"\t"+square+"\t"+cube);

}

}
}

//Guessing program
class Squareroot{
public static void main(String args[])
{
double num,sroot,rerr;
for(num=1.0;num<100.0;num++)
{ sroot=Math.sqrt(num);
System.out.println("Square root of "+num+" is "+sroot);

//compute rounding error
rerr=num-(sroot*sroot);
System.out.println("Rounding error is "+rerr);
System.out.println();
}
}
}


//Guessing program
class Squareroot{
public static void main(String args[])
{
double num,sroot,rerr;
for(num=1.0;num<100.0;num++)
{ sroot=Math.sqrt(num);
System.out.println("Square root of "+num+" is "+sroot);
}
}
}

WHAT ARE THE OUTPUTS OF THESE PROGRAMS?

AddThis