Java program to add two numbers

Very basic java coding practice or java homework questions. While learning Java, we started with converting basic calculation to using Java program. like creating calculator using Java code, printing sum of digits and all. And sometimes this type of questions also asked in core Java written interview questions, although this question is not as demanded as how to balanced the parentheses using Java program.

package com.javamakeuse.poc;

import java.util.Scanner;

/**
 * 
 * @author javamakeuse
 *
 */
public class Number {
 public static void main(String[] args) {
  try (Scanner scanner = new Scanner(System.in)) {
   System.out.println("Enter first number ");
   int a = scanner.nextInt();
   System.out.println("Enter second number");
   int b = scanner.nextInt();
   int c = a + b;
   System.out.printf("sum of %d and %d is %d", a, b, c);
  }
 }
}


OUTPUT:
Enter first number
4
Enter second number
4
sum of 4 and 4 is 8

Comments

Popular posts from this blog

What are the core classes and interfaces of hibernate 4

JSP Servlet Interview Questions For Freshers Series 1

Java Banking Finance Services and Insurance (BFSI) domain interview questions