Amazon Prime




 
Tagged
  • Flow Chart
  •  

    Sum of Two Numbers with Sum > 20

    Problem Statement

    1Problem Description :
    2Write algorithm and a flowchart that will compute the sum of two numbers. if the sum is below or equal to twenty, two numbers will be entered again. if the sum is above 20, it will display the sum.

    Algorithm

    1Input : Two numbers num1 and num2
    2Step 1: Start
    3Step 2: Declare sum to 0 (This is optional step, during step5 we can add declaration and assign directly as well)
    4Step 3: Read number num1
    5Step 4: Read number num2
    6Step 5: Add num1 and num2 and assign result to variable sum
    7Step 6: If sum is greater than 20 Goto Step 7, else Goto Step 3
    8Step 7: Print sum
    9Step 8: Stop
    10Output: Sum of num1 and num2
     

     

    Flowchart

    Sum of Two Numbers with Sum Greater than 20 - Flowchart

    Code Implementation

    1. Java
    2. C

    1import java.util.Scanner;
    2
    3public class SumOfTwoNumbersWithSumGreaterThan20 {
    4
    5 public static void main(String[] args) {
    6 // Initialize scanner to which is used to read inputs
    7 Scanner sc = new Scanner(System.in);
    8
    9 while (true) {
    10 // Read number1 and number 2
    11 System.out.print("Enter num1: ");
    12 int num1 = sc.nextInt();
    13 System.out.print("Enter num2: ");
    14 int num2 = sc.nextInt();
    15
    16 // calculate sum
    17 int sum = num1 + num2;
    18
    19 // if sum is greater than 20 print it
    20 // and break the loop
    21 if (sum > 20){
    22 System.out.printf("Sum is %d \n", sum);
    23 break;
    24 }
    25 // continue for next input
    26 System.out.printf("sum: %d is less than 20, Try again\n", sum);
    27 }
    28 }
    29}

    Output 1 :

    1Enter number1: 1
    2Enter number2: 2
    3sum: 3 is less than 20, Try again
    4Enter number1: 1
    5Enter number2: 43
    61 + 43 = 44

    Output 2 :

    1Enter number1: 21
    2Enter number2: 12
    321 + 12 = 33
     

  • Flow Chart
  •  
    ...