[java] Menampilkan seluruh bilangan ganjil dari N bilangan

METNUM

|Tugas METNUM: Halaman 130 no.3

Buatlah program yang meminta sebuah bilangan n, lalu menampilkan seluruh bilangan ganjil dari 1-n.

Jawab

//irwananwar.wordpress.com
import java.util.Scanner;
public class loop3_ganjil {
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		System.out.printf("Input sebuah bilangan : ");
		int n=s.nextInt();
		for(int i=0;i<=n;i++){
			if(i%2==1)
				System.out.printf(i+" ");
		}
	}
}

2 thoughts on “[java] Menampilkan seluruh bilangan ganjil dari N bilangan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s