Skip to content

Instantly share code, notes, and snippets.

@marvinferreira
Created May 26, 2017 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marvinferreira/0fd558be95a82fa1b3ab8a39237dc135 to your computer and use it in GitHub Desktop.
Save marvinferreira/0fd558be95a82fa1b3ab8a39237dc135 to your computer and use it in GitHub Desktop.
/*
* 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