top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a program to print the sum of the element of the array with the given below condition?

+3 votes
7,039 views

Write a program to print the sum of the element of the array with the given below condition?

If the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the numbers between them for the calculation of sum.

Array Elements - 10, 3, 6, 1, 2, 7, 9

Output: 22
i.e 10+3+9

posted Apr 12, 2017 by Atindra Kumar Nath

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
        int[] num = { 10, 3, 6, 1, 4, 7, 9 };

        Console.Write("Enter your first number");
        int num1 = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter your second number");
        int num2 = Convert.ToInt32(Console.ReadLine());

        bool flage = false;
        int res = 0;
        foreach (var item in num)
        {

            if (item == num1)
            {
                flage = true;
            }
            if (flage == true)
            {
                if (item == num2)
                {
                    flage = false;
                }
                continue;
            }
            else
            {
                res += Convert.ToInt32(item);
            }
        }
        Console.WriteLine("Total=" + res);
        Console.ReadKey();
answer Apr 13, 2017 by Prakas Pandey
Good approach
Let see what different solution I can get from others.
That code has a corner case it wont give correct output when input is :Array Elements - 7,1,2,3,6
i corrected that and solution is :
package com.Milestone1.Arrays;
/** Write a program to print the sum of the elements of the array with the given below condition. If the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the numbers between them for the calculation of sum.
Eg1) Array Elements - 10,3,6,1,2,7,9
O/P: 22   
[i.e 10+3+9]
*/
import java.util.Scanner;
public class Arrays_9 {

    public static void main(String[] args) {
            int n;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter the size of array:");
            n = sc.nextInt();
            int arr[] = new int[n];
            for(int i=0;i<n;i++){
                arr[i] = sc.nextInt();
               
            }
             sc.close();
            int num1 = 6;
            int num2 = 7;
            int sum = 0;
            boolean flag = false;
            for(int i : arr){
                if(i == num1){
                    flag = true;
                }
                if(flag == true){
                    if(i == num2){
                        flag = false;
                    }
                    continue;
                }
                else{
                    if(i == 7 ){
                        sum += 6+i;
                    }
                    else
                        sum += i;
                }
            }
            System.out.println("sum is "+sum);
           
           
           
    }

}
Can somebody explain me these lines:
else{
                    if(i == 7 ){
                        sum += 6+i;
                    }
                    else
                        sum += i;
                }
0 votes

public class Assignment8 {
public static void main(String args[])
{
//checking testcases
// int[] arr= {1,2,3,4,6,8,9,7,2};
// int[] arr= {1,2,3,6,4,2,8,7,4};
// int[] arr= {7,1,2,3,6};
int[] arr= {1,2,3};

    int sixPos=-1;
    int sevenPos=-1;
    int sum=0;

    for(int i=0;i<arr.length;i++)
    {
        if(arr[i]==6) sixPos=i;
        if(arr[i]==7) sevenPos=i;
    }

    if(sevenPos==-1) sixPos=-1;

    for(int i=0;i<arr.length;i++)
    {
        if(sixPos!=-1 && i>=sixPos && i<=sevenPos) continue;
        sum+=arr[i];
    }

    System.out.println(sum);

}

}

answer Aug 2, 2020 by anonymous
Similar Questions
+1 vote

Write a program to print the sum of the elements of the array with the given below condition. If the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the numbers between them for the calculation of sum.
Eg1) Array Elements - 10,3,6,1,2,7,9
O/P: 22
[i.e 10+3+9]
Eg2) Array Elements - 7,1,2,3,6
O/P:19
Eg3) Array Elements - 1,6,4,7,9
O/P:10

+3 votes

Rohit's teacher has asked him to write a function that takes as input parameters the first parameter will be an integer number representing. The number whose digitSum needs to be found the second parameter will be a char representing the
option which would either be 'e' or 'o' representing 'even' or 'odd' respectively.

Function signature public int digitSum(int n, char ch);

digitSum(9625, 'o')
Example 1: if given number is 9625 and the option is 'o' we must add only the odd digit i.e. 9+5=14
digitSum(2134, 'e')
Example 2: if given number is 2134 and the option  is 'e' we must add only the odd digit i.e. 2+4=6
+2 votes

Rohit wants to add the last digits of two given numbers.
For example, If the given numbers are 267 and 154, the output should be 11.

Below is the explanation -
Last digit of the 267 is 7
Last digit of the 154 is 4
Sum of 7 and 4 = 11

Write a program to help Rohit achieve this for any given two numbers.

Note: The sign of the input numbers should be ignored. i.e.
if the input numbers are 267 and 154, the sum of last two digits should be 11
if the input numbers are 267 and -154, the sum of last two digits should be 11
if the input numbers are -267 and 154, the sum of last two digits should be 11
if the input numbers are -267 and -154, the sum of last two digits should be 11

0 votes

Alex has been asked by his teacher to do an assignment on sums of digits of a number. The assignment requires Alex to find the sum of sums of digits of a given number, as per the method mentioned below.

If the given number is 582109, the Sum of Sums of Digits will be calculated as =
= (5 + 8 + 2 + 1 + 0 + 9) + (8 + 2 + 1 + 0 + 9) + (2 + 1 + 0 + 9) + (1 + 0 + 9) + (0 + 9) + (9)
= 25 + 20 + 12 + 10 + 9 + 9 = 85

Alex contacts you to help him write a program for finding the Sum of Sums of Digits for any given number, using the above method.

Help Alex by completing the login in the given function sumOfSumsOfDigits which takes as input an integer input1 representing the given number.
The function is expected to return the "Sum of Sums of Digits" of input1.

Assumptions: For this assignment, let us assume that the given number will always contain more than 1 digit, i.e. the given number will always be >9.

+1 vote

Write a program to remove the duplicate elements in an array and print
Eg) Array Elements - 12, 34, 12, 45, 67, 89
O/P: 12,34,45,67,89

...