File tree Expand file tree Collapse file tree 2 files changed +71
-0
lines changed
Expand file tree Collapse file tree 2 files changed +71
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ import java .util .Scanner ;
3+
4+ public class amstrong {
5+
6+ public static void main (String [] args ) {
7+ Scanner sc = new Scanner (System .in );
8+ System .out .print ("Enter the number for Check amstrong: " );
9+ int num = sc .nextInt ();
10+
11+
12+ if (check_amstrong (num )) {
13+ System .out .println ("Yes,Its amstrong Number" );
14+ } else {
15+ System .out .println ("Not amstrong Number" );
16+ }
17+ }
18+
19+ static boolean check_amstrong (int num ) {
20+ int originalNum = num ;
21+ int sum = 0 ;
22+ while (num != 0 ) {
23+ int digit = num % 10 ;
24+ sum += (digit * digit * digit );
25+ num = num / 10 ;
26+ }
27+ return sum == originalNum ;
28+ }
29+ }
Original file line number Diff line number Diff line change 1+
2+ public class count_digits {
3+
4+ public static void main (String [] args ) {
5+ int number = 123456710 ;
6+
7+ int result = coutDigits (number );
8+ System .out .println ("The digits : " + result );
9+
10+ int lastDigit = lastDigit (number );
11+ System .out .println ("the last digit is : " + lastDigit );
12+
13+ int sum = sumOfDigit (number );
14+ System .out .println ("The sum: " + sum );
15+
16+ }
17+
18+ static int coutDigits (int n ) {
19+ int count = 0 ;
20+ while (n != 0 ) {
21+ n = n / 10 ;
22+ count ++;
23+ }
24+ return count ;
25+ }
26+
27+ static int lastDigit (int num ) {
28+ return num % 10 ;
29+ }
30+
31+ static int sumOfDigit (int num ) {
32+ int sum = 0 ;
33+ while (num != 0 ) {
34+ int digit = num % 10 ;
35+ sum += digit ;
36+ // System.out.println("1st: " +num);
37+ num = num / 10 ;
38+ // System.out.println( "2nd " + num);
39+ }
40+ return sum ;
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments