Thursday, September 23, 2010

Looping.....

Looping1
import java.util.Scanner;
public class Looping
{
public static void main (String [] args)
{
Scanner s= new Scanner(System.in);
System.out.print("Masukan n: ");
int n=s.nextInt();
for(int i=1; i<=n; i++)
{

for(int j=1; j<=n-(i-1); j++)
{
System.out.print("*");
}
System.out.print("\n");
}
for (int a=1; a<=n-1; a++)
{
for (int b=n-a; b<=n; b++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
}

Looping2
import java.util.Scanner;
public class Looping2
{
public static void main (String [] args)
{
Scanner s= new Scanner(System.in);
System.out.print("Masukan n: ");
int n=s.nextInt();
for(int i=1; i<=n; i++)
{

for(int j=1; j<=n-(i-1); j++)
{
System.out.print(" ");
}
for(int k=1; k<=(i*2)-1; k++)
{
if(i==n && k==n)
{
System.out.print("+");
}
else 
{
System.out.print("*");
}
}
System.out.print("\n");
}
for(int a=1; a<=n; a++)
{
for(int b=n-a; b<=n; b++)
{
System.out.print(" ");
}
for (int c=(2*a)-1; c<=2*n-3; c++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
}

Looping 3
import java.util.Scanner;
public class Looping3
{
public static void main(String [] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Masukan Angka: ");
int angka=s.nextInt();
int i=1;
do 
{
angka=angka/10;
i++;
}
while (angka>0);
System.out.println(i-1);
}
}

No comments:

Post a Comment