Amazon Prime




 
Tagged
  • Flow Chart
  •  

    Sum of n Input Numbers

    Problem Statement

    1Problem Description :
    2Write Algorithm, Flowchart and Program to find sum of n input numbers
    3
    4Example 1 :
    5 Input : n = 4 and the numbers are 5, 3, 7, 4
    6 Output : 19
    7 Explanation : sum of 5, 3, 7 and 4 is 19
    8
    9Example 2 :
    10 Input : n = 3 and the numbers are 5, 3, 7
    11 Output : 15
    12 Explanation : sum of 5, 3 and 7 results to 15

    Algorithm

    1Input : n and n input numbers
    2Step 1: Start
    3Step 2: Read number n
    4Step 3: Declare sum to 0 and i to 1 (Here i is a counter variable which is used to input n numbers)
    5Step 4: Repeat steps 5 to 7 until i<=n
    6Step 5: Read number num
    7Step 6: update sum as sum = sum + num
    8Step 7: increment i
    9Step 8: Print sum
    10Output: sum
     

     

    Video Explanation

    Flowchart

    Sum Of Given N Numbers - Flowchart

    Code Implementation

    1. Java
    2. C

    1import java.util.Scanner;
    2
    3public class SumOfNInputNumbers {
    4
    5 public static void main(String[] args) {
    6 Scanner sc = new Scanner(System.in);
    7
    8 // Read n
    9 System.out.print("Enter n : ");
    10 int n = sc.nextInt();
    11
    12 // Declare sum and counter variable i
    13 int sum = 0;
    14 int i;
    15
    16 // loop to input n numbers
    17 // loop continues until i<=n
    18 for (i = 1; i <= n; i++) {
    19 // Read number
    20 System.out.printf("Enter number%d: ", i);
    21 int number = sc.nextInt();
    22
    23 // update sum
    24 sum = sum + number;
    25 }
    26 // Print sum
    27 System.out.printf("Sum is %d \n", sum);
    28 }
    29}

     

  • Flow Chart
  •  
    ...