0-1000 arası sayı oluşturup rakamları toplamını veren algoritma
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication16;
/**
*
* @author xoverx-
*/
public class JavaApplication16 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int randomSayi = (int)(Math.random()*(1000)); //0 ile 1000 arasında sayı
System.out.println("random sayi " + randomSayi); //üretilen sayıyı konsola bastırdık
int birler = randomSayi %10; //birler basamağı
randomSayi = randomSayi /10;
int onlar = randomSayi %10; //onlar basamağı
int yuzler = randomSayi /10; //yüzler basamağı
System.out.println("Toplamları ="+(birler+onlar+yuzler));
}
}
1
22
333
4444
şeklinde satır ve sütün oluşturmak için yazılan algoritma
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication17;
import static java.lang.System.out;
import java.util.Scanner;
import static javafx.scene.input.KeyCode.O;
/**
*
* @author xoverx-
*/
public class JavaApplication17 {
public static void main (String args[] ){
System.out.println("Alt alta kaç sıra olsun ? ");
Scanner inp = new Scanner(System.in);
int sira = inp.nextInt();
for (int i=1; i<=sira; i ++)
{
for (int j=0; j
{
System.out.print(i);
}
System.out.println("");
}
}
}
4 3 2 1 2 3 4
3 2 1 2 3
2 1 2
1
Sayiyi eksilterek üçgen yapma
import java.util.Scanner;
public class UcgenSayilar {
public static void main(String args[])
{
System.out.println("Sayı giriniz? ");
Scanner inp = new Scanner(System.in);
int sira = inp.nextInt();
for (int i = sira; i>=1; i--)
{
for (int j=i; j>=1; j--)
{
System.out.print(j+" ");
}
for (int k=2; k<=i; k++)
{
System.out.print(k+" ");
}
System.out.println("");
}
}
}
Tanımlı iki sayının toplamı
public class JavaOrnekleri {
public static void main(String[] args) {
int sayi1 = 10;
int sayi2 = 20;
int toplam = sayi1 + sayi2;
System.out.println("Sayıların Toplamı: " + toplam);
}
}