Java程序打印给定数字的反面
以下是反转给定数字的算法。
算法
1. Get the number to reverse. 2. Hold the number in temporary variable. 3. Start the while loop with condition temp >0. 4. Store the first digit in the temporary variable d by performing modulus operation on temp with 10. 5. Multiply the revnum (initialized with 0) with 10 and concatenate the digit obtained in the previous step. 6. Reduce one digit in the temp by dividing with 10.
示例
import java.util.Scanner;
public class ReverseOfANumber {
public static void main(String args[]) {
int d, number,temp, revnum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number ::");
number = sc.nextInt();
temp = number;
while (temp >0) {
d = temp %10;
revnum = (revnum*10)+d;
temp = temp/10;
}
System.out.println("给定数字的反向是:"+revnum);
}
}输出结果
Enter a number :: 5112115 给定数字的反向是:5112115
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短