Created
May 26, 2017 23:10
-
-
Save marvinferreira/0fd558be95a82fa1b3ab8a39237dc135 to your computer and use it in GitHub Desktop.
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
/* | |
* 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 digitos; | |
/** | |
* | |
* @author Marvin Ferreira | |
* Cógido para obter dígitos de números inteiros. | |
*/ | |
public class Digitos { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
//obterDigitoV1(); | |
//obterDigitoV2(); | |
obterDigitoV3(5432,0); | |
obterDigitoV3(5432,1); | |
obterDigitoV3(5432,2); | |
obterDigitoV3(5432 ,3); | |
} | |
public static void obterDigitoV1(){ | |
int n = 2450; | |
while (n>0){ | |
int d = n / 10; | |
int k = n - d * 10; | |
n = d; | |
System.out.println(k + " "); | |
} | |
} | |
public static void obterDigitoV2(){ | |
int num_1 = 2450; | |
while (num_1> 0){ | |
int digit = num_1%10; | |
num_1 = num_1/10; | |
System.out.println(digit); | |
} | |
} | |
public static void obterDigitoV3(int valor, int digito){ | |
Double potencia = Math.pow(10,digito); | |
int resultado = valor / potencia.intValue() % 10; | |
System.out.println(resultado); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment