1
int n; 
Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
n = in .nextInt();
System.out.println(n " + " nn ); //this is not working

how can i print using println

Leon
  • 356
  • 6
  • 10
karthikaRaj
  • 13
  • 1
  • 3

6 Answers6

0

Please format your code blocks as code.

if you want to print the same value twice like if

int n = 5;
System.out.println(String.valueOf(n) + "+" + String.valueOf(n) + String.valueOf(n);

this would print 5 + 55

if you want to print the double of n then:
System.out.println(String.valueOf(n) + " + " + n*2;

which would print 5 + 10

but im not sure what you expect your result to be

0

Something like this might help

n = in .nextInt();
for(int i=1;i<=n;i++){
    for(int j=1;j<=i;j++){
         System.out.print("n");
    }
if(i!=n) {
    System.out.print("+");
}
}

Output ::

if input =3
Output => n+nn+nnn

if input=5
output => n+nn+nnn+nnnn+nnnnn
0

Something like this?

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Input number: ");
        String n = in.nextLine();
        if (n.matches("[0-9]+")) {
            System.out.println(n + " + " + n + n + " + " + n + n + n + " = "
                    + (Integer.parseInt(n) + Integer.parseInt(n + n) + Integer.parseInt(n + n + n)));
        } else {
            System.out.println("Error: invalid input.");
        }
    }
}

Sample run-1:

Input number: 5
5 + 55 + 555 = 615

Sample run-2:

Input number: 1
1 + 11 + 111 = 123

Sample run-3:

Input number: a
Error: invalid input.
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

if you want to print the pattern of n+nn+nnn, you can probably consider the below code

Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
String n = in.nextLine();
String output = "";
for (int i = 1; i <= 3; i++) {
 output += Stream.generate(() -> "" + n).limit(i).collect(Collectors.joining()) + "+";
}
System.out.println(output.substring(0, output.length() - 1));

Output:

Input number: 5
5+55+555
theNextBigThing
  • 131
  • 3
  • 14
0
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        System.out.println("Type your number : ");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        String x=String.valueOf(n);
        System.out.println(x+ " + "+ x+x +" + " +x+x+x+ " = "+ ((Integer.parseInt(x))+(Integer.parseInt(x+x))+ (Integer.parseInt(x+x+x))));

    }
}

Output : (Example for number = 5)

Type your number : 5

5 + 55 + 555 = 615
Som
  • 1,522
  • 1
  • 15
  • 48
Jenny
  • 1
0

Well, here is yet another alternative with a slight twist.

  • n, the digit to repeat
  • k, the number of repetitions.
  • called with successiveSum(n,k)
successiveSum(1,3);
successiveSum(5,3);
successiveSum(1,4);

Prints

1 + 11 + 111  = 123
5 + 55 + 555  = 615
1 + 11 + 111 + 1111  = 1234

The method.

It works by simply building up the number by multiplying by 10 and then adding n.

public static void successiveSum(int n, int k) {
   int sum = 0;
   int nextNumb = 0;
   String s = "";
   for (int i = 0; i < k; i++) {
       nextNumb = nextNumb * 10 + n;
       s = s + nextNumb + " + ";
       sum += nextNumb;
   }
   // remove the last "+ " of the string.
   System.out.println(s.substring(0,s.length()-2) + " = " + sum);
}
WJS
  • 36,363
  • 4
  • 24
  • 39