100% found this document useful (2 votes)
33K views8 pages

Sum of Primes Excluding Largest

The document provides instructions for writing a Java program to calculate the sum of prime numbers in an integer array, excluding the largest prime number. It gives an example input array and explains how to find all prime numbers in the array, then add them together except for the largest one. It also notes a special case where if the array does not contain any prime numbers, the output should be the sum of all numbers except the largest.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
33K views8 pages

Sum of Primes Excluding Largest

The document provides instructions for writing a Java program to calculate the sum of prime numbers in an integer array, excluding the largest prime number. It gives an example input array and explains how to find all prime numbers in the array, then add them together except for the largest one. It also notes a special case where if the array does not contain any prime numbers, the output should be the sum of all numbers except the largest.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Code Explanation and Example
  • Find Key Sum Example
  • Sum of Even/Odd Digits
  • Smallest Digit in Places
  • First Repeated Element from Tail
  • First Repeated Element

SUM OF ALL PRIME NOS IN THE ARRAY EXCEPT LARGEST/SMALLEST

Find sum of all prime numbers in the array, except the largest prime number: Madhav has been assigned the task of finding the sum of
all prime numbers in a given array, except the largest prime number in the array. Madhav approaches you to help him do this by
writing a [Link] an array of numbers, you are expected to find the sum of all prime numbers in the given array. You must
however exclude the largest prime number while performing this [Link] Example -If input1 (10,41,18,50,43,31,29,25,59,96,67)
representing the given array, and,input2 = 11 representing the number of elements in the array,then the expected output is 203, which
is the sum of all prime numbers in this array except the largest prime number [Link]: The prime numbers in this array are 41,
43, 31, 29, 59 and 67. The largest prime number in this array is 67. So, let us leave out 67 and add all the other prime numbers to get
the [Link], output = 41+43+31+29+59 = [Link] conditions to be taken careNote: If the array does NOT contain any
prime number, the output should be the sum of all numbers in the array except the largest number. For example, If
input1=110,20,30,40) and input2 = 4 representing the number of elements in the array, then the expected output = 10+20+30= 60.

import [Link].*;
public class Main {
public static int sumOfPrime(int input1[],int input2)
{
List<Integer> al=new ArrayList<Integer>();
List<Integer> ls=new ArrayList<Integer>();
int sum=0,count=0,c=0;
for(int i=0;i<input2;i++)
{
count=0;
for(int j=1;j<=input1[i];j++)
{
if(input1[i]%j==0)
{
count++;
}
}
if(count==2)
{
[Link](input1[i]);
c++;
}

}
if(c==0)
{
for(int a=0;a<[Link];a++)
[Link](input1[a]);
for(int z:ls)
sum=sum+z;
//sum=[Link](ls);sum=[Link](ls);
}

else
{
for(int k:al)
sum=sum+k;
//sum=[Link](al);sum=[Link](al);
}

return sum;

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner([Link]);
int input2;input2=[Link]();
int input1[]=new int[input2];
for(int i=0;i<input2;i++)
input1[i]=[Link]();
[Link]([Link](input1,input2));

}
import [Link].*;
public class FindKey2 {
public static int find(int input1,int input2,int input3,int input4)
{
int sume=0,sumo=0,c1=1,c2=1,c3=1;
List<Integer> l1=new ArrayList<Integer>();
List<Integer> l2=new ArrayList<Integer>();
List<Integer> l3=new ArrayList<Integer>();
while(input1>0 && input2>0 && input3>0)
{
if(c1%2==0 && c2%2==0 && c3%2==0)
{
sume=sume+input1%10+input2%10+input3%10;
c1++;c2++;c3++;
input1/=10;input2/=10;input3/=10;
}
else
{
sumo=sumo+input1%10+input2%10+input3%10;
input1/=10;input2/=10;input3/=10;
c1++;c2++;c3++;
}
}
if(input4%2==0)
{
return sume-sumo;
}
else
{
return sumo-sume;
}

}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int input1,input2,input3,input4;
input1=[Link]();input2=[Link]();input3=[Link]();input4=[Link]();
[Link]([Link](input1,input2,input3,input4));

}
}
Find key sum=smallest digit of input1* smallest digit of input2* smallest digit of input3+input4
import [Link].*;
public class findkey {
public static int find(int input1,int input2,int input3,int input4)
{
int sum;
List<Integer> l1=new ArrayList<Integer>();
List<Integer> l2=new ArrayList<Integer>();
List<Integer> l3=new ArrayList<Integer>();
while(input1>0)
{
[Link](input1%10);input1=input1/10;
}
while(input2>0)
{
[Link](input2%10);input2=input2/10;
}
while(input3>0)
{
[Link](input3%10);input3=input3/10;
}
sum=[Link](l1)*[Link](l2)*[Link](l3)+input4;
return sum;
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);int input1,input2,input3,input4;
input1=[Link]();input2=[Link]();input3=[Link]();input4=[Link]();
[Link]([Link](input1,input2,input3,input4));

}
}

Find Key
Key =max of inp1*min of inp1+max of inp2*min of inp2+max of inp3*min of inp3-input4

import [Link].*;
public class Main {
public static int find(int input1,int input2,int input3,int input4)
{
int sum;
List<Integer> l1=new ArrayList<Integer>();
List<Integer> l2=new ArrayList<Integer>();
List<Integer> l3=new ArrayList<Integer>();
while(input1>0)
{
[Link](input1%10);input1=input1/10;
}
while(input2>0)
{
[Link](input2%10);input2=input2/10;
}
while(input3>0)
{
[Link](input3%10);input3=input3/10;
}
sum=([Link](l1)*[Link](l1))+([Link](l2)*[Link](l2))+
([Link](l3)*[Link](l3))-input4;
return sum;

}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);int input1,input2,input3,input4;
input1=[Link]();input2=[Link]();input3=[Link]();input4=[Link]();
[Link]([Link](input1,input2,input3,input4));
}
}
SUM OF EVEN/ODD DIGITS
import [Link].*;
public class Main {
public static int find(int input1,int input2,int input3,int input4)
{
int sum=0;
List<Integer> l1=new ArrayList<Integer>();
List<Integer> l2=new ArrayList<Integer>();
List<Integer> l3=new ArrayList<Integer>();
while(input1>0)
{
[Link](input1%10);input1=input1/10;
}
while(input2>0)
{
[Link](input2%10);input2=input2/10;
}
while(input3>0)
{
[Link](input3%10);input3=input3/10;
}
if(input4%2==0)
{
for(int i:l1)
{
if(i%2==0)
sum=sum+i;
}
for(int i:l2)
{
if(i%2==0)
sum=sum+i;
}
for(int i:l3)
{
if(i%2==0)
sum=sum+i;
}
}
else
{
for(int i:l1)
{
if(i%2!=0)
sum=sum+i;
}
for(int i:l2)
{
if(i%2!=0)
sum=sum+i;
}
for(int i:l3)
{
if(i%2!=0)
sum=sum+i;
}
}
return sum;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner([Link]);
int input1,input2,input3,input4;
input1=[Link]();
input2=[Link]();
input3=[Link]();
input4=[Link]();
[Link]([Link](input1,input2,input3,input4));
}
}
SMALLEST DIGIT IN THOUSANDS, HUNDREDTHS, TENS, UNITS PLACE

import [Link].*;

public class Main {


public static String find(int input1,int input2,int input3,int input4)
{
int sum=0;
List<Integer> l1=new ArrayList<Integer>();
List<Integer> l2=new ArrayList<Integer>();
List<Integer> l3=new ArrayList<Integer>();
List<Integer> l4=new ArrayList<Integer>();
[Link](input1%10);[Link](input2%10);[Link](input3%10);
[Link]((input1/10)%10);[Link]((input2/10)%10);[Link]((input3/10)%10);
[Link]((input1/100)%10);[Link]((input2/100)%10);[Link]((input3/100)%10);
[Link](input1/1000);[Link](input2/1000);[Link](input3/1000);

return
[Link]([Link](l4))+[Link]([Link](l3))+[Link]([Link](l2))+[Link](Collection
[Link](l1));
}

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner([Link]);
int input1,input2,input3,input4;
input1=[Link]();
input2=[Link]();
input3=[Link]();
[Link]([Link](input1,input2,input3,input4));
}

}
FIRST REPEATED ELEMENT

import [Link].*;

class Main
{
public static int printFirstRepeating(int arr[],int n)
{
// Initialize index of first repeating element
int min = -1;

// Creates an empty hashset


HashSet<Integer> set = new HashSet<>();

// Traverse the input array from right to left


for (int i=[Link]-1; i>=0; i--)
{
// If element is already in hash set, update min
if ([Link](arr[i]))
min = i;

else if(arr[i]>0) // Else add element to hash set


[Link](arr[i]);

// Print the result

if([Link]()==0)
return 0;
else
{
if (min != -1)
return arr[min];
else
return arr[0];
}

}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
Main obj = new Main();
int n=[Link]();
int arr[] = new int[n];
for(int i=0;i<n;i++)
{
arr[i]=[Link]();
}
[Link]([Link](arr, n));
}
}
First Repeated Element from Tail
Find first repeated element in the given array from the tail of the array:
Rajeev works in the data center lab of the survey department. He has been assigned the task of identifying "repeated numbers" in a given
set of numbers. He approaches you to help him achieve this. Given an array of numbers, your task is to return the first number that
repeats (appears again) starting from the last [Link] example :If input1 ([Link].2.8) representing the given array, and
input2 = 6 representing the number of elements in the array. then the result should be 2 which is the first repeated number in the array
from the rear [Link] conditions to be [Link] 1: You should ignore the negative numbers and zeros. The program should
consider only non-zero, non-negative numbers from the given array. Note 2: If no number is repeated then the output should be the last
element of the [Link] 3: If all elements in the array are negative or 0's the output should be 0.

import [Link].*;

class Main
{
public static int printFirstRepeating(int arr[],int n)
{
// Initialize index of first repeating element
int min = -1;

// Creates an empty hashset


HashSet<Integer> set = new HashSet<>();

// Traverse the input array from right to left


for (int i=0; i<n; i++)
{
// If element is already in hash set, update min
if ([Link](arr[i]))
min = i;

else if(arr[i]>0) // Else add element to hash set


[Link](arr[i]);

// Print the result

if([Link]()==0)
return 0;
else
{
if (min != -1)
return arr[min];
else
return arr[n-1];
}

}
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
Main obj = new Main();
int n=[Link]();
int arr[] = new int[n];
for(int i=0;i<n;i++)
{
arr[i]=[Link]();
}
[Link]([Link](arr, n));
}
}

You might also like