Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Write a Java program that accepts an integer (n) and computes the value of n+nn+nnn.

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write a Java program that accepts an integer (n) and computes the value of n+nn+nnn.

    import java.util.Scanner;
     
    class RicMain 
    {    
    	public static void main(String[] args) 
    	{
    		Scanner ip = new Scanner(System.in);
    		int n, m;
     
    		System.out.print("Input number: ");
    		n = ip.nextInt();
     
    		System.out.printf("%d + %d%d + %d%d%d", n, n, n, n, n, n);
     
    		m = n + 11*n + 111*n;
    		System.out.println(" = " + m);
    	}
    }

    My problem is, for m = n + 11*n + 111*n, it works for numbers 1 - 9, but for 10 and above, it wont work because for example
    if I input 10, it would output: 10 + 1010 + 101010, but for m, it is wrong because 11 * 10 != 1010. So how do I fix this?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Write a Java program that accepts an integer (n) and computes the value of n+nn+nnn.

    m = n + 11*n + 111*n, it works for numbers 1 - 9, but for 10 and above, it wont work
    Please explain why that is supposed to work.
    Where do the values 11 and 111 come from?

    What does that have to do with the post's title: computes the value of n+nn+nnn?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Write a Java program that accepts an integer (n) and computes the value of n+nn+nnn.

    It prints "1010" because you're just concatenating the String "10" twice with System.out.printf("%d + %d%d + %d%d%d", n, n, n, n, n, n). If you want it to print the individual values of 10*1, 10*11, and 10*111, you could store each answer in a separate int and print those ints. Something like this:
    int n1 = n * 1;
    int n2 = n * 11;
    int n3 = n * 111;
     
    System.out.printf("%d + %d + %d", n1, n2, n3); // Prints 10 + 110 + 1110 = 1230
     
    m = n1 + n2 + n3;
    (etc)
    Hope that helps!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Write a Java program that accepts an integer (n) and computes the value of n+nn+nnn.

    The OP doesn't understand where the values 11 and 111 come from and why they only work with single digit values.
    Those multipliers need to be computed for values with more digits. For example given a value of 123 and a desired value of 123123 the multiplier would be 1001.
    The leading 1 * 10^3 (^3 because of 3 digits) + 1
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Write a Java program and compute the sum of the digits of an integer.
    By Ricky Buen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2018, 03:21 PM
  2. java program to convert integer to Roman numbers and vice versa
    By parthicseraj in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 20th, 2014, 02:10 AM
  3. Saving number into integer in java program!
    By BioHazard in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 10th, 2013, 01:43 PM
  4. Replies: 2
    Last Post: January 25th, 2013, 09:54 PM
  5. Replies: 2
    Last Post: November 30th, 2011, 07:35 PM