Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a in Java.
Write a Java program that accepts an integer (n) and computes the value of n+nn+nnn.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.Scanner; public class JavaExamples2 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a in Java"); System.out.print("Enter Digit : "); int digit = console.nextInt(); System.out.print("Enter a number : "); String num = console.next(); System.out.println(); int sum=0; for (int i = 1; i <= digit; i++) { String number = ""; for (int j = 0; j < i; j++) { number = number + num; } System.out.println(number); sum += Integer.parseInt(number); } System.out.println("_______________+"); System.out.println(sum); } } |
Output: