java - How to separate each digit in a number? -
i working on homework assignments making program allows user enter in low , high value range , program suppose find numbers sum of cubed digits (ie 153 = 1^3 + 5^3 + 3^3) , loop has used. i'm stuck on figuring out how go this, need separate each digit of number take each digit cube it, add them check see if equals value if not repeat that's far have gotten planning wise appreciated.
/* * problem #17 on page 367 * program description - finding numbers equal sum of cubed numbers * author: jonathan wilson * date: 2/24/2015 * filename: cube.java */ //import statements import java.util.*; //for scanner class // class beginning class cube { public static void main(string[] args ) { //declare variables area int low; int high; int value; scanner scan = new scanner (system.in); //program beginning messages user here system.out.println("welcome special number program!"); system.out.println("enter low , high value range"); system.out.println("and program find numbers equal sum of cubed digits"); //collect inputs user or read in data here system.out.println("enter in low starting range "); low=scan.nextint(); //storing low range low system.out.println(); //break system.out.println("enter in high ending range "); high=scan.nextint(); //storing high range high system.out.println(); //break // check see range entered legal range, if moves on print out range , goes // main code while (high < low){//checks see if inputs valid , user doesn't try inverse range system.out.println(); system.out.println("incorrect range try again!"); system.out. low = scan.nextint(); system.out.print(low); system.out.println();//break system.out.print("please enter higher number in range... "); high = scan.nextint(); //echo input values user here system.out.println("okay, range of numbers through "+low+ " "+high+" lets start!"); // main code , calculations (count= low; count <= high; count ++){ //output results here //end program message system.out.println(); system.out.println("hope enjoyed using program!"); }// end main method } } }// end class
you can isolate digits using % or mod tool. then, go next digit, divide number 10, making last digit go away. process can used go through digits of number.
//import statements import java.util.*; //for scanner class // class beginning public class test { public static void main(string[] args) { // declare variables area int low; int high; int value; scanner scan = new scanner(system.in); // program beginning messages user here system.out.println("welcome special number program!"); system.out.println("enter low , high value range"); system.out .println("and program find numbers equal sum of cubed digits"); // collect inputs user or read in data here system.out.println("enter in low starting range "); low = scan.nextint();// storing low range low system.out.println();// break system.out.println("enter in high ending range "); high = scan.nextint();// storing high range high system.out.println();// break // check see range entered legal range, if // moves on print out range , goes // main code while (high < low) {// checks see if inputs valid , // user doesn't try inverse range system.out.println(); system.out.println("incorrect range try again!"); low = scan.nextint(); system.out.print(low); system.out.println();// break system.out.print("please enter higher number in range... "); high = scan.nextint(); } // echo input values user here system.out.println("okay, range of numbers through " + low + " " + high + " lets start!"); // main code , calculations (int count = low; count <= high; count++) { int sum=0; int original = count; while(count>0){ sum+=(count%10)*(count%10)*(count%10); count/=10; } if(sum==original){ system.out.println(original+" works number!"); } count = original; } // output results here // end program message system.out.println(); system.out.println("hope enjoyed using program!"); }// end main method }// end class
Comments
Post a Comment