Java dilinde 11'e tam bölünebilme denetimi ile ilgili örnek bir algoritma aşağıda sunulmuştur. Bu örneği, daha az kodla, daha az adımda sonuca gidecek ve daha verimli çalışacak şekilde düzenleyiniz.
DivisibleBy11.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Algoritma; | |
import java.util.Scanner; | |
public class DivisibleBy11 | |
{ | |
void Divide() | |
{ | |
// http://ercanbozkurt.blogspot.com | |
Scanner sc=new Scanner(System.in); | |
System.out.print("Test edilecek sayıyı giriniz (en az 5 haneli): "); | |
int no=sc.nextInt(); | |
sc.close(); | |
int sume=0,sumo=0,diff=0; | |
String nst=Integer.toString(no); | |
int l=nst.length(); | |
if(l>=5) | |
{ | |
for(int i=0;i<l;i++) //1 | |
{ | |
int digit = Integer.parseInt(nst.charAt(i)+""); //2 | |
if(i%2==0) | |
{ | |
sume=sume+digit; //3 | |
} | |
else | |
{ | |
sumo=sumo+digit; //4 | |
} | |
} | |
if(sume>sumo) | |
{ | |
diff=sume-sumo; | |
if(diff==0 || diff % 11 ==0) //5 | |
{ | |
System.out.println(no + " sayısı 11'e tam bölünür"); | |
} | |
else | |
{ | |
System.out.println(no + " sayısı 11'e tam bölünmez"); | |
} | |
} | |
else if(sumo>sume) | |
{ | |
diff=sumo-sume; | |
if(diff==0 || diff % 11 ==0) //6 | |
{ | |
System.out.println(no + " sayısı 11'e tam bölünür"); | |
} | |
else | |
{ | |
System.out.println(no + " sayısı 11'e tam bölünmez"); | |
} | |
} | |
else if(sumo==sume) | |
{ | |
System.out.println(no + " sayısı 11'e tam bölünür"); | |
} | |
} | |
else | |
{ | |
System.out.println("Lütfen en az 5 haneli bir sayı giriniz"); | |
} | |
} | |
} |
Hiç yorum yok:
Yorum Gönder