Real life like addition program|| Digit Games

see cheat sheet at the end for manipulating number.

·

2 min read

Problem Statement

Let xyz denote the 3- digit integer whose digits are x, y, and z from left to right.
Given a 3-digit integer, abc none of whose digits is 0, find abc+bca+cab.

Input

The input is given as follows.
abc

Constraints
abc is a 3- digit integer, none of whose digits is 0.

Output

Print the answer.

Sample Input 1
123
Sample Output 1
666

Sample Input 2
999
Sample Output 2
2997

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
        // Your code here
        Scanner sc = new Scanner(System.in);
        String n = sc.next();

        int sum = 0;
        for(int i=0; i< n.length();  i++){
            sum += n.charAt(i) - 48;
        }

        StringBuilder sb = new StringBuilder();
        int carry =0;

        for(int i=0; i< n.length();  i++){
            int actual;
            if( sum > 9){
                actual = sum % 10;
                char temp = (char) (actual+48+carry);
                carry = sum/10;
                sb.insert( 0, temp);
            }else{
                actual = sum+carry;
                char temp = (char) (actual+48);
                sb.insert(0,temp);
            }

            //System.out.print( n.charAt(i) +" ");
        }
        if( carry !=0 ){
        char temp = (char)(carry+48);
        sb.insert(0, temp);
        }

        //char test = (char) (sum+48);

        System.out.println( sb.toString() );
    }
}

Conversion:

  1. char into int

    int n = '2' - 48;

  2. int to char

    char ch = (char) (48 + 2) ;

  3. char to string

    String str = 'converted' + "";

    String str = '5' + "";

  4. String to int

    int n = Integer.parseInt("string");

Did you find this article valuable?

Support Aman by becoming a sponsor. Any amount is appreciated!