Sum of Two Numbers
Problem Statement
1Problem Description :2Write Algorithm, Flowchart and Program to Add Two Integers34Example 1 :5 Input : a = 3 and b = 76 Output : 107 Explanation : sum of 3 and 7 results to 1089Example 2 :10 Input : a = 12 and b = 711 Output : 1912 Explanation : sum of 12 and 7 results to 19
Algorithm
1Input : Two numbers num1 and num22Step 1: Start3Step 2: Declare sum to 0 (This is optional step, during step5 we can add declaration and assign directly as well)4Step 3: Read number num15Step 4: Read number num26Step 5: Add num1 and num2 and assign result to variable sum7Step 6: Print sum8Step 7: Stop9Output: Sum of num1 and num2
Video Explanation
Flowchart
Code Implementation
- Java
- C
1import java.util.Scanner;23public class SumOfTwoNumbers {45 public static void main(String[] args) {67 // Initialize scanner to which is used to read inputs8 Scanner sc = new Scanner(System.in);910 // Read number1 and number 211 System.out.print("Enter num1: ");12 int num1 = sc.nextInt();13 System.out.print("Enter num2: ");14 int num2 = sc.nextInt();1516 // calculate sum17 int sum = num1 + num2;1819 //print sum20 System.out.printf("%d + %d = %d \n", num1, num2,sum);21 }22}
1Output 1 :2Enter number1: 103Enter number2: 23410 + 23 = 33
1Output 2 :2Enter number1: 23Enter number2: 1242 + 12 = 14