Write a program to accept 20 different numbers in a Single Dimensional
Array (SDA). Display the sum of all the numbers which are divisible by
either 3 or 5.
mhers which are divisible by 3 or 5
Answers
The following codes have been written using Python.
We import the module to create the array. Once the definition of the array has been created, we use a loop to retrieve the numbers. After that, a variable is assigned to represent the sum of the numbers. We start another loop to traverse through the array and test which of the numbers are divisible by 3 or 5 using a conditional statement. If they're divisible by 3 or 5, they get added to the variable earlier assigned for the same and an appropriate statement is passed.
Q. Write a program to accept 20 different numbers in a Single Dimensional Array (SDA). Display the sum of all the numbers which are divisible by either 3 or 5.
• THIS PROGRAM IS BASED ON SINGLE DIMENSIONAL ARRAYS!
// To display the sum of numbers which are divisible by 3 or 5
import java.util.*;
public class Sum
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
int i,s = 0
int m [ ] = new int [20];
for (i = 0; i < 20 ; i + + )
{
System.out.print("Enter the number in the cell :");
m[I] = in.nextInt();
}
for(i = 0; i < 20 ; i + + )
{
if(m[I]%3 == O||m[I]%5 == O)
s = s + m [i];
}
System.out.println("The sum of numbers divisible by 3 or 5:" +s);
}
}