I am new to Java and currently working on a small class assignment. The question is as follows:
Write a program that determines whether a bank account number with 10 digits or fewer passes a validation test; it requires that we extract the digits, right to left by:
Using the modulo operator to extract the right most digit
Using integer division to remove the right-most digit from the account number to obtain a new number without it.
Beginning with the 2nd right-most digit, moving right to left, double every other digit. If it produces a value greater than 9, subtract 9 from that value.
Form the sum of all products(new digits) and the unchanged digits.
if the sum doesn't end in 0, its invalid.
Check the validity of 5113 4765 12 and 65 1234 1234
Here is my code:
long account = Long.parseLong(JOptionPane.showInputDialog( null, "Enter account number: " ));
int sum = 0;
long digit;
//5113476512
//6512341234
String str_number = String.valueOf(account);
digit = account % 10;
account /= 10;
for(int i = str_number.length() -2; i >= 0; i --){
digit = account % 10;
account /= 10;
// account%=10;
// sum += digit;
digit *= 2;
if (digit > 9){
digit -= 9;
}
sum += digit;
}
// for(int x = 0; x < digit.length; x ++){
// sum += digit[x];
// }
if (sum % 10 != 0){
JOptionPane.showMessageDialog(null, "Account number invalid");
}
else{
JOptionPane.showMessageDialog(null, "Account number valid");
}
JOptionPane.showMessageDialog(null, sum);
But I feel it doesn't follow the requirements and might not be correct. Only one of the account numbers returns valid although I'm not sure if that is supposed to be so or not. Any ideas on how to go about this?
The following implementation using a boolean flag to detect the other number shows that both account numbers are invalid:
public static boolean isValid(long acc) {
System.out.print(acc + " -> "); // debug print
int sum = 0;
boolean other = false;
while (acc > 0) {
int digit = (int) (acc % 10);
acc /= 10;
if (other) {
digit *= 2;
if (digit > 9) digit -= 9;
}
System.out.print(digit + " "); // debug print
sum += digit;
other = !other;
}
System.out.println("sum = " + sum); // debug print
return sum % 10 == 0;
}
Tests:
System.out.println(isValid(5113476512L));
System.out.println(isValid(6512341234L));
Output:
5113476512 -> 2 2 5 3 7 8 3 2 1 1 sum = 34
false
6512341234 -> 4 6 2 2 4 6 2 2 5 3 sum = 36
false
Related
So I need to be able to increase an integer if it is less than 6 digits. If it's less than six digits, the integer should increase by repeating the last digit until it hits six digits. For example, 1234 becomes 123444. The hint is to use a loop and math equation to increase it 1 digit at a time. I figured out how to decrease the number by one integer by dividing it by 10, but I cannot figure out an equation that would let me increase the number. Here is my code:
public void setAccountNum(int accountNum) {
final int MAX_LOAN_NUMBER = 999999;
if (accountNum > MAX_LOAN_NUMBER) {
System.out.println("Too many digits in account number " + accountNum);
while (accountNum > MAX_LOAN_NUMBER) {
accountNum = accountNum / 10;
}
this.accountNum = accountNum;
System.out.println(" Set to the 6-digit value of " + accountNum + ".");
}
if (accountNum < MINIMUM_LOAN_NUMBER) {
}
else {
this.accountNum = accountNum;
}
}
Let's take it easy.
// [...]
using namespace std;
int main(void) {
int n;
scanf("%d", &n); // Suppose n < 1*10^7
while (n < 100000) {
n = n * 10 + n % 10;
}
printf("%d\n", n);
}
Problem statement: Three digit sum - Find all the numbers between 1 and 999 where the sum of the 1st digit and the 2nd digit is equal to the 3rd digit.
Examples:
123 : 1+2 = 3
246 : 2+4 = 6
Java:
public class AssignmentFive {
public static void main(String[] args) {
int i=1;
int valuetwo;
int n=1;
int sum = 0;
int valuethree;
int valueone = 0;
String Numbers = "";
for (i = 1; i <= 999; i++) {
n = i;
while (n > 1) {
valueone = n % 10;/*To get the ones place digit*/
n = n / 10;
valuetwo = n % 10;/*To get the tens place digit*/
n = n / 10;
valuethree = n;/*To get the hundreds place digit*/
sum = valuethree + valuetwo;/*adding the hundreds place and
tens place*/
}
/*Checking if the ones place digit is equal to the sum and then print
the values in a string format*/
if (sum == valueone) {
Numbers = Numbers + n + " ";
System.out.println(Numbers);
}
}
}
}
I got my result :
1
10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
10000000001
100000000011
1000000000111
10000000001111
100000000011111
1000000000111111
10000000001111111
100000000011111111
1000000000111111111
Process finished with exit code 0
The result is not showing the actual result like it should be which should show values like: 123, 246 (Please refer to the problem statement above.)
Please let me know what seems to be the issue with the code and how to tweak it.
Don't know what you're trying to do with that while loop, or why you are building up a space-separated string of numbers.
Your code should be something like:
for (int n = 1; n <= 999; n++) {
int digit1 = // for you to write code here
int digit2 = // for you to write code here
int digit3 = // for you to write code here
if (digit1 + digit2 == digit3) {
// print n here
}
}
So basically your question is how to calculate the numbers, right?
My first hint for you would be how to get the first, second and third value from a 2 or 3 digit number.
For example for 3 digits you can do int hundretDigit = (n - (n % 100)) % 100. Of course this is really inefficient. But just get code working before optimizing it ;)
Just think about a way to get the "ten-digit" (2nd number). Then you add them and if they equal the third one you write System.out.println(<number>);
EDIT:
For 2 digit numbers I will give you the code:
if(i >= 10 && i <= 99) {
int leftDigit = (i - (i % 10)) / 10;
if(leftDigit == (i % 10)) {
//Left digit equals right digit (for example 33 => 3 = 3
System.out.println(i);
}
}
Try again and edit your source code. If you have more questions I will edit my (this) answer to give you a little bit more help if you need!
I have a given number. How can I find the factors of that number (for example, 5 and 3 for the number 15)? Here is the code I tried:
int factor1 = 2;
while ((a % factor1 != 0) && (a >= factor1)) {
d++;
}
if (factor1 == a){
d = 1;
}
But this gives me only the smallest factor (i.e a=3 all the time). I would like to get a random set of factors.
Loop through each number from 1 to N inclusively using the modulus operator (%). If n%currentNumber==0, they are a factor. Below, I did this using a for loop, outputting each factor as it is found.
int number=15;
for(int i = 1; i <= number; i++){
if(number%i==0){
System.out.println("Found factor: " + i);
}
}
As Theo said in a comment on this post, you can also use number/2, and arbitrarily include 1 and number.
int number=2229348;
System.out.println("Found factor: " + 1);
for(int i = 2; i <= number/2; i++){
if(number%i==0){
System.out.println("Found factor: " + i);
}
}
System.out.println("Found factor: " + number);
You can iterate through the numbers from 2 to a/2 and check if the given number divides a, which is done using the % operator:
int a = 15;
System.out.print("Divisors of " + a + ": ");
for(int i = 2; i <= a/2; ++i) {
if(a % i == 0) {
System.out.print(i + " ");
}
}
System.out.println();
This code prints all of the divisors of a. Not that you most probably want to ignore 1, since it divides all integers. Moreover, you don't need to check the numbers until a, because no number bigger than a / 2 can actually divide a apart from a itself.
The while loop with default values of a=15 and multiple=2 is already in an infinite loop. You need to correct that and check for subsequent increments of multiple whenever a%multiple ! = 0
public class Factors {
public static void main(String[] args){
/**
int multiple1=2,d=0,a=15; //multiple to be rephrased as factor
while((a%multiple1 != 0)&&(a>=multiple1)){
multiple1++; //this will increment the factor and check till the number itself
//System.out.println(multiple1+" is not a factor of a");
}
if(multiple1==a){
d=1;
}
*commented the original code
*/
int factor=1,d=0,a=20; //multiple rephrased as factor
while(a/2>=factor){ //re-arranged your while condition
factor++;
if((a%factor==0))
d++; //increment factor count whenever a factor is found
}
System.out.println("Total number of factors of a : "+(d+2)); // 1 and 'a' are by default factors of number 'a'
}
}
To find all factors inlcuding 1 and the number itself you can do something like below:
//Iterate from 2 until n/2 (inclusive) and divide n by each number.
//Return numbers that are factors (i.e. remainder = 0). Add the number itself in the end.
int[] findAllFactors(int number) {
int[] factors = IntStream.range(1, 1 + number / 2).filter(factor -> number % factor == 0).toArray();
int[] allFactors = new int[factors.length+1];
System.arraycopy(factors,0,allFactors,0,factors.length);
allFactors[factors.length] = number;
return allFactors;
}
To find only prime factors you can do something like this:
//Iterate from 2 until n/2 (inclusive) and divide n by each number.
// Return numbers that are factors (i.e. remainder = 0) and are prime
int[] findPrimeFactors(int number) {
return IntStream.range(2, 1 + number/ 2).filter(factor -> number % factor == 0 && isPrime(factor)).toArray();
}
Helper method for primality check:
//Iterate from 2 until n/2 (inclusive) and divide n by each number. Return false if at least one divisor is found
boolean isPrime(int n) {
if (n <= 1) throw new RuntimeException("Invalid input");
return !IntStream.range(2, 1+n/2).filter(x -> ((n % x == 0) && (x != n))).findFirst().isPresent();
}
If you are not on Java 8 and/or not using Lambda expressions, a simple iterative loop can be as below:
//Find all factors of a number
public Set<Integer> findFactors(int number) {
Set<Integer> factors = new TreeSet<>();
int i = 1;
factors.add(i);
while (i++ < 1 + number / 2) {
if ((number % i) == 0) {
factors.add(i);
}
}
factors.add(number);
return factors;
}
public class Abc{
public static void main(String...args){
if(args.length<2){
System.out.println("Usage : java Abc 22 3");
System.exit(1);
}
int no1=Integer.parseInt(args[0]);
int no=Integer.parseInt(args[1]),temp=0,i;
for(i=no;i<=no1;i+=no){
temp++;
}
System.out.println("Multiple of "+no+" to get "+no1+" is :--> "+temp);
//System.out.println(i+"--"+no1+"---"+no);
System.out.println("Reminder is "+(no1-i+no));
}
}
I have to use a static method TotalAverage(int n) that would calculate the average digit total of the numbers 0 + 1 + 2 + .... + n. So that totalAverage(19) would be calculated as (0 + 1 + ... + 9 + 1 + ... + 10) / 20.0. I managed to do it for the most part using the following code:
public static double TotalAverage(int n) {
double total = 0;
int count = 0;
while (n >= 0) {
total += n % 10;
n = n - 1;
count++;
}
return total / count;
}
It works for numbers up to 9, but I get incorrect results for larger numbers. I realise that once the while statements gets to 10 % 10 it adds 0 to the total and not a 10, but I can't figure out how to do it correctly.
If you're looking to sum all digits of a number then the error in your code is
total += n % 10;
which only get the ones digit from n. Use some loop to get all digits from n without modifying it (because if you modify n your outer loop will break). Try:
int temp = n;
while(temp>0) {
total += temp % 10; //add next digit
temp /= 10;
}
You could use a separate method for digit sum. Something like this would work.
private static int digitSum(int a) {
return a < 10 ? a : a%10 + digitSum(a/10);
}
Then you can replace the line
total += n % 10
with
total += digitSum(n);
I can not use the math class when trying to find the answer. I have to use the for loops when calculating the answer.
package Powers;
import TerminalIO.KeyboardReader;
public class powers {
public static void main(String [] args)
{
KeyboardReader reader= new KeyboardReader();
//Variables
int base; //The base number for the calculations
int minE; //The minimum exponent that will be used
int maxE; //The maximum exponent that will be used
int answer; //Weather the user wishes to repeat or not
double result = 1; //The result of the base to the power of the exponent
//Inputs
do { /* Start of repeat */ base = reader.readInt ("Please enter the base. ");
minE = reader.readInt ("Please enter the minimum exponent. ");
maxE = reader.readInt ("Please enter the maximum exponent. ");
//Error Checking
while (minE > maxE) {
System.out.println("There seems to be an error, your minimum exponent is greater then your maximum exponent. Please re-enter your values");
minE = reader.readInt("Enter your minimum exponent. ");
maxE = reader.readInt("Enter your maximum exponent. ");
}
//End of Error Checking
//Calculations
//Output
System.out.println(" Base Exponent Result");
System.out.println();
int expo; //The exponent by which the base will be powered to
for (expo = minE; expo <= maxE; expo++) {
//For exponent being 0
result = 1;
//For exponent being <0
if (expo <0) {
for (int i= minE;i != 0;i = i + 1) {
result = result/base;
}
}
if (expo == 0) {
result = 1 ;
}
//For exponent being 1
if (expo == 1) {
result = base;
}
//For exponent being >1
if (expo > 1) {
for (int i=1;i<=expo;i++) {
result = result*base;
}
}
System.out.println( " " + base + " " + expo + " " + result);
}
System.out.println();
answer = reader.readInt ("Do you wish to repeat? Enter 1 if yes.");
} while (answer == 1); //End of repeat
}
}
An example answer that comes out is
Please enter the base. 2
Please enter the minimum exponent. -2
Please enter the maximum exponent. 2
Base Exponent Result
2 -2 0.25
2 -1 0.25
2 0 1.0
2 1 2.0
2 2 4.0
As you can see only 0 and the positive work, not the negatives.
You did wrong initialization of i. Instead of i= minE change it to i= expo.
for (int i = expo; i != 0; i = i + 1) {
result = result/base;
}
I just added a "counter" on the negative loop. You were always storing a value of "-2" (or the lowest value) on all your calculations for negative values.
You had to add +1 (-2 + 1 = -1) so it divided correctly.
It was just a logic problem
int counter = 0;
for (expo = minE; expo <= maxE; expo++) {
//For exponent being 0
result = 1;
//For exponent being <0
if (expo < 0) {
for (int i = (minE + counter); i != 0; i = i + 1) {
result = result / base;
}
counter++;
}
Example of output:
Base Exponent Result
2 -3 0.125
2 -2 0.25
2 -1 0.5
2 0 1.0
2 1 2.0
2 2 4.0
2 3 8.0